unsigned int levenshtein_distance(const T &s1, const T & s2) { const size_t len1 = s1.length(), len2 = s2.length(); std::vector<unsigned int> col(len2+1), prevCol(len2+1); for (unsigned int i = 0; i < prevCol.size(); i++) prevCol[i] = i; for (unsigned int i = 0; i < len1; i++) { col[0] = i+1; for (unsigned int j = 0; j < len2; j++) col[j+1] = std::min( std::min(prevCol[1 + j] + 1, col[j] + 1), prevCol[j] + (s1[i]==s2[j] ? 0 : 1) ); col.swap(prevCol); } return prevCol[len2]; }
// https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#C.2B.2B unsigned int computeEditDistance(const string& s1, const string& s2) { const size_t len1 = s1.size(), len2 = s2.size(); vector<unsigned int> col(len2 + 1), prevCol(len2 + 1); for (unsigned int i = 0; i < prevCol.size(); i++) prevCol[i] = i; for (unsigned int i = 0; i < len1; i++) { col[0] = i + 1; for (unsigned int j = 0; j < len2; j++) col[j + 1] = min(min(prevCol[1 + j] + 1, col[j] + 1), prevCol[j] + (s1[i] == s2[j] ? 0 : 1)) ; col.swap(prevCol); } return prevCol[len2]; }
unsigned int GestureRecognizer::levenshteinDistance(const std::vector<int>& a, const std::vector<int>& b) { const size_t len1 = a.size(), len2 = b.size(); std::vector<unsigned int> col(len2 + 1), prevCol(len2 + 1); for (unsigned int i = 0; i < prevCol.size(); i++) prevCol[i] = i; for (unsigned int i = 0; i < len1; i++) { col[0] = i + 1; for (unsigned int j = 0; j < len2; j++) col[j + 1] = std::min(std::min(1 + col[j], 1 + prevCol[1 + j]), prevCol[j] + (a[i] == b[j] ? 0 : 1)); col.swap(prevCol); } return prevCol[len2]; }
unsigned int levenshteinDistance(const T &s1, const T & s2) { const size_t len1 = s1.size(), len2 = s2.size(); QVector<unsigned int> col(len2+1), prevCol(len2+1); for (int i = 0; i < prevCol.size(); i++) prevCol[i] = i; for (unsigned int i = 0; i < len1; i++) { col[0] = i+1; for (unsigned int j = 0; j < len2; j++) col[j+1] = qMin( qMin( 1 + col[j], 1 + prevCol[1 + j]), prevCol[j] + (s1[i]==s2[j] ? 0 : 1) ); col.swap(prevCol); } return prevCol[len2]; }
int KNUtil::similarity(const QString &string1, const QString &string2) { // Replace this into Sift4 algorithm. //Wagner–Fischer algorithm Levenshtein distance. //Based on /* http://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Levenshtein _distance#C.2B.2B*/ //And /*http://www.codeproject.com/Articles/13525/Fast-memory-efficient-Levenshtei n-algorithm*/ //The original Levenshtein Algorithm will used a matrix whose size is //length(string1) * length(string2), this is a common fast, memory efficient //version. It can reduce the memory usage to length(string1) + //length(string2). This won't reduce the time complexity. //Thanks for QString and QChar, a chinese character will be description as //a char. //Initial the size. const unsigned len1=string1.size(), len2=string2.size(); std::vector<size_t> col(len2+1), prevCol(len2+1); //Fills the vector with ascending numbers, starting by 0 //Because of the F**K clang, we can NOT use itoa, or else these thing //following can be done in only one sentence: for(unsigned i=0; i<prevCol.size(); i++) { prevCol[i]=i; } //Use double std::min instead of std::min({,,}). for(unsigned i=0; i<len1; i++) { col[0]=i+1; for(unsigned j=0; j<len2; j++) { col[j+1]=std::min(std::min(1+col[j], 1+prevCol[1+j]), prevCol[j]+(string1[i]!=string2[j])); } std::swap(col, prevCol); } return prevCol[len2]; }
unsigned int levenshteinDistance(const T &s1, const T & s2) { const size_t len1 = s1.size(), len2 = s2.size(); QVector<unsigned int> col(len2+1), prevCol(len2+1); QVector<unsigned int> &c = col; QVector<unsigned int> &p = prevCol; for (int i = 0; i < p.size(); i++) p[i] = i; for (unsigned int i = 0; i < len1; i++) { c[0] = i+1; for (unsigned int j = 0; j < len2; j++) c[j+1] = qMin( qMin( 1 + c[j], 1 + p[1 + j]), p[j] + (s1[i]==s2[j] ? 0 : 1) ); p = col; c = prevCol; } return p[len2]; }