Dictionary.cpp
Go to the documentation of this file.
00001 class Dictionary {
00002    public:
00003     /* Function that returns the size of the dictionary.
00004      * 
00005      * INPUT: none
00006      * OUTPUT: none
00007      */
00008       int size() {
00009          return list.size();
00010       }
00011 
00012     /* Function that adds an entry to the list vector.
00013      * 
00014      * INPUT: string filename: filename with dictionary file
00015      * OUTPUT: none
00016      */
00017       void add_entry(string str) {
00018       list.push_back(str);
00019     }
00020 
00021     /* Function that returns the index of the best match for str in list. 
00022      * If no good  match is found, -1 is returned.
00023      * 
00024      * INPUT: string str: sample string
00025      * OUTPUT: int: index of best match, -1 if no good match found
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++) { // for all entries in dictionary (an entry may contain multiple words)
00035             rating = 0;
00036             rightOrder = true;
00037             lastPos = -1;
00038 
00039         // separate strings into words
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             // compare each word from dict entry to each word from str
00047             for(size_t j = 0; j < dictWords.size(); j++) { // for all words in current dictionary entry
00048                for(size_t k = 0; k < strWords.size(); k++) { // for all words in currently recognized string
00049                   if(strWords[k].compare(dictWords[j]) == 0) { // dictWords contains a word from str
00050                      if(lastPos > j) rightOrder = false; // wrong word order detected, rating-- would be more aggressive
00051                      lastPos = j;
00052                      rating++;
00053               // remove words
00054                      strWords.erase(strWords.begin() + k );
00055                      k--;
00056                      dictWords.erase(dictWords.begin() + j);
00057                      j--;
00058                      break;
00059                   }
00060                }
00061             }
00062         // best match only matches half or less of the words
00063             if(rating < dictWordsOrigSize/2) {
00064               rating = 0;
00065             }
00066         
00067         // bonuses for correct length or correct order
00068             if(rating > 0) {
00069                if(rating == dictWordsOrigSize) rating++; // bonus if all words are equal, but order may be different
00070                if(rightOrder && strWordsOrigSize > 1) rating++; // bonus for correct word order of non-trivial str
00071             }
00072             
00073             if(rating > bestRating) { // update rating
00074           bestRating = rating;
00075           bestRated = i;
00076             }
00077          }
00078       // no match found
00079          if(bestRating == 0) bestRated = -1;
00080          
00081          return bestRated;
00082       }
00083             
00084    private:
00085       vector<string> list;
00086       
00087     /* Function that splits a string str up at space characters and
00088      * returns the segments in a vector<string>.
00089      * 
00090      * INPUT: string str: string to be split
00091      * OUTPUT: vector<string>: vector of string segments
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; // skip space
00100         end = str.find_first_of(' ', begin);
00101       }
00102       words.push_back(str.substr(begin, str.length() - begin));
00103       return words;
00104     }
00105 };


command_matcher
Author(s): Alexander Reiter
autogenerated on Fri Aug 28 2015 11:05:12