python - 在 Python 中运行 RFTagger
问题描述
我想在我的 Pyhton 代码中使用 RFTagger ( http://www.cis.uni-muenchen.de/~schmid/tools/RFTagger/ )。我让它工作的唯一方法是这样的:
file = open("RFTagger/temp.txt", "w")
file.write(text)
file.close()
test_tagged = check_output(["cmd/rftagger-german", "temp.txt"], cwd="RFTagger").decode("utf-8")
有没有更简单/更快的方法?或者是否有类似的库可以提供相同的输出?我特别需要它的德语。谢谢您的帮助 :)
解决方案
如果像这样运行它会快很多:
from subprocess import check_output, run
from nltk.tokenize import sent_tokenize, word_tokenize
#run this once
run(["make"], cwd="RFTagger/src")
#run this for every text (text is a string)
file = open("RFTagger/temp.txt", "w")
file.write("\n\n".join("\n".join(word_tokenize(sentence, language='german')) for sentence in sent_tokenize(text, language='german')))
file.close()
test_tagged = check_output(["src/rft-annotate", "lib/german.par", "temp.txt"], cwd="RFTagger").decode("utf-8").split("\n")
我可以将每个文本的运行时间从大约 40 秒减少到 1.5 秒。
推荐阅读
- java - 显示来自 SQLite 数据库 android studio 的用户名
- selenium - 无法确定带有嵌套 SVG 元素的 XPATH
- kotlin - Kotlin 地图地图中的括号表示法
- r - “ts”对象中的错误必须有一个或多个观察值
- c# - 如何拆分特定格式的字符串并将其分配给c#中的变量?
- mongodb - MongoDB 作为 docker 容器 - 如何从外部文件导入文档
- python - 在 Altair LayerChart 中指定绘图标题和构面标题
- java - 如何编码日文字符javamail
- azure-devops - 带有 AAD 绑定 AKS 集群的 azure devops 中的 Kubernetes 服务连接
- python - if 函数:如果列 A==1 AND 1 列 B 在列表 X 中且列 C 不为空,则为 1。否则,为 0