我知道如何在文本中获取单词的pos,但是我需要知道句子中单词的pos可能是什么,例如“ like”可以有4个词类:动词名词介词....是吗有可能从斯坦福图书馆得到它吗?
Stanford CoreNLP似乎没有与WordNet的接口,但是使用其他小型Java WordNet库之一来做到这一点很容易。对于此示例,我使用了JWI 2.3.3。
除了JWI,您还需要下载WordNet数据库的副本。例如,您可以从Princeton下载WordNet-3.0.tar.gz。解开字典。
以下代码包含一个函数,该函数返回一个单词的可能词性列表:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import edu.mit.jwi.Dictionary;
import edu.mit.jwi.item.POS;
import edu.mit.jwi.item.IIndexWord;
import edu.mit.jwi.morph.WordnetStemmer;
public class WNDemo {
/**
* Given a dictionary and a word, find all the parts of speech the
* word can be.
*/
public static Collection getPartsOfSpeech(Dictionary dict, String word) {
ArrayList<POS> parts = new ArrayList<POS>();
WordnetStemmer stemmer = new WordnetStemmer(dict);
// Check every part of speech.
for (POS pos : POS.values()) {
// Check every stem, because WordNet doesn't have every surface
// form in its database.
for (String stem : stemmer.findStems(word, pos)) {
IIndexWord iw = dict.getIndexWord(stem, pos);
if (iw != null) {
parts.add(pos);
}
}
}
return parts;
}
public static void main(String[] args) {
try {
Dictionary dict = new Dictionary(new File("WordNet-3.0/dict"));
dict.open();
System.out.println("'like' is a " + getPartsOfSpeech(dict, "like"));
} catch (IOException e) {
System.err.println("Error: " + e);
}
}
}
并输出:
'like' is a [noun, verb, adjective]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句