TRIE Spellchecker
Implement a TRIE that has both the functions AddToTRIE and CheckSpelling. Once you have created the TRIE add the words (hard code) I, in, into, inlet, inn, inner, innate, ink. These are good words to use to test your TRIE Create an interface that has one text box for entry and 2 buttons – Add To Dictionary and Spell Check. They should ad the word to the TRIE (feedback – word added) and check the word against the TRIE and return in the word was in or not in the dictionary.
by Neil Daley
HTML
<h1>
TRIE Spellchecker
</h1>
Implement a TRIE that has both the functions AddToTRIE and CheckSpelling.<br/>
Enter a word to check if it exists and add the word if it does not.<br/>
The sorting order is:<br/>
• Numbers<br/>
• Capitol letters<br/>
• Lower case letters
<h2>
<div id="checkTrue"></div>
</h2>
<h2>
<div id="checkFalse"></div>
</h2>
<div>
<input type="tb1" id="input" onkeypress="handle(event)"><br/>
<div>
<input type="button" class="button" value="Spell Check" id="bt2" onClick="checkInput()" style="color:white; background-color:blue" >
<input type="button" value="Add to dictionary" id="bt1" onClick="add()" style="color:white; background-color:blue" >
</div>
</div>
<div></div>
<div id="array"></div>
<div id="trie"></div>
JavaScript
// samples stored in an array and later fed into trie
var dictionary = ['I', 'in', 'into', 'inlet', 'inn', 'inner', 'innate', 'ink'];
//The enter key handler
function handle(e) {
var key = e.keyCode || e.which;
if (key == 13) {
input = document.getElementById("input").value;
check(input);
}
}
// function expression for the trie
var Trie = (function () {
var ALPHABET_SIZE = 26;
// Each Trie node has a key and a value
var TrieNode = function (key, value) {
this.key = key;
this.value = value;
// children arrays
this.children = [];
for (var i = 0; i < ALPHABET_SIZE; i++) {
this.children[i] = null;
}
// add current letter to array
this.putChild = function (node) {
return (this.children[node.key.charCodeAt(0)] = node);
};
// get current child
this.getChild = function(key) {
return this.children[key.charCodeAt(0)];
};
};
// evaluate child
var add = function (node, key, value) {
// target is current letter
var target = node.getChild(key.charAt(0));
// if null create new node
if (target == null) {
target = node.putChild(new TrieNode(key.charAt(0), null));
}
// if exist return value
if (key.length == 1) {
return (target.value = value);
}
// slice next letter
return add(target, key.slice(1), value);
};
// Get a stack representing the path from the key (top) to the root
var pathFromLeaf = function (path, key) {
var node = path[path.length - 1];
path.push(node.getChild(key.charAt(0)));
return (key.length == 1) ? path : pathFromLeaf(path, key.slice(1));
};
// I used this part via the public API (containsKey) further below to implement the check() function
var get = function (node,...