Java 基本单词的拼写检查

Java 基本单词的拼写检查,java,spell-checking,wordnet,jwnl,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 n

尝试使用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;
        }
    }
}
package com.domain.wordnet;
导入java.io.FileInputStream;
导入java.io.InputStream;
导入java.util.Collection;
导入net.didion.jwnl.jwnl;
导入net.didion.jwnl.JWNLException;
导入net.didion.jwnl.data.IndexWord;
导入net.didion.jwnl.data.IndexWordSet;
导入net.didion.jwnl.data.Synset;
导入net.didio.jwnl.dictionary.dictionary;
公共类拼写检查器{
私有静态字典=null;
私有静态最终字符串PROPS=“/opt/jwnl/jwnl14-rc2/config/file_properties.xml”;
静止的{
try(InputStream is=newfileinputstream(PROPS)){
初始化(is);
dictionary=dictionary.getInstance();
}捕获(例外e){
e、 printStackTrace();
}
}
公共静态void main(字符串[]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(“分析”);//true
System.out.println(isCorrect(“分析”);//false
}
公共静态布尔值isCorrect(字符串标记){
试一试{
token=token.trim().toLowerCase();
IndexWordSet=dictionary.lookupAllIndexWords(标记);
if(set==null)
返回false;
@抑制警告(“未选中”)
Collection Collection=set.getIndexWordCollection();
if(collection==null | | collection.isEmpty())
返回false;
用于(索引word:集合){
Synset[]senses=word.getSenses();
如果(senses!=null&&senses.length>0
&&感官[0]。toString().toLowerCase().contains(令牌)){
返回true;
}
}
返回false;
}捕获(JWNLException e){
e、 printStackTrace();
返回false;
}
}
}
在大多数情况下,这是很好的,但是你可以看到复数形式和一些ing形式失败。我能在不破坏英语语言规则的情况下避免使用复数形式吗

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


不知道我需要纠正的地方!或者其他解决这个问题的好方法?

您在这里犯的错误就在这个循环中

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-index)。这个词有一种意思。 像这样的

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)的代码
返回true


应该足以决定拼写检查是否正确?

isCorrect(“analying”)
返回false似乎完全正确,因为就我所知,
analying
在这里不是正确的词<代码>分析
将是。嘿@Ben,我的坏!我已经纠正了自己的拼写错误..:(但分析某些nlp库仍然是错误的?@Kris我肯定会选择其他nlp解决方案,但首先我想只使用WordNet完成我的工作,因为它已经在同一个项目中使用。是的,我只是在使用这个人在下面链接中编写的实现。)。。