1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.codec.language;
18
19 import java.util.Locale;
20
21 import org.apache.commons.codec.EncoderException;
22 import org.apache.commons.codec.StringEncoder;
23
24
25
26
27
28
29
30
31
32
33 public class MatchRatingApproachEncoder implements StringEncoder {
34
35 private static final String SPACE = " ";
36
37 private static final String EMPTY = "";
38
39
40
41
42 private static final String PLAIN_ASCII = "AaEeIiOoUu" +
43 "AaEeIiOoUuYy" +
44 "AaEeIiOoUuYy" +
45 "AaOoNn" +
46 "AaEeIiOoUuYy" +
47 "Aa" +
48 "Cc" +
49 "OoUu";
50
51
52
53
54 private static final String UNICODE = "\u00C0\u00E0\u00C8\u00E8\u00CC\u00EC\u00D2\u00F2\u00D9\u00F9" +
55 "\u00C1\u00E1\u00C9\u00E9\u00CD\u00ED\u00D3\u00F3\u00DA\u00FA\u00DD\u00FD" +
56 "\u00C2\u00E2\u00CA\u00EA\u00CE\u00EE\u00D4\u00F4\u00DB\u00FB\u0176\u0177" +
57 "\u00C3\u00E3\u00D5\u00F5\u00D1\u00F1" +
58 "\u00C4\u00E4\u00CB\u00EB\u00CF\u00EF\u00D6\u00F6\u00DC\u00FC\u0178\u00FF" +
59 "\u00C5\u00E5" + "\u00C7\u00E7" + "\u0150\u0151\u0170\u0171";
60
61 private static final String[] DOUBLE_CONSONANT =
62 { "BB", "CC", "DD", "FF", "GG", "HH", "JJ", "KK", "LL", "MM", "NN", "PP", "QQ", "RR", "SS",
63 "TT", "VV", "WW", "XX", "YY", "ZZ" };
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 String cleanName(final String name) {
79 String upperName = name.toUpperCase(Locale.ENGLISH);
80
81 final String[] charsToTrim = { "\\-", "[&]", "\\'", "\\.", "[\\,]" };
82 for (final String str : charsToTrim) {
83 upperName = upperName.replaceAll(str, EMPTY);
84 }
85
86 upperName = removeAccents(upperName);
87 return upperName.replaceAll("\\s+", EMPTY);
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101 @Override
102 public final Object encode(final Object pObject) throws EncoderException {
103 if (!(pObject instanceof String)) {
104 throw new EncoderException(
105 "Parameter supplied to Match Rating Approach encoder is not of type java.lang.String");
106 }
107 return encode((String) pObject);
108 }
109
110
111
112
113
114
115
116
117 @Override
118 public final String encode(String name) {
119
120 if (name == null || EMPTY.equalsIgnoreCase(name) || SPACE.equalsIgnoreCase(name) || name.length() == 1) {
121 return EMPTY;
122 }
123
124
125 name = cleanName(name);
126
127
128 if (SPACE.equals(name) || name.isEmpty()) {
129 return EMPTY;
130 }
131
132
133
134 name = removeVowels(name);
135
136
137 if (SPACE.equals(name) || name.isEmpty()) {
138 return EMPTY;
139 }
140
141
142 name = removeDoubleConsonants(name);
143
144 return getFirst3Last3(name);
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158
159 String getFirst3Last3(final String name) {
160 final int nameLength = name.length();
161
162 if (nameLength > 6) {
163 final String firstThree = name.substring(0, 3);
164 final String lastThree = name.substring(nameLength - 3, nameLength);
165 return firstThree + lastThree;
166 }
167 return name;
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 int getMinRating(final int sumLength) {
184 int minRating = 0;
185
186 if (sumLength <= 4) {
187 minRating = 5;
188 } else if (sumLength <= 7) {
189 minRating = 4;
190 } else if (sumLength <= 11) {
191 minRating = 3;
192 } else if (sumLength == 12) {
193 minRating = 2;
194 } else {
195 minRating = 1;
196 }
197
198 return minRating;
199 }
200
201
202
203
204
205
206
207
208
209
210
211 public boolean isEncodeEquals(String name1, String name2) {
212
213 if (name1 == null || EMPTY.equalsIgnoreCase(name1) || SPACE.equalsIgnoreCase(name1)) {
214 return false;
215 }
216 if (name2 == null || EMPTY.equalsIgnoreCase(name2) || SPACE.equalsIgnoreCase(name2)) {
217 return false;
218 }
219 if (name1.length() == 1 || name2.length() == 1) {
220 return false;
221 }
222 if (name1.equalsIgnoreCase(name2)) {
223 return true;
224 }
225
226
227 name1 = cleanName(name1);
228 name2 = cleanName(name2);
229
230
231
232
233 name1 = removeVowels(name1);
234 name2 = removeVowels(name2);
235
236
237 name1 = removeDoubleConsonants(name1);
238 name2 = removeDoubleConsonants(name2);
239
240
241 name1 = getFirst3Last3(name1);
242 name2 = getFirst3Last3(name2);
243
244
245
246 if (Math.abs(name1.length() - name2.length()) >= 3) {
247 return false;
248 }
249
250
251
252 final int sumLength = Math.abs(name1.length() + name2.length());
253 final int minRating = getMinRating(sumLength);
254
255
256
257 final int count = leftToRightThenRightToLeftProcessing(name1, name2);
258
259
260
261 return count >= minRating;
262
263 }
264
265
266
267
268
269
270
271
272
273
274
275
276
277 boolean isVowel(final String letter) {
278 return letter.equalsIgnoreCase("E") || letter.equalsIgnoreCase("A") || letter.equalsIgnoreCase("O") ||
279 letter.equalsIgnoreCase("I") || letter.equalsIgnoreCase("U");
280 }
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295 int leftToRightThenRightToLeftProcessing(final String name1, final String name2) {
296 final char[] name1Char = name1.toCharArray();
297 final char[] name2Char = name2.toCharArray();
298
299 final int name1Size = name1.length() - 1;
300 final int name2Size = name2.length() - 1;
301
302 String name1LtRStart = EMPTY;
303 String name1LtREnd = EMPTY;
304
305 String name2RtLStart = EMPTY;
306 String name2RtLEnd = EMPTY;
307
308 for (int i = 0; i < name1Char.length; i++) {
309 if (i > name2Size) {
310 break;
311 }
312
313 name1LtRStart = name1.substring(i, i + 1);
314 name1LtREnd = name1.substring(name1Size - i, name1Size - i + 1);
315
316 name2RtLStart = name2.substring(i, i + 1);
317 name2RtLEnd = name2.substring(name2Size - i, name2Size - i + 1);
318
319
320 if (name1LtRStart.equals(name2RtLStart)) {
321 name1Char[i] = ' ';
322 name2Char[i] = ' ';
323 }
324
325
326 if (name1LtREnd.equals(name2RtLEnd)) {
327 name1Char[name1Size - i] = ' ';
328 name2Char[name2Size - i] = ' ';
329 }
330 }
331
332
333 final String strA = new String(name1Char).replaceAll("\\s+", EMPTY);
334 final String strB = new String(name2Char).replaceAll("\\s+", EMPTY);
335
336
337 if (strA.length() > strB.length()) {
338 return Math.abs(6 - strA.length());
339 }
340 return Math.abs(6 - strB.length());
341 }
342
343
344
345
346
347
348
349
350
351 String removeAccents(final String accentedWord) {
352 if (accentedWord == null) {
353 return null;
354 }
355
356 final StringBuilder sb = new StringBuilder();
357 final int n = accentedWord.length();
358
359 for (int i = 0; i < n; i++) {
360 final char c = accentedWord.charAt(i);
361 final int pos = UNICODE.indexOf(c);
362 if (pos > -1) {
363 sb.append(PLAIN_ASCII.charAt(pos));
364 } else {
365 sb.append(c);
366 }
367 }
368
369 return sb.toString();
370 }
371
372
373
374
375
376
377
378
379
380
381
382
383
384 String removeDoubleConsonants(final String name) {
385 String replacedName = name.toUpperCase(Locale.ENGLISH);
386 for (final String dc : DOUBLE_CONSONANT) {
387 if (replacedName.contains(dc)) {
388 final String singleLetter = dc.substring(0, 1);
389 replacedName = replacedName.replace(dc, singleLetter);
390 }
391 }
392 return replacedName;
393 }
394
395
396
397
398
399
400
401
402
403
404
405
406
407 String removeVowels(String name) {
408
409 final String firstLetter = name.substring(0, 1);
410
411 name = name.replace("A", EMPTY);
412 name = name.replace("E", EMPTY);
413 name = name.replace("I", EMPTY);
414 name = name.replace("O", EMPTY);
415 name = name.replace("U", EMPTY);
416
417 name = name.replaceAll("\\s{2,}\\b", SPACE);
418
419
420 if (isVowel(firstLetter)) {
421 return firstLetter + name;
422 }
423 return name;
424 }
425 }