From: Tim King Date: Wed, 31 Aug 2016 22:35:06 +0000 (-0700) Subject: Removing typeof from didyoumean.cpp. X-Git-Tag: cvc5-1.0.0~6028^2~74 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=3db673c6cc8140c17ae229da86102955dce72434;p=cvc5.git Removing typeof from didyoumean.cpp. --- diff --git a/src/options/didyoumean.cpp b/src/options/didyoumean.cpp index d874c7bc7..693764b37 100644 --- a/src/options/didyoumean.cpp +++ b/src/options/didyoumean.cpp @@ -32,45 +32,42 @@ std::vector DidYouMean::getMatch(std::string input) { const int similarityThreshold = 7; const unsigned numMatchesThreshold = 10; - std::set< std::pair > scores; + typedef std::set > ScoreSet; + ScoreSet scores; std::vector ret; - for(typeof(d_words.begin()) it = d_words.begin(); it != d_words.end(); ++it) { + for (Words::const_iterator it = d_words.begin(); it != d_words.end(); ++it) { std::string s = (*it); - if( s == input ) { + if (s == input) { // if input matches AS-IS just return that ret.push_back(s); return ret; } int score; - if(s.compare(0, input.size(), input) == 0) { + if (s.compare(0, input.size(), input) == 0) { score = 0; } else { score = editDistance(input, s) + 1; } - scores.insert( make_pair(score, s) ); + scores.insert(make_pair(score, s)); } int min_score = scores.begin()->first; - for(typeof(scores.begin()) i = scores.begin(); - i != scores.end(); ++i) { - + for (ScoreSet::const_iterator i = scores.begin(); i != scores.end(); ++i) { // add if score is overall not too big, and also not much close to // the score of the best suggestion - if(i->first < similarityThreshold && i->first <= min_score + 1) { + if (i->first < similarityThreshold && i->first <= min_score + 1) { ret.push_back(i->second); #ifdef DIDYOUMEAN_DEBUG cout << i->second << ": " << i->first << std::endl; #endif } } - if(ret.size() > numMatchesThreshold ){ + if (ret.size() > numMatchesThreshold) { ret.resize(numMatchesThreshold); } return ret; } - -int DidYouMean::editDistance(const std::string& a, const std::string& b) -{ +int DidYouMean::editDistance(const std::string& a, const std::string& b) { // input string: a // desired string: b @@ -86,44 +83,42 @@ int DidYouMean::editDistance(const std::string& a, const std::string& b) int* C[3]; int ii; for (ii = 0; ii < 3; ++ii) { - C[ii] = new int[len2+1]; + C[ii] = new int[len2 + 1]; } // int C[3][len2+1]; // cost - for(int j = 0; j <= len2; ++j) { + for (int j = 0; j <= len2; ++j) { C[0][j] = j * addCost; } - for(int i = 1; i <= len1; ++i) { - - int cur = i%3; - int prv = (i+2)%3; - int pr2 = (i+1)%3; + for (int i = 1; i <= len1; ++i) { + int cur = i % 3; + int prv = (i + 2) % 3; + int pr2 = (i + 1) % 3; C[cur][0] = i * deleteCost; - for(int j = 1; j <= len2; ++j) { - - C[cur][j] = 100000000; // INF + for (int j = 1; j <= len2; ++j) { + C[cur][j] = 100000000; // INF - if(a[i-1] == b[j-1]) { + if (a[i - 1] == b[j - 1]) { // match - C[cur][j] = std::min(C[cur][j], C[prv][j-1]); - } else if(tolower(a[i-1]) == tolower(b[j-1])){ + C[cur][j] = std::min(C[cur][j], C[prv][j - 1]); + } else if (tolower(a[i - 1]) == tolower(b[j - 1])) { // switch case - C[cur][j] = std::min(C[cur][j], C[prv][j-1] + switchCaseCost); + C[cur][j] = std::min(C[cur][j], C[prv][j - 1] + switchCaseCost); } else { // substitute - C[cur][j] = std::min(C[cur][j], C[prv][j-1] + substituteCost); + C[cur][j] = std::min(C[cur][j], C[prv][j - 1] + substituteCost); } // swap - if(i >= 2 && j >= 2 && a[i-1] == b[j-2] && a[i-2] == b[j-1]) { - C[cur][j] = std::min(C[cur][j], C[pr2][j-2] + swapCost); + if (i >= 2 && j >= 2 && a[i - 1] == b[j - 2] && a[i - 2] == b[j - 1]) { + C[cur][j] = std::min(C[cur][j], C[pr2][j - 2] + swapCost); } // add - C[cur][j] = std::min(C[cur][j], C[cur][j-1] + addCost); + C[cur][j] = std::min(C[cur][j], C[cur][j - 1] + addCost); // delete C[cur][j] = std::min(C[cur][j], C[prv][j] + deleteCost); @@ -132,31 +127,35 @@ int DidYouMean::editDistance(const std::string& a, const std::string& b) std::cout << "C[" << cur << "][" << 0 << "] = " << C[cur][0] << std::endl; #endif } - } - int result = C[len1%3][len2]; + int result = C[len1 % 3][len2]; for (ii = 0; ii < 3; ++ii) { - delete [] C[ii]; + delete[] C[ii]; } return result; } -std::string DidYouMean::getMatchAsString(std::string input, int prefixNewLines, int suffixNewLines) { +std::string DidYouMean::getMatchAsString(std::string input, int prefixNewLines, + int suffixNewLines) { std::vector matches = getMatch(input); std::ostringstream oss; - if(matches.size() > 0) { - while(prefixNewLines --> 0) { oss << std::endl; } - if(matches.size() == 1) { + if (matches.size() > 0) { + while (prefixNewLines-- > 0) { + oss << std::endl; + } + if (matches.size() == 1) { oss << "Did you mean this?"; } else { oss << "Did you mean any of these?"; } - for(unsigned i = 0; i < matches.size(); ++i) { + for (unsigned i = 0; i < matches.size(); ++i) { oss << "\n " << matches[i]; } - while(suffixNewLines --> 0) { oss << std::endl; } + while (suffixNewLines-- > 0) { + oss << std::endl; + } } return oss.str(); } -}/* CVC4 namespace */ +} /* CVC4 namespace */ diff --git a/src/options/didyoumean.h b/src/options/didyoumean.h index 71a12e6fc..31b58aadb 100644 --- a/src/options/didyoumean.h +++ b/src/options/didyoumean.h @@ -19,25 +19,22 @@ #pragma once -#include #include #include +#include namespace CVC4 { class DidYouMean { + public: typedef std::set Words; - Words d_words; -public: DidYouMean() {} ~DidYouMean() {} DidYouMean(Words words) : d_words(words) {} - void addWord(std::string word) { - d_words.insert(word); - } + void addWord(std::string word) { d_words.insert(word); } std::vector getMatch(std::string input); @@ -45,9 +42,12 @@ public: * This is provided to make it easier to ensure consistency of * output. Returned string is empty if there are no matches. */ - std::string getMatchAsString(std::string input, int prefixNewLines = 2, int suffixNewLines = 0); -private: + std::string getMatchAsString(std::string input, int prefixNewLines = 2, + int suffixNewLines = 0); + + private: int editDistance(const std::string& a, const std::string& b); + Words d_words; }; -}/*CVC4 namespace*/ +} /*CVC4 namespace*/