90a*_*yss 22 python stanford-nlp sentiment-analysis
我想做的就是找到任何给定字符串的情绪(正/负/中性).在研究中,我遇到了斯坦福NLP.但遗憾的是它在Java中.关于如何让它适用于python的任何想法?
sds*_*sds 53
py-corenlp此时的最新版本(2018-10-23)为3.9.2:
wget https://nlp.stanford.edu/software/stanford-corenlp-full-2018-10-05.zip https://nlp.stanford.edu/software/stanford-english-corenlp-2018-10-05-models.jar
Run Code Online (Sandbox Code Playgroud)
curl https://nlp.stanford.edu/software/stanford-corenlp-full-2018-10-05.zip -O https://nlp.stanford.edu/software/stanford-english-corenlp-2018-10-05-models.jar -O
Run Code Online (Sandbox Code Playgroud)
如果一切都失败了,请使用浏览器;-)
unzip stanford-corenlp-full-2018-10-05.zip
mv stanford-english-corenlp-2018-10-05-models.jar stanford-corenlp-full-2018-10-05
Run Code Online (Sandbox Code Playgroud)
cd stanford-corenlp-full-2018-10-05
java -mx5g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -timeout 10000
Run Code Online (Sandbox Code Playgroud)
笔记:
timeout以毫秒为单位,我将其设置为10秒以上.如果将巨大的blob传递给服务器,则应该增加它.--help.-mx5g应该分配足够的内存,但YMMV,如果您的盒子功能不足,您可能需要修改该选项.pip install pycorenlp
Run Code Online (Sandbox Code Playgroud)
(另见官方名单).
from pycorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP('http://localhost:9000')
res = nlp.annotate("I love you. I hate him. You are nice. He is dumb",
properties={
'annotators': 'sentiment',
'outputFormat': 'json',
'timeout': 1000,
})
for s in res["sentences"]:
print("%d: '%s': %s %s" % (
s["index"],
" ".join([t["word"] for t in s["tokens"]]),
s["sentimentValue"], s["sentiment"]))
Run Code Online (Sandbox Code Playgroud)
你会得到:
0: 'I love you .': 3 Positive
1: 'I hate him .': 1 Negative
2: 'You are nice .': 3 Positive
3: 'He is dumb': 1 Negative
Run Code Online (Sandbox Code Playgroud)
sentimentValue可用于估计整个文本的情绪.Neutral(2)和Negative(1)之间,范围从VeryNegative(0)到VeryPositive(4),这似乎是非常罕见的.kill $(lsof -ti tcp:9000)9000-porttimeout如果出现超时错误,请增加(以毫秒为单位)服务器或客户端.sentiment只是一个注释器,还有更多,你可以请求几个,用逗号分隔它们:'annotators': 'sentiment,lemma'.PS.我不敢相信我添加了第9个答案,但是,我想,我必须这样做,因为现有的答案都没有帮助我(之前的8个答案中的一些现已被删除,其他一些答案已被转换为评论).
最近,斯坦福大学发布了一个新的Python程序包,该程序包实现了基于神经网络(NN)的算法,用于最重要的NLP任务:
它是用Python实现的,并使用PyTorch作为NN库。该软件包包含用于50多种语言的准确模型。
要安装,您可以使用PIP:
pip install stanfordnlp
Run Code Online (Sandbox Code Playgroud)
要执行基本任务,您可以将本机Python接口与许多NLP算法结合使用:
import stanfordnlp
stanfordnlp.download('en') # This downloads the English models for the neural pipeline
nlp = stanfordnlp.Pipeline() # This sets up a default neural pipeline in English
doc = nlp("Barack Obama was born in Hawaii. He was elected president in 2008.")
doc.sentences[0].print_dependencies()
Run Code Online (Sandbox Code Playgroud)
编辑:
到目前为止,该库尚不支持情绪分析,但我没有删除答案,因为它直接回答了问题的“ Stanford nlp for python”部分。
| 归档时间: |
|
| 查看次数: |
26415 次 |
| 最近记录: |