#include "CRSMUtil.hpp" namespace Util { int damerauLevenshteinDistance(const QString& s1, const QString& s2) { int length1 = s1.length(); int length2 = s2.length(); int d[length1+1][length2+1]; int i; int j; int cost; for(i = 0; i <= length1; i++) { d[i][0] = i; } for(j = 0; j <= length2; j++) { d[0][j] = j; } for (i = 0; i < length1;i++) { for(j = 0; j < length2; j++) { if(s1[i] == s2[j]) { cost = 0; } else { cost = 1; } d[i+1][j+1] = std::min({ d[i][j+1] + 1, // delete d[i+1][j] + 1, // insert d[i][j] + cost // substitution }); if((i > 0) && (j > 0) && (s1[i] == s2[j-1]) && (s1[i-1] == s2[j])) { d[i+1][j+1] = std::min( d[i+1][j+1], d[i-1][j-1] + cost // transposition ); } } } return d[length1][length2]; } }