我如何从process.popen附加数据

时间:2018-06-30 15:14:12

标签: java python stanford-nlp

我正在尝试运行下一个代码:

import os
import subprocess
import sys

p = subprocess.Popen(['java', '-mx2g', '-cp', '''*''', 'edu.stanford.nlp.scenegraph.RuleBasedParser', ], stdin=subprocess.PIPE,stdout=subprocess.PIPE)
out, err = p.communicate('the brown cat chased the white fox\n')

哪一个使用斯坦福大学的核心NLP,一旦subprocess.Popen()行运行该算法,就等待我通过使用communication()发送的输入,我希望将输出输出到变量our中,但结果是虽然观察终端时没有看到正确的输出:

Processing from stdin. Enter one sentence per line.
> [main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator tokenize
[main] INFO edu.stanford.nlp.pipeline.TokenizerAnnotator - TokenizerAnnotator: No tokenizer type provided. Defaulting to PTBTokenizer.
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator ssplit
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator parse
[main] INFO edu.stanford.nlp.parser.common.ParserGrammar - Loading parser from serialized file edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz ... 
done [3.3 sec].
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator lemma
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator ner
Loading classifier from edu/stanford/nlp/models/ner/english.all.3class.distsim.crf.ser.gz ... done [5.9 sec].
Loading classifier from edu/stanford/nlp/models/ner/english.muc.7class.distsim.crf.ser.gz ... done [2.0 sec].
Loading classifier from edu/stanford/nlp/models/ner/english.conll.4class.distsim.crf.ser.gz ... done [3.2 sec].
[main] INFO edu.stanford.nlp.time.JollyDayHolidays - Initializing JollyDayHoliday for SUTime from classpath edu/stanford/nlp/models/sutime/jollyday/Holidays_sutime.xml as sutime.binder.1.
Reading TokensRegex rules from edu/stanford/nlp/models/sutime/defs.sutime.txt
Jun 30, 2018 6:11:08 PM edu.stanford.nlp.ling.tokensregex.CoreMapExpressionExtractor appendRules
INFO: Read 83 rules
Reading TokensRegex rules from edu/stanford/nlp/models/sutime/english.sutime.txt
Jun 30, 2018 6:11:11 PM edu.stanford.nlp.ling.tokensregex.CoreMapExpressionExtractor appendRules
INFO: Read 267 rules
Reading TokensRegex rules from edu/stanford/nlp/models/sutime/english.holidays.sutime.txt
Jun 30, 2018 6:11:12 PM edu.stanford.nlp.ling.tokensregex.CoreMapExpressionExtractor appendRules
INFO: Read 25 rules
source              reln                target              
---                 ----                ---                 
cat-3               chase               fox-7               


Nodes               
---                 
cat-3               
  -brown               
fox-7               
  -white               

------------------------
> Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at edu.stanford.nlp.scenegraph.RuleBasedParser.main(RuleBasedParser.java:253)

1 个答案:

答案 0 :(得分:0)

似乎您从标准错误流中获得了此输出。

如果要捕获它,则应在stderr=subprocess.PIPE调用中添加Popen()参数,它将在您的err变量中可用。