A13 Trie SpellChecker

Trie Spellchecker

by scotp71

HTML

<input type = "textbox" id = "input" onkeyup = "getInput();"  />
<input type = "button" id = "addWord" value = "Add to Dictionary" onClick = "addToTrie()"/>
<input type = "button" id = "spellCheck" value = "Check Spelling" onClick = "checkSpelling()"/>


<div id = "output"></div>
</br>
</br>
<div id = "output2"></div>

JavaScript

let Node = function() {
	this.keys = new Map();
	this.end = false;
	this.setEnd = function() {
		this.end = true;
	};
	this.isEnd = function() {
		return this.end;
	};
};

let Trie = function() {

	this.root = new Node();

	this.add = function(input, node = this.root) {
		if (input.length == 0) {
			node.setEnd();
			return;
		} else if (!node.keys.has(input[0])) {
			node.keys.set(input[0], new Node());
			return this.add(input.substr(1), node.keys.get(input[0]));
		} else {
			return this.add(input.substr(1), node.keys.get(input[0]));
		}
	};

	this.isWord = function(word) {
		let node = this.root;
		while (word.length > 1) {
			if (!node.keys.has(word[0])) {
				return false;
			} else {
				node = node.keys.get(word[0]);
				word = word.substr(1);
			}
		}
		return (node.keys.has(word) && node.keys.get(word).isEnd()) ? 
      true : false;
	}

	this.print = function() {
		let words = new Array();
		let search = function(node, string) {
			if (node.keys.size != 0) {
				for (let letter of node.keys.keys()) {
					search(node.keys.get(letter), string.concat(letter));
				}
				if (node.isEnd()) {
					words.push(string);
				}
			} else {
				string.length > 0 ? words.push(string) : undefined;
				return;
			}
		}
		search(this.root, new String());
		return words.length > 0 ? words : mo;
	}
  
  
this.display = function(_text) {
	let words2 = new Array();
  words2 = myTrie.print();
  var text = _text;
  var disWords = new Array();
  var sameLetters = false;
  disWords.length = 0;
  for (var i=0; i<words2.length; i++){
  	var x = words2[i];
    for (var j=0; j<text.length; j++){
    	var y = text.charAt(j);
      var z = x.charAt(j);
      sameLetters = checkLetters(y, z);
      if(sameLetters==false){
      break;
      }
    }
    if (sameLetters == true){
        disWords.push(x);
    }
  }
  return disWords;
}

checkLetters = function(y, z){
	var sameLetters = false;
  if (y!=z){
  	return sameLetters;
  }
  if (y==z){
  	sameLetters = true;
  }
  return...