COP3530 Assignment 13 - TRIE Spellchecker

by Jeff Santos

HTML

<input type="textbox" id="inputText"/>
<input type="button" onClick="AddToTRIE(document.getElementById('inputText').value);" value="Add To Dictionary"/>
<input type="button" onClick="CheckSpelling(document.getElementById('inputText').value);" value="Spell Check"/>
<div id="output"/>

JavaScript

// Classes
function TRIENode(id, content, last)
{
	this.id = id;
	this.next = null;
	this.last = last;
	this.content = content;
	this.values = new List();
	
	this.add = function(word, pos = 0)
	{
		if(pos < word.length)
		{
			var char = word.toLowerCase().charAt(pos);
			var appendedNode = null;
			var index = this.values.indexOf(char);
			if(index == null)
			{
				appendedNode = this.values.append(char);
			}
			else
			{
				appendedNode = this.values[index];
			}
			pos++;
			appendedNode.add(word, pos);
		}
	}
	
	this.check = function(word, pos = 0)
	{
		if(pos < word.length)
		{
			var char = word.toLowerCase().charAt(pos);
			var nextNode = null;
			var index = this.values.indexOf(char);
			if(index == null)
			{
				return false
			}
			nextNode = this.values[index];
			pos++;
			return nextNode.check(word, pos);
		}
		return true;
	}
}

function List()
{
	this.head = null;
	this.length = 0;

	this.append = function(content)
	{
		// Go to end of list if length is not 0
		if (this.head != null)
		{
				this.goto_end();
		}

		// Create new node
		var node = new TRIENode(this.length, content, null);

		// Check if new node is first node
		if (this.head != null)
		{
				node.last = this.head;
				this.head.next = node;
		}
		
		// Add index operator
		this[this.length] = node;
		
		// Update list properties
		this.head = node;
		this.length++;
		
		return node;
	}

	this.goto_begin = function()
	{
		// Reset head to beginning of list
		while (this.previous()) {}
	}

	this.goto_end = function()
	{
		// Traverse list until next is null
		while (this.next()) {}
	}

	this.previous = function()
	{
		if (this.head.last != null)
		{
			this.head = this.head.last;
			return true;
		}
		else
		{
			return false;
		}
	}

	this.next = function()
	{
		if (this.head.next != null)
		{
			this.head = this.head.next;
			return true;
		}
		else
		{
			return false;
		}
	}

	this.clear = function()
	{
		this.head = null;
		this.length = 0;
		
		// Remove all node...