7171 * @author wbrogden@bga.com
7272 * @author bayard@generationjava.com
7373 * @author Tim O'Brien
74- * @version $Id: Metaphone.java,v 1.7 2003/10/05 21:45:48 tobrien Exp $
74+ * @version $Id: Metaphone.java,v 1.8 2003/10/12 19:48:14 tobrien Exp $
7575 */
7676public class Metaphone implements StringEncoder {
7777
@@ -115,8 +115,7 @@ public Metaphone() {
115115 public String metaphone (String txt ) {
116116 int mtsz = 0 ;
117117 boolean hard = false ;
118- if ((txt == null )
119- || (txt .length () == 0 )) {
118+ if ((txt == null ) || (txt .length () == 0 )) {
120119 return "" ;
121120 }
122121 // single character is itself
@@ -170,12 +169,10 @@ public String metaphone(String txt) {
170169 int wdsz = local .length ();
171170 int n = 0 ;
172171
173- while ((mtsz < maxCodeLen ) // max code size of 4 works well
174- && (n < wdsz )) {
172+ while ((mtsz < maxCodeLen ) && (n < wdsz )) { // max code size of 4 works well
175173 char symb = local .charAt (n ) ;
176174 // remove duplicate letters except C
177- if ((symb != 'C' )
178- && (n > 0 ) && (local .charAt (n - 1 ) == symb )) {
175+ if ((symb != 'C' ) && (n > 0 ) && (local .charAt (n - 1 ) == symb )) {
179176 n ++ ;
180177 } else { // not dup
181178 switch (symb ) {
@@ -186,9 +183,7 @@ public String metaphone(String txt) {
186183 }
187184 break ; // only use vowel if leading char
188185 case 'B' :
189- if ((n > 0 )
190- && !(n + 1 == wdsz ) // not MB at end of word
191- && (local .charAt (n - 1 ) == 'M' )) {
186+ if ((n > 0 ) && !(n + 1 == wdsz ) && (local .charAt (n - 1 ) == 'M' )) { // not MB at end of word
192187 code .append (symb );
193188 } else {
194189 code .append (symb );
@@ -197,32 +192,25 @@ public String metaphone(String txt) {
197192 break ;
198193 case 'C' : // lots of C special cases
199194 /* discard if SCI, SCE or SCY */
200- if ((n > 0 )
201- && (local .charAt (n - 1 ) == 'S' )
202- && (n + 1 < wdsz )
203- && (frontv .indexOf (local .charAt (n + 1 )) >= 0 )) {
195+ if ((n > 0 ) && (local .charAt (n - 1 ) == 'S' ) && (n + 1 < wdsz ) && (frontv .indexOf (local .charAt (n + 1 )) >= 0 )) {
204196 break ;
205197 }
206198 tmpS = local .toString ();
207199 if (tmpS .indexOf ("CIA" , n ) == n ) { // "CIA" -> X
208200 code .append ('X' ); mtsz ++; break ;
209201 }
210- if ((n + 1 < wdsz )
211- && (frontv .indexOf (local .charAt (n + 1 )) >= 0 )) {
202+ if ((n + 1 < wdsz ) && (frontv .indexOf (local .charAt (n + 1 )) >= 0 )) {
212203 code .append ('S' );
213204 mtsz ++;
214205 break ; // CI,CE,CY -> S
215206 }
216- if ((n > 0 )
217- && (tmpS .indexOf ("SCH" , n - 1 ) == n - 1 )) { // SCH->sk
207+ if ((n > 0 ) && (tmpS .indexOf ("SCH" , n - 1 ) == n - 1 )) { // SCH->sk
218208 code .append ('K' ) ;
219209 mtsz ++;
220210 break ;
221211 }
222212 if (tmpS .indexOf ("CH" , n ) == n ) { // detect CH
223- if ((n == 0 )
224- && (wdsz >= 3 ) // CH consonant -> K consonant
225- && (vowels .indexOf (local .charAt (2 )) < 0 )) {
213+ if ((n == 0 ) && (wdsz >= 3 ) && (vowels .indexOf (local .charAt (2 )) < 0 )) { // CH consonant -> K consonant
226214 code .append ('K' );
227215 } else {
228216 code .append ('X' ); // CHvowel -> X
@@ -234,40 +222,30 @@ public String metaphone(String txt) {
234222 }
235223 break ;
236224 case 'D' :
237- if ((n + 2 < wdsz ) // DGE DGI DGY -> J
238- && (local .charAt (n + 1 ) == 'G' )
239- && (frontv .indexOf (local .charAt (n + 2 )) >= 0 )) {
225+ if ((n + 2 < wdsz ) && (local .charAt (n + 1 ) == 'G' ) && (frontv .indexOf (local .charAt (n + 2 )) >= 0 )) { // DGE DGI DGY -> J
240226 code .append ('J' ); n += 2 ;
241227 } else {
242228 code .append ('T' );
243229 }
244230 mtsz ++;
245231 break ;
246232 case 'G' : // GH silent at end or before consonant
247- if ((n + 2 == wdsz )
248- && (local .charAt (n + 1 ) == 'H' )) {
233+ if ((n + 2 == wdsz ) && (local .charAt (n + 1 ) == 'H' )) {
249234 break ;
250235 }
251- if ((n + 2 < wdsz )
252- && (local .charAt (n + 1 ) == 'H' )
253- && (vowels .indexOf (local .charAt (n + 2 )) < 0 )) {
236+ if ((n + 2 < wdsz ) && (local .charAt (n + 1 ) == 'H' ) && (vowels .indexOf (local .charAt (n + 2 )) < 0 )) {
254237 break ;
255238 }
256239 tmpS = local .toString ();
257- if ((n > 0 )
258- && (tmpS .indexOf ("GN" , n ) == n )
259- || (tmpS .indexOf ("GNED" , n ) == n )) {
240+ if ((n > 0 ) && (tmpS .indexOf ("GN" , n ) == n ) || (tmpS .indexOf ("GNED" , n ) == n )) {
260241 break ; // silent G
261242 }
262- if ((n > 0 )
263- && (local .charAt (n - 1 ) == 'G' )) {
243+ if ((n > 0 ) && (local .charAt (n - 1 ) == 'G' )) {
264244 hard = true ;
265245 } else {
266246 hard = false ;
267247 }
268- if ((n + 1 < wdsz )
269- && (frontv .indexOf (local .charAt (n + 1 )) >= 0 )
270- && (!hard )) {
248+ if ((n + 1 < wdsz ) && (frontv .indexOf (local .charAt (n + 1 )) >= 0 ) && (!hard )) {
271249 code .append ('J' );
272250 } else {
273251 code .append ('K' );
@@ -278,8 +256,7 @@ public String metaphone(String txt) {
278256 if (n + 1 == wdsz ) {
279257 break ; // terminal H
280258 }
281- if ((n > 0 )
282- && (varson .indexOf (local .charAt (n - 1 )) >= 0 )) {
259+ if ((n > 0 ) && (varson .indexOf (local .charAt (n - 1 )) >= 0 )) {
283260 break ;
284261 }
285262 if (vowels .indexOf (local .charAt (n + 1 )) >= 0 ) {
@@ -307,8 +284,7 @@ public String metaphone(String txt) {
307284 mtsz ++ ;
308285 break ;
309286 case 'P' :
310- if ((n + 1 < wdsz )
311- && (local .charAt (n + 1 ) == 'H' )) {
287+ if ((n + 1 < wdsz ) && (local .charAt (n + 1 ) == 'H' )) {
312288 // PH -> F
313289 code .append ('F' );
314290 } else {
@@ -322,9 +298,7 @@ public String metaphone(String txt) {
322298 break ;
323299 case 'S' :
324300 tmpS = local .toString ();
325- if ((tmpS .indexOf ("SH" , n ) == n )
326- || (tmpS .indexOf ("SIO" , n ) == n )
327- || (tmpS .indexOf ("SIA" , n ) == n )) {
301+ if ((tmpS .indexOf ("SH" , n ) == n ) || (tmpS .indexOf ("SIO" , n ) == n ) || (tmpS .indexOf ("SIA" , n ) == n )) {
328302 code .append ('X' );
329303 } else {
330304 code .append ('S' );
@@ -333,8 +307,7 @@ public String metaphone(String txt) {
333307 break ;
334308 case 'T' :
335309 tmpS = local .toString (); // TIA TIO -> X
336- if ((tmpS .indexOf ("TIA" , n ) == n )
337- || (tmpS .indexOf ("TIO" , n ) == n )) {
310+ if ((tmpS .indexOf ("TIA" , n ) == n ) || (tmpS .indexOf ("TIO" , n ) == n )) {
338311 code .append ('X' );
339312 mtsz ++;
340313 break ;
@@ -353,8 +326,7 @@ public String metaphone(String txt) {
353326 case 'V' :
354327 code .append ('F' ); mtsz ++;break ;
355328 case 'W' : case 'Y' : // silent if not followed by vowel
356- if ((n + 1 < wdsz )
357- && (vowels .indexOf (local .charAt (n + 1 )) >= 0 )) {
329+ if ((n + 1 < wdsz ) && (vowels .indexOf (local .charAt (n + 1 )) >= 0 )) {
358330 code .append (symb );
359331 mtsz ++;
360332 }
@@ -388,9 +360,7 @@ public String metaphone(String txt) {
388360 public Object encode (Object pObject ) throws EncoderException {
389361 Object result ;
390362 if (!(pObject instanceof java .lang .String )) {
391- throw new EncoderException ("Parameter supplied to Metaphone "
392- + "encode is not of type "
393- + "java.lang.String" );
363+ throw new EncoderException ("Parameter supplied to Metaphone encode is not of type java.lang.String" );
394364 } else {
395365 result = metaphone ((String ) pObject );
396366 }
0 commit comments