# Data Structures and Algorithms Project 3 - Spellchecker In this p...

## Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList; // used only for storing word suggestions
import java.util.Collections; // used only for sorting the word suggestions

public class SpellChecker {

// Inner class used to implement the Lexicon as a Trie
static class Lexicon {

private LexiconNode root;

// this constructor is used to build a Lexicon from the words available
// in the data file with name - inputFileName. Each line in the data file
// should contain a seperate word
public Lexicon(String inputFileName) throws FileNotFoundException {
// create the root node
root = new LexiconNode();

// read each line of input file that contains a single word and insert
// them to the Lexicon
Scanner fileScanner = new Scanner(new File(inputFileName));
while (fileScanner.hasNext()) {
String word = fileScanner.nextLine();
insertWord(word.toLowerCase()); // to ignore case when inserting
}
fileScanner.close();
}

// this method is used to check whether a particular word is contained
// in the Lexicon
public boolean containsWord(String word) {

// search the word, by going deeper in the Lexicon letter by letter
LexiconNode currentNode = root;
for (int i = 0; i < word.length(); i++) {
char letter = word.charAt(i);
int letterIndex = letter - 'a';

// no subtree is found for the current letter. therefore, return
// false to indicate the word is not available in the Lexicon
if (currentNode.children[letterIndex] == null) {
return false;
}

currentNode = currentNode.children[letterIndex];
}

// check isWord of the last node related to the word to find whether
// the word is marked as a full word in the Lexicon. if it's a full
// word, return true. Otherwise, return false.
if (currentNode.isWord) {
return true;
} else {
return false;
}

}

// this method inserts a new word into the Lexicon
private void insertWord(String word) {

// insert the word, letter by letter into the Lexicon
LexiconNode currentNode = root;
for (int i = 0; i < word.length(); i++) {
char letter = word.charAt(i);
int letterIndex = letter - 'a';

// create a new node if there's no node already representing the
// letter in corresponding location
if (currentNode.children[letterIndex] == null) {
currentNode.children[letterIndex] = new LexiconNode();
}

currentNode = currentNode.children...

By purchasing this solution you'll be able to access the following files:
Solution.java.

\$85.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

### Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.