spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Unexpected results from Metaphone algorithm


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":

System.out.println(meta.getMaxCodeLen());
System.out.println(meta.encode("cricket"));
System.out.println(meta.encode("criket"));
System.out.println(meta.encode("criketgame"));

Output (note "criketgame" is truncated from "KRKTKM" to "KRKT", which matches "cricket"):

4
KRKT
KRKT
KRKT


Solution: Set the maximum code length to something appropriate for your application and the expected input. For example:

meta.setMaxCodeLen(8);
System.out.println(meta.encode("cricket"));
System.out.println(meta.encode("criket"));
System.out.println(meta.encode("criketgame"));

Now outputs:

KRKT
KRKT
KRKTKM

And now your original test gives the expected results:

Metaphone meta = new Metaphone();
meta.setMaxCodeLen(8);
System.out.println(meta.isMetaphoneEqual("cricket","criket"));
System.out.println(meta.isMetaphoneEqual("cricket","criketgame"));

Printing:

true
false

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:

The instance field maxCodeLen is mutable but is not volatile, and accesses are not synchronized. If an instance of the class is shared between threads, the caller needs to ensure that suitable synchronization is used to ensure safe publication of the value between threads, and must not invoke setMaxCodeLen(int) after initial setup.


Categories : Java

Related to : Unexpected results from Metaphone algorithm
Nodejs util.inherits produce unexpected results
var Car = function(){ var self=this; EventEmitter.call(this); var make=1; Car.prototype.getMake = function() { return make; }; Car.prototype.setMake = function(val) { make=val; return make; }; }; does not make sense. Car.prototype.getMake affects every single instance of Car, so what you are basically saying is, every time a new Car() instance

Categories : Node Js
Printing the values of a number is giving unexpected results
The problem you're having is that you're not actually changing the value of num. Num is always 8, and so you are just printing out various operations on the number 8. Why don't you try something like this: int num = 8; System.out.println("Value is now " + (num)); num += 2; System.out.println("Value is now " + (num)); num -= 4; System.out.println("Value is now " + (num)); num *= 5; System.out.pri

Categories : Java
Array.sort() is producing unexpected results when elements are equal?
The property of a sorting algorithm that leaves elements that compare as equal in their original list order is called stability. The JavaScript spec specifically allows for implementations to use unstable sort algorithms. From the spec: The sort is not necessarily stable (that is, elements that compare equal do not necessarily remain in their original order). Now, how you fix your problem d

Categories : Javascript
Search a listbox, results will not highlight when searching results found
Your logic would only work if the last item was the match. For Each item As String In lstMain.SelectedItems If item.ToUpper.Substring(0, 24).Contains(strSearchField.ToUpper) Then lstMain.SelectedItem = item Exit For 'so the found item stays selected End If Next or For i As Integer = 0 To lstMain.Items.Count - 1 If lstMain.Items(i).ToUpper.Substring(0, 24).Contains(strSearchField

Categories : Vb.Net
How to redirect (301) search results page which do not return results
Replace old-page.php code with redirection code to new-page.php. so in your case if you want to a new search.php code you should have two php files. First, the old-search.php which contains the following code, and then the new-search.php code which is the one that should be specified in the link. ---old-search.php--- <?php // PHP permanent URL redirection header("Location: http://www.yourdoma

Categories : PHP
Recently Add
Redirecting the output directory of 'mvn package' or 'mvn compile' command
No suitable constructor found for ProductoExtranjero
java Composite design pattern(Directory &File)
Java JTree's ui refresh after removing node from parent
First REST Spring application
How to cancel Indexing of a Solr document using Update Request Processor
PowerMock - Mock a Singleton with a Private Constructor
Calling a Postgres stored function SQL error
Where to store Morphlines Java custom command class?
Generic repository using map
How can I scroll a ScrolledComposited in Eclipse SWT Design view?
2 Frames/layout in 1 Activity
Writing a switch differently
Next button opens another activity when its reaches the array limit
Is EclipseLink MOXy capable of applying JSR-303 Bean Validation when unmarshalling XML to object?
Why my jdk can't work,and before the java_home, there is a space that is not from me
How to add List of objects in a Map
How to make notepad++ function like regular notepad in cmd?
Cell renderer and the lost focus
how can I implement iterable for LinkedList>
Disable Androids image-crunch in eclipse (run as) builds
java 8 lambda myMap.stream().count() != myMap.size() after merging myMap
Issue with Calendar calculation that spans 2 calendar years
JSF 2.0 Spring bean injection
Java Regex ReplaceAll with grouping
Getting any word and last word using sed
Clicking on link on JEditorPane throws IOException
printing out difference of two arrays
Spring Bean Alias in JavaConfig
Using Factory Method to Create Generics
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.