我得到了拼写检查器的代码。它不读取words.txt文件。它只会打开对话框来选择文件。当我选择words.txt文件时,对话框关闭,没有任何反应。
我不确定这段代码出了什么问题。我一直在检查,似乎一切都很好。有人能告诉我哪里出错了吗?
谢谢。
import java.io.*;
import java.util.Scanner;
import java.util.HashSet;
import javax.swing.*;
import java.util.TreeSet;
/**
* This class works as a basic spell-checker. It uses the file words.txt to
* check whether a given word is correctly spelled.
*/
public class SpellChecker {
public static void main(String[] args) {
Scanner words;
HashSet<String> dict = new HashSet<String>();
Scanner userFile;
try {
words = new Scanner(new File("src/words.txt"));
while (words.hasNext()) {
String word = words.next();
dict.add(word.toLowerCase());
}
userFile = new Scanner(getInputFileNameFromUser());
// Skip over any non-letter characters in the file.
userFile.useDelimiter("[^a-zA-Z]+");
HashSet<String> badWords = new HashSet<String>();
while (userFile.hasNext()) {
String userWord = userFile.next();
userWord = userWord.toLowerCase();
if (!dict.contains(userWord) &&
!badWords.contains(userWord)) {
badWords.add(userWord);
TreeSet<String> goodWords = new TreeSet<String>();
goodWords = corrections(userWord, dict);
System.out.print(userWord + ": ");
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("\n");
count++;
}
}
}
}
}
catch (FileNotFoundException e) {
System.exit(0);
}
} // end main()
/**
* 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.
*
* @return A file selected by the user, if any. Otherwise, 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();
} // end getInputFileNameFromUser()
/*
* Gives a list of possible correct spellings for misspelled words which
* are variations of a a given word that are present in the dictionary.
*
* @return A tree set containing a list of possible corrections to the
* misspelled word.
*/
static TreeSet<String> corrections(String badWord, HashSet<String> dictionary) {
TreeSet<String> possibleWords = new TreeSet<String>();
String subStr1, subStr2, possibility;
for (int i = 0; i < badWord.length(); i++) {
// Remove character i from the word.
subStr1 = badWord.substring(0, i);
subStr2 = badWord.substring(i + 1);
// Delete any one of the letters from the misspelled word.
possibility = subStr1 + subStr2;
if (dictionary.contains(possibility))
possibleWords.add(possibility);
// Change any letter in the misspelled word into any other
// letter.
for (char ch = 'a'; ch <= 'z'; ch++) {
possibility = subStr1 + ch + subStr2;
if (dictionary.contains(possibility))
possibleWords.add(possibility);
}
// Divide the word into two substrings.
subStr1 = badWord.substring(0, i);
subStr2 = badWord.substring(i);
// Insert any letter at any point in the misspelled word.
for (char ch = 'a'; ch <= 'z'; ch++) {
possibility = subStr1 + ch + subStr2;
if (dictionary.contains(possibility))
possibleWords.add(possibility);
}
// Insert a space at any point in the misspelled word and check
// that both of the words that are produced are in the dictionary.
char ch = ' ';
possibility = subStr1 + ch + subStr2;
if (dictionary.contains(subStr1) && dictionary.contains(subStr2))
possibleWords.add(possibility);
}
// Swap any two neighbouring characters in the misspelled word.
for (int i = 1; i < badWord.length(); i++) {
subStr1 = badWord.substring(0, i - 1);
char ch1 = badWord.charAt(i - 1);
char ch2 = badWord.charAt(i);
subStr2 = badWord.substring(i + 1);
possibility = subStr1 + ch2 + ch1 + subStr2;
if (dictionary.contains(possibility))
possibleWords.add(possibility);
}
return possibleWords;
} // end corrections()
} // end class SpellChecker
https://stackoverflow.com/questions/51513284
复制相似问题