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