Advertisement
Guest User

Untitled

a guest
May 29th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. class word {
  8.         public:
  9.         string Word;
  10.         vector<string> bigrams;
  11.         int freq;
  12.         word(string str, int f): bigrams() {
  13.             Word = str; freq = f;
  14.             if (str.size()==1) bigrams.push_back(str);
  15.             for (int i=0; i<str.size()-1; i++) {
  16.                 string tmp("ab");
  17.                 tmp[0] = str[i]; tmp[1] = str[i+1];
  18.                 bigrams.push_back(tmp);
  19.             }
  20.         }
  21.         bool operator<(const word &obj) const {
  22.             if (this->freq != obj.freq) return this->freq > obj.freq;
  23.             return this->Word < obj.Word;
  24.         }
  25. };
  26.  
  27. double getK(word A, word B) {
  28.     double Intersection=0, Union;
  29.     int tmp[30] = {0};
  30.     for (string a : A.bigrams) for (int i=0; i<B.bigrams.size(); i++) {
  31.         string b = B.bigrams[i];
  32.         if (a == b && tmp[i]==0) {
  33.             tmp[i] = 1;
  34.             Intersection += 1.0;
  35.             break;
  36.         }
  37.     }
  38.     Union = A.bigrams.size() + B.bigrams.size() - Intersection;
  39.     //cout << A.Word << "#" << B.Word << " = " << Intersection/Union << "\n";
  40.     return Intersection/Union;
  41. }
  42. string checkSpelling(vector<word> &dict, string str) {
  43.     word a(str,0);
  44.     double max=0.0;
  45.     string MaxWord = dict.at(0).Word;
  46.     for (word w : dict) {
  47.         double k = getK(a,w);
  48.         if (k > max) {
  49.             max = k;
  50.             MaxWord = w.Word;
  51.         }
  52.     }
  53.     return MaxWord;
  54. }
  55.  
  56. int main() {
  57.     fstream words("count_big.txt");
  58.     string str; int num;
  59.     vector<word> dict;
  60.     dict.reserve(60000);
  61.  
  62.     while (words >> str) {
  63.         words >> num;
  64.         dict.push_back(word(str,num));
  65.     }
  66.     words.close();
  67.     sort(dict.begin(), dict.end());
  68.    
  69.     while (cin >> str) {
  70.         cout << checkSpelling(dict,str) << endl;
  71.     }
  72.    
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement