Skip to content

Commit fb28595

Browse files
committed
Replace fuse.js with levenshtein distance algorithm
1 parent 3aeb13e commit fb28595

File tree

12 files changed

+9546
-9163
lines changed

12 files changed

+9546
-9163
lines changed

package-lock.json

Lines changed: 9452 additions & 9107 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
"discord-api-types": "^0.37.37",
2929
"discord.js": "^14.8.0",
3030
"ffmpeg-static": "^5.1.0",
31-
"fuse.js": "^6.6.2",
3231
"gm": "^1.25.0",
3332
"jimp": "^0.22.7",
3433
"libsodium-wrappers": "^0.7.11",
@@ -43,4 +42,4 @@
4342
"jpeg-js": "0.4.4",
4443
"protobufjs": "7.2.4"
4544
}
46-
}
45+
}

src/commands/craft.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module.exports = {
6060
let itemId = null;
6161
if (craftItemName !== null) {
6262
const item = client.items.getClosestItemIdByName(craftItemName)
63-
if (item === undefined) {
63+
if (item === null) {
6464
const str = client.intlGet(guildId, 'noItemWithNameFound', {
6565
name: craftItemName
6666
});

src/commands/despawn.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module.exports = {
5454
let itemId = null;
5555
if (despawnItemName !== null) {
5656
const item = client.items.getClosestItemIdByName(despawnItemName)
57-
if (item === undefined) {
57+
if (item === null) {
5858
const str = client.intlGet(guildId, 'noItemWithNameFound', {
5959
name: despawnItemName
6060
});

src/commands/market.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ module.exports = {
118118
let itemId = null;
119119
if (searchItemName !== null) {
120120
const item = client.items.getClosestItemIdByName(searchItemName)
121-
if (item === undefined) {
121+
if (item === null) {
122122
const str = client.intlGet(interaction.guildId, 'noItemWithNameFound', {
123123
name: searchItemName
124124
});
@@ -236,7 +236,7 @@ module.exports = {
236236
let itemId = null;
237237
if (subscribeItemName !== null) {
238238
const item = client.items.getClosestItemIdByName(subscribeItemName)
239-
if (item === undefined) {
239+
if (item === null) {
240240
const str = client.intlGet(interaction.guildId, 'noItemWithNameFound', {
241241
name: subscribeItemName
242242
});
@@ -304,7 +304,7 @@ module.exports = {
304304
let itemId = null;
305305
if (subscribeItemName !== null) {
306306
const item = client.items.getClosestItemIdByName(subscribeItemName)
307-
if (item === undefined) {
307+
if (item === null) {
308308
const str = client.intlGet(interaction.guildId, 'noItemWithNameFound', {
309309
name: subscribeItemName
310310
});

src/commands/recycle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module.exports = {
6060
let itemId = null;
6161
if (recycleItemName !== null) {
6262
const item = client.items.getClosestItemIdByName(recycleItemName)
63-
if (item === undefined) {
63+
if (item === null) {
6464
const str = client.intlGet(guildId, 'noItemWithNameFound', {
6565
name: recycleItemName
6666
});

src/commands/research.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module.exports = {
5555
let itemId = null;
5656
if (researchItemName !== null) {
5757
const item = client.items.getClosestItemIdByName(researchItemName)
58-
if (item === undefined) {
58+
if (item === null) {
5959
const str = client.intlGet(guildId, 'noItemWithNameFound', {
6060
name: researchItemName
6161
});

src/commands/stack.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module.exports = {
5454
let itemId = null;
5555
if (stackItemName !== null) {
5656
const item = client.items.getClosestItemIdByName(stackItemName)
57-
if (item === undefined) {
57+
if (item === null) {
5858
const str = client.intlGet(guildId, 'noItemWithNameFound', {
5959
name: stackItemName
6060
});

src/structures/Items.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,19 @@
2121
const Fs = require('fs');
2222
const Path = require('path');
2323

24-
const Fuse = require('fuse.js')
24+
const Utils = require('../util/utils.js');
2525

2626
class Items {
2727
constructor() {
2828
this._items = JSON.parse(Fs.readFileSync(
2929
Path.join(__dirname, '..', 'staticFiles', 'items.json'), 'utf8'));
3030

31-
const flattenedItems = Object.keys(this.items).map(id => ({ id, ...this.items[id] }));
32-
this._fuse = new Fuse(flattenedItems, {
33-
keys: [{
34-
name: 'name',
35-
weight: 0.7,
36-
}, {
37-
name: 'shortname',
38-
weight: 0.3,
39-
}]
40-
});
31+
this._itemNames = Object.values(this.items).map(item => item.name);
4132
}
4233

43-
/* Getters and Setters */
34+
/* Getters */
4435
get items() { return this._items; }
45-
set items(items) { this._items = items; }
46-
get fuse() { return this._fuse; }
47-
set fuse(fuse) { this._fuse = fuse; }
36+
get itemNames() { return this._itemNames; }
4837

4938
addItem(id, content) { this.items[id] = content; }
5039
removeItem(id) { delete this.items[id]; }
@@ -70,11 +59,12 @@ class Items {
7059
}
7160

7261
getClosestItemIdByName(name) {
73-
const result = this.fuse.search(name)[0];
74-
if (result) {
75-
return this.fuse.search(name)[0].item.id;
62+
const closestString = Utils.findClosestString(name, this.itemNames);
63+
if (closestString !== null) {
64+
const id = Object.entries(this.items).find(([key, value]) => value.name === closestString);
65+
return id ? id[0] : null;
7666
}
77-
return undefined;
67+
return null;
7868
}
7969
}
8070

src/structures/RustLabs.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
1919
*/
2020

21-
const Fuse = require('fuse.js')
22-
2321
const Items = require('./Items');
2422
const RustlabsBuildingBlocks = require('../staticFiles/rustlabsBuildingBlocks.json');
2523
const RustlabsOther = require('../staticFiles/rustlabsOther.json');
@@ -32,6 +30,7 @@ const SmeltingData = require('../staticFiles/rustlabsSmeltingData.json');
3230
const DespawnData = require('../staticFiles/rustlabsDespawnData.json');
3331
const StackData = require('../staticFiles/rustlabsStackData.json');
3432
const DecayData = require('../staticFiles/rustlabsDecayData.json');
33+
const Utils = require('../util/utils.js');
3534

3635
const IGNORED_RECYCLE_ITEMS = [
3736
'-946369541' /* Low Grade Fuel */
@@ -85,11 +84,8 @@ class RustLabs {
8584
'sulfurLowFirst'
8685
];
8786

88-
const flattenedBuildingBlocks = Object.keys(this.rustlabsBuildingBlocks).map(e => ({ ['name']: e }));
89-
this._fuseBuildingBlocks = new Fuse(flattenedBuildingBlocks, { keys: [{ name: 'name', weight: 0.1 }] });
90-
91-
const flattenedOther = Object.keys(this.rustlabsOther).map(e => ({ ['name']: e }));
92-
this._fuseOther = new Fuse(flattenedOther, { keys: [{ name: 'name', weight: 0.1 }] });
87+
this._buildingBlocks = Object.keys(this.rustlabsBuildingBlocks);
88+
this._other = Object.keys(this.rustlabsOther);
9389
}
9490

9591

@@ -111,8 +107,8 @@ class RustLabs {
111107
get durabilityGroups() { return this._durabilityGroups }
112108
get durabilityWhich() { return this._durabilityWhich; }
113109
get orderedBy() { return this._orderedBy; }
114-
get fuseBuildingBlocks() { return this._fuseBuildingBlocks; }
115-
get fuseOther() { return this._fuseOther; }
110+
get buildingBlocks() { return this._buildingBlocks; }
111+
get other() { return this._other; }
116112

117113

118114
/***********************************************************************************
@@ -146,27 +142,27 @@ class RustLabs {
146142
/**
147143
* Get the closest building block name by name.
148144
* @param {string} name The name of the building block.
149-
* @return {string|undefined} undefined if the building block couldnt be found, otherwise the closest name.
145+
* @return {string|null} null if the building block couldnt be found, otherwise the closest name.
150146
*/
151147
getClosestBuildingBlockNameByName(name) {
152-
const result = this.fuseBuildingBlocks.search(name);
153-
if (result.length !== 0) {
154-
return result[0].item.name;
148+
const closestString = Utils.findClosestString(name, this.buildingBlocks);
149+
if (closestString !== null) {
150+
return closestString;
155151
}
156-
return undefined;
152+
return null;
157153
}
158154

159155
/**
160156
* Get the closest other name by name.
161157
* @param {string} name The name of the other.
162-
* @return {string|undefined} undefined if the other couldnt be found, otherwise the closest name.
158+
* @return {string|null} null if the other couldnt be found, otherwise the closest name.
163159
*/
164160
getClosestOtherNameByName(name) {
165-
const result = this.fuseOther.search(name);
166-
if (result.length !== 0) {
167-
return result[0].item.name;
161+
const closestString = Utils.findClosestString(name, this.other);
162+
if (closestString !== null) {
163+
return closestString;
168164
}
169-
return undefined;
165+
return null;
170166
}
171167

172168
/**

0 commit comments

Comments
 (0)