src/lib/src/functions.cpp
/**
 * Return the levenshtein distance between two strings.
 * @param	s1	First string.
 * @param	s2	Second string.
 * @return		The levenshtein distance between s1 and s2.
 */
Return the levenshtein distance between two strings.
@param       s1      First string.
@param       s2      Second string.
@return              The levenshtein distance between s1 and s2.
Return the levenshtein distance between two strings . @param s1 First string . @param s2 Second string . @return The levenshtein distance between s1 and s2 .
int levenshtein(QString s1, QString s2)
{
	const int len1 = s1.size(), len2 = s2.size();
	QVector<QVector<int>> d(len1 + 1, QVector<int>(len2 + 1));
	d[0][0] = 0;
	for (int i = 1; i <= len1; ++i) {
		d[i][0] = i;
	}
	for (int i = 1; i <= len2; ++i) {
		d[0][i] = i;
	}
	for (int i = 1; i <= len1; ++i) {
		for (int j = 1; j <= len2; ++j) {
			const int a = qMin(d[i - 1][j] + 1, d[i][j - 1] + 1);
			const int b = d[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 0 : 1);
			d[i][j] = qMin(a, b);
		}
	}
	return d[len1][len2];
}
int levenshtein ( QString s1 , QString s2 ) { const int len1 = s1 . size ( ) , len2 = s2 . size ( ) ; QVector < QVector < int > > d ( len1 + 1 , QVector < int > ( len2 + 1 ) ) ; d [ 0 ] [ 0 ] = 0 ; for ( int i = 1 ; i <= len1 ; ++ i ) { d [ i ] [ 0 ] = i ; } for ( int i = 1 ; i <= len2 ; ++ i ) { d [ 0 ] [ i ] = i ; } for ( int i = 1 ; i <= len1 ; ++ i ) { for ( int j = 1 ; j <= len2 ; ++ j ) { const int a = qMin ( d [ i - 1 ] [ j ] + 1 , d [ i ] [ j - 1 ] + 1 ) ; const int b = d [ i - 1 ] [ j - 1 ] + ( s1 [ i - 1 ] == s2 [ j - 1 ] ? 0 : 1 ) ; d [ i ] [ j ] = qMin ( a , b ) ; } } return d [ len1 ] [ len2 ] ; }
Return the levenshtein distance between two strings.
Return the levenshtein distance between two strings .