【发布时间】:2019-07-05 06:15:32
【问题描述】:
尝试使用 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;
}
}
}
在大多数情况下都很好,但您可以看到使用 plural 和一些 ing 形式失败。我是否可以在不破坏英语语言规则的情况下避免使用复数和ing形式?
如果您看到,在 WordNet 浏览器中,changes 是一个有效的词,但在 Java API 中是无效的。
不知道哪里需要改正!或者有什么其他好的方法可以解决这个问题?
【问题讨论】:
-
isCorrect("analying")返回 false 似乎完全正确,因为据我所知,analying在这里不是正确的词。analyzing会。 -
嘿@Ben,我的错!我已经纠正了自己的拼写错误.. :( 但对于 analyzing 来说仍然是错误的
-
尝试一些 nlp 库?
-
@Kris 当然我会选择其他 NLP 解决方案,但首先我想只使用 WordNet 完成我的工作,因为它已经在同一个项目中使用了。
标签: java spell-checking wordnet jwnl