Assignment 13

TRIE Spellchecker

by Kristy Bond

HTML

<html>
<body>

<h2>TRIE Spellchecker</h2>
<h3> image courtesy of https://codereview.stackexchange.com/<h3>

</h3>
 
</h3>
<img src="https://i.stack.imgur.com/rftvb.png" alt="Diagram of TRIE" width="400" height="400"> <br>


<input type="textbox" id="X" value="" />
<input type="button" value="Add To Dictionary" onclick="AddTo();" />
<input type="button" value="Spell Check" onclick="Check();" />
<p id="output"></p>
<p id="output2"> </p>

</body>
</html>


<!--

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.

JavaScript

function Trie(key) {
    this.key = key;
    this.value;
}

Trie.prototype.AddToTrie = function (name) {

    var node = this,
        nameLength = name.length,
        i = 0,
        currentLetter;

    for (i = 0; i < nameLength; i++) {
        currentLetter = name[i];
        node = node[currentLetter] || (node[currentLetter] = new Trie(currentLetter));
    }

    node.value = name;
    node.name = name;

};

Trie.prototype.SpellCheck = function (name) {

    var node = this,
        nameLength = name.length,
        i, node;

    for (i = 0; i < nameLength; i++) {
        if (!(node = node[name[i]])) break;
    }

    return (i === nameLength) ? 'Correct as added (true)' : 'Value not found or spelled wrong(false)';
};

//Add the hard coded in words. Everything else will need to be added manually or else it will return error. 
var dict = new Trie();
dict.AddToTrie("I");
dict.AddToTrie("in");
dict.AddToTrie("into");
dict.AddToTrie("inlet");
dict.AddToTrie("inn");
dict.AddToTrie("inner");
dict.AddToTrie("innate");
dict.AddToTrie("ink");



//This can be used to load in the whole dictionary quickly. Something to look into later on when JQUERY and sideloaded scripts are allowed. console.log(JSON.stringify(dict));


function AddTo() {
document.getElementById("output2").innerHTML = "";
var Value = document.getElementById("X").value;
if (Value == 0){var A ="Please Enter A Value";
return A;}

dict.AddToTrie(Value);
document.getElementById("output").innerHTML= "Word Added = " + Value; 

}


function Check() {
document.getElementById("output").innerHTML = "";
document.getElementById("output2").innerHTML = "Word Checked: ";
var Value = document.getElementById("X").value;

if (Value == 0) { var A = "Please Enter A Value";
return A;}

document.getElementById("output").innerHTML = dict.SpellCheck(Value);
document.getElementById("output2").innerHTML += Value;


}