Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

python 3.5 - cannot use pycorenlp for python3.5 through terminal

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...