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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.providers.contacts;
import com.android.providers.contacts.ContactsDatabaseHelper.NameLookupType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
/**
* Logic for matching contacts' data and accumulating match scores.
*/
public class ContactMatcher {
// Best possible match score
public static final int MAX_SCORE = 100;
// Suggest to aggregate contacts if their match score is equal or greater than this threshold
public static final int SCORE_THRESHOLD_SUGGEST = 50;
// Automatically aggregate contacts if their match score is equal or greater than this threshold
public static final int SCORE_THRESHOLD_PRIMARY = 70;
// Automatically aggregate contacts if the match score is equal or greater than this threshold
// and there is a secondary match (phone number, email etc).
public static final int SCORE_THRESHOLD_SECONDARY = 50;
// Score for missing data (as opposed to present data but a bad match)
private static final int NO_DATA_SCORE = -1;
// Score for matching phone numbers
private static final int PHONE_MATCH_SCORE = 71;
// Score for matching email addresses
private static final int EMAIL_MATCH_SCORE = 71;
// Score for matching nickname
private static final int NICKNAME_MATCH_SCORE = 71;
// Maximum number of characters in a name to be considered by the matching algorithm.
private static final int MAX_MATCHED_NAME_LENGTH = 30;
// Scores a multiplied by this number to allow room for "fractional" scores
private static final int SCORE_SCALE = 1000;
public static final int MATCHING_ALGORITHM_EXACT = 0;
public static final int MATCHING_ALGORITHM_CONSERVATIVE = 1;
public static final int MATCHING_ALGORITHM_APPROXIMATE = 2;
// Minimum edit distance between two names to be considered an approximate match
public static final float APPROXIMATE_MATCH_THRESHOLD = 0.82f;
/**
* Name matching scores: a matrix by name type vs. candidate lookup type.
* For example, if the name type is "full name" while we are looking for a
* "full name", the score may be 99. If we are looking for a "nickname" but
* find "first name", the score may be 50 (see specific scores defined
* below.)
* <p>
* For approximate matching, we have a range of scores, let's say 40-70. Depending one how
* similar the two strings are, the score will be somewhere between 40 and 70, with the exact
* match producing the score of 70. The score may also be 0 if the similarity (distance)
* between the strings is below the threshold.
* <p>
* We use a string matching algorithm, which is particularly suited for
* name matching. See {@link NameDistance}.
*/
private static int[] sMinScore =
new int[NameLookupType.TYPE_COUNT * NameLookupType.TYPE_COUNT];
private static int[] sMaxScore =
new int[NameLookupType.TYPE_COUNT * NameLookupType.TYPE_COUNT];
/*
* Note: the reverse names ({@link NameLookupType#FULL_NAME_REVERSE},
* {@link NameLookupType#FULL_NAME_REVERSE_CONCATENATED} may appear to be redundant. They are
* not! They are useful in three-way aggregation cases when we have, for example, both
* John Smith and Smith John. A third contact with the name John Smith should be aggregated
* with the former rather than the latter. This is why "reverse" matches have slightly lower
* scores than direct matches.
*/
static {
setScoreRange(NameLookupType.NAME_EXACT,
NameLookupType.NAME_EXACT, 99, 99);
setScoreRange(NameLookupType.NAME_VARIANT,
NameLookupType.NAME_VARIANT, 90, 90);
setScoreRange(NameLookupType.NAME_COLLATION_KEY,
NameLookupType.NAME_COLLATION_KEY, 50, 80);
setScoreRange(NameLookupType.NAME_COLLATION_KEY,
NameLookupType.EMAIL_BASED_NICKNAME, 30, 60);
setScoreRange(NameLookupType.NAME_COLLATION_KEY,
NameLookupType.NICKNAME, 50, 60);
setScoreRange(NameLookupType.EMAIL_BASED_NICKNAME,
NameLookupType.EMAIL_BASED_NICKNAME, 50, 60);
setScoreRange(NameLookupType.EMAIL_BASED_NICKNAME,
NameLookupType.NAME_COLLATION_KEY, 50, 60);
setScoreRange(NameLookupType.EMAIL_BASED_NICKNAME,
NameLookupType.NICKNAME, 50, 60);
setScoreRange(NameLookupType.NICKNAME,
NameLookupType.NICKNAME, 50, 60);
setScoreRange(NameLookupType.NICKNAME,
NameLookupType.NAME_COLLATION_KEY, 50, 60);
setScoreRange(NameLookupType.NICKNAME,
NameLookupType.EMAIL_BASED_NICKNAME, 50, 60);
}
/**
* Populates the cells of the score matrix and score span matrix
* corresponding to the {@code candidateNameType} and {@code nameType}.
*/
private static void setScoreRange(int candidateNameType, int nameType, int scoreFrom, int scoreTo) {
int index = nameType * NameLookupType.TYPE_COUNT + candidateNameType;
sMinScore[index] = scoreFrom;
sMaxScore[index] = scoreTo;
}
/**
* Returns the lower range for the match score for the given {@code candidateNameType} and
* {@code nameType}.
*/
private static int getMinScore(int candidateNameType, int nameType) {
int index = nameType * NameLookupType.TYPE_COUNT + candidateNameType;
return sMinScore[index];
}
/**
* Returns the upper range for the match score for the given {@code candidateNameType} and
* {@code nameType}.
*/
private static int getMaxScore(int candidateNameType, int nameType) {
int index = nameType * NameLookupType.TYPE_COUNT + candidateNameType;
return sMaxScore[index];
}
/**
* Captures the max score and match count for a specific contact. Used in an
* contactId - MatchScore map.
*/
public static class MatchScore implements Comparable<MatchScore> {
private long mContactId;
private boolean mKeepIn;
private boolean mKeepOut;
private int mPrimaryScore;
private int mSecondaryScore;
private int mMatchCount;
public MatchScore(long contactId) {
this.mContactId = contactId;
}
public void reset(long contactId) {
this.mContactId = contactId;
mKeepIn = false;
mKeepOut = false;
mPrimaryScore = 0;
mSecondaryScore = 0;
mMatchCount = 0;
}
public long getContactId() {
return mContactId;
}
public void updatePrimaryScore(int score) {
if (score > mPrimaryScore) {
mPrimaryScore = score;
}
mMatchCount++;
}
public void updateSecondaryScore(int score) {
if (score > mSecondaryScore) {
mSecondaryScore = score;
}
mMatchCount++;
}
public void keepIn() {
mKeepIn = true;
}
public void keepOut() {
mKeepOut = true;
}
public int getScore() {
if (mKeepOut) {
return 0;
}
if (mKeepIn) {
return MAX_SCORE;
}
int score = (mPrimaryScore > mSecondaryScore ? mPrimaryScore : mSecondaryScore);
// Ensure that of two contacts with the same match score the one with more matching
// data elements wins.
return score * SCORE_SCALE + mMatchCount;
}
/**
* Descending order of match score.
*/
public int compareTo(MatchScore another) {
return another.getScore() - getScore();
}
@Override
public String toString() {
return mContactId + ": " + mPrimaryScore + "/" + mSecondaryScore + "(" + mMatchCount
+ ")";
}
}
private final HashMap<Long, MatchScore> mScores = new HashMap<Long, MatchScore>();
private final ArrayList<MatchScore> mScoreList = new ArrayList<MatchScore>();
private int mScoreCount = 0;
private final NameDistance mNameDistanceConservative = new NameDistance();
private final NameDistance mNameDistanceApproximate = new NameDistance(MAX_MATCHED_NAME_LENGTH);
private MatchScore getMatchingScore(long contactId) {
MatchScore matchingScore = mScores.get(contactId);
if (matchingScore == null) {
if (mScoreList.size() > mScoreCount) {
matchingScore = mScoreList.get(mScoreCount);
matchingScore.reset(contactId);
} else {
matchingScore = new MatchScore(contactId);
mScoreList.add(matchingScore);
}
mScoreCount++;
mScores.put(contactId, matchingScore);
}
return matchingScore;
}
/**
* Checks if there is a match and updates the overall score for the
* specified contact for a discovered match. The new score is determined
* by the prior score, by the type of name we were looking for, the type
* of name we found and, if the match is approximate, the distance between the candidate and
* actual name.
*/
public void matchName(long contactId, int candidateNameType, String candidateName,
int nameType, String name, int algorithm) {
int maxScore = getMaxScore(candidateNameType, nameType);
if (maxScore == 0) {
return;
}
if (candidateName.equals(name)) {
updatePrimaryScore(contactId, maxScore);
return;
}
if (algorithm == MATCHING_ALGORITHM_EXACT) {
return;
}
int minScore = getMinScore(candidateNameType, nameType);
if (minScore == maxScore) {
return;
}
byte[] decodedCandidateName = Hex.decodeHex(candidateName);
byte[] decodedName = Hex.decodeHex(name);
NameDistance nameDistance = algorithm == MATCHING_ALGORITHM_CONSERVATIVE ?
mNameDistanceConservative : mNameDistanceApproximate;
int score;
float distance = nameDistance.getDistance(decodedCandidateName, decodedName);
if (distance > APPROXIMATE_MATCH_THRESHOLD) {
score = (int)(minScore + (maxScore - minScore) * (1.0f - distance));
} else {
score = 0;
}
updatePrimaryScore(contactId, score);
}
public void updateScoreWithPhoneNumberMatch(long contactId) {
updateSecondaryScore(contactId, PHONE_MATCH_SCORE);
}
public void updateScoreWithEmailMatch(long contactId) {
updateSecondaryScore(contactId, EMAIL_MATCH_SCORE);
}
public void updateScoreWithNicknameMatch(long contactId) {
updateSecondaryScore(contactId, NICKNAME_MATCH_SCORE);
}
private void updatePrimaryScore(long contactId, int score) {
getMatchingScore(contactId).updatePrimaryScore(score);
}
private void updateSecondaryScore(long contactId, int score) {
getMatchingScore(contactId).updateSecondaryScore(score);
}
public void keepIn(long contactId) {
getMatchingScore(contactId).keepIn();
}
public void keepOut(long contactId) {
getMatchingScore(contactId).keepOut();
}
public void clear() {
mScores.clear();
mScoreCount = 0;
}
/**
* Returns a list of IDs for contacts that are matched on secondary data elements
* (phone number, email address, nickname). We still need to obtain the approximate
* primary score for those contacts to determine if any of them should be aggregated.
* <p>
* May return null.
*/
public List<Long> prepareSecondaryMatchCandidates(int threshold) {
ArrayList<Long> contactIds = null;
for (int i = 0; i < mScoreCount; i++) {
MatchScore score = mScoreList.get(i);
if (score.mKeepOut) {
continue;
}
int s = score.mSecondaryScore;
if (s >= threshold) {
if (contactIds == null) {
contactIds = new ArrayList<Long>();
}
contactIds.add(score.mContactId);
}
score.mPrimaryScore = NO_DATA_SCORE;
}
return contactIds;
}
/**
* Returns the contactId with the best match score over the specified threshold or -1
* if no such contact is found.
*/
public long pickBestMatch(int threshold) {
long contactId = -1;
int maxScore = 0;
for (int i = 0; i < mScoreCount; i++) {
MatchScore score = mScoreList.get(i);
if (score.mKeepOut) {
continue;
}
if (score.mKeepIn) {
return score.mContactId;
}
int s = score.mPrimaryScore;
if (s == NO_DATA_SCORE) {
s = score.mSecondaryScore;
}
if (s >= threshold && s > maxScore) {
contactId = score.mContactId;
maxScore = s;
}
}
return contactId;
}
/**
* Returns matches in the order of descending score.
*/
public List<MatchScore> pickBestMatches(int threshold) {
int scaledThreshold = threshold * SCORE_SCALE;
List<MatchScore> matches = mScoreList.subList(0, mScoreCount);
Collections.sort(matches);
int count = 0;
for (int i = 0; i < mScoreCount; i++) {
MatchScore matchScore = matches.get(i);
if (matchScore.getScore() >= scaledThreshold) {
count++;
} else {
break;
}
}
return matches.subList(0, count);
}
@Override
public String toString() {
return mScoreList.toString();
}
}
|