Skip to content

Commit 3d9bdc3

Browse files
Use LevenshteinDistance for item search autocomplete
1 parent 3945d72 commit 3d9bdc3

4 files changed

Lines changed: 35 additions & 3 deletions

File tree

src/main/java/technobot/commands/economy/BuyCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
55
import net.dv8tion.jda.api.interactions.commands.OptionType;
66
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
7+
import org.apache.commons.lang3.StringUtils;
78
import technobot.TechnoBot;
89
import technobot.commands.Category;
910
import technobot.commands.Command;

src/main/java/technobot/commands/economy/ItemCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void execute(SlashCommandInteractionEvent event) {
121121
}
122122
}
123123
case "remove" -> {
124-
String name = event.getOption("name").getAsString();
124+
String name = configHandler.findClosestItem(event.getOption("name").getAsString());
125125
if (configHandler.containsItem(name)) {
126126
configHandler.removeItem(name);
127127
text = EmbedUtils.BLUE_X + " Item has been removed from the store. Users will still be able to `/use` this item if they already own it.";
@@ -132,7 +132,7 @@ public void execute(SlashCommandInteractionEvent event) {
132132
}
133133
}
134134
case "erase" -> {
135-
String name = event.getOption("name").getAsString();
135+
String name = configHandler.findClosestItem(event.getOption("name").getAsString());
136136
if (configHandler.containsItem(name)) {
137137
configHandler.eraseItem(name);
138138
text = EmbedUtils.BLUE_X + " Item has been erased and can never be purchased or used.";

src/main/java/technobot/commands/economy/UseCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public void execute(SlashCommandInteractionEvent event) {
3838

3939
// Check if item exists & is in inventory
4040
String itemName = event.getOption("item").getAsString();
41+
itemName = guildData.configHandler.findClosestItem(itemName);
4142
ItemAndCount response = getItemForUse(itemName, userID, guildData);
4243
Item item = response.item();
4344
if (item == null) {

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.mongodb.client.model.Filters;
44
import com.mongodb.client.model.Updates;
55
import net.dv8tion.jda.api.entities.Guild;
6+
import org.apache.commons.lang3.StringUtils;
67
import org.bson.conversions.Bson;
78
import technobot.TechnoBot;
89
import technobot.data.cache.Config;
@@ -204,7 +205,12 @@ private void cancelExpireTimer(String itemID) {
204205
* @return an Item object.
205206
*/
206207
public Item getItem(String name) {
207-
String uuid = config.getShop().get(name.toLowerCase());
208+
name = name.toLowerCase();
209+
String uuid = config.getShop().get(name);
210+
if (uuid == null) {
211+
String key = findClosestItem(name);
212+
uuid = config.getShop().get(key);
213+
}
208214
return config.getItems().get(uuid);
209215
}
210216

@@ -218,6 +224,30 @@ public Item getItemByID(String uuid) {
218224
return config.getItems().get(uuid);
219225
}
220226

227+
/**
228+
* Finds the item with the name that most closely matches the input.
229+
*
230+
* @param input the name of the item searching for.
231+
* @return the name of the item that most closely matches the input. Null if no match at all.
232+
*/
233+
public String findClosestItem(String input) {
234+
int value = Integer.MAX_VALUE;
235+
int longest = 0;
236+
String itemName = null;
237+
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();
245+
}
246+
}
247+
if (itemName != null && value >= longest) return null;
248+
return itemName;
249+
}
250+
221251
/**
222252
* Reset all leveling config settings to their default.
223253
*/

0 commit comments

Comments
 (0)