@@ -107,8 +107,22 @@ export function stringify(this: any, key: string, value: any) {
107107 return value ;
108108}
109109
110- export function wagnerFischer ( word : string , directory : string [ ] ) : { distance : number , index : number , string : string ; } [ ] {
111- const distances : { distance : number , index : number , string : string ; } [ ] = [ ] ;
110+
111+ export function closeEnoughQuestionMark ( distance : number , text : string ) : boolean {
112+ return distance < 1.5 * Math . sqrt ( text . length ) ; // more lenient for longer words
113+ }
114+
115+ type wagnerFischerResult = {
116+ distance : number ,
117+ index : number ,
118+ string : string | string [ ] ,
119+ } ;
120+ /** Wagner–Fischer algorithm is a dynamic programming algorithm that computes the edit distance between two strings of characters */
121+ export function wagnerFischer ( word : string , directory : string [ ] ) : { distance : number , index : number , string : string ; } [ ] ;
122+ /** Interestingly this also works with arrays of strings */
123+ export function wagnerFischer ( words : string [ ] , directorys : string [ ] [ ] ) : { distance : number , index : number , string : string [ ] ; } [ ] ;
124+ export function wagnerFischer ( word : string | string [ ] , directory : string [ ] | string [ ] [ ] ) : wagnerFischerResult [ ] {
125+ const distances : wagnerFischerResult [ ] = [ ] ;
112126 let index = 0 ;
113127
114128 for ( const targetWord of directory ) {
@@ -137,7 +151,12 @@ export function wagnerFischer(word: string, directory: string[]): { distance: nu
137151 prev = next ;
138152 }
139153
140- const distance = { 'distance' : prev [ targetWord . length ] , index : index , string : targetWord } ;
154+ const distance = {
155+ distance : prev [ targetWord . length ] ,
156+ index : index ,
157+ string : targetWord ,
158+ prev : prev ,
159+ } ;
141160 distances . push ( distance ) ;
142161
143162 index ++ ;
0 commit comments