I am working on a game where the user has to fill in a name of a celebrity. If the name is not 100% correct but nearly correct, the compare should succeed. Is there a ready to use function in java or something someone ever has written so I can use it ?
Asked
Active
Viewed 788 times
0
-
1"Fuzzy" is the word you are looking for. – Fildor Jun 22 '16 at 14:18
-
You could compare it letter by letter and check how many a user got right compared to what it actually is. – Okx Jun 22 '16 at 14:18
-
This might be a too broad question, Please define your threshold for `If the name is not 100% correct but nearly correct`. – Enzokie Jun 22 '16 at 14:19
-
2Maybe you find something in the answers to this question: http://stackoverflow.com/q/327513/982149 – Fildor Jun 22 '16 at 14:20
-
1You may be looking for something like the [Levenshtein distance](https://en.m.wikipedia.org/wiki/Levenshtein_distance) to define similarity of the strings. – Andy Turner Jun 22 '16 at 14:20
-
@Fildor You are right, that is what I am looking for :) – Andre Jun 22 '16 at 14:20
1 Answers
0
What you are looking for is the Levenshtein algortithm
You'll find here some Java implementations : https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Java
Or, if you don't want/need to understand how it works, you can get the score directly from Apache StringUtils : getLevenshteinDistance
And if you want to get the percentage of similarities, you can do :
int lev = StringUtils.getLevenshteinDistance(s1, s2);
double ratio = ((double) lev) / (Math.max(s1.length, s2.length));

VLEFF
- 533
- 3
- 14