Class PhoneticEngine
java.lang.Object
org.apache.commons.codec.language.bm.PhoneticEngine
Converts words into potential phonetic representations.
This is a two-stage process. Firstly, the word is converted into a phonetic representation that takes into account the likely source language. Next, this phonetic representation is converted into a pan-European 'average' representation, allowing comparison between different versions of essentially the same word from different languages.
This class is intentionally immutable and thread-safe. If you wish to alter the settings for a PhoneticEngine, you must make a new one with the updated settings.
Ported from phoneticengine.php
- Since:
- 1.6
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for a PhoneticEngine.(package private) static final classManipulates a set of phonemes as they are being built up.private static final classA function closure capturing the application of a list of rules to an input sequence at a particular offset. -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionPhoneticEngine(NameType nameType, RuleType ruleType, boolean concatenate) Deprecated.PhoneticEngine(NameType nameType, RuleType ruleType, boolean concatenate, int maxPhonemes) Deprecated.Usebuilder()instead.privatePhoneticEngine(PhoneticEngine.Builder builder) Creates a new, fully-configured phonetic engine. -
Method Summary
Modifier and TypeMethodDescriptionprivate PhoneticEngine.PhonemeBuilderapplyFinalRules(PhoneticEngine.PhonemeBuilder phonemeBuilder, Map<String, List<Rule>> finalRules) Applies the final rules to convert from a language-specific phonetic representation to a language-independent representation.static PhoneticEngine.Builderbuilder()Creates a new builder for a PhoneticEngine.Encodes a string to its phonetic representation.encode(String input, Languages.LanguageSet languageSet) Encodes an input string into an output phonetic representation, given a set of possible origin languages.getLang()Gets the Lang language guessing rules being used.intGets the maximum number of phonemes the engine will calculate for a given input.Gets the NameType being used.Gets the RuleType being used.booleanisConcat()Tests whether multiple phonetic encodings are concatenated or just the first one is kept.private static StringJoins some strings with an internal separator.private intlastRepeat(String source, String prefix, int prefixLen) Finds the index past the last occurrence of a repeating prefix in a string, starting from the beginning of the string and moving forward.
-
Field Details
-
NAME_PREFIXES
-
QUOTE
-
concat
private final boolean concat -
lang
-
maxInputLength
private final int maxInputLength -
maxPhonemes
private final int maxPhonemes -
nameType
-
ruleType
-
-
Constructor Details
-
PhoneticEngine
Creates a new, fully-configured phonetic engine.- Parameters:
builder- The builder to use for configuration.- Throws:
IllegalArgumentException- Thrown if ruleType is RULES.
-
PhoneticEngine
Deprecated.Usebuilder()instead.Generates a new, fully-configured phonetic engine.- Parameters:
nameType- the type of names it will use, null is treated asNameType.GENERIC.ruleType- the type of rules it will apply, null is treated asRuleType.APPROX.concatenate- if it will concatenate multiple encodings.
-
PhoneticEngine
@Deprecated public PhoneticEngine(NameType nameType, RuleType ruleType, boolean concatenate, int maxPhonemes) Deprecated.Usebuilder()instead.Generates a new, fully-configured phonetic engine.- Parameters:
nameType- the type of names it will use, null is treated asNameType.GENERIC.ruleType- the type of rules it will apply, null is treated asRuleType.APPROX.concatenate- if it will concatenate multiple encodings.maxPhonemes- the maximum number of phonemes that will be handled, less than 0 will reset to the default of 20.- Throws:
IllegalArgumentException- Thrown if ruleType is RULES.- Since:
- 1.7
-
-
Method Details
-
builder
Creates a new builder for a PhoneticEngine.- Returns:
- a new builder for a PhoneticEngine.
- Since:
- 1.23.0
-
join
Joins some strings with an internal separator.- Parameters:
strings- Strings to join.sep- String to separate them with.- Returns:
- A single String consisting of each element of
stringsinterleaved bysep.
-
applyFinalRules
private PhoneticEngine.PhonemeBuilder applyFinalRules(PhoneticEngine.PhonemeBuilder phonemeBuilder, Map<String, List<Rule>> finalRules) Applies the final rules to convert from a language-specific phonetic representation to a language-independent representation.- Parameters:
phonemeBuilder- The current phonemes.finalRules- The final rules to apply.- Returns:
- The resulting phonemes.
-
encode
Encodes a string to its phonetic representation.- Parameters:
input- the String to encode, not null.- Returns:
- The encoding of the input.
- Throws:
IllegalArgumentException- Thrown if the input is longer than the maximum allowed length.
-
encode
Encodes an input string into an output phonetic representation, given a set of possible origin languages.- Parameters:
input- String to phoneticise; a String with dashes or spaces separating each word, not null.languageSet- set of possible origin languages.- Returns:
- A phonetic representation of the input; a String containing '-'-separated phonetic representations of the input.
- Throws:
IllegalArgumentException- Thrown if the input is longer than the maximum allowed length.
-
getLang
Gets the Lang language guessing rules being used.- Returns:
- The Lang in use.
-
getMaxPhonemes
public int getMaxPhonemes()Gets the maximum number of phonemes the engine will calculate for a given input.- Returns:
- The maximum number of phonemes.
- Since:
- 1.7
-
getNameType
Gets the NameType being used.- Returns:
- The NameType in use.
-
getRuleType
Gets the RuleType being used.- Returns:
- The RuleType in use.
-
isConcat
public boolean isConcat()Tests whether multiple phonetic encodings are concatenated or just the first one is kept.- Returns:
- true if multiple phonetic encodings are returned, false if just the first is.
-
lastRepeat
Finds the index past the last occurrence of a repeating prefix in a string, starting from the beginning of the string and moving forward.- Parameters:
source- The source string to search within.prefix- The prefix to look for.prefixLen- The length of the prefix.- Returns:
- The index in the source string where the last occurrence of the repeating prefix ends.
-
builder()instead.