cannot use pycorenlp for python3.5 through terminal

梦想的初衷 提交于 2019-11-30 09:31:36

问题


My default python is 2.7, but have to do this project in python3.5

I installed pycorenlp through this command line: pip3 install pycorenlp.

And it is showing I have already installed it:

Requirement already satisfied (use --upgrade to upgrade): pycorenlp in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages Requirement already satisfied (use --upgrade to upgrade): requests in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (from pycorenlp)

However, when I type python3.5 through terminal to enter into python environment, then type from pycorenlp import StanfordCoreNLP, it is showing error:

ImportError: No module named pycorenlp

I have also tried the solutions here but for python3.5, such as using sudo, chmod, none of them worked.

Do you know how to solve this problem? I have to run the code through the terminal, and have to use pycorenlp


回答1:


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



来源:https://stackoverflow.com/questions/42896027/cannot-use-pycorenlp-for-python3-5-through-terminal

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