Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <string>
- #include <algorithm>
- #include <vector>
- #include <cmath>
- using namespace std;
- template <typename Document, typename Term>
- vector<double> ComputeTfIdfs(const Document& documents, const Term& word){
- vector<double> tfidf;
- int document_count = 0;
- for(const auto& document: documents){
- int freq = count(document.begin(), document.end(), word);
- if(freq > 0){
- ++document_count;
- }
- tfidf.push_back(static_cast<double>(freq) / document.size());
- }
- double idf = log(static_cast<double>(documents.size()) / document_count);
- for(auto& tf_idf: tfidf){
- tf_idf *= idf;
- }
- return tfidf;
- }
- int main() {
- const vector<vector<string>> documents = {
- {"белый"s, "кот"s, "и"s, "модный"s, "ошейник"s},
- {"пушистый"s, "кот"s, "пушистый"s, "хвост"s},
- {"ухоженный"s, "пёс"s, "выразительные"s, "глаза"s},
- };
- const auto& tf_idfs = ComputeTfIdfs(documents, "кот"s);
- for (const double tf_idf : tf_idfs) {
- cout << tf_idf << " "s;
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement