小编ary*_*ary的帖子

如何阻止java拼写检查程序纠正重复单词

我已经实现了一个执行以下操作的程序:

  1. 将网页中的所有单词扫描成字符串(使用jsoup)
  2. 过滤掉所有HTML标记和代码
  3. 将这些单词放入拼写检查程序并提供建议

拼写检查程序将dictionary.txt文件加载到数组中,并将字符串输入与字典内的单词进行比较.

我当前的问题是,当输入包含多次相同的单词时,例如"teh program was teh worst",代码将打印出来

You entered 'teh', did you mean 'the'?
You entered 'teh', did you mean 'the'?
Run Code Online (Sandbox Code Playgroud)

有时网站会反复出现多个单词,这可能会变得混乱.

如果可能的话,打印单词以及错误拼写的次数将是完美的,但是对每个打印一次的单词施加限制就足够了.

我的程序有一些方法和两个类,但拼写检查方法如下:

注意:原始代码包含一些删除标点符号的"if"语句,但为了清楚起见,我删除了它们.

static boolean suggestWord;

public static String checkWord(String wordToCheck) {
        String wordCheck;
        String word = wordToCheck.toLowerCase();

    if ((wordCheck = (String) dictionary.get(word)) != null) {
        suggestWord = false; // no need to ask for suggestion for a correct
                                // word.
        return wordCheck;
    }

    // If after all of these checks a word could not …
Run Code Online (Sandbox Code Playgroud)

java algorithm if-statement spell-checking

3
推荐指数
1
解决办法
667
查看次数

标签 统计

algorithm ×1

if-statement ×1

java ×1

spell-checking ×1