简体   繁体   中英

Getting a pexpect EOF error when trying to run Stanford CoreNLP

I'm trying to run Stanford's CoreNLP using a Python wrapper. When I run the code I get the error message:

Traceback (most recent call last):                                                                                                                                                                                                               
  File "./corenlp.py", line 257, in <module>
    nlp = StanfordCoreNLP()
  File "./corenlp.py", line 176, in __init__
    self.corenlp.expect("done.", timeout=200) # Loading PCFG (~3sec)
  File "/home/user1/anaconda3/envs/user1conda/lib/python3.7/site-packages/pexpect/spawnbase.py", line 344, in expect
    timeout, searchwindowsize, async_)
  File "/home/user1/anaconda3/envs/user1conda/lib/python3.7/site-packages/pexpect/spawnbase.py", line 372, in expect_list
    return exp.expect_loop(timeout)
  File "/home/user1/anaconda3/envs/user1conda/lib/python3.7/site-packages/pexpect/expect.py", line 179, in expect_loop
    return self.eof(e)
  File "/home/user1/anaconda3/envs/user1conda/lib/python3.7/site-packages/pexpect/expect.py", line 122, in eof
    raise exc
pexpect.exceptions.EOF: End Of File (EOF). Exception style platform.
<pexpect.pty_spawn.spawn object at 0x7fde11758350>
command: /home/user1/anaconda3/envs/user1conda/bin/java
args: ['/home/user1/anaconda3/envs/user1conda/bin/java', '-Xmx1800m', '-cp', './stanford-corenlp-python/stanford-corenlp-full-2018-10-05/stanford-corenlp-3.9.2.jar:./stanford-corenlp-python/stanford-corenlp-full-2018-10-05/stanford-corenlp-3.9.2-models.jar:./stanford-corenlp-python/stanford-corenlp-full-2018-10-05/joda-time.jar:./stanford-corenlp-python/stanford-corenlp-full-2018-10-05/xom.jar:./stanford-corenlp-python/stanford-corenlp-full-2018-10-05/jollyday.jar', 'edu.stanford.nlp.pipeline.StanfordCoreNLP', '-props', 'default.properties']
buffer (last 100 chars): b''
before (last 100 chars): b'rdCoreNLP.java:188)\r\n\tat edu.stanford.nlp.pipeline.StanfordCoreNLP.main(StanfordCoreNLP.java:1388)\r\n'
after: <class 'pexpect.exceptions.EOF'>
match: None
match_index: None
exitstatus: None
flag_eof: True
pid: 28826
child_fd: 5
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
    0: re.compile(b'done.')

I've tried looking at some other answers here, but wasn't able to receive a solution to my problem. This question from two years ago is on the same lines of mine, but has no answers.

What might be some things I could try? Thanks.

As per the documentation :

Special EOF and TIMEOUT patterns

There are two special patterns to match the End Of File ( EOF ) or a Timeout condition ( TIMEOUT ). You can pass these patterns to expect() . These patterns are not regular expressions. Use them like predefined constants.

If the child has died and you have read all the child's output then ordinarily expect() will raise an EOF exception. You can read everything up to the EOF without generating an exception by using the EOF pattern expect .

Also, from the same documentation :

Exceptions

  • End Of File (EOF) in read(). Exception style platform.
  • End Of File (EOF) in read(). Empty string style platform.

Some UNIX platforms will throw an exception when you try to read from a file descriptor in the EOF state. Other UNIX platforms instead quietly return an empty string to indicate that the EOF state has been reached.

If you wish to read up to the end of the child's output without generating an EOF exception, then use the expect(pexpect.EOF) method.

this error arises simply either when

  • what you expect is not what your spawned process gave
  • or you consumed the current stream and there is no more character to match

the following has wrong expectation, so EOF error comes

import pexpect
child = pexpect.spawn('echo hello')
# or on windows
# from pexpect import popen_spawn
# child = pexpect.popen_spawn.PopenSpawn('echo hello')
child.expect("hey") # expected vs reality, and comes the EOF error

the following code consumes the stream then gets the error

import pexpect
child = pexpect.spawn('echo hello')
# or on windows
# from pexpect import popen_spawn
# child = pexpect.popen_spawn.PopenSpawn('echo hello')
child.expect("hello") # consume first worst
child.expect("")      # comsume nothing, can be multiple, won't affect
child.expect("\n")    # consume end of line
child.expect("\n")    # now there is no more char to consume, then comes error

if the current stream is ended, that means either the spawned process ended or it expects some input to continue, where you will proceed with a send/write operation.

there are also \r , \r\n and \n differences depending on the program spawned. The last two can both be matched with \n (new line) but if they use \r (carriage return) only then the error will arise. I heard Mac programs may also use \f (form feed) instead, so heads up.

By the way, pexpect allows you to automate what would you normally do manually by hand. So be sure to check the version of the application and note what exactly it prints to the screen, and what it expects in return from the user. Version differences are a pretty common source of errors.

PS: I admit I didn't check for what is this "Exception style platform" part of the error. What I would suggest is to first check the above 2 possibilities, then start panicking:)

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