拼写检查基本单词

时间:2019-07-05 06:15:32

标签: java spell-checking wordnet jwnl

尝试使用WordNet检查拼写是否正确或拼写错误。这是到目前为止我完成的SpellChecker.java实现...

package com.domain.wordnet;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;

import net.didion.jwnl.JWNL;
import net.didion.jwnl.JWNLException;
import net.didion.jwnl.data.IndexWord;
import net.didion.jwnl.data.IndexWordSet;
import net.didion.jwnl.data.Synset;
import net.didion.jwnl.dictionary.Dictionary;

public class SpellChecker {

    private static Dictionary dictionary = null;
    private static final String PROPS = "/opt/jwnl/jwnl14-rc2/config/file_properties.xml";

    static {
        try(InputStream is = new FileInputStream(PROPS)) {
            JWNL.initialize(is);
            dictionary = Dictionary.getInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println(isCorrect("change"));    //  true
        System.out.println(isCorrect("changes"));   //  false
        System.out.println(isCorrect("changed"));   //  true
        System.out.println(isCorrect("changing"));  //  true
        System.out.println();
        System.out.println(isCorrect("analyze"));   //  true
        System.out.println(isCorrect("analyzed"));  //  true
        System.out.println(isCorrect("analyzing")); //  false
    }

    public static boolean isCorrect(String token) {
        try {
            token = token.trim().toLowerCase();
            IndexWordSet set = dictionary.lookupAllIndexWords(token);
            if(set == null)
                return false;

            @SuppressWarnings("unchecked")
            Collection<IndexWord> collection = set.getIndexWordCollection();
            if(collection == null || collection.isEmpty())
                return false;

            for(IndexWord word : collection) {
                Synset[] senses = word.getSenses();
                if(senses != null && senses.length > 0
                        && senses[0].toString().toLowerCase().contains(token)) {
                    return true;
                }
            }

            return false;
        } catch (JWNLException e) {
            e.printStackTrace();
            return false;
        }
    }
}

在大多数情况下都很好,但是您会看到由于复数和某些 ing 表格而失败。是否可以在不破坏英语规则的情况下避免复数 ing 表格?

如果您在WordNet浏览器中看到changes是一个有效的单词,但是在Java API中,它是无效的。

enter image description here

不知道我需要在哪里纠正!还是任何其他解决此问题的好方法?

1 个答案:

答案 0 :(得分:1)

您在这里犯的错误是在此循环中

for(IndexWord word : collection) {
                Synset[] senses = word.getSenses();
                if(senses != null && senses.length > 0
                        && senses[0].toString().toLowerCase().contains(token)) {
                    return true;
                }
            }

Synset[] senses = word.getSenses()行返回该单词的所有含义,但是您仅检查第一个(0索引)。这个词将在一种意义上可用。 像这样

for (IndexWord word : collection) {

            Synset[] senses = word.getSenses();
            for(Synset sense:senses){
                if(sense.getGloss().toLowerCase().contains(token)){return true;}
            }

        }

此外, ing 形式的单词可能无法理解。我不确定您为什么要搜索感官来决定其有效词。

类似于if(set.getLemma() != null) return true;的代码

足以确定正确的拼写检查吗?