Corenlp.py不加载任何模块

时间:2019-02-26 05:10:19

标签: python-2.7 nlp stanford-nlp

  

Corenlp.py不加载任何模块。

下面给出了我的代码,当我运行这些代码时,出现了超时错误,而未加载模型。我该如何解决?如果不完成命令,则代码将超时。

class StanfordCoreNLP(object):
    """
    Command-line interaction with Stanford's CoreNLP java utilities.
    Can be run as a JSON-RPC server or imported as a module.
    """
    def __init__(self, corenlp_path=None):
        """
        Checks the location of the jar files.
        Spawns the server as a process.
        """
        jars = ["stanford-corenlp-3.9.2.jar",
                "stanford-corenlp-3.9.2-models.jar",
                "joda-time.jar",
                "xom.jar",
                "jollyday.jar"]

        # if CoreNLP libraries are in a different directory,
        # change the corenlp_path variable to point to them
        if not corenlp_path:
            corenlp_path = "C:/Python27/stanford-corenlp-full-2018-10-05/"

        java_path = "java"
        classname = "edu.stanford.nlp.pipeline.StanfordCoreNLP"
        # include the properties file, so you can change defaults
        # but any changes in output format will break parse_parser_results()
        props = "-props default.properties" 

        # add and check classpaths
        jars = [corenlp_path + jar for jar in jars]
        for jar in jars:
            if not os.path.exists(jar):
                logger.error("Error! Cannot locate %s" % jar)
                sys.exit(1)

        # spawn the server
        start_corenlp = "%s -Xmx1800m -cp %s %s %s" % (java_path, ':'.join(jars), classname, props)
        if VERBOSE:
            logger.debug(start_corenlp)
        self.corenlp = pexpect.popen_spawn.PopenSpawn(start_corenlp)#popen_spawn.PopenS
        #self.corenlp.expect(pexpect.EOF, timeout=None)
        # show progress bar while loading the models
        widgets = ['Loading Models: ', Fraction()]
        pbar = ProgressBar(widgets=widgets, maxval=5, force_update=True).start()
        #i = self.corenlp.expect(  pexpect.TIMEOUT, pexpect.EOF,  searchwindowsize=-1, async=False)

        self.corenlp.expect("done.", timeout=20) # Load pos tagger model (~5sec)
        pbar.update(1)
        self.corenlp.expect("done.", timeout=200) # Load NER-all classifier (~33sec)
        pbar.update(2)
        self.corenlp.expect("done.", timeout=600) # Load NER-muc classifier (~60sec)
        pbar.update(3)
        self.corenlp.expect("done.", timeout=600) # Load CoNLL classifier (~50sec)
        pbar.update(4)
        self.corenlp.expect("done.", timeout=200) # Loading PCFG (~3sec)
        pbar.update(5)
        self.corenlp.expect("Entering interactive shell.")
        pbar.finish()

##        if i == 1:
##            print i
##            self.corenlp.sendline('yes')
##        elif i == 0:
##            print i
##            print "Timeout"
##            
##        elif i == 2:
##            print "EOF"
##        print(self.corenlp.before)
        print i
    def _parse(self, text):
        """
        This is the core interaction with the parser.

        It returns a Python data-structure, while the parse()
        function returns a JSON object
        """
        # clean up anything leftover
        print self, text
        while True:
            try:
                self.corenlp.read_nonblocking (4000, 0.3)
            except pexpect.TIMEOUT:
                break

        self.corenlp.sendline(text)

        # How much time should we give the parser to parse it?
        # the idea here is that you increase the timeout as a 
        # function of the text's length.
        # anything longer than 5 seconds requires that you also
        # increase timeout=5 in jsonrpc.py
        print "length",len(text)
        max_expected_time = min(40, 3 + len(text) / 20.0)
        print max_expected_time ,type(max_expected_time )
        end_time = float(time.time()) + float(max_expected_time)

        incoming = ""
        while True:
            # Time left, read more data
            try:
                incoming += self.corenlp.read_nonblocking(2000, 1)
                print incoming
                if "\nNLP>" in incoming: 
                    break
                time.sleep(0.0001)
                print time
            except pexpect.TIMEOUT:
                if (float(end_time) - float(time.time())) < 0:
                    print end_time - time.time(),end_time , time.time()
                    logger.error("Error: Timeout with input '%s'" % (incoming))
                    print logger
                    return {'error': "timed out after %f seconds" % max_expected_time}
                else:
                    continue
            except pexpect.EOF:
                print pexpect
                break

        if VERBOSE: 
            logger.debug("%s\n%s" % ('='*40, incoming))
        try:
            results = parse_parser_results(incoming)
        except Exception, e:
            if VERBOSE: 
                logger.debug(traceback.format_exc())
            raise e

        return results

错误:  载入模型:0/5

Traceback (most recent call last):
  File "D:\fahma\corefernce resolution\stanford-corenlp-python-master\corenlp.py", line 281, in <module>
    nlp = StanfordCoreNLP()
  File "D:\fahma\corefernce resolution\stanford-corenlp-python-master\corenlp.py", line 173, in __init__
    self.corenlp.expect("done.", timeout=20) # Load pos tagger model (~5sec)
  File "C:\Python27\lib\site-packages\pexpect\spawnbase.py", line 341, in expect
    timeout, searchwindowsize, async_)
  File "C:\Python27\lib\site-packages\pexpect\spawnbase.py", line 369, in expect_list
    return exp.expect_loop(timeout)
  File "C:\Python27\lib\site-packages\pexpect\expect.py", line 117, in expect_loop
    return self.eof(e)
  File "C:\Python27\lib\site-packages\pexpect\expect.py", line 63, in eof
    raise EOF(msg)
EOF: End Of File (EOF).
<pexpect.popen_spawn.PopenSpawn object at 0x021863B0>
searcher: searcher_re:
    0: re.compile('done.')

0 个答案:

没有答案