將Stanford Parser納入NLTK for Python
Stanford Parser是一個綜合性的自然語言解析工具,可以無縫整合到NLTK中廣泛流行的Python 自然語言處理工具包。以下是如何實現此目的的詳細指南:
Python實作:
import os from nltk.parse import stanford # Set environment variables to specify jar paths os.environ['STANFORD_PARSER'] = '/path/to/standford/jars' os.environ['STANFORD_MODELS'] = '/path/to/standford/jars' # Initialize the parser parser = stanford.StanfordParser(model_path="/location/of/englishPCFG.ser.gz") # Parse sentences sentences = parser.raw_parse_sents(("Hello, My name is Melroy.", "What is your name?")) print(sentences) # GUI visualization 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 (Oracle JDK 8) 以避免執行階段錯誤。
安裝過程:
在Python 中執行以下指令: import nltk; nltk.download()
以上是如何在 Python 中將斯坦福解析器與 NLTK 整合?的詳細內容。更多資訊請關注PHP中文網其他相關文章!