KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > codec > language > MetaphoneTest


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.codec.language;
18
19 import junit.framework.Test;
20 import junit.framework.TestSuite;
21 import org.apache.commons.codec.StringEncoder;
22 import org.apache.commons.codec.StringEncoderAbstractTest;
23
24 /**
25  * @version $Id: MetaphoneTest.java,v 1.12 2004/04/19 01:14:29 ggregory Exp $
26  * @author Apache Software Foundation
27  */

28 public class MetaphoneTest extends StringEncoderAbstractTest {
29
30     public static Test suite() {
31         return (new TestSuite(MetaphoneTest.class));
32     }
33
34     private Metaphone metaphone = null;
35
36     public MetaphoneTest(String JavaDoc name) {
37         super(name);
38     }
39
40     public void assertIsMetaphoneEqual(String JavaDoc source, String JavaDoc[] matches) {
41         // match source to all matches
42
for (int i = 0; i < matches.length; i++) {
43             assertTrue("Source: " + source + ", should have same Metaphone as: " + matches[i],
44                        this.getMetaphone().isMetaphoneEqual(source, matches[i]));
45         }
46         // match to each other
47
for (int i = 0; i < matches.length; i++) {
48             for (int j = 0; j < matches.length; j++) {
49                 assertTrue(this.getMetaphone().isMetaphoneEqual(matches[i], matches[j]));
50             }
51         }
52     }
53
54     public void assertMetaphoneEqual(String JavaDoc[][] pairs) {
55         this.validateFixture(pairs);
56         for (int i = 0; i < pairs.length; i++) {
57             String JavaDoc name0 = pairs[i][0];
58             String JavaDoc name1 = pairs[i][1];
59             String JavaDoc failMsg = "Expected match between " + name0 + " and " + name1;
60             assertTrue(failMsg, this.getMetaphone().isMetaphoneEqual(name0, name1));
61             assertTrue(failMsg, this.getMetaphone().isMetaphoneEqual(name1, name0));
62         }
63     }
64     /**
65      * @return Returns the metaphone.
66      */

67     private Metaphone getMetaphone() {
68         return this.metaphone;
69     }
70
71     protected StringEncoder makeEncoder() {
72         return new Metaphone();
73     }
74
75     /**
76      * @param metaphone
77      * The metaphone to set.
78      */

79     private void setMetaphone(Metaphone metaphone) {
80         this.metaphone = metaphone;
81     }
82
83     public void setUp() throws Exception JavaDoc {
84         super.setUp();
85         this.setMetaphone(new Metaphone());
86     }
87
88     public void tearDown() throws Exception JavaDoc {
89         super.tearDown();
90         this.setMetaphone(null);
91     }
92
93     public void testIsMetaphoneEqual1() {
94         this.assertMetaphoneEqual(new String JavaDoc[][] { { "Case", "case" }, {
95                 "CASE", "Case" }, {
96                 "caSe", "cAsE" }, {
97                 "quick", "cookie" }
98         });
99     }
100
101     /**
102      * Matches computed from http://www.lanw.com/java/phonetic/default.htm
103      */

104     public void testIsMetaphoneEqual2() {
105         this.assertMetaphoneEqual(new String JavaDoc[][] { { "Lawrence", "Lorenza" }, {
106                 "Gary", "Cahra" }, });
107     }
108
109     /**
110      * Initial AE case.
111      *
112      * Match data computed from http://www.lanw.com/java/phonetic/default.htm
113      */

114     public void testIsMetaphoneEqualAero() {
115         this.assertIsMetaphoneEqual("Aero", new String JavaDoc[] { "Eure" });
116     }
117
118     /**
119      * Initial WH case.
120      *
121      * Match data computed from http://www.lanw.com/java/phonetic/default.htm
122      */

123     public void testIsMetaphoneEqualWhite() {
124         this.assertIsMetaphoneEqual(
125             "White",
126             new String JavaDoc[] { "Wade", "Wait", "Waite", "Wat", "Whit", "Wiatt", "Wit", "Wittie", "Witty", "Wood", "Woodie", "Woody" });
127     }
128
129     /**
130      * Initial A, not followed by an E case.
131      *
132      * Match data computed from http://www.lanw.com/java/phonetic/default.htm
133      */

134     public void testIsMetaphoneEqualAlbert() {
135         this.assertIsMetaphoneEqual("Albert", new String JavaDoc[] { "Ailbert", "Alberik", "Albert", "Alberto", "Albrecht" });
136     }
137
138     /**
139      * Match data computed from http://www.lanw.com/java/phonetic/default.htm
140      */

141     public void testIsMetaphoneEqualGary() {
142         this.assertIsMetaphoneEqual(
143             "Gary",
144             new String JavaDoc[] {
145                 "Cahra",
146                 "Cara",
147                 "Carey",
148                 "Cari",
149                 "Caria",
150                 "Carie",
151                 "Caro",
152                 "Carree",
153                 "Carri",
154                 "Carrie",
155                 "Carry",
156                 "Cary",
157                 "Cora",
158                 "Corey",
159                 "Cori",
160                 "Corie",
161                 "Correy",
162                 "Corri",
163                 "Corrie",
164                 "Corry",
165                 "Cory",
166                 "Gray",
167                 "Kara",
168                 "Kare",
169                 "Karee",
170                 "Kari",
171                 "Karia",
172                 "Karie",
173                 "Karrah",
174                 "Karrie",
175                 "Karry",
176                 "Kary",
177                 "Keri",
178                 "Kerri",
179                 "Kerrie",
180                 "Kerry",
181                 "Kira",
182                 "Kiri",
183                 "Kora",
184                 "Kore",
185                 "Kori",
186                 "Korie",
187                 "Korrie",
188                 "Korry" });
189     }
190
191     /**
192      * Match data computed from http://www.lanw.com/java/phonetic/default.htm
193      */

194     public void testIsMetaphoneEqualJohn() {
195         this.assertIsMetaphoneEqual(
196             "John",
197             new String JavaDoc[] {
198                 "Gena",
199                 "Gene",
200                 "Genia",
201                 "Genna",
202                 "Genni",
203                 "Gennie",
204                 "Genny",
205                 "Giana",
206                 "Gianna",
207                 "Gina",
208                 "Ginni",
209                 "Ginnie",
210                 "Ginny",
211                 "Jaine",
212                 "Jan",
213                 "Jana",
214                 "Jane",
215                 "Janey",
216                 "Jania",
217                 "Janie",
218                 "Janna",
219                 "Jany",
220                 "Jayne",
221                 "Jean",
222                 "Jeana",
223                 "Jeane",
224                 "Jeanie",
225                 "Jeanna",
226                 "Jeanne",
227                 "Jeannie",
228                 "Jen",
229                 "Jena",
230                 "Jeni",
231                 "Jenn",
232                 "Jenna",
233                 "Jennee",
234                 "Jenni",
235                 "Jennie",
236                 "Jenny",
237                 "Jinny",
238                 "Jo Ann",
239                 "Jo-Ann",
240                 "Jo-Anne",
241                 "Joan",
242                 "Joana",
243                 "Joane",
244                 "Joanie",
245                 "Joann",
246                 "Joanna",
247                 "Joanne",
248                 "Joeann",
249                 "Johna",
250                 "Johnna",
251                 "Joni",
252                 "Jonie",
253                 "Juana",
254                 "June",
255                 "Junia",
256                 "Junie" });
257     }
258
259     /**
260      * Initial KN case.
261      *
262      * Match data computed from http://www.lanw.com/java/phonetic/default.htm
263      */

264     public void testIsMetaphoneEqualKnight() {
265         this.assertIsMetaphoneEqual(
266             "Knight",
267             new String JavaDoc[] {
268                 "Hynda",
269                 "Nada",
270                 "Nadia",
271                 "Nady",
272                 "Nat",
273                 "Nata",
274                 "Natty",
275                 "Neda",
276                 "Nedda",
277                 "Nedi",
278                 "Netta",
279                 "Netti",
280                 "Nettie",
281                 "Netty",
282                 "Nita",
283                 "Nydia" });
284     }
285     /**
286      * Match data computed from http://www.lanw.com/java/phonetic/default.htm
287      */

288     public void testIsMetaphoneEqualMary() {
289         this.assertIsMetaphoneEqual(
290             "Mary",
291             new String JavaDoc[] {
292                 "Mair",
293                 "Maire",
294                 "Mara",
295                 "Mareah",
296                 "Mari",
297                 "Maria",
298                 "Marie",
299                 "Mary",
300                 "Maura",
301                 "Maure",
302                 "Meara",
303                 "Merrie",
304                 "Merry",
305                 "Mira",
306                 "Moira",
307                 "Mora",
308                 "Moria",
309                 "Moyra",
310                 "Muire",
311                 "Myra",
312                 "Myrah" });
313     }
314
315     /**
316      * Match data computed from http://www.lanw.com/java/phonetic/default.htm
317      */

318     public void testIsMetaphoneEqualParis() {
319         this.assertIsMetaphoneEqual("Paris", new String JavaDoc[] { "Pearcy", "Perris", "Piercy", "Pierz", "Pryse" });
320     }
321
322     /**
323      * Match data computed from http://www.lanw.com/java/phonetic/default.htm
324      */

325     public void testIsMetaphoneEqualPeter() {
326         this.assertIsMetaphoneEqual(
327             "Peter",
328             new String JavaDoc[] { "Peadar", "Peder", "Pedro", "Peter", "Petr", "Peyter", "Pieter", "Pietro", "Piotr" });
329     }
330
331     /**
332      * Match data computed from http://www.lanw.com/java/phonetic/default.htm
333      */

334     public void testIsMetaphoneEqualRay() {
335         this.assertIsMetaphoneEqual("Ray", new String JavaDoc[] { "Ray", "Rey", "Roi", "Roy", "Ruy" });
336     }
337
338     /**
339      * Match data computed from http://www.lanw.com/java/phonetic/default.htm
340      */

341     public void testIsMetaphoneEqualSusan() {
342         this.assertIsMetaphoneEqual(
343             "Susan",
344             new String JavaDoc[] {
345                 "Siusan",
346                 "Sosanna",
347                 "Susan",
348                 "Susana",
349                 "Susann",
350                 "Susanna",
351                 "Susannah",
352                 "Susanne",
353                 "Suzann",
354                 "Suzanna",
355                 "Suzanne",
356                 "Zuzana" });
357     }
358
359     /**
360      * Initial WR case.
361      *
362      * Match data computed from http://www.lanw.com/java/phonetic/default.htm
363      */

364     public void testIsMetaphoneEqualWright() {
365         this.assertIsMetaphoneEqual("Wright", new String JavaDoc[] { "Rota", "Rudd", "Ryde" });
366     }
367
368     /**
369      * Match data computed from http://www.lanw.com/java/phonetic/default.htm
370      */

371     public void testIsMetaphoneEqualXalan() {
372         this.assertIsMetaphoneEqual(
373             "Xalan",
374             new String JavaDoc[] { "Celene", "Celina", "Celine", "Selena", "Selene", "Selina", "Seline", "Suellen", "Xylina" });
375     }
376
377     public void testMetaphone() {
378         assertEquals("HL", this.getMetaphone().metaphone("howl"));
379         assertEquals("TSTN", this.getMetaphone().metaphone("testing"));
380         assertEquals("0", this.getMetaphone().metaphone("The"));
381         assertEquals("KK", this.getMetaphone().metaphone("quick"));
382         assertEquals("BRN", this.getMetaphone().metaphone("brown"));
383         assertEquals("FKS", this.getMetaphone().metaphone("fox"));
384         assertEquals("JMPT", this.getMetaphone().metaphone("jumped"));
385         assertEquals("OFR", this.getMetaphone().metaphone("over"));
386         assertEquals("0", this.getMetaphone().metaphone("the"));
387         assertEquals("LS", this.getMetaphone().metaphone("lazy"));
388         assertEquals("TKS", this.getMetaphone().metaphone("dogs"));
389     }
390     
391     public void testWordEndingInMB() {
392         assertEquals( "KM", this.getMetaphone().metaphone("COMB") );
393         assertEquals( "TM", this.getMetaphone().metaphone("TOMB") );
394         assertEquals( "WM", this.getMetaphone().metaphone("WOMB") );
395     }
396
397     public void testDiscardOfSCEOrSCIOrSCY() {
398         assertEquals( "SNS", this.getMetaphone().metaphone("SCIENCE") );
399         assertEquals( "SN", this.getMetaphone().metaphone("SCENE") );
400         assertEquals( "S", this.getMetaphone().metaphone("SCY") );
401     }
402
403     public void testWordsWithCIA() {
404         assertEquals( "XP", this.getMetaphone().metaphone("CIAPO") );
405     }
406
407     public void testTranslateOfSCHAndCH() {
408         assertEquals( "SKTL", this.getMetaphone().metaphone("SCHEDULE") );
409         assertEquals( "SKMT", this.getMetaphone().metaphone("SCHEMATIC") );
410
411         assertEquals( "KRKT", this.getMetaphone().metaphone("CHARACTER") );
412         assertEquals( "TX", this.getMetaphone().metaphone("TEACH") );
413     }
414
415     public void testTranslateToJOfDGEOrDGIOrDGY() {
416         assertEquals( "TJ", this.getMetaphone().metaphone("DODGY") );
417         assertEquals( "TJ", this.getMetaphone().metaphone("DODGE") );
418         assertEquals( "AJMT", this.getMetaphone().metaphone("ADGIEMTI") );
419     }
420
421     public void testDiscardOfSilentHAfterG() {
422         assertEquals( "KNT", this.getMetaphone().metaphone("GHENT") );
423         assertEquals( "B", this.getMetaphone().metaphone("BAUGH") );
424     }
425
426     public void testDiscardOfSilentGN() {
427         assertEquals( "N", this.getMetaphone().metaphone("GNU") );
428         assertEquals( "SNT", this.getMetaphone().metaphone("SIGNED") );
429     }
430
431     public void testPHTOF() {
432         assertEquals( "FX", this.getMetaphone().metaphone("PHISH") );
433     }
434
435     public void testSHAndSIOAndSIAToX() {
436         assertEquals( "XT", this.getMetaphone().metaphone("SHOT") );
437         assertEquals( "OTXN", this.getMetaphone().metaphone("ODSIAN") );
438         assertEquals( "PLXN", this.getMetaphone().metaphone("PULSION") );
439     }
440
441     public void testTIOAndTIAToX() {
442         assertEquals( "OX", this.getMetaphone().metaphone("OTIA") );
443         assertEquals( "PRXN", this.getMetaphone().metaphone("PORTION") );
444     }
445
446     public void testTCH() {
447         assertEquals( "RX", this.getMetaphone().metaphone("RETCH") );
448         assertEquals( "WX", this.getMetaphone().metaphone("WATCH") );
449     }
450
451     public void testExceedLength() {
452         // should be AKSKS, but istruncated by Max Code Length
453
assertEquals( "AKSK", this.getMetaphone().metaphone("AXEAXE") );
454     }
455
456     public void testSetMaxLengthWithTruncation() {
457         // should be AKSKS, but istruncated by Max Code Length
458
this.getMetaphone().setMaxCodeLen( 6 );
459         assertEquals( "AKSKSK", this.getMetaphone().metaphone("AXEAXEAXE") );
460     }
461
462     public void validateFixture(String JavaDoc[][] pairs) {
463         if (pairs.length == 0) {
464             fail("Test fixture is empty");
465         }
466         for (int i = 0; i < pairs.length; i++) {
467             if (pairs[i].length != 2) {
468                 fail("Error in test fixture in the data array at index " + i);
469             }
470         }
471     }
472
473 }
474
Popular Tags