Skip to content
This repository was archived by the owner on Jul 17, 2020. It is now read-only.

Commit a0d447c

Browse files
author
Zirak
committed
Merge branch 'master' of git://github.com/Elusive138/SO-ChatBot into Elusive138-master
2 parents 678a232 + 2e778e3 commit a0d447c

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

source/bot.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ bot.Command = function ( cmd ) {
366366
cmd.del = function () {
367367
bot.info.forgotten += 1;
368368
delete bot.commands[ cmd.name ];
369+
bot.commandDictionary.trie.del(cmd.name);
369370
};
370371

371372
return cmd;

source/suggestionDict.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ TrieNode.prototype.add = function( word ) {
2222
node.word = word;
2323
};
2424

25+
TrieNode.prototype.del = function(word, i) {
26+
i = i || 0;
27+
var node = this;
28+
var char = word[i++];
29+
30+
// recursively delete all trie nodes that are left empty after removing the command from the leaf
31+
if (node.children[char]) {
32+
node.children[char].del(word, i);
33+
if (Object.keys(node.children[char].children).length === 0 && node.children[char].word === null) {
34+
delete node.children[char];
35+
}
36+
}
37+
38+
if (node.word === word) {
39+
node.word = null;
40+
}
41+
}
42+
2543
//Having a small maxCost will increase performance greatly, experiment with
2644
//values of 1-3
2745
function SuggestionDictionary ( maxCost ) {

0 commit comments

Comments
 (0)