Go to the documentation of this file.00001 class Dictionary {
00002 public:
00003
00004
00005
00006
00007
00008 int size() {
00009 return list.size();
00010 }
00011
00012
00013
00014
00015
00016
00017 void add_entry(string str) {
00018 list.push_back(str);
00019 }
00020
00021
00022
00023
00024
00025
00026
00027 int findBestMatch(string str) {
00028 int rating;
00029 int bestRating = 0;
00030 int bestRated;
00031 bool rightOrder;
00032 size_t lastPos;
00033
00034 for(size_t i = 0; i < list.size(); i++) {
00035 rating = 0;
00036 rightOrder = true;
00037 lastPos = -1;
00038
00039
00040 vector<string> dictWords = stringToWords(list[i]);
00041 int dictWordsOrigSize = dictWords.size();
00042
00043 vector<string> strWords = stringToWords(str);
00044 int strWordsOrigSize = strWords.size();
00045
00046
00047 for(size_t j = 0; j < dictWords.size(); j++) {
00048 for(size_t k = 0; k < strWords.size(); k++) {
00049 if(strWords[k].compare(dictWords[j]) == 0) {
00050 if(lastPos > j) rightOrder = false;
00051 lastPos = j;
00052 rating++;
00053
00054 strWords.erase(strWords.begin() + k );
00055 k--;
00056 dictWords.erase(dictWords.begin() + j);
00057 j--;
00058 break;
00059 }
00060 }
00061 }
00062
00063 if(rating < dictWordsOrigSize/2) {
00064 rating = 0;
00065 }
00066
00067
00068 if(rating > 0) {
00069 if(rating == dictWordsOrigSize) rating++;
00070 if(rightOrder && strWordsOrigSize > 1) rating++;
00071 }
00072
00073 if(rating > bestRating) {
00074 bestRating = rating;
00075 bestRated = i;
00076 }
00077 }
00078
00079 if(bestRating == 0) bestRated = -1;
00080
00081 return bestRated;
00082 }
00083
00084 private:
00085 vector<string> list;
00086
00087
00088
00089
00090
00091
00092
00093 vector<string> stringToWords(string str) {
00094 vector<string> words;
00095 size_t begin = 0;
00096 size_t end = str.find_first_of(' ', begin);
00097 while(end != string::npos) {
00098 words.push_back(str.substr(begin, end-begin));
00099 begin = end + 1;
00100 end = str.find_first_of(' ', begin);
00101 }
00102 words.push_back(str.substr(begin, str.length() - begin));
00103 return words;
00104 }
00105 };