1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec.language;
19
20 import org.apache.commons.codec.EncoderException;
21 import org.apache.commons.codec.StringEncoder;
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 public class Metaphone implements StringEncoder {
47
48
49
50
51 private static final String VOWELS = "AEIOU";
52
53
54
55
56 private static final String FRONTV = "EIY";
57
58
59
60
61 private static final String VARSON = "CSPTG";
62
63
64
65
66 private int maxCodeLen = 4;
67
68
69
70
71 public Metaphone() {
72 super();
73 }
74
75
76
77
78
79
80
81
82
83
84
85 public String metaphone(String txt) {
86 boolean hard = false;
87 if (txt == null || txt.length() == 0) {
88 return "";
89 }
90
91 if (txt.length() == 1) {
92 return txt.toUpperCase(java.util.Locale.ENGLISH);
93 }
94
95 char[] inwd = txt.toUpperCase(java.util.Locale.ENGLISH).toCharArray();
96
97 StringBuilder local = new StringBuilder(40);
98 StringBuilder code = new StringBuilder(10);
99
100 switch(inwd[0]) {
101 case 'K':
102 case 'G':
103 case 'P':
104 if (inwd[1] == 'N') {
105 local.append(inwd, 1, inwd.length - 1);
106 } else {
107 local.append(inwd);
108 }
109 break;
110 case 'A':
111 if (inwd[1] == 'E') {
112 local.append(inwd, 1, inwd.length - 1);
113 } else {
114 local.append(inwd);
115 }
116 break;
117 case 'W':
118 if (inwd[1] == 'R') {
119 local.append(inwd, 1, inwd.length - 1);
120 break;
121 }
122 if (inwd[1] == 'H') {
123 local.append(inwd, 1, inwd.length - 1);
124 local.setCharAt(0, 'W');
125 } else {
126 local.append(inwd);
127 }
128 break;
129 case 'X':
130 inwd[0] = 'S';
131 local.append(inwd);
132 break;
133 default:
134 local.append(inwd);
135 }
136
137 int wdsz = local.length();
138 int n = 0;
139
140 while (code.length() < this.getMaxCodeLen() &&
141 n < wdsz ) {
142 char symb = local.charAt(n);
143
144 if (symb != 'C' && isPreviousChar( local, n, symb ) ) {
145 n++;
146 } else {
147 switch(symb) {
148 case 'A':
149 case 'E':
150 case 'I':
151 case 'O':
152 case 'U':
153 if (n == 0) {
154 code.append(symb);
155 }
156 break;
157 case 'B':
158 if ( isPreviousChar(local, n, 'M') &&
159 isLastChar(wdsz, n) ) {
160 break;
161 }
162 code.append(symb);
163 break;
164 case 'C':
165
166 if ( isPreviousChar(local, n, 'S') &&
167 !isLastChar(wdsz, n) &&
168 FRONTV.indexOf(local.charAt(n + 1)) >= 0 ) {
169 break;
170 }
171 if (regionMatch(local, n, "CIA")) {
172 code.append('X');
173 break;
174 }
175 if (!isLastChar(wdsz, n) &&
176 FRONTV.indexOf(local.charAt(n + 1)) >= 0) {
177 code.append('S');
178 break;
179 }
180 if (isPreviousChar(local, n, 'S') &&
181 isNextChar(local, n, 'H') ) {
182 code.append('K');
183 break;
184 }
185 if (isNextChar(local, n, 'H')) {
186 if (n == 0 &&
187 wdsz >= 3 &&
188 isVowel(local,2) ) {
189 code.append('K');
190 } else {
191 code.append('X');
192 }
193 } else {
194 code.append('K');
195 }
196 break;
197 case 'D':
198 if (!isLastChar(wdsz, n + 1) &&
199 isNextChar(local, n, 'G') &&
200 FRONTV.indexOf(local.charAt(n + 2)) >= 0) {
201 code.append('J'); n += 2;
202 } else {
203 code.append('T');
204 }
205 break;
206 case 'G':
207 if (isLastChar(wdsz, n + 1) &&
208 isNextChar(local, n, 'H')) {
209 break;
210 }
211 if (!isLastChar(wdsz, n + 1) &&
212 isNextChar(local,n,'H') &&
213 !isVowel(local,n+2)) {
214 break;
215 }
216 if (n > 0 &&
217 ( regionMatch(local, n, "GN") ||
218 regionMatch(local, n, "GNED") ) ) {
219 break;
220 }
221 if (isPreviousChar(local, n, 'G')) {
222
223 hard = true;
224 } else {
225 hard = false;
226 }
227 if (!isLastChar(wdsz, n) &&
228 FRONTV.indexOf(local.charAt(n + 1)) >= 0 &&
229 !hard) {
230 code.append('J');
231 } else {
232 code.append('K');
233 }
234 break;
235 case 'H':
236 if (isLastChar(wdsz, n)) {
237 break;
238 }
239 if (n > 0 &&
240 VARSON.indexOf(local.charAt(n - 1)) >= 0) {
241 break;
242 }
243 if (isVowel(local,n+1)) {
244 code.append('H');
245 }
246 break;
247 case 'F':
248 case 'J':
249 case 'L':
250 case 'M':
251 case 'N':
252 case 'R':
253 code.append(symb);
254 break;
255 case 'K':
256 if (n > 0) {
257 if (!isPreviousChar(local, n, 'C')) {
258 code.append(symb);
259 }
260 } else {
261 code.append(symb);
262 }
263 break;
264 case 'P':
265 if (isNextChar(local,n,'H')) {
266
267 code.append('F');
268 } else {
269 code.append(symb);
270 }
271 break;
272 case 'Q':
273 code.append('K');
274 break;
275 case 'S':
276 if (regionMatch(local,n,"SH") ||
277 regionMatch(local,n,"SIO") ||
278 regionMatch(local,n,"SIA")) {
279 code.append('X');
280 } else {
281 code.append('S');
282 }
283 break;
284 case 'T':
285 if (regionMatch(local,n,"TIA") ||
286 regionMatch(local,n,"TIO")) {
287 code.append('X');
288 break;
289 }
290 if (regionMatch(local,n,"TCH")) {
291
292 break;
293 }
294
295 if (regionMatch(local,n,"TH")) {
296 code.append('0');
297 } else {
298 code.append('T');
299 }
300 break;
301 case 'V':
302 code.append('F'); break;
303 case 'W':
304 case 'Y':
305 if (!isLastChar(wdsz,n) &&
306 isVowel(local,n+1)) {
307 code.append(symb);
308 }
309 break;
310 case 'X':
311 code.append('K');
312 code.append('S');
313 break;
314 case 'Z':
315 code.append('S');
316 break;
317 default:
318
319 break;
320 }
321 n++;
322 }
323 if (code.length() > this.getMaxCodeLen()) {
324 code.setLength(this.getMaxCodeLen());
325 }
326 }
327 return code.toString();
328 }
329
330 private boolean isVowel(StringBuilder string, int index) {
331 return VOWELS.indexOf(string.charAt(index)) >= 0;
332 }
333
334 private boolean isPreviousChar(StringBuilder string, int index, char c) {
335 boolean matches = false;
336 if( index > 0 &&
337 index < string.length() ) {
338 matches = string.charAt(index - 1) == c;
339 }
340 return matches;
341 }
342
343 private boolean isNextChar(StringBuilder string, int index, char c) {
344 boolean matches = false;
345 if( index >= 0 &&
346 index < string.length() - 1 ) {
347 matches = string.charAt(index + 1) == c;
348 }
349 return matches;
350 }
351
352 private boolean regionMatch(StringBuilder string, int index, String test) {
353 boolean matches = false;
354 if( index >= 0 &&
355 index + test.length() - 1 < string.length() ) {
356 String substring = string.substring( index, index + test.length());
357 matches = substring.equals( test );
358 }
359 return matches;
360 }
361
362 private boolean isLastChar(int wdsz, int n) {
363 return n + 1 == wdsz;
364 }
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379 @Override
380 public Object encode(Object obj) throws EncoderException {
381 if (!(obj instanceof String)) {
382 throw new EncoderException("Parameter supplied to Metaphone encode is not of type java.lang.String");
383 }
384 return metaphone((String) obj);
385 }
386
387
388
389
390
391
392
393 @Override
394 public String encode(String str) {
395 return metaphone(str);
396 }
397
398
399
400
401
402
403
404
405
406 public boolean isMetaphoneEqual(String str1, String str2) {
407 return metaphone(str1).equals(metaphone(str2));
408 }
409
410
411
412
413
414 public int getMaxCodeLen() { return this.maxCodeLen; }
415
416
417
418
419
420 public void setMaxCodeLen(int maxCodeLen) { this.maxCodeLen = maxCodeLen; }
421
422 }