Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Does anyone have method to find best match of string in array?

Basically I'm just trying to find a way to find the closest match (not necessarily exact) of a String

For example, find "delicous" in {"pie", "delicious", "test"}

This is pretty obvious, but the values in the array might not always be that distinct.

Could someone please help me with a way to achieve this.

like image 545
Alex Coleman Avatar asked May 31 '12 02:05

Alex Coleman


Video Answer


2 Answers

Depends on how you define "closest" but one common way is by using a Levenshtein Distance score. Apache Commons has such a method in StringUtils.

From there your search method basically becomes: find the string in the collection which has the smallest Levenshtein distance for a given input.

like image 162
Andrew White Avatar answered Nov 15 '22 22:11

Andrew White


There's nothing built into Java for that. You might try a third-party library like SecondString or FREJ.

like image 40
Ted Hopp Avatar answered Nov 15 '22 21:11

Ted Hopp