Question
Please complete the findClosestWords method (in Java)! Instructions: ----------------------------------------------------------------------------------------------------------------------- SpellChecker.java import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class SpellChecker extends JFrame
Please complete the findClosestWords method (in Java)!
Instructions:
-----------------------------------------------------------------------------------------------------------------------
SpellChecker.java import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*;
public class SpellChecker extends JFrame {
JTextArea textArea; JTextField stringField; JScrollPane scrollPane;
String[] words;
public SpellChecker () { // Set some parameters of the frame (window) that's brought up. this.setSize (600, 600); this.setTitle ("Spell checker"); this.setResizable (true);
// This is how stuff is put into a frame. Container cPane = this.getContentPane(); textArea = new JTextArea (); scrollPane = new JScrollPane (textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); cPane.add (scrollPane, BorderLayout.CENTER); // Make the controls. JPanel panel = new JPanel (); JLabel label = new JLabel ("Enter string: "); panel.add (label); stringField = new JTextField (30); panel.add (stringField); JButton button = new JButton ("Go"); button.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { handleButtonClick(); } } ); panel.add (button); cPane.add (panel, BorderLayout.SOUTH);
// Read in dictionary. words = WordTool.getDictionary (); this.setVisible (true); }
String inputStr;
// When the user clicks the button, this method gets called. // It's where we need to respond.
void handleButtonClick () { // Extract the string from the textfield where the user typed the strings. inputStr = stringField.getText ();
String[] matchedWords = findClosestWords (inputStr, words);
String outputStr = "";
if (matchedWords == null) { outputStr = "No matches found"; } else { for (int i=0; i outputStr += matchedWords[i] + " "; } }
// Put the output string in the text box. String text = textArea.getText (); text += outputStr + " "; textArea.setText (text); }
static String[] findClosestWords (String inputWord, String[] words){ // Complete this method // Feel free to change return type and parameters }
static void test (){ String inputStr = "asdf"; String[] words = {"asdf", "aadf", "bsdf", "asdd", "aadd", "asdff", "aasdf", "asd", "assdf"}; String[] matched = findClosestWords (inputStr, words); print (inputStr, matched); }
static void print (String word, String[] matchedWords){ System.out.println ("Words that matched: " + word); for (int i=0; i System.out.println (" " + matchedWords[i]); } }
public static void main (String[] argv){ test (); //SpellChecker checker = new SpellChecker ();
}
}
1. Find close words Given a single word, compare against a dictionary of words. The output should be all the words in the dictionary that are "close in spelling" to the input word. Define "close" as follows: Words w1 and w2 are close in spelling if any one of the following is true: The two words are exactly the same (of course). w1 and w2 are of the same length and differ in only one letter. For example, "cat" and "cut", differ in only the second letter, as do "cat" and "eat". But "book" and "bulk" differ in at least two letters. - The lengths of w1 and w2 differ by one letter and the shorter word can be obtained from the longer word by removing one letter. For example, in comparing "cat" and "scat", you can remove 's' from "scat" and get the other word. Similarly, in comparing "cart" and "cat", you can remove the 'r' from the first word to get the second one. However, in comparing "cat" and "cake", removal of a letter from the longer one does not result in the shorter oneStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started