Skip to content

Commit 9c2d0a2

Browse files
Swap out Levenshtein Distance for better, more efficient custom algorithm
1 parent 3d9bdc3 commit 9c2d0a2

1 file changed

Lines changed: 9 additions & 15 deletions

File tree

src/main/java/technobot/handlers/ConfigHandler.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
import technobot.data.cache.Config;
1010
import technobot.data.cache.Item;
1111

12-
import java.util.HashMap;
13-
import java.util.Map;
14-
import java.util.TimerTask;
12+
import java.util.*;
1513
import java.util.concurrent.Executors;
1614
import java.util.concurrent.ScheduledExecutorService;
1715
import java.util.concurrent.ScheduledFuture;
@@ -231,21 +229,17 @@ public Item getItemByID(String uuid) {
231229
* @return the name of the item that most closely matches the input. Null if no match at all.
232230
*/
233231
public String findClosestItem(String input) {
234-
int value = Integer.MAX_VALUE;
235-
int longest = 0;
236-
String itemName = null;
232+
String closestMatch = null;
233+
double bestValue = 0;
237234
for (String name : config.getShop().keySet()) {
238-
int temp = StringUtils.getLevenshteinDistance(input, name);
239-
if (temp < value) {
240-
value = temp;
241-
itemName = name;
242-
}
243-
if (name.length() > longest) {
244-
longest = name.length();
235+
if (name.contains(input)) {
236+
double result = StringUtils.getJaroWinklerDistance(name, input);
237+
if (result >= bestValue) {
238+
closestMatch = name;
239+
}
245240
}
246241
}
247-
if (itemName != null && value >= longest) return null;
248-
return itemName;
242+
return closestMatch;
249243
}
250244

251245
/**

0 commit comments

Comments
 (0)