[英]How do I do dependency parsing in NLTK?
我們可以使用NLTK的Stanford Parser。
您需要從他們的網站下載兩件事:
確保您的語言模型版本與您的Stanford CoreNLP解析器版本匹配!
截至2018年5月22日,當前的CoreNLP版本為3.9.1。
下載兩個文件后,將zip文件解壓縮到您喜歡的任何位置。
接下來,加載模型並通過NLTK使用它
from nltk.parse.stanford import StanfordDependencyParser
path_to_jar = 'path_to/stanford-parser-full-2014-08-27/stanford-parser.jar'
path_to_models_jar = 'path_to/stanford-parser-full-2014-08-27/stanford-parser-3.4.1-models.jar'
dependency_parser = StanfordDependencyParser(path_to_jar=path_to_jar, path_to_models_jar=path_to_models_jar)
result = dependency_parser.raw_parse('I shot an elephant in my sleep')
dep = result.next()
list(dep.triples())
最后一行的輸出是:
[((u'shot', u'VBD'), u'nsubj', (u'I', u'PRP')),
((u'shot', u'VBD'), u'dobj', (u'elephant', u'NN')),
((u'elephant', u'NN'), u'det', (u'an', u'DT')),
((u'shot', u'VBD'), u'prep', (u'in', u'IN')),
((u'in', u'IN'), u'pobj', (u'sleep', u'NN')),
((u'sleep', u'NN'), u'poss', (u'my', u'PRP$'))]
我想這就是你想要的。
如果您需要更好的性能,那么spacy( https://spacy.io/ )是最佳選擇。 用法很簡單:
import spacy
nlp = spacy.load('en')
sents = nlp(u'A woman is walking through the door.')
您將獲得一個依賴關系樹作為輸出,並且可以非常輕松地挖掘出所需的每條信息。 您還可以定義自己的自定義管道。 在他們的網站上查看更多。
我認為您可以使用基於語料庫的依賴解析器,而不是NLTK提供的基於語法的解析器。
在Python中,即使對少量文本進行基於語料庫的依存關系解析也不是理想的性能。 因此,在NLTK中,它們確實為MaltParser (基於語料庫的依賴項解析器)提供了包裝 。
您可能會發現有關句子的RDF表示的其他問題。
如果您想認真對待依賴關系解析,請不要使用NLTK,因為所有算法都是過時且緩慢的。 嘗試這樣的事情: https : //spacy.io/
要使用NLTK的Stanford Parser
1)在本地主機上運行CoreNLP Server
在此處下載Stanford CoreNLP (以及適用於您的語言的模型文件)。 可以通過運行以下命令來啟動服務器( 此處有更多詳細信息)
# Run the server using all jars in the current directory (e.g., the CoreNLP home directory)
java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000
或通過NLTK API(需要先配置CORENLP_HOME
環境變量)
os.environ["CORENLP_HOME"] = "dir"
client = corenlp.CoreNLPClient()
# do something
client.stop()
2)從NLTK調用依賴性解析器
>>> from nltk.parse.corenlp import CoreNLPDependencyParser
>>> dep_parser = CoreNLPDependencyParser(url='http://localhost:9000')
>>> parse, = dep_parser.raw_parse(
... 'The quick brown fox jumps over the lazy dog.'
... )
>>> print(parse.to_conll(4))
The DT 4 det
quick JJ 4 amod
brown JJ 4 amod
fox NN 5 nsubj
jumps VBZ 0 ROOT
over IN 9 case
the DT 9 det
lazy JJ 9 amod
dog NN 5 nmod
. . 5 punct
請參閱此處的詳細文檔 ,以及該問題。NLTK CoreNLPDependencyParser:無法建立連接 。
從斯坦福解析器文檔中:“可以使用解析器包中提供的EnglishGrammaticalStructure類,使用我們的軟件在短語結構樹上獲得依賴關系。 http://nlp.stanford.edu/software/stanford-dependencies.shtml
依賴關系手冊還提到:“或者我們的轉換工具可以將其他選區解析器的輸出轉換為斯坦福依賴關系表示形式。” http://nlp.stanford.edu/software/dependencies_manual.pdf
目前,NLTK中似乎都未實現任何功能。
晚會晚了一點,但我想在SpaCy中添加一些示例代碼,以獲取所需的輸出:
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("I shot an elephant in my sleep")
for token in doc:
print("{2}({3}-{6}, {0}-{5})".format(token.text, token.tag_, token.dep_, token.head.text, token.head.tag_, token.i+1, token.head.i+1))
這是輸出,非常類似於您想要的輸出:
nsubj(shot-2, I-1)
ROOT(shot-2, shot-2)
det(elephant-4, an-3)
dobj(shot-2, elephant-4)
prep(shot-2, in-5)
poss(sleep-7, my-6)
pobj(in-5, sleep-7)
希望有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.