使用 Python 將史丹佛解析器整合到 NLTK
史丹佛解析器可以在 NLTK 中使用嗎?
是的,可以使用 Python 在 NLTK 框架內使用史丹佛解析器。以下Python 程式碼片段示範如何實現此目的:
import os from nltk.parse import stanford # Specify paths to Stanford Parser and models os.environ['STANFORD_PARSER'] = '/path/to/standford/jars' os.environ['STANFORD_MODELS'] = '/path/to/standford/jars' # Initialize the Stanford Parser parser = stanford.StanfordParser(model_path="/location/of/the/englishPCFG.ser.gz") # Parse a list of sample sentences sentences = parser.raw_parse_sents(("Hello, My name is Melroy.", "What is your name?")) print sentences # Visualize the dependency tree for line in sentences: for sentence in line: sentence.draw()
此範例展示了所提供句子的解析依賴樹:
[Tree('ROOT', [Tree('S', [Tree('INTJ', [Tree('UH', ['Hello'])]), Tree(',', [',']), Tree('NP', [Tree('PRP$', ['My']), Tree('NN', ['name'])]), Tree('VP', [Tree('VBZ', ['is']), Tree('ADJP', [Tree('JJ', ['Melroy'])])]), Tree('.', ['.'])])]), Tree('ROOT', [Tree('SBARQ', [Tree('WHNP', [Tree('WP', ['What'])]), Tree('SQ', [Tree('VBZ', ['is']), Tree('NP', [Tree('PRP$', ['your']), Tree('NN', ['name'])])]), Tree('.', ['?'])])])}
重點:
需要 Java JRE 1.8 (Java Development Kit 8)。
安裝說明:
import nltk nltk.download()
以上是如何在 Python 中將斯坦福解析器與 NLTK 整合?的詳細內容。更多資訊請關注PHP中文網其他相關文章!