From 70a1cb5777c476c62a7c44c1d423129fa74b42eb Mon Sep 17 00:00:00 2001 From: Yannic Link Date: Wed, 30 Mar 2022 23:18:56 +0200 Subject: [PATCH] =?UTF-8?q?Jaccard-Distanz=20hat=20leider=20nicht=20ausger?= =?UTF-8?q?eicht=20f=C3=BCr=20Strings=20mit=20gleichen=20Substrings=20und?= =?UTF-8?q?=20unterschiedlicher=20L=C3=A4nge=20und=20so.=20Naja=20hier=20i?= =?UTF-8?q?s=20was=20besseres?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../command/commands/PlayCommand.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) 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() {