Assignment 13
by zazagalaxy
HTML
<h1>
<center>TRIE Spellchecker</center>
</h1>
<h3>
<center>Assignment 13</center>
</h3>
<input type="button" id="addword" value="Add Word" onclick="addword();" />
<input type="button" id="check" value="Spellcheck" onclick="checkbutton();" />
<input type="textbox" id="input" onkeyup="start();" />
<br>
<div id="output">
</div>
<div id="output1">
</div>
</div>
CSS
h1, h3, div, input, body{
color: SpringGreen;
background-color: black;
}
JavaScript
var Node = function(key) {
this.key = key;
this.parent = null;
this.children = {};
this.end = false;
}
var Trie = function() {
this.root = new Node();
}
var trie1 = new Trie();
Node.prototype.get = function() {
var out = [];
var node = this;
while (node !== null) {
out.unshift(node.key);
node = node.parent;
}
return out.join('');
}
// Method to add word to trie
Trie.prototype.add = function(word) {
var node = this.root;
word = word.toLowerCase();
for (var i = 0; i < word.length; i++) {
if (!node.children[word[i]]) {
node.children[word[i]] = new Node([word[i]]);
node.children[word[i]].parent = node;
}
node = node.children[word[i]];
if (i == word.length - 1) {
node.end = true;
}
}
}
//SpellcheckMethod
Trie.prototype.spellcheck = function(word) {
var node = this.root;
var out = [];
for (var i = 0; i < word.length; i++) {
if (node.children[word[i]]) {
node = node.children[word[i]];
} else {
return "Unable to discover the word";
}
}
checkall(node, out);
return out;
}
var checkall = function(node, out) {
if (node.end) {
out.unshift(node.get());
}
for (var child in node.children) {
checkall(node.children[child], out);
}
}
var spelling = function(word) {
let out = trie1.spellcheck(word);
let isword = false;
for (var t = 0; t < out.length; t++) {
if (out[t] == word) {
isword = true;
}
}
return isword;
}
function start() {
var i = document.getElementById('input').value.toLowerCase();
var out2 = "";
if (!i || i == " ") {
out2 = "";
} else {
var out1 = trie1.spellcheck(i);
for (var t = out1.length - 1; t >= 0; t--) {
if (out1[t] == "i") {
out1[t] = out1[t].toUpperCase();
}
out2 += out1[t] + "<br>";
}
if (out1 == "Could not discover the word") {
out2 = out1;
}
}
document.getElementById('output').innerHTML = out2;
}
function addword() {
var out = "";
var i =...