首页 > 解决方案 > 在 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")

有没有更简单/更快的方法?或者是否有类似的库可以提供相同的输出?我特别需要它的德语。谢谢您的帮助 :)

标签: pythoncommand-linenlp

解决方案


如果像这样运行它会快很多:

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 秒。


推荐阅读