diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java index 24c5699..0af32fd 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java @@ -113,17 +113,25 @@ public class PlayCommand extends YoshiCommand { } private String getBestMatch(String word, List choices) { - double bestScore = 1.0; - String bestMatching = null; + double bestJaccardScore = 1.0; + double bestLengthSimilarity = 0.0; + int originalLength = word.length(); + String bestMatch = null; + for (String file : choices) { - double score = (new JaccardDistance()).apply(word, file); - if (score < bestScore) { - bestScore = score; - bestMatching = file; + if(file.equals(word)) return file; + int fileLength = file.length(); + double jaccardScore = (new JaccardDistance()).apply(word, file); + double lengthSimilarity = (float)(Math.min(originalLength, fileLength)) + / (float)(Math.max(originalLength, fileLength)); + if (jaccardScore < bestJaccardScore + || ((jaccardScore == bestJaccardScore) && (lengthSimilarity > bestLengthSimilarity))) { + bestJaccardScore = jaccardScore; + bestLengthSimilarity = lengthSimilarity; } } - return bestMatching; + return bestMatch; } private List getAllFiles() {