有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java通过Stanford解析器提取所有名词、形容词和文本

我试图通过斯坦福语法分析器从给定的文本中提取所有的名词和形容词

我当前的尝试是在树对象的getChildrenAsList()中使用模式匹配来定位以下内容:

(NN paper), (NN algorithm), (NN information), ...      

并将它们保存在数组中

输入句子:

In this paper we present an algorithm that extracts semantic information from an arbitrary text.

结果-字符串:

[(S (PP (IN In) (NP (DT this) (NN paper))) (NP (PRP we)) (VP (VBP present) (NP (NP (DT an) (NN algorithm)) (SBAR (WHNP (WDT that)) (S (VP (VBD extracts) (NP (JJ semantic) (NN information)) (PP (IN from) (NP (DT an) (ADJP (JJ arbitrary)) (NN text)))))))) (. .))]

我尝试使用模式匹配,因为在Stanford解析器中找不到返回所有单词类(例如名词)的方法

是否有更好的方法提取这些单词类,或者解析器是否提供了特定的方法

public static void main(String[] args) {
    String str = "In this paper we present an algorithm that extracts semantic information from an arbitrary text.";
    LexicalizedParser lp = new LexicalizedParser("englishPCFG.ser.gz"); 
    Tree parseS = (Tree) lp.apply(str);
    System.out.println("tr.getChildrenAsList().toString()"+ parseS.getChildrenAsList().toString());
    }
}

共 (2) 个答案

  1. # 1 楼答案

    我相信您一定知道nltk(自然语言工具包) 只需安装这个python库以及maxent pos标记器,下面的代码就可以实现这一点。标记员已经在宾夕法尼亚大学接受过培训,因此标记没有什么不同。上面的代码不是,但我喜欢nltk,因此

        import nltk
        nouns=[]
        adj=[]
         #read the text into the variable "text"
        text = nltk.word_tokenize(text)
        tagged=nltk.pos_tag(text)
        for i in tagged:
          if i[1][0]=="N":
            nouns+=[i[0]]
          elif i[1][0]=="J":
            adj+=[i[0]]
    
  2. # 2 楼答案

    顺便说一句,如果你想要的只是词性,比如名词和动词,你应该只使用词性标记,比如斯坦福词性标记。它将运行几个数量级更快,至少同样准确

    但是您可以使用解析器来完成。您想要的方法是taggedYield(),它返回一个List<TaggedWord>。你有吗

    List<TaggedWord> taggedWords = (Tree) lp.apply(str);
    for (TaggedWord tw : taggedWords) {
      if (tw.tag().startsWith("N") || tw.tag().startsWith("J")) {
        System.out.printf("%s/%s%n", tw.word(), tw.tag());
      }
    }
    

    (这种方法很有帮助,因为在Penn treebank标记集中,所有且只有形容词和名词标记以J或N开头。您可以更一般地检查标记集中的成员身份。)

    另外,stanford nlp标签最适合stackoverflow上的stanford nlp工具