Advertisement
Guest User

Untitled

a guest
May 30th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <vector>
  4. #include <set>
  5. #include <string>
  6. #include <algorithm>
  7. #include <sstream>
  8. #include <fstream>
  9.  
  10. using namespace std;
  11.  
  12. class WtoB{
  13. public:
  14.     string word;
  15.     set<string> bigrams;
  16.     int frequency;
  17.     WtoB(string word, set<string> bigrams, int frequency){
  18.         this->word = word;
  19.         for(auto i = bigrams.begin(); i != bigrams.end(); i++){
  20.             this->bigrams.insert(*i);
  21.         }
  22.         this->frequency = frequency;
  23.     }
  24. };
  25.  
  26. set<string> making_bigrams(string &a){
  27.     unsigned long length = a.length();
  28.     set<string> bigrams;
  29.     for(int i = 0; i < length-1; i++){
  30.         string temp = "", temp1;
  31.         temp += a[i];
  32.         temp1+= a[i+1];
  33.         bigrams.insert(temp+temp1);
  34.         temp = "";
  35.     }
  36.     if(bigrams.size() == 0 && a.length() == 1) bigrams.insert(a);
  37.     return bigrams;
  38. }
  39.  
  40. double Similarity(set<string> &a, set<string> &b){
  41.     set<string> for_intersect, for_union;
  42.     set_intersection(a.begin(), a.end(), b.begin(), b.end(), inserter(for_intersect, for_intersect.begin()));
  43.     //cout << for_intersect.size() << ' ';
  44.     set_union(a.begin(), a.end(), b.begin(), b.end(), inserter(for_union, for_union.begin()));
  45.     //cout << for_union.size() << ' ';
  46.     return (double)for_intersect.size() / (double)for_union.size();
  47. }
  48.  
  49. int main(int argc, const char * argv[]) {
  50.     string a, a1;
  51.     int frequency = 0;
  52.     vector<WtoB> dictionary, result, result1;
  53.     ifstream for_open;
  54.     for_open.open("/Users/Vlad/Desktop/count_big1.txt");
  55.     while(getline(for_open, a)){
  56.         istringstream iss(a);
  57.         iss >> a1 >> frequency;
  58.         dictionary.push_back(WtoB(a1, making_bigrams(a1), frequency));
  59.     }
  60.     while(cin){
  61.         string previous = a;
  62.         cin >> a;
  63.         if(a == previous) break;
  64.         //cout << a << endl;
  65.         set<string> temp = making_bigrams(a);
  66.         double max_similarity = -1;
  67.         for(int i = 0; i < dictionary.size(); i++){
  68.             double similarity = Similarity(temp, dictionary[i].bigrams);
  69.             if(max_similarity == similarity){
  70.                 result.emplace_back(dictionary[i]);
  71.             }
  72.             else if(max_similarity < similarity){
  73.                 max_similarity = similarity;
  74.                 if(result.size() > 0) result.clear();
  75.                 result.emplace_back(dictionary[i]);
  76.             }
  77.         }
  78.         if(result.size() != 1){
  79.             int max_frequency = 0;
  80.             for(int i = 0; i < result.size(); i++){
  81.                 if(max_frequency < result[i].frequency){
  82.                     max_frequency = result[i].frequency;
  83.                     if(result1.size() > 0) result1.clear();
  84.                     result1.emplace_back(result[i]);
  85.                 }
  86.                 else if(max_frequency == result1[i].frequency) result1.emplace_back(result[i]);
  87.             }
  88.             if(result1.size() > 1){
  89.                 long size = result1.size();
  90.                 string min_str = result1[0].word;
  91.                 for(long i = 1; i < size; i++){
  92.                     if(min_str < result1[i].word) min_str = result1[i].word;
  93.                 }
  94.                 cout << min_str << endl;
  95.             }
  96.             else if(result1.size() == 1){
  97.                 cout << result1[0].word << endl;
  98.             }
  99.         }
  100.         else{
  101.             cout << result[0].word << endl;
  102.         }
  103.     }
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement