Skip to content

Commit 587643e

Browse files
committed
bugfix for similar_text.js
Fixing similar_text.js based on findings on http://stackoverflow.com/questions/14136349/how-does-similar-text-work Basically, in some cases the algorithm is not implemented correctly. It shouldn't produce the same result when swapping the input words but it seems to do so.
1 parent 2eb33d0 commit 587643e

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

functions/strings/similar_text.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function similar_text (first, second, percent) {
99
// * returns 2: 0
1010
// * example 3: similar_text('Hello World!', null, 1);
1111
// * returns 3: 58.33
12+
// * bugfixed by: Jarkko Rantavuori based on findings in stackoverflow (http://stackoverflow.com/questions/14136349/how-does-similar-text-work)
1213
if (first === null || second === null || typeof first === 'undefined' || typeof second === 'undefined') {
1314
return 0;
1415
}
@@ -41,7 +42,7 @@ function similar_text (first, second, percent) {
4142

4243
if (sum) {
4344
if (pos1 && pos2) {
44-
sum += this.similar_text(first.substr(0, pos2), second.substr(0, pos2));
45+
sum += this.similar_text(first.substr(0, pos1), second.substr(0, pos2));
4546
}
4647

4748
if ((pos1 + max < firstLength) && (pos2 + max < secondLength)) {

0 commit comments

Comments
 (0)