-
Notifications
You must be signed in to change notification settings - Fork 1
/
jaroWinkler.cpp
153 lines (124 loc) · 3.92 KB
/
jaroWinkler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// modified uf8-supported version of
// https://github.com/TriviaMarketing/Jaro-Winkler
#include <string>
#include <vector>
#include <algorithm>
#include "jaroWinkler.hpp"
#include "utf8_unicode.hpp"
using std::string;
using std::vector;
using std::min;
using std::max;
double jaroDistance(const string& a, const string& b)
{
// Register strings length.
//int aLength(a.size());
//int bLength(b.size());
// Register strings length utf8 compatible
vector<string> a_ = utf8_split(a);
vector<string> b_ = utf8_split(b);
int aLength = a_.size();
int bLength = b_.size();
// If one string has null length, we return 0.
if (aLength == 0 || bLength == 0)
{
return 0.0;
}
// Calculate max length range.
int maxRange(max(0, max(aLength, bLength) / 2 - 1));
// Creates 2 vectors of integers.
vector<bool> aMatch(aLength, false);
vector<bool> bMatch(bLength, false);
// Calculate matching characters.
int matchingCharacters(0);
for (int aIndex(0); aIndex < aLength; ++aIndex)
{
// Calculate window test limits (limit inferior to 0 and superior to bLength).
int minIndex(max(aIndex - maxRange, 0));
int maxIndex(min(aIndex + maxRange + 1, bLength));
if (minIndex >= maxIndex)
{
// No more common character because we don't have characters in b to test with characters in a.
break;
}
for (int bIndex(minIndex); bIndex < maxIndex; ++bIndex)
{
if (!bMatch.at(bIndex) && a_.at(aIndex) == b_.at(bIndex))
{
// Found some new match.
aMatch[aIndex] = true;
bMatch[bIndex] = true;
++matchingCharacters;
break;
}
}
}
// If no matching characters, we return 0.
if (matchingCharacters == 0)
{
return 0.0;
}
// Calculate character transpositions.
vector<int> aPosition(matchingCharacters, 0);
vector<int> bPosition(matchingCharacters, 0);
for (int aIndex(0), positionIndex(0); aIndex < aLength; ++aIndex)
{
if (aMatch.at(aIndex))
{
aPosition[positionIndex] = aIndex;
++positionIndex;
}
}
for (int bIndex(0), positionIndex(0); bIndex < bLength; ++bIndex)
{
if (bMatch.at(bIndex))
{
bPosition[positionIndex] = bIndex;
++positionIndex;
}
}
// Counting half-transpositions.
int transpositions(0);
for (int index(0); index < matchingCharacters; ++index)
{
if (a_.at(aPosition.at(index)) != b_.at(bPosition.at(index)))
{
++transpositions;
}
}
// Clear the vectors
//aMatch.clear();
//bMatch.clear();
// Calculate Jaro distance.
return (
JARO_WEIGHT_STRING_A * matchingCharacters / aLength +
JARO_WEIGHT_STRING_B * matchingCharacters / bLength +
JARO_WEIGHT_TRANSPOSITIONS * (matchingCharacters - transpositions / 2) / matchingCharacters
);
}
double jaroWinklerDistance(const string&a, const string& b)
{
// Calculate Jaro distance.
double distance(jaroDistance(a, b));
vector<string> a_ = utf8_split(a);
vector<string> b_ = utf8_split(b);
if (distance > JARO_WINKLER_BOOST_THRESHOLD)
{
// Calculate common string prefix.
int commonPrefix(0);
for (int index(0), indexEnd(min(min(size_t(a_.size()),size_t(b_.size())), size_t(JARO_WINKLER_PREFIX_SIZE))); index < indexEnd; ++index)
{
if (a_.at(index) == b_.at(index))
{
++commonPrefix;
}
else
{
break;
}
}
// Calculate Jaro-Winkler distance.
distance += JARO_WINKLER_SCALING_FACTOR * commonPrefix * (1.0 - distance);
}
return distance;
}