22 * ====================================================================
33 *
44 * The Apache Software License, Version 1.1
5- *
6- * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
7- * reserved.
8- *
5+ *
6+ * Copyright (c) 2001-2003 The Apache Software Foundation. All rights reserved.
7+ *
98 * Redistribution and use in source and binary forms, with or without
10- * modification, are permitted provided that the following conditions
11- * are met:
12- *
13- * 1. Redistributions of source code must retain the above copyright
14- * notice, this list of conditions and the following disclaimer.
15- *
16- * 2. Redistributions in binary form must reproduce the above copyright
17- * notice, this list of conditions and the following disclaimer in
18- * the documentation and/or other materials provided with the
19- * distribution.
20- *
21- * 3. The end-user documentation included with the redistribution,
22- * if any, must include the following acknowledgement:
23- * "This product includes software developed by the
24- * Apache Software Foundation (http://www.apache.org/)."
25- * Alternately, this acknowledgement may appear in the software itself,
26- * if and wherever such third-party acknowledgements normally appear.
27- *
28- * 4. The names "Apache", "The Jakarta Project", "Commons", and "Apache Software
29- * Foundation" must not be used to endorse or promote products derived
30- * from this software without prior written permission. For written
31- * permission, please contact apache@apache.org.
32- *
33- * 5. Products derived from this software may not be called "Apache",
34- * "Apache" nor may "Apache" appear in their name without prior
35- * written permission of the Apache Software Foundation.
36- *
37- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40- * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48- * SUCH DAMAGE.
9+ * modification, are permitted provided that the following conditions are met: 1.
10+ * Redistributions of source code must retain the above copyright notice, this
11+ * list of conditions and the following disclaimer. 2. Redistributions in
12+ * binary form must reproduce the above copyright notice, this list of
13+ * conditions and the following disclaimer in the documentation and/or other
14+ * materials provided with the distribution. 3. The end-user documentation
15+ * included with the redistribution, if any, must include the following
16+ * acknowledgement: "This product includes software developed by the Apache
17+ * Software Foundation (http://www.apache.org/)." Alternately, this
18+ * acknowledgement may appear in the software itself, if and wherever such
19+ * third-party acknowledgements normally appear. 4. The names "Apache", "The
20+ * Jakarta Project", "Commons", and "Apache Software Foundation" must not be
21+ * used to endorse or promote products derived from this software without prior
22+ * written permission. For written permission, please contact
23+ * apache@apache.org. 5. Products derived from this software may not be called
24+ * "Apache", "Apache" nor may "Apache" appear in their name without prior
25+ * written permission of the Apache Software Foundation.
26+ *
27+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30+ * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4937 * ====================================================================
50- *
51- * This software consists of voluntary contributions made by many
52- * individuals on behalf of the Apache Software Foundation. For more
53- * information on the Apache Software Foundation, please see
54- * <http://www.apache.org/>.
55- *
56- */
38+ *
39+ * This software consists of voluntary contributions made by many individuals
40+ * on behalf of the Apache Software Foundation. For more information on the
41+ * Apache Software Foundation, please see <http://www.apache.org/> .
42+ *
43+ */
5744
5845package org .apache .commons .codec .language ;
5946
6855 * @author bayard@generationjava.com
6956 * @author Tim O'Brien
7057 * @author Gary Gregory
71- * @version $Id: Soundex.java,v 1.13 2003/11/12 19:02:57 ggregory Exp $
58+ * @version $Id: Soundex.java,v 1.14 2003/12/10 00:04:46 ggregory Exp $
7259 */
7360public class Soundex implements StringEncoder {
7461
@@ -84,11 +71,93 @@ public class Soundex implements StringEncoder {
8471 */
8572 public static final char [] US_ENGLISH_MAPPING = "01230120022455012623010202" .toCharArray ();
8673
74+ /**
75+ * Returns the difference between the Soundex values of two Strings. For
76+ * Soundex, this return value ranges from 0 through 4: 0 indicates little or
77+ * no similarity, and 4 indicates strong similarity or identical values.
78+ *
79+ * @param s1
80+ * A String.
81+ * @param s2
82+ * A String.
83+ * @return The return value ranges from 0 through 4: 0 indicates little or
84+ * no similarity, and 4 indicates strong similarity or identical
85+ * values.
86+ *
87+ * @see #difference(StringEncoder,String,String)
88+ * @see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp">
89+ * MS T-SQL DIFFERENCE</a>
90+ *
91+ * @throws EncoderException
92+ * if an error occurs encoding one of the strings
93+ */
94+ public int difference (String s1 , String s2 ) throws EncoderException {
95+ return difference (this , s1 , s2 );
96+ }
97+
98+ /**
99+ * Returns the difference between the encoded values of two Strings. The
100+ * higher the difference factor, the more similar the strings. For Soundex,
101+ * this return value ranges from 0 through 4: 0 indicates little or no
102+ * similarity, and 4 indicates strong similarity or identical values.
103+ *
104+ * @param encoder
105+ * The encoder to use to encode the String parameters with.
106+ * @param s1
107+ * A String.
108+ * @param s2
109+ * A String.
110+ * @return an integer from 0 to the length of the shorter string. The
111+ * smaller the number, the more different the strings are.
112+ *
113+ * @see #differenceEncoded(String,String)
114+ * @see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp">
115+ * MS T-SQL DIFFERENCE</a>
116+ *
117+ * @throws EncoderException
118+ * if an error occurs encoding one of the strings
119+ */
120+ public static int difference (StringEncoder encoder , String s1 , String s2 ) throws EncoderException {
121+ return differenceEncoded (encoder .encode (s1 ), encoder .encode (s2 ));
122+ }
123+
124+ /**
125+ * Returns the difference between the values of two encoded Strings. The
126+ * higher the difference factor, the more similar the strings. For Soundex,
127+ * this return value ranges from 0 through 4: 0 indicates little or no
128+ * similarity, and 4 indicates strong similarity or identical values.
129+ *
130+ * @param es1
131+ * An encoded String.
132+ * @param es2
133+ * An encoded String.
134+ * @return an integer from 0 to the length of the shorter string. The
135+ * smaller the number, the more different the strings are.
136+ *
137+ * @see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp">
138+ * MS T-SQL DIFFERENCE</a>
139+ */
140+ public static int differenceEncoded (String es1 , String es2 ) {
141+
142+ if (es1 == null || es2 == null ) {
143+ return 0 ;
144+ }
145+ int lengthToMatch = Math .min (es1 .length (), es2 .length ());
146+ int diff = 0 ;
147+ for (int i = 0 ; i < lengthToMatch ; i ++) {
148+ if (es1 .charAt (i ) == es2 .charAt (i )) {
149+ diff ++;
150+ }
151+ }
152+ return diff ;
153+ }
154+
87155 /**
88156 * The maximum length of a Soundex code - Soundex codes are only four
89157 * characters by definition.
90- *
91- * @deprecated This feature is not needed since the encoding size must be constant.
158+ *
159+ * @deprecated This feature is not needed since the encoding size must be
160+ * constant.
92161 */
93162 private int maxLength = 4 ;
94163
@@ -123,6 +192,10 @@ public Soundex(char[] mapping) {
123192 /**
124193 * Cleans up the input string before Soundex processing by only returning
125194 * upper case letters.
195+ *
196+ * @param str
197+ * The String to clean
198+ * @return a clean String.
126199 */
127200 private String clean (String str ) {
128201 if (str == null || str .length () == 0 ) {
@@ -211,7 +284,8 @@ private char getMappingCode(String str, int index) {
211284 /**
212285 * Returns the maxLength. Standard Soundex
213286 *
214- * @deprecated This feature is not needed since the encoding size must be constant.
287+ * @deprecated This feature is not needed since the encoding size must be
288+ * constant.
215289 * @return int
216290 */
217291 public int getMaxLength () {
@@ -227,6 +301,10 @@ private char[] getSoundexMapping() {
227301
228302 /**
229303 * Maps the given upper-case character to it's Soudex code.
304+ *
305+ * @param c
306+ * An upper-case character.
307+ * @return A Soundex code.
230308 */
231309 private char map (char c ) {
232310 return this .getSoundexMapping ()[c - 'A' ];
@@ -235,7 +313,8 @@ private char map(char c) {
235313 /**
236314 * Sets the maxLength.
237315 *
238- * @deprecated This feature is not needed since the encoding size must be constant.
316+ * @deprecated This feature is not needed since the encoding size must be
317+ * constant.
239318 * @param maxLength
240319 * The maxLength to set
241320 */
@@ -266,7 +345,7 @@ public String soundex(String str) {
266345 if (str .length () == 0 ) {
267346 return str ;
268347 }
269- char out [] = { '0' , '0' , '0' , '0' };
348+ char out [] = {'0' , '0' , '0' , '0' };
270349 char last , mapped ;
271350 int incount = 1 , count = 1 ;
272351 out [0 ] = str .charAt (0 );
@@ -283,4 +362,4 @@ public String soundex(String str) {
283362 return new String (out );
284363 }
285364
286- }
365+ }
0 commit comments