Skip to content

Commit 1e3eca0

Browse files
Merge branch 'main' of https://github.com/powpow58/F-Sharp into main (#10)
* Update IsPalindrome.fs * Update IsPalindrome.fs * Updated Perfect Numbers, Check Anagram. Is Palindrome, Lower, Remove Duplicates, Reverse Letters, Reverse Words, Upper Algorithms * Added Strings Test Methods * Updated RemoveDeplicatesTests * Updated ReverseLettersTests * Fixed String Algorithms * Update RabinKarp.fs * Jaro Winkler Co-authored-by: Allister Isaiah Harvey <[email protected]>
1 parent ef1cd90 commit 1e3eca0

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Algorithms.Tests.Strings
1+
namespace Algorithms.Tests.Strings
22

33
open Microsoft.VisualStudio.TestTools.UnitTesting
44
open Algorithms.Strings
@@ -7,13 +7,13 @@ open Algorithms.Strings
77
type JaroWinklerTests () =
88
[<TestMethod>]
99
[<DataRow("martha", "marhta", 0.9611111111111111)>]
10-
[<DataRow("CRATE", "TRACE", 0.8483333333333334)>]
10+
[<DataRow("CRATE", "TRACE", 0.7333333333333334)>]
1111
[<DataRow("test", "dbdbdbdb", 0.0)>]
12-
[<DataRow("hello world", "HeLLo W0rlD", 0.7131313131313132)>]
12+
[<DataRow("test", "test", 1.0)>]
13+
[<DataRow("hello world", "HeLLo W0rlD", 0.6363636363636364)>]
1314
[<DataRow("test", "", 0.0)>]
14-
[<DataRow("hello", "world", 0.49)>]
15-
[<DataRow("hell**o", "*world", 0.373015873015873)>]
15+
[<DataRow("hello", "world", 0.4666666666666666)>]
16+
[<DataRow("hell**o", "*world", 0.4365079365079365)>]
1617
member this.jaroWinkler (str1:string, str2:string, expected:float) =
1718
let actual = JaroWinkler.jaroWinkler(str1, str2)
1819
Assert.AreEqual(expected, actual)
19-

Algorithms/Strings/JaroWinkler.fs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Algorithms.Strings
1+
namespace Algorithms.Strings
22

33
open Microsoft.FSharp.Collections
44

@@ -16,39 +16,40 @@ module JaroWinkler =
1616
let mutable istr1 = _str1
1717
let mutable istr2 = _str2
1818
let mutable matched = []
19-
19+
2020
let limit =
2121
int (floor (double (min _str1.Length str2.Length) / 2.0))
22-
22+
2323
istr1
2424
|> Seq.iteri
2525
(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
3131
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+
3435
matched |> List.fold (+) ""
35-
36+
3637
// matching characters
3738
let matching1 = getMatchedCharacters (str1, str2)
3839
let matching2 = getMatchedCharacters (str2, str1)
3940
let matchCount = matching1.Length
4041
let mutable jaro = 0.0
41-
42+
4243
// Transposition
4344
let transpositions =
4445
floor (
4546
double (
4647
(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) ]
4849
.Length
4950
)
5051
)
51-
52+
5253
if matchCount = 0 then
5354
jaro <- 0.0
5455
else
@@ -58,13 +59,18 @@ module JaroWinkler =
5859
+ (double) matchCount / (double) str2.Length
5960
+ ((double) matchCount - transpositions)
6061
/ (double) matchCount)
61-
62+
6263
// Common prefix up to 4 characters
6364
let mutable prefixLen = 0
6465

66+
let mutable c1C2BoolList : bool list = []
67+
6568
if str1.Length = str2.Length then
6669
for c1, c2 in Array.zip (str1.[..4].ToCharArray()) (str2.[..4].ToCharArray()) do
6770
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

Comments
 (0)