2011-05-18 86 views
3

我試圖通過斯坦福解析器從給定文本中提取所有名詞和形容詞。通過斯坦福解析器提取所有名詞,形容詞形式和文本

我的當前的嘗試在getChildrenAsList(使用模式匹配)的樹的對象定位像的東西:

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

,並將其保存在數組中。

輸入句子:

在本文中,我們提出,它可以提取任意文本語義信息的算法。

結果 - 字符串:

[(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)))))))) (. .))] 

我嘗試使用模式匹配,因爲我無法找到斯坦福解析器返回類似的名詞,例如所有詞類的方法。

有沒有更好的方法來提取這些詞類或解析器提供特定的方法?

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()); 
    } 
} 

回答

6

順便說一句,如果你想要的是像名詞和動詞詞性,你應該只使用語音惡搞,比如斯坦福POS惡搞的一部分。它會更快地運行幾個數量級,並且至少是一樣的準確。

但是你可以用解析器來完成。你想要的方法是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()); 
    } 
} 

(此方法削減角落,知道一切,只是形容詞和名詞的標籤中設置的賓州樹庫標記爲J或N開始,你可以更普遍地在一組標籤檢查會員)

ps使用標記stanford-nlp最適合用於stackoverflow上的Stanford NLP工具。

1

我相信你會知道nltk(自然語言工具包) 只是安裝這個python庫,還有maxent pos tagger以及下面的代碼應該做的伎倆。標記器已經在Penn上進行了培訓,因此標記沒有區別。上面的代碼不是,但我喜歡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]]