Как добавить данные из process.popen - PullRequest
0 голосов
/ 30 июня 2018

Я пытаюсь запустить следующий код:

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

Который использует ядро ​​НЛП Стэнфорда, как только строка subprocess.Popen () запускает алгоритм, ожидающий ввода, который я отправляю, с помощью connect (), я ожидаю получить свой вывод в переменной 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 голосов
/ 30 июня 2018

Кажется, вы получили этот вывод из стандартной ошибки потока.

Если вы хотите захватить его, вы должны добавить аргумент stderr=subprocess.PIPE к вызову Popen(), и он будет доступен в вашей переменной err.

...