【发布时间】:2012-07-05 20:03:57
【问题描述】:
我有一个关于 WordNet 和MIT JWI(用于访问 WordNet 的 Java API)的非常简单的问题:我将一个文件读入一个字符串数组,然后将其拆分为单词。如何使用getPOS() 获取仅包含名词的单独字符串数组?谢谢!
我尝试过的示例:
公开课测试{
public static void main(String[] args) {
String sentence1 = "The cat ate the fish";
String[] s1Split = sentence1.split(" ");
String wnhome = "C:/Program Files/WordNet/2.1";
String path = wnhome + File.separator + "dict";
URL url = new URL("file", null , path);
IDictionary dict = new Dictionary(url);
dict.open();
for (int i = 0; i <s1.length; i++) {
//this is where I got confused, wanted to use something like:
//Word w = dict.getIndexWord(s1[i], ..) but I need a POS argument,
//and I can't find another suitable method
//if w.getPOS() is a noun I would add it to a separate vector
}
}
}
编辑:刚刚想到另一个 - 使用w = dict.getIndexWord(s1[i], POS.NOUN) 之类的东西是否可靠,如果名词不存在,w 将为空?这值得一试吗?
EDIT2:所以我的问题 atm 是否有任何方法可以将字符串(单词)转换为 Wordnet 对象,以便可以在其上使用 getPOS()?
【问题讨论】:
-
这看起来不对。我认为您需要使用 dict.open() 的结果,对吗?迭代句子中的字符有什么用?
-
也许你想要 sentence1.split(" ") ?
-
是的,对不起,编辑了代码,它是 String[] s1Split = sentence1.split(" ");
-
阅读 Javadoc - 一切尽在其中。如果没有找到则返回 null