Skip to content

Commit 9da7d2a

Browse files
committed
More Soundex test data and code clean up.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/codec/trunk@130228 13f79535-47bb-0310-9956-ffa450edef68
1 parent efed057 commit 9da7d2a

2 files changed

Lines changed: 114 additions & 60 deletions

File tree

src/java/org/apache/commons/codec/language/Soundex.java

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,17 @@
6767
*
6868
* @author bayard@generationjava.com
6969
* @author Tim O'Brien
70-
* @author ggregory@seagullsw.com
71-
* @version $Id: Soundex.java,v 1.9 2003/10/12 19:48:15 tobrien Exp $
70+
* @author Gary Gregory
71+
* @version $Id: Soundex.java,v 1.10 2003/11/04 02:43:09 ggregory Exp $
7272
*/
7373
public class Soundex implements StringEncoder {
7474

75+
/**
76+
* This static variable contains an instance of the
77+
* Soundex using the US_ENGLISH mapping.
78+
*/
79+
public static final Soundex US_ENGLISH = new Soundex();
80+
7581
/**
7682
* This is a default mapping of the 26 letters used
7783
* in US english.
@@ -80,10 +86,10 @@ public class Soundex implements StringEncoder {
8086
"01230120022455012623010202".toCharArray();
8187

8288
/**
83-
* This static variable contains an instance of the
84-
* Soundex using the US_ENGLISH mapping.
89+
* The maximum length of a Soundex code - Soundex codes are
90+
* only four characters by definition.
8591
*/
86-
public static final Soundex US_ENGLISH = new Soundex();
92+
private int maxLength = 4;
8793

8894
/**
8995
* Every letter of the alphabet is "mapped" to a numerical
@@ -93,12 +99,6 @@ public class Soundex implements StringEncoder {
9399
*/
94100
private char[] soundexMapping;
95101

96-
/**
97-
* The maximum length of a Soundex code - Soundex codes are
98-
* only four characters by definition.
99-
*/
100-
private int maxLength = 4;
101-
102102
/**
103103
* Creates an instance of the Soundex object using the default
104104
* US_ENGLISH mapping.
@@ -117,30 +117,7 @@ public Soundex() {
117117
* code for a given character
118118
*/
119119
public Soundex(char[] mapping) {
120-
this.soundexMapping = mapping;
121-
}
122-
123-
/**
124-
* Retreives the Soundex code for a given String object.
125-
*
126-
* @param str String to encode using the Soundex algorithm
127-
* @return A soundex code for the String supplied
128-
*/
129-
public String soundex(String str) {
130-
if (null == str || str.length() == 0) { return str; }
131-
132-
char out[] = { '0', '0', '0', '0' };
133-
char last, mapped;
134-
int incount = 1, count = 1;
135-
out[0] = Character.toUpperCase(str.charAt(0));
136-
last = getMappingCode(str.charAt(0));
137-
while ((incount < str.length()) && (mapped = getMappingCode(str.charAt(incount++))) != 0 && (count < maxLength)) {
138-
if ((mapped != '0') && (mapped != last)) {
139-
out[count++] = mapped;
140-
}
141-
last = mapped;
142-
}
143-
return new String(out);
120+
this.setSoundexMapping(mapping);
144121
}
145122

146123
/**
@@ -174,10 +151,8 @@ public Object encode(Object pObject) throws EncoderException {
174151
*
175152
* @param pString A String object to encode
176153
* @return A Soundex code corresponding to the String supplied
177-
* @throws EncoderException throws exception if there is an
178-
* encoding-specific problem
179154
*/
180-
public String encode(String pString) throws EncoderException {
155+
public String encode(String pString) {
181156
return (soundex(pString));
182157
}
183158

@@ -191,7 +166,7 @@ private char getMappingCode(char c) {
191166
if (!Character.isLetter(c)) {
192167
return 0;
193168
} else {
194-
return soundexMapping[Character.toUpperCase(c) - 'A'];
169+
return this.getSoundexMapping()[Character.toUpperCase(c) - 'A'];
195170
}
196171
}
197172

@@ -200,7 +175,14 @@ private char getMappingCode(char c) {
200175
* @return int
201176
*/
202177
public int getMaxLength() {
203-
return maxLength;
178+
return this.maxLength;
179+
}
180+
181+
/**
182+
* @return Returns the soundexMapping.
183+
*/
184+
private char[] getSoundexMapping() {
185+
return this.soundexMapping;
204186
}
205187

206188
/**
@@ -211,4 +193,34 @@ public void setMaxLength(int maxLength) {
211193
this.maxLength = maxLength;
212194
}
213195

196+
/**
197+
* @param soundexMapping The soundexMapping to set.
198+
*/
199+
private void setSoundexMapping(char[] soundexMapping) {
200+
this.soundexMapping = soundexMapping;
201+
}
202+
203+
/**
204+
* Retreives the Soundex code for a given String object.
205+
*
206+
* @param str String to encode using the Soundex algorithm
207+
* @return A soundex code for the String supplied
208+
*/
209+
public String soundex(String str) {
210+
if (null == str || str.length() == 0) { return str; }
211+
212+
char out[] = { '0', '0', '0', '0' };
213+
char last, mapped;
214+
int incount = 1, count = 1;
215+
out[0] = Character.toUpperCase(str.charAt(0));
216+
last = getMappingCode(str.charAt(0));
217+
while ((incount < str.length()) && (mapped = getMappingCode(str.charAt(incount++))) != 0 && (count < this.getMaxLength())) {
218+
if ((mapped != '0') && (mapped != last)) {
219+
out[count++] = mapped;
220+
}
221+
last = mapped;
222+
}
223+
return new String(out);
224+
}
225+
214226
}

src/test/org/apache/commons/codec/language/SoundexTest.java

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,52 +64,94 @@
6464
import org.apache.commons.codec.StringEncoderAbstractTest;
6565

6666
/**
67-
* @version $Revision: 1.4 $ $Date: 2003/10/05 21:45:49 $
67+
* @version $Revision: 1.5 $ $Date: 2003/11/04 02:43:09 $
6868
* @author Rodney Waldhoff
69+
* @author Gary Gregory
6970
*/
7071
public class SoundexTest extends StringEncoderAbstractTest {
7172

73+
public static Test suite() {
74+
return (new TestSuite(SoundexTest.class));
75+
}
76+
77+
private Soundex _encoder = null;
78+
7279
public SoundexTest(String name) {
7380
super(name);
7481
}
82+
/**
83+
* @return Returns the _encoder.
84+
*/
85+
public Soundex getEncoder() {
86+
return this._encoder;
87+
}
7588

76-
public static Test suite() {
77-
return (new TestSuite(SoundexTest.class));
89+
protected StringEncoder makeEncoder() {
90+
return new Soundex();
91+
}
92+
93+
/**
94+
* @param _encoder The _encoder to set.
95+
*/
96+
public void setEncoder(Soundex encoder) {
97+
this._encoder = encoder;
7898
}
7999

80100
public void setUp() throws Exception {
81101
super.setUp();
82-
_encoder = new Soundex();
102+
this.setEncoder(new Soundex());
83103
}
84104

85105
public void tearDown() throws Exception {
86106
super.tearDown();
87-
_encoder = null;
88-
}
89-
90-
protected StringEncoder makeEncoder() {
91-
return new Soundex();
107+
this.setEncoder(null);
92108
}
93109

94110
// ------------------------------------------------------------------------
95111

96112
public void testEncode() throws Exception {
97-
assertEquals("T235",_encoder.encode("testing"));
98-
assertEquals("T000",_encoder.encode("The"));
99-
assertEquals("Q200",_encoder.encode("quick"));
100-
assertEquals("B650",_encoder.encode("brown"));
101-
assertEquals("F200",_encoder.encode("fox"));
102-
assertEquals("J513",_encoder.encode("jumped"));
103-
assertEquals("O160",_encoder.encode("over"));
104-
assertEquals("T000",_encoder.encode("the"));
105-
assertEquals("L200",_encoder.encode("lazy"));
106-
assertEquals("D200",_encoder.encode("dogs"));
113+
assertEquals("T235",this.getEncoder().encode("testing"));
114+
assertEquals("T000",this.getEncoder().encode("The"));
115+
assertEquals("Q200",this.getEncoder().encode("quick"));
116+
assertEquals("B650",this.getEncoder().encode("brown"));
117+
assertEquals("F200",this.getEncoder().encode("fox"));
118+
assertEquals("J513",this.getEncoder().encode("jumped"));
119+
assertEquals("O160",this.getEncoder().encode("over"));
120+
assertEquals("T000",this.getEncoder().encode("the"));
121+
assertEquals("L200",this.getEncoder().encode("lazy"));
122+
assertEquals("D200",this.getEncoder().encode("dogs"));
107123
}
108124

125+
/**
126+
* Examples from
127+
* http://www.bradandkathy.com/genealogy/overviewofsoundex.html
128+
*/
129+
public void testEncode2() throws Exception {
130+
assertEquals("A462",this.getEncoder().encode("Allricht"));
131+
assertEquals("E166",this.getEncoder().encode("Eberhard"));
132+
assertEquals("E521",this.getEncoder().encode("Engebrethson"));
133+
assertEquals("H512",this.getEncoder().encode("Heimbach"));
134+
assertEquals("H524",this.getEncoder().encode("Hanselmann"));
135+
assertEquals("H431",this.getEncoder().encode("Hildebrand"));
136+
assertEquals("K152",this.getEncoder().encode("Kavanagh"));
137+
assertEquals("L530",this.getEncoder().encode("Lind, Van"));
138+
assertEquals("L222",this.getEncoder().encode("Lukaschowsky"));
139+
assertEquals("M235",this.getEncoder().encode("McDonnell"));
140+
assertEquals("M200",this.getEncoder().encode("McGee"));
141+
// Fix me?
142+
//assertEquals("O165",this.getEncoder().encode("O'Brien"));
143+
assertEquals("O155",this.getEncoder().encode("Opnian"));
144+
assertEquals("O155",this.getEncoder().encode("Oppenheimer"));
145+
// Fix me?
146+
//assertEquals("S460",this.getEncoder().encode("Swhgler"));
147+
assertEquals("R355",this.getEncoder().encode("Riedemanas"));
148+
assertEquals("Z300",this.getEncoder().encode("Zita"));
149+
assertEquals("Z325",this.getEncoder().encode("Zitzmeinn"));
150+
}
151+
109152
public void testMaxLength() throws Exception {
110153
Soundex soundex = new Soundex();
111154
soundex.setMaxLength( soundex.getMaxLength() );
112155
}
113156

114-
private Soundex _encoder = null;
115157
}

0 commit comments

Comments
 (0)