@@ -1176,12 +1176,12 @@ export function longestCommonSuffix(x: string, y: string): string {
11761176 * ```
11771177 */
11781178export function longestUncommonInfixes ( x : string , y : string ) : [ string , string ] {
1179- const X = x . length , Y = y . length ;
11801179 const i = longestCommonPrefix ( x , y ) . length ;
1181- const j = longestCommonSuffix ( x , y ) . length ;
1182- return [ x . substring ( i , X - j ) , y . substring ( i , Y - j ) ] ;
1183- }
1184- //#endregion
1180+ const xs = x . substring ( i ) ;
1181+ const ys = y . substring ( i ) ;
1182+ const j = longestCommonSuffix ( xs , ys ) . length ;
1183+ return [ x . substring ( i , x . length - j ) , y . substring ( i , y . length - j ) ] ;
1184+ } //#endregion
11851185
11861186
11871187
@@ -2042,20 +2042,24 @@ export function tverskyDistance(x: string, y: string, n: number, a: number=1, b:
20422042 * @returns [matching characters in x, matching characters in y]
20432043 */
20442044function rangedMatches ( x : string , y : string , d : number ) : [ string , string ] {
2045- // 1. Prepare strike-off buffer
2046- let mx = "" , my = "" ;
2045+ let mx = "" ;
2046+ let my = "" ;
20472047 const by : ( string | null ) [ ] = Array . from ( y ) ;
2048- // 2. Get matches in first string
2049- for ( let i = 0 , I = x . length ; i < I ; i ++ ) {
2050- let j = by . lastIndexOf ( x . charAt ( i ) , i ) ;
2051- if ( j < 0 || i - j > d ) j = by . indexOf ( x . charAt ( i ) , i + 1 ) ;
2052- if ( j < 0 || j - i > d ) continue ;
2053- mx += x . charAt ( i ) ;
2054- by [ j ] = null ;
2048+ for ( let i = 0 ; i < x . length ; i ++ ) {
2049+ const c = x . charAt ( i ) ;
2050+ const lo = Math . max ( 0 , i - d ) ;
2051+ const hi = Math . min ( y . length - 1 , i + d ) ;
2052+ for ( let j = lo ; j <= hi ; j ++ ) {
2053+ if ( by [ j ] === c ) {
2054+ mx += c ;
2055+ by [ j ] = null ;
2056+ break ;
2057+ }
2058+ }
2059+ }
2060+ for ( let j = 0 ; j < y . length ; j ++ ) {
2061+ if ( by [ j ] === null ) my += y . charAt ( j ) ;
20552062 }
2056- // 3. Get matches in second string
2057- for ( let j = 0 , J = y . length ; j < J ; j ++ )
2058- if ( by [ j ] == null ) my += y . charAt ( j ) ;
20592063 return [ mx , my ] ;
20602064}
20612065// countRangedMatches()?
@@ -2079,11 +2083,14 @@ function rangedMatches(x: string, y: string, d: number): [string, string] {
20792083 * ```
20802084 */
20812085export function jaroSimilarity ( x : string , y : string ) : number {
2082- const X = x . length , Y = y . length ;
2083- const d = Math . floor ( Math . max ( X , Y ) / 2 ) - 1 ;
2084- const [ mx , my ] = rangedMatches ( x , y , d ) , m = mx . length ;
2085- const t = hammingDistance ( mx , my ) / 2 ;
2086- return m ? ( m / X + m / Y + ( m - t ) / m ) / 3 : 0 ;
2086+ const X = x . length ;
2087+ const Y = y . length ;
2088+ if ( X === 0 && Y === 0 ) return 1 ;
2089+ const d = Math . max ( 0 , Math . floor ( Math . max ( X , Y ) / 2 ) - 1 ) ;
2090+ const [ mx , my ] = rangedMatches ( x , y , d ) ;
2091+ const m = mx . length ;
2092+ const t = hammingDistance ( mx , my ) / 2 ;
2093+ return m ? ( m / X + m / Y + ( m - t ) / m ) / 3 : 0 ;
20872094}
20882095
20892096
0 commit comments