Instructions
Objective
Write a program to create a spell checker that uses dictionary that stores its words in a balanced binary tree in java language.
Requirements and Specifications
PROGRAM DESCRIPTION
Create a spell checker that uses dictionary that stores its words in a balanced binary tree.
TASKS & 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.
Right-click on the JavaCoreTemplate project and select Copy. Right-click in an empty area of the Package Explorer and select Paste.
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 2035, your project name must be MMarciano_SpellChecker_F35.
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 java assignment binary tree code.
Source Code
BASIC SPELL CHECKER
package spellchecker;
import java.io.File;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class BasicSpellChecker implements SpellChecker {
private Dictionary dictionary = new BasicDictionary();
private String text;
private int previous = 0;
/**
* Replaces the current dictionary with words imported from the specified text file. Words in the file are in
* lexicographical (ASCII) order, one word per line.
*
* @param filename Name (possibly including a path) of the file to import.
* @throws Exception
*/
@Override
public void importDictionary(String filename) throws Exception {
dictionary.importFile(filename);
}
/**
* Loads the specified file from a previously saved dictionary tree file. The file format is text, with one word per
* line.
*
* @param filename Name (possibly including a path) of the file to load from.
* @throws Exception
*/
@Override
public void loadDictionary(String filename) throws Exception {
dictionary.load(filename);
}
/**
* Saves the dictionary tree to the specified file in preorder. The file format is text, with one word per line.
*
* @param filename Name (possibly including a path) of the file to save to. If the file exists, it is overwritten.
* @throws Exception
*/
@Override
public void saveDictionary(String filename) throws Exception {
dictionary.save(filename);
}
/**
* Loads the specified text document.
*
* @param filename
* @throws Exception
*/
@Override
public void loadDocument(String filename) throws Exception {
text = Files.readString(Path.of(filename));
}
/**
* Saves the specified text document.
*
* @param filename
* @throws Exception
*/
@Override
public void saveDocument(String filename) throws Exception {
try(PrintWriter printWriter = new PrintWriter(filename)) {
printWriter.print(text);
}
}
/**
* @return Returns the text in the document.
*/
@Override
public String getText() {
return text;
}
/**
* Starts/continues a spell check of the text. Use the regular expression below to match words (it's not great, but
* it's simple and works OK for basic text).
*
*
* \b[\w']+\b
*
*
* The method returns when the first unknown word is located or when the end of the document is reached (whichever
* comes first). The index of the character after the unknown word is retained for use as the starting index for
* subsequent calls to spellCheck where continueFromPrevious is true.
*
* @param continueFromPrevious If false, a new spell check is started from the first character of the document. If true, the spell
* check continues from it's current location.
* @return If no unknown word is found this method returns null. Otherwise, it returns a String array organized as
* follows:
*
*
* [0] = Unknown word
* [1] = Index of start of unknown word. The index is the position within the entire document.
* [2] = Preceeding word in the dictionary . If the unknown word is before all words in the dictionary, this element should be "".
* [3] = Succeeding word in the dictionary. If the unknown word is after all words in the dictionary, this element should be "".
* e.g.
* [0] = "spelm"
* [1] = "224"
* [2] = "spells"
* [3] = "spelt"
*
*/
@Override
public String[] spellCheck(boolean continueFromPrevious) {
if (!continueFromPrevious) {
previous = 0;
}
Pattern pattern = Pattern.compile("\\b[\\w']+\\b");
Matcher matcher = pattern.matcher(text);
while (matcher.find(previous)) {
String word = matcher.group();
String[] searchResult = dictionary.find(word);
previous = matcher.end() + 1;
if (searchResult != null) {
return new String[]{word, Integer.toString(matcher.start()), searchResult[0], searchResult[1]};
}
}
previous = 0;
return null;
}
@Override
public void addWordToDictionary(String word) {
dictionary.add(word);
}
/**
* Replaces text in the document from startIndex to just before endIndex with the given replacementText.
*
* NOTE: Be sure to update your spell checker index by adding the difference between the length of the replacement
* text and the length of the text that was replaced.
*
* @param startIndex Index of the first character to replace.
* @param endIndex Index of the character after the last character to replace.
* @param replacementText
*/
@Override
public void replaceText(int startIndex, int endIndex, String replacementText) {
text = text.substring(0, startIndex) + replacementText + text.substring(endIndex);
}
}
SPELL CHECKER
package spellchecker;
// 01/12/2022
public interface SpellChecker {
/**
* Replaces the current dictionary with words imported from the specified text file. Words in the file are in
* lexicographical (ASCII) order, one word per line.
*
* @param filename
* Name (possibly including a path) of the file to import.
* @throws Exception
*/
public void importDictionary(String filename) throws Exception;
/**
* Loads the specified file from a previously saved dictionary tree file. The file format is text, with one word per
* line.
*
* @param filename
* Name (possibly including a path) of the file to load from.
* @throws Exception
*/
public void loadDictionary(String filename) throws Exception;
/**
* Saves the dictionary tree to the specified file in preorder. The file format is text, with one word per line.
*
* @param filename
* Name (possibly including a path) of the file to save to. If the file exists, it is overwritten.
* @throws Exception
*/
public void saveDictionary(String filename) throws Exception;
/**
* Loads the specified text document.
*
* @param filename
* @throws Exception
*/
public void loadDocument(String filename) throws Exception;
/**
* Saves the specified text document.
*
* @param filename
* @throws Exception
*/
public void saveDocument(String filename) throws Exception;
/**
*
* @return Returns the text in the document.
*/
public String getText();
/**
* Starts/continues a spell check of the text. Use the regular expression below to match words (it's not great, but
* it's simple and works OK for basic text).
*
*
* \b[\w']+\b
*
*
* The method returns when the first unknown word is located or when the end of the document is reached (whichever
* comes first). The index of the character after the unknown word is retained for use as the starting index for
* subsequent calls to spellCheck where continueFromPrevious is true.
*
* @param continueFromPrevious
* If false, a new spell check is started from the first character of the document. If true, the spell
* check continues from it's current location.
* @return If no unknown word is found this method returns null. Otherwise, it returns a String array organized as
* follows:
*
*
* [0] = Unknown word
* [1] = Index of start of unknown word. The index is the position within the entire document.
* [2] = Preceeding word in the dictionary . If the unknown word is before all words in the dictionary, this element should be "".
* [3] = Succeeding word in the dictionary. If the unknown word is after all words in the dictionary, this element should be "".
* e.g.
* [0] = "spelm"
* [1] = "224"
* [2] = "spells"
* [3] = "spelt"
*
*/
public String[] spellCheck(boolean continueFromPrevious);
public void addWordToDictionary(String word);
/**
* Replaces text in the document from startIndex to just before endIndex with the given replacementText.
*
* NOTE: Be sure to update your spell checker index by adding the difference between the length of the replacement
* text and the length of the text that was replaced.
*
* @param startIndex
* Index of the first character to replace.
* @param endIndex
* Index of the character after the last character to replace.
* @param replacementText
*/
public void replaceText(int startIndex, int endIndex, String replacementText);
}
Similar Services
At Programming Homework Help, we offer expert assistance for all your coding needs. Whether you're struggling with assignments, projects, or learning new languages, our team provides reliable and timely support to help you succeed in your programming journey.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java