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 Refined Soundex value. A refined soundex code is
024: * optimized for spell checking words. Soundex method originally developed by
025: * <CITE>Margaret Odell</CITE> and <CITE>Robert Russell</CITE>.
026: *
027: * @author Apache Software Foundation
028: * @version $Id: RefinedSoundex.java,v 1.21 2004/06/05 18:32:04 ggregory Exp $
029: */
030: public class RefinedSoundex implements StringEncoder {
031:
032: /**
033: * This static variable contains an instance of the RefinedSoundex using
034: * the US_ENGLISH mapping.
035: */
036: public static final RefinedSoundex US_ENGLISH = new RefinedSoundex();
037:
038: /**
039: * RefinedSoundex is *refined* for a number of reasons one being that the
040: * mappings have been altered. This implementation contains default
041: * mappings for US English.
042: */
043: public static final char[] US_ENGLISH_MAPPING = "01360240043788015936020505"
044: .toCharArray();
045:
046: /**
047: * Every letter of the alphabet is "mapped" to a numerical value. This char
048: * array holds the values to which each letter is mapped. This
049: * implementation contains a default map for US_ENGLISH
050: */
051: private char[] soundexMapping;
052:
053: /**
054: * Creates an instance of the RefinedSoundex object using the default US
055: * English mapping.
056: */
057: public RefinedSoundex() {
058: this (US_ENGLISH_MAPPING);
059: }
060:
061: /**
062: * Creates a refined soundex instance using a custom mapping. This
063: * constructor can be used to customize the mapping, and/or possibly
064: * provide an internationalized mapping for a non-Western character set.
065: *
066: * @param mapping
067: * Mapping array to use when finding the corresponding code for
068: * a given character
069: */
070: public RefinedSoundex(char[] mapping) {
071: this .soundexMapping = mapping;
072: }
073:
074: /**
075: * Returns the number of characters in the two encoded Strings that are the
076: * same. This return value ranges from 0 to the length of the shortest
077: * encoded String: 0 indicates little or no similarity, and 4 out of 4 (for
078: * example) indicates strong similarity or identical values. For refined
079: * Soundex, the return value can be greater than 4.
080: *
081: * @param s1
082: * A String that will be encoded and compared.
083: * @param s2
084: * A String that will be encoded and compared.
085: * @return The number of characters in the two encoded Strings that are the
086: * same from 0 to to the length of the shortest encoded String.
087: *
088: * @see SoundexUtils#difference(StringEncoder,String,String)
089: * @see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp">
090: * MS T-SQL DIFFERENCE</a>
091: *
092: * @throws EncoderException
093: * if an error occurs encoding one of the strings
094: * @since 1.3
095: */
096: public int difference(String s1, String s2) throws EncoderException {
097: return SoundexUtils.difference(this , s1, s2);
098: }
099:
100: /**
101: * Encodes an Object using the refined soundex algorithm. This method is
102: * provided in order to satisfy the requirements of the Encoder interface,
103: * and will throw an EncoderException if the supplied object is not of type
104: * java.lang.String.
105: *
106: * @param pObject
107: * Object to encode
108: * @return An object (or type java.lang.String) containing the refined
109: * soundex code which corresponds to the String supplied.
110: * @throws EncoderException
111: * if the parameter supplied is not of type java.lang.String
112: */
113: public Object encode(Object pObject) throws EncoderException {
114: if (!(pObject instanceof java.lang.String)) {
115: throw new EncoderException(
116: "Parameter supplied to RefinedSoundex encode is not of type java.lang.String");
117: }
118: return soundex((String) pObject);
119: }
120:
121: /**
122: * Encodes a String using the refined soundex algorithm.
123: *
124: * @param pString
125: * A String object to encode
126: * @return A Soundex code corresponding to the String supplied
127: */
128: public String encode(String pString) {
129: return soundex(pString);
130: }
131:
132: /**
133: * Returns the mapping code for a given character. The mapping codes are
134: * maintained in an internal char array named soundexMapping, and the
135: * default values of these mappings are US English.
136: *
137: * @param c
138: * char to get mapping for
139: * @return A character (really a numeral) to return for the given char
140: */
141: char getMappingCode(char c) {
142: if (!Character.isLetter(c)) {
143: return 0;
144: }
145: return this .soundexMapping[Character.toUpperCase(c) - 'A'];
146: }
147:
148: /**
149: * Retreives the Refined Soundex code for a given String object.
150: *
151: * @param str
152: * String to encode using the Refined Soundex algorithm
153: * @return A soundex code for the String supplied
154: */
155: public String soundex(String str) {
156: if (str == null) {
157: return null;
158: }
159: str = SoundexUtils.clean(str);
160: if (str.length() == 0) {
161: return str;
162: }
163:
164: StringBuffer sBuf = new StringBuffer();
165: sBuf.append(str.charAt(0));
166:
167: char last, current;
168: last = '*';
169:
170: for (int i = 0; i < str.length(); i++) {
171:
172: current = getMappingCode(str.charAt(i));
173: if (current == last) {
174: continue;
175: } else if (current != 0) {
176: sBuf.append(current);
177: }
178:
179: last = current;
180:
181: }
182:
183: return sBuf.toString();
184: }
185: }
|