****YOU CAN DOWNLOAD THOSE FILES FROM HERE*** https://drive.google.com/open?id=0
ID: 3814772 • 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
Java Code :
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.text.*;
class JavaDictionary extends JFrame implements DocumentListener,ListSelectionListener{
//Declare variables
private JList wordlist;
private JTextField txtbox;
private JTextPane txtPane;
private Highlighter.HighlightPainter cyanPainter;
private Highlighter.HighlightPainter redPainter;
public JavaDictionary(){
super("A Simple Dictionary: English-Khmer dictionary");
//Create the JPanel panel object and make sure the components on it be aligned left
JPanel panel=new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
setLayout(null);/*setting the layout of displaying controls to null
will enable you to specify the locations of your controls to display */
//Create JTextField object and add DocumentListener to allow it to respond to changing value event
txtbox=new JTextField();
txtbox.getDocument().addDocumentListener(this);
//Create JTextPane object and set its font to Khmer OS with italic style and 18 in font size
Font khmerfont=new Font("Khmer OS",Font.ITALIC,18);
txtPane = new JTextPane();
txtPane.setFont(khmerfont);
//txtPane.setEditable(false);
//Create JList object that accepts the DefaultListModel listmodel as its parameter.
//Then populate this list object with the English terms read from the file by using addToList() method
DefaultListModel listmodel=new DefaultListModel();
addToList(listmodel);
wordlist=new JList(listmodel);
wordlist.setVisibleRowCount(20);
wordlist.addListSelectionListener(this);
//Add JScrollPane panel object to the list object to make sure it can be scrolled vertically.
//This object will be put in the JPanel pane object
JScrollPane scr=new JScrollPane(wordlist);
panel.add(scr);
//Define the locations and dimensions of objects then display them on the JFame window object
panel.setBounds(10,80,150,480);
txtbox.setBounds(10,40,panel.getWidth(),25);
txtPane.setBounds(panel.getWidth(),80,749,480);
add(txtbox);
add(panel);
add(txtPane);
//Add WindowListern to the JFrame to make it reacts to the closing event
addWindowListener(new Listener());
//Set the size of the JFrame window object
pack();
setSize(800,475);
//Set the background color of the JFrame window object
Container content=getContentPane();
content.setBackground(Color.PINK);
//Don't allow the user to resize the JFrame window object
setResizable(false);
//Make use the user can see the JFrame window object
setVisible(true);
}
//This method works when the window is closing
class Listener extends WindowAdapter{
public void windowClosing(WindowEvent e){System.exit(0);}
}
public void changedUpdate(DocumentEvent e){
}
//This method works when the user type text in the text box
public void insertUpdate(DocumentEvent e){
selectlist(txtbox.getText());
}
//This method works when the user removes letters in the text box
//It calls the selectlist() method to select an item of the list
// And translation text is also shown by subsequently calling the showtran() method
public void removeUpdate(DocumentEvent e){
selectlist(txtbox.getText());
}
//This method works when the user make a change to the selection of the list items
//Translation text is also shown by calling this showtran() method
public void valueChanged(ListSelectionEvent e){
showtran(wordlist.getSelectedValue());
}
public void selectlist(String text){
// Get number of items in the list
int size = wordlist.getModel().getSize();
// Select the list item that matches the text typed in the text box
//Binary search algorithm is used to speed up the search process
int index=binsearch(wordlist, text,size);
if(index!=-1) {wordlist.setSelectedIndex(index); wordlist.ensureIndexIsVisible(index);}
}
public int binsearch(JList list,String target, int n){
int i;
int pos=-1;
int found=0;
//get the middle item of the list
Object item = list.getModel().getElementAt(n/2);
String itemstr=(String)item;
//Just to make sure the length of the middle item is greater than the text typed by the user
if(itemstr.length()<=target.length())
itemstr=itemstr.concat("Learn Java to build a simple dictionary program");
if(target.compareTo(itemstr.substring(0,target.length()))<=0){//look in the first half
for(i=0;i<=n/2 && found!=1;i++){
item =list.getModel().getElementAt(i);
itemstr=(String)item;
if(itemstr.length()>=target.length())
if(target.compareTo(itemstr.substring(0,target.length()))==0){ found=1;pos=i;break;}
}
}
else{ //look in the second haft
for(i=n/2+1;i<n && found!=1;i++){
item =list.getModel().getElementAt(i);
itemstr=(String)item;
if(itemstr.length()>=target.length())
if(target.compareTo(itemstr.substring(0,target.length()))==0){ found=1;pos=i;break;}
}
}
return pos;
}
public void addToList(DefaultListModel listmodel){
try{ //catch errors if any
String text;
FileReader fr=new FileReader("dictdata.txt");
BufferedReader br=new BufferedReader(fr);
//text=br.readLine();
while ((text=br.readLine())!=null){
//Read lines of English term from the file using readLine() method
listmodel.addElement(text.substring(0,text.indexOf('_')));
}
br.close();//close file using close() method
} catch(Exception ie){System.out.println("IO problem!");ie.printStackTrace();}
}
public void showtran(Object t){
try{ //catch errors if any
txtPane.setText("");
String text;
String item=(String)t;
FileReader fr=new FileReader("dictdata.txt");
BufferedReader br=new BufferedReader(fr);
//text=br.readLine();
while ((text=br.readLine())!=null){
//Read lines of translation text from the file using readLine() method
if(item.compareTo(text.substring(0, item.length()))==0){
String st=text.substring(item.length()+1); //Display unicode characters
byte[] chars=st.getBytes();
String unitext=new String(chars,"UTF-8");
int index=unitext.indexOf('.');
//coloring translation text
highlight(unitext.substring(0,index),Color.PINK);
highlight(unitext.substring(index,unitext.length()),Color.BLUE);
break;
}
}
br.close();//close file using close() method
} catch(Exception ie){System.out.println("IO problem!");ie.printStackTrace();}
}
public void highlight(String content, Color c)
{
//Coloring text appended to the JTextPane
//The color of the text is specified by c argument
//The text is aligned in justified layout
StyleContext style = StyleContext.getDefaultStyleContext();
AttributeSet aset = style.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
aset = style.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);
int len = txtPane.getDocument().getLength();
txtPane.setCaretPosition(len);
txtPane.setCharacterAttributes(aset, false);
txtPane.replaceSelection(content);
}
}
//Show the dictionary window
public class EnKhDictionary{
public static void main(String[] args){
new JavaDictionary();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.