首页 > 解决方案 > 错误的返回类型/扫描仪

问题描述

强调文本我目前正在尝试运行此代码(使用两个不同的文件)。一个文件只是一个单词字典,另一个文件只是一个包含一些文本的文件。当我运行此代码时,我的代码会输出 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();
        }
    }
}

标签: javalistjava.util.scannerhashset

解决方案


推荐阅读