@@ -48,22 +48,22 @@ public class Metaphone implements StringEncoder {
4848 /**
4949 * Five values in the English language
5050 */
51- private static final String VOWELS = "AEIOU" ;
51+ private static final String VOWELS = "AEIOU" ;
5252
5353 /**
5454 * Variable used in Metaphone algorithm
5555 */
56- private static final String FRONTV = "EIY" ;
56+ private static final String FRONTV = "EIY" ;
5757
5858 /**
5959 * Variable used in Metaphone algorithm
6060 */
61- private static final String VARSON = "CSPTG" ;
61+ private static final String VARSON = "CSPTG" ;
6262
6363 /**
6464 * The max code length for metaphone is 4
6565 */
66- private int maxCodeLen = 4 ;
66+ private int maxCodeLen = 4 ;
6767
6868 /**
6969 * Creates an instance of the Metaphone encoder
@@ -83,24 +83,24 @@ public Metaphone() {
8383 * @return A metaphone code corresponding to the String supplied
8484 */
8585 public String metaphone (String txt ) {
86- boolean hard = false ;
86+ boolean hard = false ;
8787 if (txt == null || txt .length () == 0 ) {
88- return "" ;
88+ return "" ;
8989 }
9090 // single character is itself
9191 if (txt .length () == 1 ) {
92- return txt .toUpperCase (java .util .Locale .ENGLISH ) ;
92+ return txt .toUpperCase (java .util .Locale .ENGLISH );
9393 }
9494
95- char [] inwd = txt .toUpperCase (java .util .Locale .ENGLISH ).toCharArray () ;
95+ char [] inwd = txt .toUpperCase (java .util .Locale .ENGLISH ).toCharArray ();
9696
9797 StringBuilder local = new StringBuilder (40 ); // manipulate
98- StringBuilder code = new StringBuilder (10 ) ; // output
98+ StringBuilder code = new StringBuilder (10 ); // output
9999 // handle initial 2 characters exceptions
100100 switch (inwd [0 ]) {
101- case 'K' :
102- case 'G' :
103- case 'P' : /* looking for KN, etc*/
101+ case 'K' :
102+ case 'G' :
103+ case 'P' : /* looking for KN, etc*/
104104 if (inwd [1 ] == 'N' ) {
105105 local .append (inwd , 1 , inwd .length - 1 );
106106 } else {
@@ -114,10 +114,10 @@ public String metaphone(String txt) {
114114 local .append (inwd );
115115 }
116116 break ;
117- case 'W' : /* looking for WR or WH */
117+ case 'W' : /* looking for WR or WH */
118118 if (inwd [1 ] == 'R' ) { // WR -> R
119119 local .append (inwd , 1 , inwd .length - 1 );
120- break ;
120+ break ;
121121 }
122122 if (inwd [1 ] == 'H' ) {
123123 local .append (inwd , 1 , inwd .length - 1 );
@@ -126,38 +126,42 @@ public String metaphone(String txt) {
126126 local .append (inwd );
127127 }
128128 break ;
129- case 'X' : /* initial X becomes S */
129+ case 'X' : /* initial X becomes S */
130130 inwd [0 ] = 'S' ;
131131 local .append (inwd );
132- break ;
133- default :
132+ break ;
133+ default :
134134 local .append (inwd );
135135 } // now local has working string with initials fixed
136136
137137 int wdsz = local .length ();
138- int n = 0 ;
138+ int n = 0 ;
139139
140140 while (code .length () < this .getMaxCodeLen () &&
141141 n < wdsz ) { // max code size of 4 works well
142- char symb = local .charAt (n ) ;
142+ char symb = local .charAt (n );
143143 // remove duplicate letters except C
144144 if (symb != 'C' && isPreviousChar ( local , n , symb ) ) {
145- n ++ ;
145+ n ++;
146146 } else { // not dup
147147 switch (symb ) {
148- case 'A' : case 'E' : case 'I' : case 'O' : case 'U' :
148+ case 'A' :
149+ case 'E' :
150+ case 'I' :
151+ case 'O' :
152+ case 'U' :
149153 if (n == 0 ) {
150154 code .append (symb );
151155 }
152- break ; // only use vowel if leading char
153- case 'B' :
156+ break ; // only use vowel if leading char
157+ case 'B' :
154158 if ( isPreviousChar (local , n , 'M' ) &&
155159 isLastChar (wdsz , n ) ) { // B is silent if word ends in MB
156160 break ;
157161 }
158162 code .append (symb );
159163 break ;
160- case 'C' : // lots of C special cases
164+ case 'C' : // lots of C special cases
161165 /* discard if SCI, SCE or SCY */
162166 if ( isPreviousChar (local , n , 'S' ) &&
163167 !isLastChar (wdsz , n ) &&
@@ -175,8 +179,8 @@ public String metaphone(String txt) {
175179 }
176180 if (isPreviousChar (local , n , 'S' ) &&
177181 isNextChar (local , n , 'H' ) ) { // SCH->sk
178- code .append ('K' ) ;
179- break ;
182+ code .append ('K' );
183+ break ;
180184 }
181185 if (isNextChar (local , n , 'H' )) { // detect CH
182186 if (n == 0 &&
@@ -189,17 +193,17 @@ public String metaphone(String txt) {
189193 } else {
190194 code .append ('K' );
191195 }
192- break ;
193- case 'D' :
196+ break ;
197+ case 'D' :
194198 if (!isLastChar (wdsz , n + 1 ) &&
195199 isNextChar (local , n , 'G' ) &&
196200 FRONTV .indexOf (local .charAt (n + 2 )) >= 0 ) { // DGE DGI DGY -> J
197- code .append ('J' ); n += 2 ;
201+ code .append ('J' ); n += 2 ;
198202 } else {
199203 code .append ('T' );
200204 }
201- break ;
202- case 'G' : // GH silent at end or before consonant
205+ break ;
206+ case 'G' : // GH silent at end or before consonant
203207 if (isLastChar (wdsz , n + 1 ) &&
204208 isNextChar (local , n , 'H' )) {
205209 break ;
@@ -216,9 +220,9 @@ public String metaphone(String txt) {
216220 }
217221 if (isPreviousChar (local , n , 'G' )) {
218222 // NOTE: Given that duplicated chars are removed, I don't see how this can ever be true
219- hard = true ;
223+ hard = true ;
220224 } else {
221- hard = false ;
225+ hard = false ;
222226 }
223227 if (!isLastChar (wdsz , n ) &&
224228 FRONTV .indexOf (local .charAt (n + 1 )) >= 0 &&
@@ -227,10 +231,10 @@ public String metaphone(String txt) {
227231 } else {
228232 code .append ('K' );
229233 }
230- break ;
234+ break ;
231235 case 'H' :
232236 if (isLastChar (wdsz , n )) {
233- break ; // terminal H
237+ break ; // terminal H
234238 }
235239 if (n > 0 &&
236240 VARSON .indexOf (local .charAt (n - 1 )) >= 0 ) {
@@ -241,34 +245,34 @@ public String metaphone(String txt) {
241245 }
242246 break ;
243247 case 'F' :
244- case 'J' :
245- case 'L' :
248+ case 'J' :
249+ case 'L' :
246250 case 'M' :
247- case 'N' :
248- case 'R' :
251+ case 'N' :
252+ case 'R' :
249253 code .append (symb );
250254 break ;
251- case 'K' :
255+ case 'K' :
252256 if (n > 0 ) { // not initial
253257 if (!isPreviousChar (local , n , 'C' )) {
254258 code .append (symb );
255259 }
256260 } else {
257261 code .append (symb ); // initial K
258262 }
259- break ;
260- case 'P' :
263+ break ;
264+ case 'P' :
261265 if (isNextChar (local ,n ,'H' )) {
262266 // PH -> F
263267 code .append ('F' );
264268 } else {
265269 code .append (symb );
266270 }
267- break ;
268- case 'Q' :
271+ break ;
272+ case 'Q' :
269273 code .append ('K' );
270274 break ;
271- case 'S' :
275+ case 'S' :
272276 if (regionMatch (local ,n ,"SH" ) ||
273277 regionMatch (local ,n ,"SIO" ) ||
274278 regionMatch (local ,n ,"SIA" )) {
@@ -277,7 +281,7 @@ public String metaphone(String txt) {
277281 code .append ('S' );
278282 }
279283 break ;
280- case 'T' :
284+ case 'T' :
281285 if (regionMatch (local ,n ,"TIA" ) ||
282286 regionMatch (local ,n ,"TIO" )) {
283287 code .append ('X' );
@@ -293,22 +297,28 @@ public String metaphone(String txt) {
293297 } else {
294298 code .append ('T' );
295299 }
296- break ;
297- case 'V' :
298- code .append ('F' ); break ;
299- case 'W' : case 'Y' : // silent if not followed by vowel
300+ break ;
301+ case 'V' :
302+ code .append ('F' ); break ;
303+ case 'W' :
304+ case 'Y' : // silent if not followed by vowel
300305 if (!isLastChar (wdsz ,n ) &&
301306 isVowel (local ,n +1 )) {
302307 code .append (symb );
303308 }
304- break ;
305- case 'X' :
306- code .append ('K' ); code .append ('S' );
307- break ;
308- case 'Z' :
309- code .append ('S' ); break ;
309+ break ;
310+ case 'X' :
311+ code .append ('K' );
312+ code .append ('S' );
313+ break ;
314+ case 'Z' :
315+ code .append ('S' );
316+ break ;
317+ default :
318+ // do nothing
319+ break ;
310320 } // end switch
311- n ++ ;
321+ n ++;
312322 } // end else from symb != 'C'
313323 if (code .length () > this .getMaxCodeLen ()) {
314324 code .setLength (this .getMaxCodeLen ());
0 commit comments