简体   繁体   中英

How to make system call in python and store the output in a given output directory?

I was working with Stanford CoreNLP, right now I'm running the coreNLP toolkit by using the following command from command-line:

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>

This generates xml files with the required annotation. Now I need to using this command inside a function in python such that it stores the output in the output_dir. The function is like:

def preprocess(file_list.txt, ouptut_dir)

I read about system calls, and using subprocess, but I didn't quite understand how to use it such that it writes the output to the given output_dir.

Please help!!!

我建议您使用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)

Note the abbrevation I added to the commandline to not have to format all the jars into the command, you need to obviously replace that with the jar-list you are passing.

That really does not have much to do with subprocess , but rather on how Stanford CoreNLP is used from the CLI. Assuming that the -outputDirectory flag tells it where to store it's output, it's a simple matter of passing the correct CLI argument. Here is one proposition:

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
    ])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM