Assignment 13
TRIE Spellchecker
by Jenni Meiklejohn
HTML
<h1><center> Assignment 13</center></h1>
<h2><center> TRIE Spellchecker </center></h2> TRIE List:
<p id="trieList"></p>
<br/> Enter a word:
<input type="textbox" id="word" />
<input type="button" value="Add to Dictionary" onClick="AddToTRIE()" />
<input type="button" value="Spell Check" onClick="CheckSpelling()" />
<br/>
<p id="output"></p>
JavaScript
function TRIE() {
this.words = 0;
this.baseNodes = [];
}
TRIE.prototype.dict = function() {
this.addTRIE("I");
this.addTRIE("in");
this.addTRIE("into");
this.addTRIE("inlet");
this.addTRIE("inn");
this.addTRIE("inner");
this.addTRIE("innate");
this.addTRIE("ink");
}
TRIE.prototype.addTRIE = function(word, index) {
if (index === undefined) {
index = 0;
}
if (index === word.length) {
this.words++;
return;
}
n = word[index];
if (this.baseNodes[n] === undefined) {
this.baseNodes[n] = new TRIE();
}
base = this.baseNodes[n];
base.addTRIE(word, index + 1);
}
TRIE.prototype.count = function(word, index) {
var count = 0;
if (word.length == 0) {
count = 0;
}
if (index === undefined) {
index = 0;
}
if (index === word.length) {
count = this.words;
}
n = word[index];
baseNode = this.baseNodes[n];
if (baseNode !== undefined) {
count = baseNode.count(word, index + 1);
}
return count;
}
TRIE.prototype.print = function(word) {
var baseNode;
var result = [];
var a = "";
if (word === undefined) {
word = "";
}
if (this === undefined) {
return [];
}
if (this.words > 0) {
result.push(word);
}
for (n in this.baseNodes) {
baseNode = this.baseNodes[n];
result = result.concat(baseNode.print(word + n));
}
for (var i = 0; i < result.length; i++) {
a += result[i] + " ";
}
return a;
}
TRIE.prototype.addDict = function(word, index) {
clear();
var word = document.getElementById("word").value;
this.addTRIE(word);
document.getElementById("output").innerHTML += word + " - Word Added";
document.getElementById("trieList").innerHTML += this.print();
}
TRIE.prototype.spellCheck = function(word) {
document.getElementById("output").innerHTML = "";
var word = document.getElementById("word").value;
if (this.count(word) > 0) {
document.getElementById("output").innerHTML += word + " is in dictionary.";
} else {
...