Question
Hello, Can someone change this code for me! like use a different approach for the statement to do the same thing but in a different
Hello,
Can someone change this code for me! like use a different approach for the statement to do the same thing but in a different approach. No need to change the methods just the codes of them.
Thanks.
import java.io.*;
import java.util.HashSet;
import java.util.TreeSet;
import javax.swing.JFileChooser;
import java.util.Scanner;
/**
* this program is to check if the inputted word's
* spelling from the dictionary is correct, if not
* it gives a suggestion of words for
* any the word that was misspelled
* @Unit 5 programming assignment
*
*/
public class Dictionary {
public static void main(String[] args) {
try {
// to create the new dictionary set
HashSet
// to read the text file that includes the words of the dictionary
Scanner textFile = new Scanner(new File("words.txt"));
while (textFile.hasNext()) {
String tk = textFile.next();
// to add the words from the text file to the dictionary with converting them to lowercase
splchk.add(tk.toLowerCase());
}
//to have the user select a file to read from
Scanner userTxtFile = new Scanner(getInputFileNameFromUser());
userTxtFile.useDelimiter("[^a-zA-Z]+");
// this statement to check the Set size that the assignment required which should be 72875
int listSize = splchk.size();
System.out.println("The dictionary includes " + listSize + " words.");
// to go through the file words and convert them into lowercase
HashSet
while (userTxtFile.hasNext()) {
String txtWord = userTxtFile.next();
txtWord = txtWord.toLowerCase();
if (!splchk.contains(txtWord) &&
!badWords.contains(txtWord)) {
badWords.add(txtWord);
TreeSet
goodWords = corrections(txtWord, splchk);
System.out.print(txtWord + ": ");
// if the correction method returns an empty output print out "no suggestions"
if (goodWords.isEmpty())
System.out.println("(no suggestions)");
else {
int count = 0;
for (String goodWord: goodWords) {
System.out.print(goodWord);
if (count < goodWords.size() - 1)
System.out.print(", ");
else
System.out.print(" ");
count++;
}
}
}
}
}
catch (FileNotFoundException e) {
}
}
/**
* Lets the user select an input file using a standard file
* selection dialog box.If the user cancels the dialog
* without selecting a file, the return value is null.
*/
static File getInputFileNameFromUser() {
JFileChooser fileDialog = new JFileChooser();
fileDialog.setDialogTitle("Select File for Input");
int option = fileDialog.showOpenDialog(null);
if (option != JFileChooser.APPROVE_OPTION)
return null;
else
return fileDialog.getSelectedFile();
}
/** to provide a list of possible correct spellings for any misspelled word
*
* @param badWord = the word that is misspelled
* @param dictionary = the words list that the dictionary contains
* @return suggestion of correct spellings of any misspelled word
*/
static TreeSet
TreeSet
String subString1;
String subString2;
String possibility;
for (int i = 0; i < badWord.length(); i++) {
//to delete character i.
subString1 = badWord.substring(0, i);
subString2 = badWord.substring(i + 1);
//to provide suggestion of words
possibility = subString1 + subString2;
if (dictionary.contains(possibility))
possibleWords.add(possibility);
//to add or remove any letter from the misspelled words
for (char ch = 'a'; ch <= 'z'; ch++) {
possibility = subString1 + ch + subString2;
if (dictionary.contains(possibility))
possibleWords.add(possibility);
}
//to divide the word into two substrings.
subString1 = badWord.substring(0, i);
subString2 = badWord.substring(i);
//to replace any letter any where in the misspelled words
for (char ch = 'a'; ch <= 'z'; ch++) {
possibility = subString1 + ch + subString2;
if (dictionary.contains(possibility))
possibleWords.add(possibility);
}
/**
* to insert a space anywhere in the misspelled words
* and to check the availability of the word
* within the dictionary words list
*/
char ch = ' ';
possibility = subString1 + ch + subString2;
if (dictionary.contains(subString1) && dictionary.contains(subString2))
possibleWords.add(possibility);
}
//to swap any neighboring characters in the misspelled words
for (int i = 1; i < badWord.length(); i++) {
subString1 = badWord.substring(0, i - 1);
char s1 = badWord.charAt(i - 1);
char s2 = badWord.charAt(i);
subString2 = badWord.substring(i + 1);
possibility = subString1 + s2 + s1 + subString2;
if (dictionary.contains(possibility))
possibleWords.add(possibility);
}
return possibleWords;
}
}
Step 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