• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.apache.commons.codec.language;
18 
19 import org.apache.commons.codec.EncoderException;
20 import org.apache.commons.codec.StringEncoder;
21 
22 /**
23  * Encodes a string into a Soundex value. Soundex is an encoding used to relate similar names, but can also be used as a
24  * general purpose scheme to find word with similar phonemes.
25  *
26  * @author Apache Software Foundation
27  * @version $Id: Soundex.java,v 1.26 2004/07/07 23:15:24 ggregory Exp $
28  */
29 public class Soundex implements StringEncoder {
30 
31     /**
32      * An instance of Soundex using the US_ENGLISH_MAPPING mapping.
33      *
34      * @see #US_ENGLISH_MAPPING
35      */
36     public static final Soundex US_ENGLISH = new Soundex();
37 
38     /**
39      * This is a default mapping of the 26 letters used in US English. A value of <code>0</code> for a letter position
40      * means do not encode.
41      * <p>
42      * (This constant is provided as both an implementation convenience and to allow Javadoc to pick
43      * up the value for the constant values page.)
44      * </p>
45      *
46      * @see #US_ENGLISH_MAPPING
47      */
48     public static final String US_ENGLISH_MAPPING_STRING = "01230120022455012623010202";
49 
50     /**
51      * This is a default mapping of the 26 letters used in US English. A value of <code>0</code> for a letter position
52      * means do not encode.
53      *
54      * @see Soundex#Soundex(char[])
55      */
56     public static final char[] US_ENGLISH_MAPPING = US_ENGLISH_MAPPING_STRING.toCharArray();
57 
58     // BEGIN android-note
59     // Removed @see reference to SoundexUtils below, since the class isn't
60     // public.
61     // END android-note
62     /**
63      * Encodes the Strings and returns the number of characters in the two encoded Strings that are the same. This
64      * return value ranges from 0 through 4: 0 indicates little or no similarity, and 4 indicates strong similarity or
65      * identical values.
66      *
67      * @param s1
68      *                  A String that will be encoded and compared.
69      * @param s2
70      *                  A String that will be encoded and compared.
71      * @return The number of characters in the two encoded Strings that are the same from 0 to 4.
72      *
73      * @see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp"> MS
74      *          T-SQL DIFFERENCE </a>
75      *
76      * @throws EncoderException
77      *                  if an error occurs encoding one of the strings
78      * @since 1.3
79      */
difference(String s1, String s2)80     public int difference(String s1, String s2) throws EncoderException {
81         return SoundexUtils.difference(this, s1, s2);
82     }
83 
84     /**
85      * The maximum length of a Soundex code - Soundex codes are only four characters by definition.
86      *
87      * @deprecated This feature is not needed since the encoding size must be constant. Will be removed in 2.0.
88      */
89     private int maxLength = 4;
90 
91     /**
92      * Every letter of the alphabet is "mapped" to a numerical value. This char array holds the values to which each
93      * letter is mapped. This implementation contains a default map for US_ENGLISH
94      */
95     private char[] soundexMapping;
96 
97     /**
98      * Creates an instance using US_ENGLISH_MAPPING
99      *
100      * @see Soundex#Soundex(char[])
101      * @see Soundex#US_ENGLISH_MAPPING
102      */
Soundex()103     public Soundex() {
104         this(US_ENGLISH_MAPPING);
105     }
106 
107     /**
108      * Creates a soundex instance using the given mapping. This constructor can be used to provide an internationalized
109      * mapping for a non-Western character set.
110      *
111      * Every letter of the alphabet is "mapped" to a numerical value. This char array holds the values to which each
112      * letter is mapped. This implementation contains a default map for US_ENGLISH
113      *
114      * @param mapping
115      *                  Mapping array to use when finding the corresponding code for a given character
116      */
Soundex(char[] mapping)117     public Soundex(char[] mapping) {
118         this.setSoundexMapping(mapping);
119     }
120 
121     /**
122      * Encodes an Object using the soundex algorithm. This method is provided in order to satisfy the requirements of
123      * the Encoder interface, and will throw an EncoderException if the supplied object is not of type java.lang.String.
124      *
125      * @param pObject
126      *                  Object to encode
127      * @return An object (or type java.lang.String) containing the soundex code which corresponds to the String
128      *             supplied.
129      * @throws EncoderException
130      *                  if the parameter supplied is not of type java.lang.String
131      * @throws IllegalArgumentException
132      *                  if a character is not mapped
133      */
encode(Object pObject)134     public Object encode(Object pObject) throws EncoderException {
135         if (!(pObject instanceof String)) {
136             throw new EncoderException("Parameter supplied to Soundex encode is not of type java.lang.String");
137         }
138         return soundex((String) pObject);
139     }
140 
141     /**
142      * Encodes a String using the soundex algorithm.
143      *
144      * @param pString
145      *                  A String object to encode
146      * @return A Soundex code corresponding to the String supplied
147      * @throws IllegalArgumentException
148      *                  if a character is not mapped
149      */
encode(String pString)150     public String encode(String pString) {
151         return soundex(pString);
152     }
153 
154     /**
155      * Used internally by the SoundEx algorithm.
156      *
157      * Consonants from the same code group separated by W or H are treated as one.
158      *
159      * @param str
160      *                  the cleaned working string to encode (in upper case).
161      * @param index
162      *                  the character position to encode
163      * @return Mapping code for a particular character
164      * @throws IllegalArgumentException
165      *                  if the character is not mapped
166      */
getMappingCode(String str, int index)167     private char getMappingCode(String str, int index) {
168         char mappedChar = this.map(str.charAt(index));
169         // HW rule check
170         if (index > 1 && mappedChar != '0') {
171             char hwChar = str.charAt(index - 1);
172             if ('H' == hwChar || 'W' == hwChar) {
173                 char preHWChar = str.charAt(index - 2);
174                 char firstCode = this.map(preHWChar);
175                 if (firstCode == mappedChar || 'H' == preHWChar || 'W' == preHWChar) {
176                     return 0;
177                 }
178             }
179         }
180         return mappedChar;
181     }
182 
183     /**
184      * Returns the maxLength. Standard Soundex
185      *
186      * @deprecated This feature is not needed since the encoding size must be constant. Will be removed in 2.0.
187      * @return int
188      */
getMaxLength()189     public int getMaxLength() {
190         return this.maxLength;
191     }
192 
193     /**
194      * Returns the soundex mapping.
195      *
196      * @return soundexMapping.
197      */
getSoundexMapping()198     private char[] getSoundexMapping() {
199         return this.soundexMapping;
200     }
201 
202     /**
203      * Maps the given upper-case character to it's Soudex code.
204      *
205      * @param ch
206      *                  An upper-case character.
207      * @return A Soundex code.
208      * @throws IllegalArgumentException
209      *                  Thrown if <code>ch</code> is not mapped.
210      */
map(char ch)211     private char map(char ch) {
212         int index = ch - 'A';
213         if (index < 0 || index >= this.getSoundexMapping().length) {
214             throw new IllegalArgumentException("The character is not mapped: " + ch);
215         }
216         return this.getSoundexMapping()[index];
217     }
218 
219     /**
220      * Sets the maxLength.
221      *
222      * @deprecated This feature is not needed since the encoding size must be constant. Will be removed in 2.0.
223      * @param maxLength
224      *                  The maxLength to set
225      */
setMaxLength(int maxLength)226     public void setMaxLength(int maxLength) {
227         this.maxLength = maxLength;
228     }
229 
230     /**
231      * Sets the soundexMapping.
232      *
233      * @param soundexMapping
234      *                  The soundexMapping to set.
235      */
setSoundexMapping(char[] soundexMapping)236     private void setSoundexMapping(char[] soundexMapping) {
237         this.soundexMapping = soundexMapping;
238     }
239 
240     /**
241      * Retreives the Soundex code for a given String object.
242      *
243      * @param str
244      *                  String to encode using the Soundex algorithm
245      * @return A soundex code for the String supplied
246      * @throws IllegalArgumentException
247      *                  if a character is not mapped
248      */
soundex(String str)249     public String soundex(String str) {
250         if (str == null) {
251             return null;
252         }
253         str = SoundexUtils.clean(str);
254         if (str.length() == 0) {
255             return str;
256         }
257         char out[] = {'0', '0', '0', '0'};
258         char last, mapped;
259         int incount = 1, count = 1;
260         out[0] = str.charAt(0);
261         last = getMappingCode(str, 0);
262         while ((incount < str.length()) && (count < out.length)) {
263             mapped = getMappingCode(str, incount++);
264             if (mapped != 0) {
265                 if ((mapped != '0') && (mapped != last)) {
266                     out[count++] = mapped;
267                 }
268                 last = mapped;
269             }
270         }
271         return new String(out);
272     }
273 
274 }
275