BeiderMorseEncoder.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.codec.language.bm;

  18. import org.apache.commons.codec.EncoderException;
  19. import org.apache.commons.codec.StringEncoder;

  20. /**
  21.  * Encodes strings into their Beider-Morse phonetic encoding.
  22.  * <p>
  23.  * Beider-Morse phonetic encodings are optimised for family names. However, they may be useful for a wide range of
  24.  * words.
  25.  * <p>
  26.  * This encoder is intentionally mutable to allow dynamic configuration through bean properties. As such, it is mutable,
  27.  * and may not be thread-safe. If you require a guaranteed thread-safe encoding then use {@link PhoneticEngine}
  28.  * directly.
  29.  * <p>
  30.  * <b>Encoding overview</b>
  31.  * <p>
  32.  * Beider-Morse phonetic encodings is a multi-step process. Firstly, a table of rules is consulted to guess what
  33.  * language the word comes from. For example, if it ends in "<code>ault</code>" then it infers that the word is French.
  34.  * Next, the word is translated into a phonetic representation using a language-specific phonetics table. Some runs of
  35.  * letters can be pronounced in multiple ways, and a single run of letters may be potentially broken up into phonemes at
  36.  * different places, so this stage results in a set of possible language-specific phonetic representations. Lastly, this
  37.  * language-specific phonetic representation is processed by a table of rules that re-writes it phonetically taking into
  38.  * account systematic pronunciation differences between languages, to move it towards a pan-indo-european phonetic
  39.  * representation. Again, sometimes there are multiple ways this could be done and sometimes things that can be
  40.  * pronounced in several ways in the source language have only one way to represent them in this average phonetic
  41.  * language, so the result is again a set of phonetic spellings.
  42.  * <p>
  43.  * Some names are treated as having multiple parts. This can be due to two things. Firstly, they may be hyphenated. In
  44.  * this case, each individual hyphenated word is encoded, and then these are combined end-to-end for the final encoding.
  45.  * Secondly, some names have standard prefixes, for example, "<code>Mac/Mc</code>" in Scottish (English) names. As
  46.  * sometimes it is ambiguous whether the prefix is intended or is an accident of the spelling, the word is encoded once
  47.  * with the prefix and once without it. The resulting encoding contains one and then the other result.
  48.  * <p>
  49.  * <b>Encoding format</b>
  50.  * <p>
  51.  * Individual phonetic spellings of an input word are represented in upper- and lower-case roman characters. Where there
  52.  * are multiple possible phonetic representations, these are joined with a pipe (<code>|</code>) character. If multiple
  53.  * hyphenated words where found, or if the word may contain a name prefix, each encoded word is placed in elipses and
  54.  * these blocks are then joined with hyphens. For example, "<code>d'ortley</code>" has a possible prefix. The form
  55.  * without prefix encodes to "<code>ortlaj|ortlej</code>", while the form with prefix encodes to "
  56.  * <code>dortlaj|dortlej</code>". Thus, the full, combined encoding is "<code>(ortlaj|ortlej)-(dortlaj|dortlej)</code>".
  57.  * <p>
  58.  * The encoded forms are often quite a bit longer than the input strings. This is because a single input may have many
  59.  * potential phonetic interpretations. For example, "<code>Renault</code>" encodes to "
  60.  * <code>rYnDlt|rYnalt|rYnult|rinDlt|rinalt|rinult</code>". The <code>APPROX</code> rules will tend to produce larger
  61.  * encodings as they consider a wider range of possible, approximate phonetic interpretations of the original word.
  62.  * Down-stream applications may wish to further process the encoding for indexing or lookup purposes, for example, by
  63.  * splitting on pipe (<code>|</code>) and indexing under each of these alternatives.
  64.  * <p>
  65.  * <b>Note</b>: this version of the Beider-Morse encoding is equivalent with v3.4 of the reference implementation.
  66.  * </p>
  67.  * @see <a href="http://stevemorse.org/phonetics/bmpm.htm">Beider-Morse Phonetic Matching</a>
  68.  * @see <a href="http://stevemorse.org/phoneticinfo.htm">Reference implementation</a>
  69.  *
  70.  * <p>
  71.  * This class is Not ThreadSafe
  72.  * </p>
  73.  * @since 1.6
  74.  * @version $Id: BeiderMorseEncoder.java 1744724 2016-05-20 12:24:04Z sebb $
  75.  */
  76. public class BeiderMorseEncoder implements StringEncoder {
  77.     // Implementation note: This class is a spring-friendly facade to PhoneticEngine. It allows read/write configuration
  78.     // of an immutable PhoneticEngine instance that will be delegated to for the actual encoding.

  79.     // a cached object
  80.     private PhoneticEngine engine = new PhoneticEngine(NameType.GENERIC, RuleType.APPROX, true);

  81.     @Override
  82.     public Object encode(final Object source) throws EncoderException {
  83.         if (!(source instanceof String)) {
  84.             throw new EncoderException("BeiderMorseEncoder encode parameter is not of type String");
  85.         }
  86.         return encode((String) source);
  87.     }

  88.     @Override
  89.     public String encode(final String source) throws EncoderException {
  90.         if (source == null) {
  91.             return null;
  92.         }
  93.         return this.engine.encode(source);
  94.     }

  95.     /**
  96.      * Gets the name type currently in operation.
  97.      *
  98.      * @return the NameType currently being used
  99.      */
  100.     public NameType getNameType() {
  101.         return this.engine.getNameType();
  102.     }

  103.     /**
  104.      * Gets the rule type currently in operation.
  105.      *
  106.      * @return the RuleType currently being used
  107.      */
  108.     public RuleType getRuleType() {
  109.         return this.engine.getRuleType();
  110.     }

  111.     /**
  112.      * Discovers if multiple possible encodings are concatenated.
  113.      *
  114.      * @return true if multiple encodings are concatenated, false if just the first one is returned
  115.      */
  116.     public boolean isConcat() {
  117.         return this.engine.isConcat();
  118.     }

  119.     /**
  120.      * Sets how multiple possible phonetic encodings are combined.
  121.      *
  122.      * @param concat
  123.      *            true if multiple encodings are to be combined with a '|', false if just the first one is
  124.      *            to be considered
  125.      */
  126.     public void setConcat(final boolean concat) {
  127.         this.engine = new PhoneticEngine(this.engine.getNameType(),
  128.                                          this.engine.getRuleType(),
  129.                                          concat,
  130.                                          this.engine.getMaxPhonemes());
  131.     }

  132.     /**
  133.      * Sets the type of name. Use {@link NameType#GENERIC} unless you specifically want phonetic encodings
  134.      * optimized for Ashkenazi or Sephardic Jewish family names.
  135.      *
  136.      * @param nameType
  137.      *            the NameType in use
  138.      */
  139.     public void setNameType(final NameType nameType) {
  140.         this.engine = new PhoneticEngine(nameType,
  141.                                          this.engine.getRuleType(),
  142.                                          this.engine.isConcat(),
  143.                                          this.engine.getMaxPhonemes());
  144.     }

  145.     /**
  146.      * Sets the rule type to apply. This will widen or narrow the range of phonetic encodings considered.
  147.      *
  148.      * @param ruleType
  149.      *            {@link RuleType#APPROX} or {@link RuleType#EXACT} for approximate or exact phonetic matches
  150.      */
  151.     public void setRuleType(final RuleType ruleType) {
  152.         this.engine = new PhoneticEngine(this.engine.getNameType(),
  153.                                          ruleType,
  154.                                          this.engine.isConcat(),
  155.                                          this.engine.getMaxPhonemes());
  156.     }

  157.     /**
  158.      * Sets the number of maximum of phonemes that shall be considered by the engine.
  159.      *
  160.      * @param maxPhonemes
  161.      *            the maximum number of phonemes returned by the engine
  162.      * @since 1.7
  163.      */
  164.     public void setMaxPhonemes(final int maxPhonemes) {
  165.         this.engine = new PhoneticEngine(this.engine.getNameType(),
  166.                                          this.engine.getRuleType(),
  167.                                          this.engine.isConcat(),
  168.                                          maxPhonemes);
  169.     }

  170. }