Assignment 13 Spellchecker
by austinmillett
HTML
<!-- Heading 1 -->
<h2> Austin Millett </h2>
<!-- Heading 2 -->
<h3> Assignment 13 - Spellchecker </h3>
<!-- Output for dictionary list -->
<h4> Dictionary List: </h4>
<p id = "output2"> </p>
<!-- Textbox 1 -->
Please enter a word: <br/>
<input type = "textbox" id = "enteredWord" /> <br/>
<!-- Button for Add to Dictionary -->
<input type = "button" id = "dictionary" value = "Add to Dictionary" onClick = "AddToTRIE()" /> <br/>
<!-- Button for Check Spelling -->
<input type = "button" id = "check" value = "Check Spelling" onClick = "CheckSpelling()" /> <br/> <br/>
<!-- Output for code -->
<p id = "output1"> </p>
CSS
/* Design for first "Add to Dictionary" button */
#dictionary {
background-color: black;
border: 2px solid;
color: white;
padding: 4px 8px;
text-align: center;
font-size: 15px;
}
/* Design for first "Check Spelling" button */
#check {
background-color: black;
border: 2px solid;
color: white;
padding: 4px 8px;
text-align: center;
font-size: 15px;
}
JavaScript
function TRIE() {
this.words = 0;
this.prefixes = 0;
this.childNodes = [];
}
TRIE.prototype.addTRIE = function(word, position) {
if (word.length == 0) {
return;
}
if (position === undefined) {
position = 0;
}
if (position === word.length) {
this.words++;
return;
}
this.prefixes++;
p = word[position];
if (this.childNodes[p] === undefined) {
this.childNodes[p] = new TRIE();
}
child = this.childNodes[p];
child.addTRIE(word, position + 1);
}
TRIE.prototype.count = function(word, position) {
var count = 0;
if (word.length == 0) {
count = 0;
}
if (position === undefined) {
position = 0;
}
if (position === word.length) {
count = this.words;
}
p = word[position];
childNode = this.childNodes[p];
if (childNode !== undefined) {
count = childNode.count(word, position + 1);
}
return count;
}
TRIE.prototype.print = function(word) {
var childNode;
var returned = [];
var s = "";
if (word === undefined) {
word = "";
}
if (this === undefined) {
return [];
}
if (this.words > 0) {
returned.push(word);
}
for (p in this.childNodes) {
childNode = this.childNodes[p];
returned = returned.concat(childNode.print(word + p));
}
for (var i = 0; i < returned.length; i++) {
s += returned[i] + " ";
}
return s;
}
// Stored dictionary list
TRIE.prototype.makeDictionary = 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");
}
// Add new input to stored dictionary list
var trieTree = new TRIE();
trieTree.makeDictionary();
document.getElementById("output2").innerHTML += trieTree.print();
function AddToTRIE() {
trieTree.addDictionary();
}
function CheckSpelling() {
trieTree.check();
}
// Add to dictionary
TRIE.prototype.addDictionary = function(word, position) {
clearDisplay();
var word = document.getElementById("enteredWord").value;
this.addTRIE(word);
// "someWord" is now added to the dictionary
document.getElementById("output1").innerHTML += word + " is...