[英]How to make system call in python and store the output in a given output directory?
我正在使用Stanford CoreNLP,現在正在通過從命令行使用以下命令來運行coreNLP工具包:
java -cp stanford-corenlp-2012-07-09.jar:stanford-corenlp-2012-07-06-models.jar:xom.jar:
joda-time.jar -Xmx3g edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,
pos,lemma,ner -filelist file_list.txt -outputDirectory <OUTPUT DIRECTORY PATH>
這將生成帶有所需注釋的xml文件。 現在,我需要在python中的函數內使用此命令,以便將輸出存儲在output_dir中。 函數就像:
def preprocess(file_list.txt, ouptut_dir)
我閱讀了有關系統調用和使用子進程的信息,但我不太了解如何使用它,以便將輸出寫入給定的output_dir。
請幫忙!!!
我建議您使用Stanford Core NLP工具的Python接口,而不是通過子進程等調用它。
import subprocess
def preprocess(input_file, output_dir):
cmd = ["java", "-cp", "<the-whole-jar-mess>", "-Xmx3g",
"edu.stanford.nlp.pipeline.StanfordCoreNLP",
"-annotators", "tokenize,ssplit,pos,lemma,ner",
"-filelist", input_file, "-outputDirectory",
output_dir]
subprocess.check_call(cmd)
請注意,我在命令行中添加的縮寫不必將所有jar格式化為命令,您顯然需要將其替換為要傳遞的jar列表。
確實,這與subprocess
沒有太大關系,而是與如何從CLI使用Stanford CoreNLP無關。 假設-outputDirectory
標志告訴它輸出的存儲位置,只需傳遞正確的CLI參數即可。 這是一個命題:
import subprocess
def preprocess(fname, output_dir):
subprocess.check_call([
'java',
'-cp',
'stanford-corenlp-2012-07-09.jar:stanford-corenlp-2012-07-06-models.jar:xom.jar:joda-time.jar',
'-Xmx3g', 'edu.stanford.nlp.pipeline.StanfordCoreNLP'
'-annotators', 'tokenize,ssplit,pos,lemma,ner',
'-filelist', fname,
'-outputDirectory', output_dir
])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.