2017-03-15 35 views
1

我是NLP和Python中的新手。我試圖通過使用Tregex工具和Python子流程庫從StanfordCoreNLP的分析樹中提取名詞短語的子集。特別是,我試圖找到並提取符合以下模式的名詞短語:'(NP [$ VP]> S)|(NP [$ VP]> S \ n)|(NP \ n [$ VP] > S)|(NP \ n [$ VP]> S \ n)'在Tregex語法中。在Python中使用Stanford Tregex

例如,下面是原文,保存在名爲「文本」的字符串:

text = ('Pusheen and Smitha walked along the beach. "I want to surf", said Smitha, the CEO of Tesla. However, she fell off the surfboard') 

使用Python包裝運行StanfordCoreNLP解析器後,我得到了3句以下3種樹木:

output1['sentences'][0]['parse'] 

Out[58]: '(ROOT\n (S\n (NP (NNP Pusheen)\n  (CC and)\n  (NNP Smitha))\n (VP (VBD walked)\n  (PP (IN along)\n  (NP (DT the) (NN beach))))\n (. .)))' 

output1['sentences'][1]['parse'] 

Out[59]: "(ROOT\n (SINV (`` ``)\n (S\n  (NP (PRP I))\n  (VP (VBP want)\n  (PP (TO to)\n   (NP (NN surf) ('' '')))))\n (, ,)\n (VP (VBD said))\n (NP\n  (NP (NNP Smitha))\n  (, ,)\n  (NP\n  (NP (DT the) (NNP CEO))\n  (PP (IN of)\n   (NP (NNP Tesla)))))\n (. .)))" 

output1['sentences'][2]['parse'] 

Out[60]: '(ROOT\n (S\n (ADVP (RB However))\n (, ,)\n (NP (PRP she))\n (VP (VBD fell)\n  (PRT (RP off))\n  (NP (DT the) (NN surfboard)))))' 

我想提取下列3個名詞短語(每個句子)並將其保存爲在Python變量(或令牌的列表):

  • (NP(NNP Pusheen)\ n(CC和)\ n(NNP Smitha))
  • (NP(PRP I))
  • (NP(PRP她))

爲了您的信息中,我使用tregex從命令行用下面的代碼:

cd stanford-tregex-2016-10-31 
java -cp 'stanford-tregex.jar:' edu.stanford.nlp.trees.tregex.TregexPattern -f -s '(NP[$VP]>S)|(NP[$VP]>S\n)|(NP\n[$VP]>S)|(NP\n[$VP]>S\n)' /Users/AS/stanford-tregex-2016-10-31/exampletree.txt 

輸出爲:

Pattern string: 
(NP[$VP]>S)|(NP[$VP]>S\n)|(NP\n[$VP]>S)|(NP\n[$VP]>S\n) 
Parsed representation: 
or 
    Root NP 
     and 
     $ VP 
     > S 
    Root NP 
     and 
     $ VP 
     > S\n 
    Root NP\n 
     and 
     $ VP 
     > S 
    Root NP\n 
     and 
     $ VP 
     > S\n 
Reading trees from file(s) file path 
\# /Users/AS/stanford-tregex-2016-10-31/exampletree.txt 
(NP (NNP Pusheen) \n (CC and) \n (NNP Smitha)) 
\# /Users/AS/stanford-tregex-2016-10-31/exampletree.txt 
(NP\n (NP (NNP Smitha)) \n (, ,) \n (NP\n (NP (DT the) (NN spokesperson)) \n (PP (IN of) \n (NP (DT the) (NNP CIA)))) \n (, ,)) 
\# /Users/AS/stanford-tregex-2016-10-31/exampletree.txt 
(NP (PRP They)) 
There were 3 matches in total. 

如何在Python中複製此結果?

供您參考,我發現通過谷歌下面的文章,這是有關我的問題,但過時的(https://mailman.stanford.edu/pipermail/parser-user/2010-July/000606.html):

[解析用戶]變量輸入到Tregex

克里斯托弗·曼寧曼寧在斯坦福.edu Wed Jul 7 17:41:32 PDT 2010 Hi Haiyang,

對不起,緩慢的回覆,在學年結束的時候事情太忙了。

在2010年6月1日,8:56 PM,海陽AI寫道:

親愛的,

我希望這是尋求幫助的地方了。

是,雖然我們只能給出任何具體的Python非常有限的幫助.....

但是,這似乎是簡單的(我認爲)。

如果你想要的是模式在stdin中輸入的樹上運行,則需要在「NP」之前的參數列表中添加「-filter」標誌。

如果模式後沒有指定文件,並且沒有給出「-filter」標誌,那麼它會在固定的默認句子上運行該模式....

Chris。

我正在研究與Tregex相關的項目。我試圖從python調用Tregex,但我不知道如何將數據提供給Tregex,而不是來自傳統文件,而是來自變量。例如,我試圖從一個給定的變量(例如文本,已經被解析樹,使用斯坦福分析器),用下面的代碼計數的「NP」的號碼,

高清tregex(文本):
tregex_dir =「/ root/nlp/stanford-tregex-2009-08-30 /」 op = Popen([「java」,「-mx900m」,「-cp」,「stanford-tregex.jar:」,「edu。 stanford.nlp.trees.tregex.TregexPattern」, 「NP」], CWD = tregex_dir, 標準輸出= PIPE, 標準輸入= PIPE, 標準錯誤= STDOUT) 解析度= op.communicate(輸入文本=)[0] return res

r結果如下所示。它沒有搜索變量的內容,但不知何故又回到「使用默認樹」。任何人都可以幫我一把嗎?我在這裏呆了很長時間。真的很感謝你的時間和幫助。 模式字符串: NP 解析後的表示:使用默認的樹 根NP (NP (NP(DT本)(NN酒)) (CC和) (NP(DT這些)(NNS蝸牛)))

(NP(DT此)(NN酒))

(NP(DT這些)(NNS蝸牛))

有總共3個匹配。

- Haiyang AI,Ph.D.應用語言學的學生 部 賓夕法尼亞州立大學


分析器用戶郵件列表 分析器用戶在lists.stanford.edu https://mailman.stanford.edu/mailman/listinfo/parser-user

回答

1

爲什麼不使用斯坦福CoreNLP服務器!

1.)啓動服務器!

java -Xmx4g edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 - timeout 15000 

2.)創建一個python請求!

import requests 

url = "http://localhost:9000/tregex" 
request_params = {"pattern": "(NP[$VP]>S)|(NP[$VP]>S\\n)|(NP\\n[$VP]>S)|(NP\\n[$VP]>S\\n)"} 
text = "Pusheen and Smitha walked along the beach." 
r = requests.post(url, data=text, params=request_params) 
print r.json() 

3.)以下是結果!

{u'sentences': [{u'0': {u'namedNodes': [], u'match': u'(NP (NNP Pusheen)\n (CC and)\n (NNP Smitha))\n'}}]} 
+0

非常感謝您的及時回覆!不幸的是,我得到了響應'InvalidSchema:在輸入'r = requests.post(url,data = text,params = request_params)'後沒有找到'localhost:9000/tregex'的連接適配器。你有什麼想法,爲什麼?我還必須使用代碼'java -mx4g -cp「*」edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000'來啓動服務器,因爲1)的代碼不起作用。僅供參考,我在Mac Yosemite上使用Python 3.5(10.10)。 – Andrea

+0

'r =請求。post(url,data = text,params = request_params)' Traceback(最近一次調用最後一次): 'File「」,第1行, r = requests.post(url ,data = text,params = request_params) 'File「/Users/anaconda/lib/python3.5/site-packages/requests/api.py」,第110行,後 return request('post', url,data = data,json = json,** kwargs)' 'File「/Users/anaconda/lib/python3.5/site-packages/requests/api.py」,第56行,請求 return session .request(method = method,url = url,** kwargs)' – Andrea

+0

'File「/Users/anaconda/lib/python3.5/site-packages/requests/sessions.py」,第488行,請求 resp = self.send(prep,** send_kwargs)' 'File「/Users/anaconda/lib/python3.5/site-packages/requests/sessions.py」,第603行,發送 adapter = self.get_adapter(url = request.url)' 'File「 /Users/anaconda/lib/python3.5/site-packages/requests/sessions.py「,第685行,在get_adapter raise InvalidSchema(」沒有找到'%s'的連接適配器「%url)' ' InvalidSchema:找不到'localhost:9000/tregex''的連接適配器。 – Andrea

相關問題