RefinedSoundex.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;

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

  20. /**
  21.  * Encodes a string into a Refined Soundex value. A refined soundex code is
  22.  * optimized for spell checking words. Soundex method originally developed by
  23.  * <CITE>Margaret Odell</CITE> and <CITE>Robert Russell</CITE>.
  24.  *
  25.  * <p>This class is immutable and thread-safe.</p>
  26.  *
  27.  * @version $Id: RefinedSoundex.java 1811347 2017-10-06 15:21:18Z ggregory $
  28.  */
  29. public class RefinedSoundex implements StringEncoder {

  30.     /**
  31.      * Mapping:
  32.      * <pre>
  33.      * 0: A E I O U Y H W
  34.      * 1: B P
  35.      * 2: F V
  36.      * 3: C K S
  37.      * 4: G J
  38.      * 5: Q X Z
  39.      * 6: D T
  40.      * 7: L
  41.      * 8: M N
  42.      * 9: R
  43.      * </pre>
  44.      * @since 1.4
  45.      */
  46.     //                                                      ABCDEFGHIJKLMNOPQRSTUVWXYZ
  47.     public static final String US_ENGLISH_MAPPING_STRING = "01360240043788015936020505";

  48.    /**
  49.      * RefinedSoundex is *refined* for a number of reasons one being that the
  50.      * mappings have been altered. This implementation contains default
  51.      * mappings for US English.
  52.      */
  53.     private static final char[] US_ENGLISH_MAPPING = US_ENGLISH_MAPPING_STRING.toCharArray();

  54.     /**
  55.      * Every letter of the alphabet is "mapped" to a numerical value. This char
  56.      * array holds the values to which each letter is mapped. This
  57.      * implementation contains a default map for US_ENGLISH
  58.      */
  59.     private final char[] soundexMapping;

  60.     /**
  61.      * This static variable contains an instance of the RefinedSoundex using
  62.      * the US_ENGLISH mapping.
  63.      */
  64.     public static final RefinedSoundex US_ENGLISH = new RefinedSoundex();

  65.      /**
  66.      * Creates an instance of the RefinedSoundex object using the default US
  67.      * English mapping.
  68.      */
  69.     public RefinedSoundex() {
  70.         this.soundexMapping = US_ENGLISH_MAPPING;
  71.     }

  72.     /**
  73.      * Creates a refined soundex instance using a custom mapping. This
  74.      * constructor can be used to customize the mapping, and/or possibly
  75.      * provide an internationalized mapping for a non-Western character set.
  76.      *
  77.      * @param mapping
  78.      *                  Mapping array to use when finding the corresponding code for
  79.      *                  a given character
  80.      */
  81.     public RefinedSoundex(final char[] mapping) {
  82.         this.soundexMapping = new char[mapping.length];
  83.         System.arraycopy(mapping, 0, this.soundexMapping, 0, mapping.length);
  84.     }

  85.     /**
  86.      * Creates a refined Soundex instance using a custom mapping. This constructor can be used to customize the mapping,
  87.      * and/or possibly provide an internationalized mapping for a non-Western character set.
  88.      *
  89.      * @param mapping
  90.      *            Mapping string to use when finding the corresponding code for a given character
  91.      * @since 1.4
  92.      */
  93.     public RefinedSoundex(final String mapping) {
  94.         this.soundexMapping = mapping.toCharArray();
  95.     }

  96.     /**
  97.      * Returns the number of characters in the two encoded Strings that are the
  98.      * same. This return value ranges from 0 to the length of the shortest
  99.      * encoded String: 0 indicates little or no similarity, and 4 out of 4 (for
  100.      * example) indicates strong similarity or identical values. For refined
  101.      * Soundex, the return value can be greater than 4.
  102.      *
  103.      * @param s1
  104.      *                  A String that will be encoded and compared.
  105.      * @param s2
  106.      *                  A String that will be encoded and compared.
  107.      * @return The number of characters in the two encoded Strings that are the
  108.      *             same from 0 to to the length of the shortest encoded String.
  109.      *
  110.      * @see SoundexUtils#difference(StringEncoder,String,String)
  111.      * @see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp">
  112.      *          MS T-SQL DIFFERENCE</a>
  113.      *
  114.      * @throws EncoderException
  115.      *                  if an error occurs encoding one of the strings
  116.      * @since 1.3
  117.      */
  118.     public int difference(final String s1, final String s2) throws EncoderException {
  119.         return SoundexUtils.difference(this, s1, s2);
  120.     }

  121.     /**
  122.      * Encodes an Object using the refined soundex algorithm. This method is
  123.      * provided in order to satisfy the requirements of the Encoder interface,
  124.      * and will throw an EncoderException if the supplied object is not of type
  125.      * java.lang.String.
  126.      *
  127.      * @param obj
  128.      *                  Object to encode
  129.      * @return An object (or type java.lang.String) containing the refined
  130.      *             soundex code which corresponds to the String supplied.
  131.      * @throws EncoderException
  132.      *                  if the parameter supplied is not of type java.lang.String
  133.      */
  134.     @Override
  135.     public Object encode(final Object obj) throws EncoderException {
  136.         if (!(obj instanceof String)) {
  137.             throw new EncoderException("Parameter supplied to RefinedSoundex encode is not of type java.lang.String");
  138.         }
  139.         return soundex((String) obj);
  140.     }

  141.     /**
  142.      * Encodes a String using the refined soundex algorithm.
  143.      *
  144.      * @param str
  145.      *                  A String object to encode
  146.      * @return A Soundex code corresponding to the String supplied
  147.      */
  148.     @Override
  149.     public String encode(final String str) {
  150.         return soundex(str);
  151.     }

  152.     /**
  153.      * Returns the mapping code for a given character. The mapping codes are
  154.      * maintained in an internal char array named soundexMapping, and the
  155.      * default values of these mappings are US English.
  156.      *
  157.      * @param c
  158.      *                  char to get mapping for
  159.      * @return A character (really a numeral) to return for the given char
  160.      */
  161.     char getMappingCode(final char c) {
  162.         if (!Character.isLetter(c)) {
  163.             return 0;
  164.         }
  165.         return this.soundexMapping[Character.toUpperCase(c) - 'A'];
  166.     }

  167.     /**
  168.      * Retrieves the Refined Soundex code for a given String object.
  169.      *
  170.      * @param str
  171.      *                  String to encode using the Refined Soundex algorithm
  172.      * @return A soundex code for the String supplied
  173.      */
  174.     public String soundex(String str) {
  175.         if (str == null) {
  176.             return null;
  177.         }
  178.         str = SoundexUtils.clean(str);
  179.         if (str.length() == 0) {
  180.             return str;
  181.         }

  182.         final StringBuilder sBuf = new StringBuilder();
  183.         sBuf.append(str.charAt(0));

  184.         char last, current;
  185.         last = '*';

  186.         for (int i = 0; i < str.length(); i++) {

  187.             current = getMappingCode(str.charAt(i));
  188.             if (current == last) {
  189.                 continue;
  190.             } else if (current != 0) {
  191.                 sBuf.append(current);
  192.             }

  193.             last = current;

  194.         }

  195.         return sBuf.toString();
  196.     }
  197. }