错误的返回类型/扫描仪

时间:2018-11-25 03:43:24

标签: java list java.util.scanner hashset

强调文本我目前正在尝试运行此代码(带有两个不同的文件)。一个文件只是单词的字典,另一个文件只是带有一些文本的文件。当我运行此代码时,我的代码将输出getIncorrectWords中提供的文件的整个文本。我知道这是错误的,因为存在此SpellChecker应该拾取的错误(并且只应打印错误的单词)。我回去对构造函数进行了一些更改,但似乎无法在代码中找到错误。任何人都可以提供有关我的程序可能出什么问题的一些输入,并提供一些有关如何解决它的提示或建议吗?作为参考,我正在做一个SpellChecker,它在HashSet中存储“字典”-一些定义的单词。我应该解析另一个文件,并检查字典中未定义的单词并将其存储在列表中。这是通过我的getIncorrectWords方法完成的。最后,我为我的getSuggestions方法中的单词可能提供了建议。这些建议可通过以下方式找到:

添加一个字符-尝试在字符串的每个点添加一个字符,看看是否创建了一个有效的单词。

删除一个字符-尝试从任意位置删除一个字符,看看是否创建有效的单词。

交换相邻字符-尝试在字符串中的任意位置交换两个连续的字符,看看是否创建有效的单词。

感谢您的时间和投入!

public class SpellChecker {
    private Set<String> dictionary = new HashSet<>();

    public SpellChecker(String fileName) {
        dictionary = getDictionary(fileName);
    }

    public List<String> getIncorrectWords(String fileName) {
        File file = new File(fileName);
        List<String> words = new ArrayList<>();
        try {
            Scanner input = new Scanner(file);
            while (input.hasNextLine()) {
                String word = input.nextLine().trim().toLowerCase();
                if (!dictionary.contains(word)) {
                    words.add(word);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return words;
    }

    public static List<String> getSuggestions(String word){
    List<String> letters = Arrays.asList("a", "b", "c", "d", "e", "f", "g",
                                       "h", "i", "j", "k", "l", "m", "n",
                                       "o", "p", "q", "r", "s", "t", "u",
                                       "v", "w", "x", "y", "z"); 
    List<String> suggestions = new ArrayList<String>();
    StringBuilder builder = new StringBuilder(word); 
    for(int i = 0; i <= builder.length(); i++){
        for(String string: letters){
            StringBuilder suggestion = new StringBuilder(builder.toString());
            suggestion.insert(i, string); 
            if(dictionary.contains(suggestion.toString().toLowerCase())){
                suggestions.add(suggestion.toString());
            }
        }
    }
    for(int i = 0; i <= builder.length()-2; i++){
        for(String string: letters){
            StringBuilder suggestion = new StringBuilder(builder.toString());
            char one = suggestion.charAt(i + 1);
            char two = suggestion.charAt(i);
            suggestion.replace(i, i + 1, String.valueOf(one));
            suggestion.replace(i+1, i + 2, String.valueOf(two));
            if(dictionary.contains(suggestion.toString().toLowerCase())){
                suggestions.add(suggestion.toString());
            }
        }
    }
    for(int i = 0; i <= builder.length(); i++){
        for(String string: letters){
            StringBuilder suggestion = new StringBuilder(builder.toString());
            suggestion.replace(i, i + 1, "");
            if(dictionary.contains(suggestion.toString().toLowerCase())){
                suggestions.add(suggestion.toString());
            }
        }
   }
   return suggestions;
}

    public Set<String> getDictionary(String fileName) {
        File file = new File(fileName);
        try {
            Scanner input = new Scanner(file);
            while (input.hasNextLine()) {
                String word = input.nextLine().trim();
                dictionary.add(word.toLowerCase());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return dictionary;
    }
}

这是我调用函数的主类。

public class SpellCheckerTester {

    public static void main(String[] args) throws IOException {

        SpellChecker checker = new SpellChecker("words.txt");
        List<String> incorrectWords = checker.getIncorrectWords("test.txt");

        for (String word : incorrectWords) {
            List<String> suggestions = checker.getSuggestions(word);
            System.out.print(word + " - ");
            for (String suggestion : suggestions) {
                System.out.print(suggestion + " ");
            }
            System.out.println();
        }
    }
}

0 个答案:

没有答案