****YOU CAN DOWNLOAD THOSE FILES FROM HERE*** https://drive.google.com/open?id=0
ID: 3833452 • Letter: #
Question
****YOU CAN DOWNLOAD THOSE FILES FROM HERE***
https://drive.google.com/open?id=0B2gb5h869g2aaVNYMjAzeDRMRjQ
Create a spell checker that uses dictionary that stores its words in a balanced binary tree.
Tasks and Requirements
NOTE: Naming is critical in the tasks and requirements described below. If the names don't match those described below exactly, your project will not be graded.
Create a copy of the Java SE Project Template. The project name must follow this pattern: {FLname}_SpellChecker_{TERM}, where {FLname} is replaced by the first letter of your first name plus your last name, and {TERM} is the current semester and year. E.g. if your name is Maria Marciano and it's Fall 2014, your project name must be MMarciano_SpellChecker_F14.
Create a package named spellchecker in your project.
BinaryTreeNode class:
Download BinaryTreeNode.java, and put it in the spellchecker package.
Your BinaryTree class must use BinaryTreeNode to store its words.
Do not modify BinaryTreeNode.java.
BasicDictionary class:
Download the Dictionary.java interface and put it in the spellchecker package.
Read the comments above each method to understand what they are supposed to do.
Create a new class named BasicDictionary in the spellchecker package. In the class creation wizard, click the Add (interface) button, and search for Dictionary. Check the "Inherited abstract methods" box. Click Finish. Eclipse will create the class and automatically add method stubs that meet the Dictionary interface.
You do not modify the Dictionary interface. You add your code to the BasicDictionary class.
BasicDictionary must use a binary tree to store the words in the dictionary. BasicDictionary can hold the root of the binary tree, but a stronger design would use a separate BinaryTree class.
You must write your own binary tree code.
BasicSpellChecker class:
Download the SpellChecker.java interface and put it in the spellchecker package.
Read the comments above each method to understand what they are supposed to do.
In BasicSpellChecker, modify the public class BasicSpellChecker to include implements SpellChecker.
You can use Eclipse's QuickFix support to add the required methods to BasicSpellChecker.
You do not modify the SpellChecker interface. You add your spell-checking code to BasicSpellChecker's methods.
Add the unit testing classes. These classes will be used to test the code that you write.
Create a new package in the src folder called sbccunittest. To be clear: sbccunittest should be a child of the src folder, not of the spellchecker package.
Download SpellCheckerTester.java into sbccunittest.
Download test_files.zip into your project root and unzip them into your project's root folder.
Spell-checking notes
Be sure to read the SpellChecker.spellCheck() documentation carefully. It specifies the regular expression that you must use for iterating through words in the document.
It also describes an instance variable of BasicSpellChecker that you need to create and update - the current spell checking index.
Also be sure to read the SpellChecker.replaceText() documentation carefully. It describes how the spell checking index is to be adjusted when replaceText() is called.
Note that the test documents are Windows files, which means that each line ends with (i.e. two characters).
Ignore case when comparing words.
Unit Testing
Debug all java compilation Errors (see the Problems view). The unit tests can't be run until these errors are fixed.
In the Package Explorer, right-click on the sbccunittest package | Run As... | JUnit Test.
Initially, all of the tests will probably fail. However, as you add functionality to the BasicDictionary class, tests will begin to pass.
Work on BasicDictionary first. Continue adding functionality to the BasicDictionary until all of the dictionary tests (see Scoring section below) pass. Then start working on BasicSpellChecker and its associated tests.
There is no user interface requirement for this assignment. However, you might find it interesting to build a simple console interface that allows the user to specify a dictionary and a text document to spell check. When an unknown word is found, give the user options to ignore the word, change it themselves, replace it with the preceeding/succeeding word, or end the spell checking. When spell checking is completed, overwrite the original text document with the spell-checked version.
*** When importing the dictionary, build a complete tree
Explanation / Answer
PROGRAM CODE:
BasicDictionary.java
package spellchecker;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class BasicDictionary implements Dictionary {
BinaryTree tree;
public BasicDictionary() {
tree = new BinaryTree();
}
@Override
public void importFile(String filename) throws Exception {
tree.makeEmpty();
Scanner fileReader = new Scanner(new File(filename));
while(fileReader.hasNextLine())
{
add(fileReader.nextLine());
}
fileReader.close();
}
@Override
public void load(String filename) throws Exception {
importFile(filename);
}
@Override
public void save(String filename) throws Exception {
String tokens[] = tree.toArray();
PrintWriter writer = new PrintWriter(new File(filename));
for(int i=0; i<tokens.length; i++)
{
writer.write(tokens[i] + " ");
}
writer.close();
}
@Override
public String[] find(String word) {
if(tree.find(word))
return null;
else
{
return tree.startsWith(word.substring(0, 3));
}
}
@Override
public void add(String word) {
if(!tree.find(word))
tree.insert(word);
}
@Override
public BinaryTreeNode getRoot() {
return tree.getRoot();
}
@Override
public int getCount() {
return tree.size();
}
}
BinaryTree.java
package spellchecker;
public class BinaryTree {
protected int size;
protected BinaryTreeNode root;
public BinaryTree() {
makeEmpty();
}
public void makeEmpty() {
size = 0;
root = null;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public BinaryTreeNode getRoot()
{
return root;
}
public void insert(String value) {
if (root == null) {
root = new BinaryTreeNode(value);
} else {
insertHelper(value, root);
}
size++;
}
private void insertHelper(String entry, BinaryTreeNode node) {
if (entry.compareTo(node.value) <= 0) {
if (node.left == null) {
node.left = new BinaryTreeNode(entry);
} else {
insertHelper(entry, node.left);
}
} else {
if (node.right == null) {
node.right = new BinaryTreeNode(entry);
} else {
insertHelper(entry, node.right);
}
}
}
public boolean find(String key) {
BinaryTreeNode node = findHelper(key, root);
if (node == null) {
return false;
} else {
return true;
}
}
private BinaryTreeNode findHelper(String key, BinaryTreeNode node) {
if(node == null)
return null;
if(key.compareTo(node.value) == 0)
return node;
else
{
BinaryTreeNode found = findHelper(key, node.left);
if(found == null)
found = findHelper(key, node.right);
return found;
}
}
public String[] startsWith(String word)
{
String tokens[] = toArray();
String words[] = new String[2];
for(int i=0; i<tokens.length; i++)
{
if(tokens[i].startsWith(word))
{
words[0] = tokens[1];
words[1] = tokens[0];
}
}
if(words.length>0)
return words;
else return new String[]{""};
}
public String[] toArray()
{
return preorder(root, "").split(" ");
}
public String remove(String key) {
if(root == null)
return null;
else
{
BinaryTreeNode node = findHelper(key, root);
size--;
if(node == null)
return null;
else
{
root = remove(key, root);
return node.value;
}
}
}
private BinaryTreeNode remove(String key, BinaryTreeNode node )
{
if( node == null )
return null; // Item not found; do nothing
if( key.compareTo( node.value ) < 0 )
node.left = remove( key, node.left );
else if( key.compareTo( node.value ) > 0 )
node.right = remove( key, node.right);
if( node.left != null && node.right != null ) // Two children
{
node.value= findMin( node.right ).value;
node.right = remove(node.value, node.right );
}
else
node = ( node.left != null ) ? node.left : node.right;
return node;
}
private BinaryTreeNode findMin( BinaryTreeNode node )
{
if( node == null )
return null;
else if( node.left == null )
return node;
return findMin( node.left );
}
public String preorder(BinaryTreeNode node, String result)
{
if (node == null)
return result;
/* first print data of node */
result = node.value + " ";
/* then recur on left sutree */
result += preorder(node.left, result);
/* now recur on right subtree */
result += preorder(node.right, result);
return result;
}
public String toString() {
if (root == null) {
return "";
} else {
return preorder(root, "");
}
}
}
BasicSpellChecker.java
package spellchecker;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class BasicSpellChecker implements SpellChecker {
private Dictionary dict;
private String document;
private int spellCheckIndex;
public BasicSpellChecker() {
dict = new BasicDictionary();
document = "";
spellCheckIndex = 0;
}
@Override
public void importDictionary(String filename) throws Exception {
dict.importFile(filename);
}
@Override
public void loadDictionary(String filename) throws Exception {
dict.load(filename);
}
@Override
public void saveDictionary(String filename) throws Exception {
dict.save(filename);
}
@Override
public void loadDocument(String filename) throws Exception {
Scanner fileReader = new Scanner(new File(filename));
while(fileReader.hasNextLine())
{
document +=fileReader.nextLine() + " ";
}
fileReader.close();
}
@Override
public void saveDocument(String filename) throws Exception {
PrintWriter writer = new PrintWriter(new File(filename));
writer.write(document);
writer.close();
}
@Override
public String getText() {
return document;
}
@Override
public String[] spellCheck(boolean continueFromPrevious) {
String word = "";
if(!continueFromPrevious)
spellCheckIndex = 0;
while(spellCheckIndex<document.length())
{
if(document.charAt(spellCheckIndex) != ' ')
{
word += document.charAt(spellCheckIndex);
spellCheckIndex++;
}
else
{
String tokens[] = dict.find(word);
if(tokens[0] == "")
return null;
else
return new String[]{word, String.valueOf(spellCheckIndex), tokens[0], tokens[1]};
}
}
return null;
}
@Override
public void addWordToDictionary(String word) {
dict.add(word);
}
@Override
public void replaceText(int startIndex, int endIndex, String replacementText) {
String newString = document.substring(0, startIndex-1) + replacementText + document.substring(endIndex);
document = newString;
spellCheckIndex += Math.abs(replacementText.length() - (endIndex-startIndex));
}
}
BinaryTreeNode.java
package spellchecker;
public class BinaryTreeNode {
public BinaryTreeNode left;
public BinaryTreeNode right;
public String value;
BinaryTreeNode(String value) {
this.value = value;
}
BinaryTreeNode(BinaryTreeNode node) {
left = node.left;
right = node.right;
value = node.value;
}
public String toString() {
String s = "";
if (left != null) {
s = "(" + left.toString() + ")";
}
s = s + value;
if (right != null) {
s = s + "(" + right + ")";
}
return s;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.