@@ -14,39 +14,38 @@ define(String.prototype, {
14
14
replaceAll : function ( find : string , replace : string ) {
15
15
return this . replace ( new RegExp ( escapeRegExp ( find ) , "g" ) , replace ) ;
16
16
} ,
17
- similarity : function ( second : string ) {
18
- const first = this . replace ( / \s + / g, "" ) ;
19
- second = second . replace ( / \s + / g, "" ) ;
20
- if ( ! first . length && ! second . length ) return 1 ; // if both are empty strings
21
- if ( ! first . length || ! second . length ) return 0 ; // if only one is empty string
22
- if ( first === second ) return 1 ; // identical
23
- if ( first . length === 1 && second . length === 1 ) return 0 ; // both are 1-letter strings
24
- if ( first . length < 2 || second . length < 2 ) return 0 ; // if either is a 1-letter string
25
- const firstBigrams = new Map ( ) ;
26
- const lowBigrams = new Map ( ) ;
27
- for ( let i = 0 ; i < first . length - 1 ; i ++ ) {
28
- const bigram = first . substring ( i , i + 2 ) ;
29
- const count = firstBigrams . has ( bigram ) ? firstBigrams . get ( bigram ) + 1 : 1 ;
30
- const countLow = lowBigrams . has ( bigram . toLowerCase ( ) ) ? lowBigrams . get ( bigram . toLowerCase ( ) ) + 1 : 1 ;
17
+ similarity : function ( s2 : string ) {
18
+ var s1 = this . toLowerCase ( ) ;
19
+ s2 = s2 . toLowerCase ( ) ;
31
20
32
- lowBigrams . set ( bigram . toLowerCase ( ) , countLow ) ;
33
- firstBigrams . set ( bigram , count ) ;
21
+ if ( s1 . length < s2 . length ) {
22
+ s1 = s2 ;
23
+ s2 = this ;
34
24
}
35
- let intersectionSize = 0 ;
36
- for ( let i = 0 ; i < second . length - 1 ; i ++ ) {
37
- const bigram = second . substring ( i , i + 2 ) ;
38
- const count = firstBigrams . has ( bigram ) ? firstBigrams . get ( bigram ) : 0 ;
39
- const countLow = firstBigrams . has ( bigram . toLowerCase ( ) ) ? firstBigrams . get ( bigram . toLowerCase ( ) ) : 0 ;
40
- if ( count > 0 ) {
41
- firstBigrams . set ( bigram , count - 1 ) ;
42
- intersectionSize ++ ;
43
- }
44
- if ( countLow > 0 ) {
45
- firstBigrams . set ( bigram . toLowerCase ( ) , countLow - 1 ) ;
46
- intersectionSize += 0.9 ;
25
+ var longerLength = s1 . length ;
26
+ if ( longerLength == 0 ) {
27
+ return 1.0 ;
28
+ }
29
+
30
+ var costs = new Array ( ) ;
31
+ for ( var i = 0 ; i <= s1 . length ; i ++ ) {
32
+ var lastValue = i ;
33
+ for ( var j = 0 ; j <= s2 . length ; j ++ ) {
34
+ if ( i == 0 ) costs [ j ] = j ;
35
+ else {
36
+ if ( j > 0 ) {
37
+ var newValue = costs [ j - 1 ] ;
38
+ if ( s1 . charAt ( i - 1 ) != s2 . charAt ( j - 1 ) )
39
+ newValue = Math . min ( Math . min ( newValue , lastValue ) , costs [ j ] ) + 1 ;
40
+ costs [ j - 1 ] = lastValue ;
41
+ lastValue = newValue ;
42
+ }
43
+ }
47
44
}
45
+ if ( i > 0 ) costs [ s2 . length ] = lastValue ;
48
46
}
49
- return ( 2.0 * intersectionSize ) / ( first . length + second . length - 2 ) ;
47
+
48
+ return ( longerLength - costs [ s2 . length ] ) / parseFloat ( longerLength ) ;
50
49
} ,
51
50
join : function ( iterate : string [ ] ) {
52
51
if ( typeof iterate === "string" ) return iterate ;
0 commit comments