Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
59
Task/Jaro-similarity/Mathematica/jaro-similarity.math
Normal file
59
Task/Jaro-similarity/Mathematica/jaro-similarity.math
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
ClearAll[JaroDistance]
|
||||
JaroDistance[s_String, t_String] := Module[{slen, tlen, maxdistance, smatches, tmatches, matches, transpositions, start, end, k, schar, tchar},
|
||||
slen = StringLength[s];
|
||||
tlen = StringLength[t];
|
||||
schar = Characters[s];
|
||||
tchar = Characters[t];
|
||||
If[slen == tlen == 0,
|
||||
1
|
||||
,
|
||||
maxdistance = Floor[Max[slen, tlen]/2] - 1;
|
||||
smatches = ConstantArray[False, slen];
|
||||
tmatches = ConstantArray[False, tlen];
|
||||
matches = transpositions = 0;
|
||||
Do[
|
||||
start = Max[0, i - maxdistance];
|
||||
end = Min[i + maxdistance + 1, tlen];
|
||||
start = Max[1, i - maxdistance];
|
||||
end = Min[i + maxdistance + 1, tlen];
|
||||
|
||||
Do[
|
||||
If[! tmatches[[j]],
|
||||
If[schar[[i]] == tchar[[j]],
|
||||
smatches[[i]] = True;
|
||||
tmatches[[j]] = True;
|
||||
matches++;
|
||||
Break[];
|
||||
]
|
||||
]
|
||||
,
|
||||
{j, start, end}
|
||||
]
|
||||
,
|
||||
{i, slen}
|
||||
];
|
||||
If[matches == 0,
|
||||
0
|
||||
,
|
||||
k = 1;
|
||||
Do[
|
||||
If[smatches[[i]],
|
||||
While[! tmatches[[k]],
|
||||
k++;
|
||||
];
|
||||
If[schar[[i]] != tchar[[k]],
|
||||
transpositions++;
|
||||
];
|
||||
k++;
|
||||
]
|
||||
,
|
||||
{i, slen}
|
||||
];
|
||||
N@(matches/slen + matches/tlen + (matches - transpositions/2)/matches)/3
|
||||
]
|
||||
]
|
||||
]
|
||||
JaroDistance["DWAYNE", "DUANE"]
|
||||
JaroDistance["MARTHA", "MARHTA"]
|
||||
JaroDistance["DIXON", "DICKSONX"]
|
||||
JaroDistance["JELLYFISH", "SMELLYFISH"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue