1
- namespace Algorithms.Strings
1
+ namespace Algorithms.Strings
2
2
3
3
open Microsoft.FSharp .Collections
4
4
@@ -16,39 +16,40 @@ module JaroWinkler =
16
16
let mutable istr1 = _ str1
17
17
let mutable istr2 = _ str2
18
18
let mutable matched = []
19
-
19
+
20
20
let limit =
21
21
int ( floor ( double ( min _ str1.Length str2.Length) / 2.0 ))
22
-
22
+
23
23
istr1
24
24
|> Seq.iteri
25
25
( fun i l ->
26
- let left = ( int) ( max 0 ( i - limit))
27
-
28
- let right = ( int) ( min ( i + limit + 1 ) istr2.Length)
29
-
30
- if ( istr2.[ left.. right]) .Contains( l) then
26
+ let left = int( max 0 ( i - limit))
27
+
28
+ let right = int( min ( i + limit + 1 ) istr2.Length)
29
+
30
+ if ( istr2.[ left.. right - 1 ]) .Contains( l) then
31
31
matched <- List.append matched [ ( string) l ]
32
- istr2 <- $" {istr2.[0..istr2.IndexOf(l)]} {istr2.[istr2.IndexOf(l) + 1..]}" )
33
-
32
+ let myIndex = ( istr2.IndexOf( l))
33
+ istr2 <- $" {istr2.[0..istr2.IndexOf(l) - 1]} {istr2.[istr2.IndexOf(l) + 1..]}" )
34
+
34
35
matched |> List.fold (+) " "
35
-
36
+
36
37
// matching characters
37
38
let matching1 = getMatchedCharacters ( str1, str2)
38
39
let matching2 = getMatchedCharacters ( str2, str1)
39
40
let matchCount = matching1.Length
40
41
let mutable jaro = 0.0
41
-
42
+
42
43
// Transposition
43
44
let transpositions =
44
45
floor (
45
46
double (
46
47
( double)
47
- [ for c1, c2 in List.zip [ matching1 ] [ matching2 ] -> ( c1, c2) ]
48
+ [ for c1, c2 in List.zip [ matching1 ] [ matching2 ] do if c1 <> c2 then ( c1, c2) ]
48
49
.Length
49
50
)
50
51
)
51
-
52
+
52
53
if matchCount = 0 then
53
54
jaro <- 0.0
54
55
else
@@ -58,13 +59,18 @@ module JaroWinkler =
58
59
+ ( double) matchCount / ( double) str2.Length
59
60
+ (( double) matchCount - transpositions)
60
61
/ ( double) matchCount)
61
-
62
+
62
63
// Common prefix up to 4 characters
63
64
let mutable prefixLen = 0
64
65
66
+ let mutable c1C2BoolList : bool list = []
67
+
65
68
if str1.Length = str2.Length then
66
69
for c1, c2 in Array.zip ( str1.[.. 4 ]. ToCharArray()) ( str2.[.. 4 ]. ToCharArray()) do
67
70
if c1 = c2 then
68
- prefixLen <- prefixLen + 1
69
-
70
- jaro + 0.1 * ( double) prefixLen * ( 1.0 - jaro)
71
+ c1C2BoolList <- List.append c1C2BoolList [ true ]
72
+ else
73
+ c1C2BoolList <- List.append c1C2BoolList [ false ]
74
+ if ( c1C2BoolList |> List.exists( fun x -> ( not x))) then
75
+ prefixLen <- prefixLen + ( c1C2BoolList |> List.findIndex( fun x -> ( not x)))
76
+ jaro + 0.1 * ( double) prefixLen * ( 1.0 - jaro)
0 commit comments