I am using phonetic matching for different words in Java. i used Soundex but its too crude. i switched to Metaphone and realized it was better. However, when i rigorously tested it. i found weird behaviour. i was to ask whether thats the way metaphone works or am i using it in wrong way. In following example its works fine:-
Metaphone meta = new Metaphone();
if (meta.isMetaphoneEqual("cricket","criket")) System.out.prinlnt("Match 1");
if (meta.isMetaphoneEqual("cricket","criketgame")) System.out.prinlnt("Match 2");
This would Print
Match 1
Mathc 2
Now "cricket" does sound like "criket" but how come "cricket" and "criketgame" are the same. If some one would explain this. it would be of great help.
Your usage is slightly incorrect. A quick investigation of the encoded strings and default maximum code length shows that it is 4, which truncates the end of the longer "criketgame":
Output (note "criketgame" is truncated from "KRKTKM" to "KRKT", which matches "cricket"):
Solution: Set the maximum code length to something appropriate for your application and the expected input. For example:
Now outputs:
And now your original test gives the expected results:
Printing:
As an aside, you may also want to experiment with
DoubleMetaphone
, which is an improved version of the algorithm.By the way, note the caveat from the documentation regarding thread-safety: