【发布时间】:2021-06-14 12:12:28
【问题描述】:
我正在使用 NLTK 的 StanfordDependencyParser 来生成依赖树。这是代码
cpath = "path to stanford-corenlp-4.2.0-models-english.jar" + os.pathsep + "path to stanford-parser.jar"
if cpath not in os.environ['CLASSPATH']:
os.environ['CLASSPATH'] = cpath + os.pathsep + os.environ['CLASSPATH']
# TODO: DEPRECATED
# self.dependency_parser_instance_corenlp = StanfordDependencyParser(path_to_models_jar="path to stanford-corenlp-4.2.0-models-english.jar", encoding='utf8')
dependencies = [list(parse.triples()) for parse in self.dependency_parser_instance_corenlp.raw_parse(query)]
# Encode every string in tree to utf8 so string matching will work
for dependency in dependencies[0]:
dependency[0][0].encode('utf-8')
dependency[0][1].encode('utf-8')
dependency[1].encode('utf-8')
dependency[2][0].encode('utf-8')
dependency[2][1].encode('utf-8')
对于 10 个单词的句子,生成输出大约需要 1.5 秒。这是预期的吗?你能请一些提高速度的步骤吗? 我已经尝试过使用 SR 解析器并从模型 JAR 中删除所有额外的文件夹(如 coref、lexparser、ner、tagger)。
【问题讨论】:
标签: python nltk stanford-nlp