1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec.language.bm;
19
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.EnumMap;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28 import java.util.Locale;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.TreeSet;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class PhoneticEngine {
51
52
53
54
55
56
57
58 static final class PhonemeBuilder {
59
60
61
62
63
64
65
66
67
68 public static PhonemeBuilder empty(final Languages.LanguageSet languages) {
69 return new PhonemeBuilder(Collections.singleton(new Rule.Phoneme("", languages)));
70 }
71
72 private final Set<Rule.Phoneme> phonemes;
73
74 private PhonemeBuilder(final Set<Rule.Phoneme> phonemes) {
75 this.phonemes = phonemes;
76 }
77
78
79
80
81
82
83
84 public PhonemeBuilder append(final CharSequence str) {
85 final Set<Rule.Phoneme> newPhonemes = new LinkedHashSet<Rule.Phoneme>();
86
87 for (final Rule.Phoneme ph : this.phonemes) {
88 newPhonemes.add(ph.append(str));
89 }
90
91 return new PhonemeBuilder(newPhonemes);
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105 public PhonemeBuilder apply(final Rule.PhonemeExpr phonemeExpr, final int maxPhonemes) {
106 final Set<Rule.Phoneme> newPhonemes = new LinkedHashSet<Rule.Phoneme>();
107
108 EXPR: for (final Rule.Phoneme left : this.phonemes) {
109 for (final Rule.Phoneme right : phonemeExpr.getPhonemes()) {
110 final Rule.Phoneme join = left.join(right);
111 if (!join.getLanguages().isEmpty()) {
112 if (newPhonemes.size() < maxPhonemes) {
113 newPhonemes.add(join);
114 } else {
115 break EXPR;
116 }
117 }
118 }
119 }
120
121 return new PhonemeBuilder(newPhonemes);
122 }
123
124
125
126
127
128
129 public Set<Rule.Phoneme> getPhonemes() {
130 return this.phonemes;
131 }
132
133
134
135
136
137
138
139
140 public String makeString() {
141 final StringBuilder sb = new StringBuilder();
142
143 for (final Rule.Phoneme ph : this.phonemes) {
144 if (sb.length() > 0) {
145 sb.append("|");
146 }
147 sb.append(ph.getPhonemeText());
148 }
149
150 return sb.toString();
151 }
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166 private static final class RulesApplication {
167 private final List<Rule> finalRules;
168 private final CharSequence input;
169
170 private PhonemeBuilder phonemeBuilder;
171 private int i;
172 private final int maxPhonemes;
173 private boolean found;
174
175 public RulesApplication(final List<Rule> finalRules, final CharSequence input,
176 final PhonemeBuilder phonemeBuilder, final int i, final int maxPhonemes) {
177 if (finalRules == null) {
178 throw new NullPointerException("The finalRules argument must not be null");
179 }
180 this.finalRules = finalRules;
181 this.phonemeBuilder = phonemeBuilder;
182 this.input = input;
183 this.i = i;
184 this.maxPhonemes = maxPhonemes;
185 }
186
187 public int getI() {
188 return this.i;
189 }
190
191 public PhonemeBuilder getPhonemeBuilder() {
192 return this.phonemeBuilder;
193 }
194
195
196
197
198
199
200
201
202 public RulesApplication invoke() {
203 this.found = false;
204 int patternLength = 0;
205 for (final Rule rule : this.finalRules) {
206 final String pattern = rule.getPattern();
207 patternLength = pattern.length();
208
209 if (!rule.patternAndContextMatches(this.input, this.i)) {
210 continue;
211 }
212
213 this.phonemeBuilder = this.phonemeBuilder.apply(rule.getPhoneme(), maxPhonemes);
214 this.found = true;
215 break;
216 }
217
218 if (!this.found) {
219 patternLength = 1;
220 }
221
222 this.i += patternLength;
223 return this;
224 }
225
226 public boolean isFound() {
227 return this.found;
228 }
229 }
230
231 private static final Map<NameType, Set<String>> NAME_PREFIXES = new EnumMap<NameType, Set<String>>(NameType.class);
232
233 static {
234 NAME_PREFIXES.put(NameType.ASHKENAZI,
235 Collections.unmodifiableSet(
236 new HashSet<String>(Arrays.asList("bar", "ben", "da", "de", "van", "von"))));
237 NAME_PREFIXES.put(NameType.SEPHARDIC,
238 Collections.unmodifiableSet(
239 new HashSet<String>(Arrays.asList("al", "el", "da", "dal", "de", "del", "dela", "de la",
240 "della", "des", "di", "do", "dos", "du", "van", "von"))));
241 NAME_PREFIXES.put(NameType.GENERIC,
242 Collections.unmodifiableSet(
243 new HashSet<String>(Arrays.asList("da", "dal", "de", "del", "dela", "de la", "della",
244 "des", "di", "do", "dos", "du", "van", "von"))));
245 }
246
247
248
249
250
251
252
253 private static CharSequence cacheSubSequence(final CharSequence cached) {
254
255 final CharSequence[][] cache = new CharSequence[cached.length()][cached.length()];
256 return new CharSequence() {
257 @Override
258 public char charAt(final int index) {
259 return cached.charAt(index);
260 }
261
262 @Override
263 public int length() {
264 return cached.length();
265 }
266
267 @Override
268 public CharSequence subSequence(final int start, final int end) {
269 if (start == end) {
270 return "";
271 }
272
273 CharSequence res = cache[start][end - 1];
274 if (res == null) {
275 res = cached.subSequence(start, end);
276 cache[start][end - 1] = res;
277 }
278 return res;
279 }
280 };
281 }
282
283
284
285
286
287
288
289 private static String join(final Iterable<String> strings, final String sep) {
290 final StringBuilder sb = new StringBuilder();
291 final Iterator<String> si = strings.iterator();
292 if (si.hasNext()) {
293 sb.append(si.next());
294 }
295 while (si.hasNext()) {
296 sb.append(sep).append(si.next());
297 }
298
299 return sb.toString();
300 }
301
302 private static final int DEFAULT_MAX_PHONEMES = 20;
303
304 private final Lang lang;
305
306 private final NameType nameType;
307
308 private final RuleType ruleType;
309
310 private final boolean concat;
311
312 private final int maxPhonemes;
313
314
315
316
317
318
319
320
321
322
323
324 public PhoneticEngine(final NameType nameType, final RuleType ruleType, final boolean concat) {
325 this(nameType, ruleType, concat, DEFAULT_MAX_PHONEMES);
326 }
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341 public PhoneticEngine(final NameType nameType, final RuleType ruleType, final boolean concat,
342 final int maxPhonemes) {
343 if (ruleType == RuleType.RULES) {
344 throw new IllegalArgumentException("ruleType must not be " + RuleType.RULES);
345 }
346 this.nameType = nameType;
347 this.ruleType = ruleType;
348 this.concat = concat;
349 this.lang = Lang.instance(nameType);
350 this.maxPhonemes = maxPhonemes;
351 }
352
353
354
355
356
357
358
359
360
361 private PhonemeBuilder applyFinalRules(final PhonemeBuilder phonemeBuilder, final List<Rule> finalRules) {
362 if (finalRules == null) {
363 throw new NullPointerException("finalRules can not be null");
364 }
365 if (finalRules.isEmpty()) {
366 return phonemeBuilder;
367 }
368
369 final Set<Rule.Phoneme> phonemes = new TreeSet<Rule.Phoneme>(Rule.Phoneme.COMPARATOR);
370
371 for (final Rule.Phoneme phoneme : phonemeBuilder.getPhonemes()) {
372 PhonemeBuilder subBuilder = PhonemeBuilder.empty(phoneme.getLanguages());
373 final CharSequence phonemeText = cacheSubSequence(phoneme.getPhonemeText());
374
375 for (int i = 0; i < phonemeText.length();) {
376 final RulesApplication rulesApplication =
377 new RulesApplication(finalRules, phonemeText, subBuilder, i, maxPhonemes).invoke();
378 final boolean found = rulesApplication.isFound();
379 subBuilder = rulesApplication.getPhonemeBuilder();
380
381 if (!found) {
382
383 subBuilder = subBuilder.append(phonemeText.subSequence(i, i + 1));
384 }
385
386 i = rulesApplication.getI();
387 }
388
389 phonemes.addAll(subBuilder.getPhonemes());
390 }
391
392 return new PhonemeBuilder(phonemes);
393 }
394
395
396
397
398
399
400
401
402 public String encode(final String input) {
403 final Languages.LanguageSet languageSet = this.lang.guessLanguages(input);
404 return encode(input, languageSet);
405 }
406
407
408
409
410
411
412
413
414
415
416 public String encode(String input, final Languages.LanguageSet languageSet) {
417 final List<Rule> rules = Rule.getInstance(this.nameType, RuleType.RULES, languageSet);
418
419 final List<Rule> finalRules1 = Rule.getInstance(this.nameType, this.ruleType, "common");
420
421 final List<Rule> finalRules2 = Rule.getInstance(this.nameType, this.ruleType, languageSet);
422
423
424
425 input = input.toLowerCase(Locale.ENGLISH).replace('-', ' ').trim();
426
427 if (this.nameType == NameType.GENERIC) {
428 if (input.length() >= 2 && input.substring(0, 2).equals("d'")) {
429 final String remainder = input.substring(2);
430 final String combined = "d" + remainder;
431 return "(" + encode(remainder) + ")-(" + encode(combined) + ")";
432 }
433 for (final String l : NAME_PREFIXES.get(this.nameType)) {
434
435 if (input.startsWith(l + " ")) {
436
437 final String remainder = input.substring(l.length() + 1);
438 final String combined = l + remainder;
439 return "(" + encode(remainder) + ")-(" + encode(combined) + ")";
440 }
441 }
442 }
443
444 final List<String> words = Arrays.asList(input.split("\\s+"));
445 final List<String> words2 = new ArrayList<String>();
446
447
448 switch (this.nameType) {
449 case SEPHARDIC:
450 for (final String aWord : words) {
451 final String[] parts = aWord.split("'");
452 final String lastPart = parts[parts.length - 1];
453 words2.add(lastPart);
454 }
455 words2.removeAll(NAME_PREFIXES.get(this.nameType));
456 break;
457 case ASHKENAZI:
458 words2.addAll(words);
459 words2.removeAll(NAME_PREFIXES.get(this.nameType));
460 break;
461 case GENERIC:
462 words2.addAll(words);
463 break;
464 default:
465 throw new IllegalStateException("Unreachable case: " + this.nameType);
466 }
467
468 if (this.concat) {
469
470 input = join(words2, " ");
471 } else if (words2.size() == 1) {
472
473 input = words.iterator().next();
474 } else {
475
476 final StringBuilder result = new StringBuilder();
477 for (final String word : words2) {
478 result.append("-").append(encode(word));
479 }
480
481 return result.substring(1);
482 }
483
484 PhonemeBuilder phonemeBuilder = PhonemeBuilder.empty(languageSet);
485
486
487 final CharSequence inputCache = cacheSubSequence(input);
488 for (int i = 0; i < inputCache.length();) {
489 final RulesApplication rulesApplication =
490 new RulesApplication(rules, inputCache, phonemeBuilder, i, maxPhonemes).invoke();
491 i = rulesApplication.getI();
492 phonemeBuilder = rulesApplication.getPhonemeBuilder();
493 }
494
495
496 phonemeBuilder = applyFinalRules(phonemeBuilder, finalRules1);
497
498 phonemeBuilder = applyFinalRules(phonemeBuilder, finalRules2);
499
500 return phonemeBuilder.makeString();
501 }
502
503
504
505
506
507
508 public Lang getLang() {
509 return this.lang;
510 }
511
512
513
514
515
516
517 public NameType getNameType() {
518 return this.nameType;
519 }
520
521
522
523
524
525
526 public RuleType getRuleType() {
527 return this.ruleType;
528 }
529
530
531
532
533
534
535 public boolean isConcat() {
536 return this.concat;
537 }
538
539
540
541
542
543
544
545 public int getMaxPhonemes() {
546 return this.maxPhonemes;
547 }
548 }