cannot use pycorenlp for python3.5 through terminal

大城市里の小女人 提交于 2019-11-29 15:57:12

You could try using the Stanford CoreNLP server if you want to access Stanford CoreNLP in Python. Download available here: http://stanfordnlp.github.io/CoreNLP/download.html

  1. Start the Java server. I'll provide the command here, but you could just as easily add a line of Python code that calls this command with subprocess and starts the server and gets back the process id.

    cd /path/to/stanford-corenlp-full-2016-10-31 ; java -Xmx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000 -annotators tokenize,ssplit,pos,lemma,ner,parse,mention,coref
    

Here is some info on the Java server: http://stanfordnlp.github.io/CoreNLP/corenlp-server.html

Note that is just an example command and you can provide any list of annotators you want.

Now the Java server will be running and you can issue calls to it while your Python program runs. Here is a basic example using the requests library.

  1. Make a basic call to the Stanford CoreNLP server:

    import requests
    
    url = 'http://localhost:9000/?'
    request_params = {'outputFormat': 'json'}
    text = "This is a test sentence."
    r = requests.post(url,data=text,params=request_params)
    print r.json()
    

You will get return JSON with the annotations.

  1. Shut down the server.

There is also a Python wrapper we use internally for accessing the server available in stanza I think the wrapper could work with Python 3, but I am not positive. If you have issues the Python code I provided should work fine with Python 3.

Here is the GitHub for stanza: https://github.com/stanfordnlp/stanza

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!