001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.codec.language;
018:
019: import org.apache.commons.codec.EncoderException;
020: import org.apache.commons.codec.StringEncoder;
021:
022: /**
023: * Encodes a string into a Soundex value. Soundex is an encoding used to relate similar names, but can also be used as a
024: * general purpose scheme to find word with similar phonemes.
025: *
026: * @author Apache Software Foundation
027: * @version $Id: Soundex.java,v 1.26 2004/07/07 23:15:24 ggregory Exp $
028: */
029: public class Soundex implements StringEncoder {
030:
031: /**
032: * An instance of Soundex using the US_ENGLISH_MAPPING mapping.
033: *
034: * @see #US_ENGLISH_MAPPING
035: */
036: public static final Soundex US_ENGLISH = new Soundex();
037:
038: /**
039: * This is a default mapping of the 26 letters used in US English. A value of <code>0</code> for a letter position
040: * means do not encode.
041: * <p>
042: * (This constant is provided as both an implementation convenience and to allow Javadoc to pick
043: * up the value for the constant values page.)
044: * </p>
045: *
046: * @see #US_ENGLISH_MAPPING
047: */
048: public static final String US_ENGLISH_MAPPING_STRING = "01230120022455012623010202";
049:
050: /**
051: * This is a default mapping of the 26 letters used in US English. A value of <code>0</code> for a letter position
052: * means do not encode.
053: *
054: * @see Soundex#Soundex(char[])
055: */
056: public static final char[] US_ENGLISH_MAPPING = US_ENGLISH_MAPPING_STRING
057: .toCharArray();
058:
059: /**
060: * Encodes the Strings and returns the number of characters in the two encoded Strings that are the same. This
061: * return value ranges from 0 through 4: 0 indicates little or no similarity, and 4 indicates strong similarity or
062: * identical values.
063: *
064: * @param s1
065: * A String that will be encoded and compared.
066: * @param s2
067: * A String that will be encoded and compared.
068: * @return The number of characters in the two encoded Strings that are the same from 0 to 4.
069: *
070: * @see SoundexUtils#difference(StringEncoder,String,String)
071: * @see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp"> MS
072: * T-SQL DIFFERENCE </a>
073: *
074: * @throws EncoderException
075: * if an error occurs encoding one of the strings
076: * @since 1.3
077: */
078: public int difference(String s1, String s2) throws EncoderException {
079: return SoundexUtils.difference(this , s1, s2);
080: }
081:
082: /**
083: * The maximum length of a Soundex code - Soundex codes are only four characters by definition.
084: *
085: * @deprecated This feature is not needed since the encoding size must be constant. Will be removed in 2.0.
086: */
087: private int maxLength = 4;
088:
089: /**
090: * Every letter of the alphabet is "mapped" to a numerical value. This char array holds the values to which each
091: * letter is mapped. This implementation contains a default map for US_ENGLISH
092: */
093: private char[] soundexMapping;
094:
095: /**
096: * Creates an instance using US_ENGLISH_MAPPING
097: *
098: * @see Soundex#Soundex(char[])
099: * @see Soundex#US_ENGLISH_MAPPING
100: */
101: public Soundex() {
102: this (US_ENGLISH_MAPPING);
103: }
104:
105: /**
106: * Creates a soundex instance using the given mapping. This constructor can be used to provide an internationalized
107: * mapping for a non-Western character set.
108: *
109: * Every letter of the alphabet is "mapped" to a numerical value. This char array holds the values to which each
110: * letter is mapped. This implementation contains a default map for US_ENGLISH
111: *
112: * @param mapping
113: * Mapping array to use when finding the corresponding code for a given character
114: */
115: public Soundex(char[] mapping) {
116: this .setSoundexMapping(mapping);
117: }
118:
119: /**
120: * Encodes an Object using the soundex algorithm. This method is provided in order to satisfy the requirements of
121: * the Encoder interface, and will throw an EncoderException if the supplied object is not of type java.lang.String.
122: *
123: * @param pObject
124: * Object to encode
125: * @return An object (or type java.lang.String) containing the soundex code which corresponds to the String
126: * supplied.
127: * @throws EncoderException
128: * if the parameter supplied is not of type java.lang.String
129: * @throws IllegalArgumentException
130: * if a character is not mapped
131: */
132: public Object encode(Object pObject) throws EncoderException {
133: if (!(pObject instanceof String)) {
134: throw new EncoderException(
135: "Parameter supplied to Soundex encode is not of type java.lang.String");
136: }
137: return soundex((String) pObject);
138: }
139:
140: /**
141: * Encodes a String using the soundex algorithm.
142: *
143: * @param pString
144: * A String object to encode
145: * @return A Soundex code corresponding to the String supplied
146: * @throws IllegalArgumentException
147: * if a character is not mapped
148: */
149: public String encode(String pString) {
150: return soundex(pString);
151: }
152:
153: /**
154: * Used internally by the SoundEx algorithm.
155: *
156: * Consonants from the same code group separated by W or H are treated as one.
157: *
158: * @param str
159: * the cleaned working string to encode (in upper case).
160: * @param index
161: * the character position to encode
162: * @return Mapping code for a particular character
163: * @throws IllegalArgumentException
164: * if the character is not mapped
165: */
166: private char getMappingCode(String str, int index) {
167: char mappedChar = this .map(str.charAt(index));
168: // HW rule check
169: if (index > 1 && mappedChar != '0') {
170: char hwChar = str.charAt(index - 1);
171: if ('H' == hwChar || 'W' == hwChar) {
172: char preHWChar = str.charAt(index - 2);
173: char firstCode = this .map(preHWChar);
174: if (firstCode == mappedChar || 'H' == preHWChar
175: || '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: */
189: public int getMaxLength() {
190: return this .maxLength;
191: }
192:
193: /**
194: * Returns the soundex mapping.
195: *
196: * @return soundexMapping.
197: */
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: */
211: private char map(char ch) {
212: int index = ch - 'A';
213: if (index < 0 || index >= this .getSoundexMapping().length) {
214: throw new IllegalArgumentException(
215: "The character is not mapped: " + ch);
216: }
217: return this .getSoundexMapping()[index];
218: }
219:
220: /**
221: * Sets the maxLength.
222: *
223: * @deprecated This feature is not needed since the encoding size must be constant. Will be removed in 2.0.
224: * @param maxLength
225: * The maxLength to set
226: */
227: public void setMaxLength(int maxLength) {
228: this .maxLength = maxLength;
229: }
230:
231: /**
232: * Sets the soundexMapping.
233: *
234: * @param soundexMapping
235: * The soundexMapping to set.
236: */
237: private void setSoundexMapping(char[] soundexMapping) {
238: this .soundexMapping = soundexMapping;
239: }
240:
241: /**
242: * Retreives the Soundex code for a given String object.
243: *
244: * @param str
245: * String to encode using the Soundex algorithm
246: * @return A soundex code for the String supplied
247: * @throws IllegalArgumentException
248: * if a character is not mapped
249: */
250: public String soundex(String str) {
251: if (str == null) {
252: return null;
253: }
254: str = SoundexUtils.clean(str);
255: if (str.length() == 0) {
256: return str;
257: }
258: char out[] = { '0', '0', '0', '0' };
259: char last, mapped;
260: int incount = 1, count = 1;
261: out[0] = str.charAt(0);
262: last = getMappingCode(str, 0);
263: while ((incount < str.length()) && (count < out.length)) {
264: mapped = getMappingCode(str, incount++);
265: if (mapped != 0) {
266: if ((mapped != '0') && (mapped != last)) {
267: out[count++] = mapped;
268: }
269: last = mapped;
270: }
271: }
272: return new String(out);
273: }
274:
275: }
|