我要做的就是找到任何给定字符串的情绪(正/负/中性)。在研究中,我遇到了斯坦福大学NLP。但是可悲的是它在Java中。关于如何使它适用于python的任何想法?
py-corenlp
py-corenlp
当前(2018-10-23)的最新版本是3.9.2:
Stanford CoreNLP
如果没有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
,则可能有wget
:
wget
如果其他所有方法均失败,请使用浏览器;-)
curl
curl
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
注意:
unzip stanford-corenlp-full-2018-10-05.zip
mv stanford-english-corenlp-2018-10-05-models.jar stanford-corenlp-full-2018-10-05
以毫秒为单位,我将其设置为10秒以上。如果将大量Blob传递给服务器,则应增加它。cd stanford-corenlp-full-2018-10-05
java -mx5g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -timeout 10000
列出它们。timeout
应该分配足够的more options,但是YMMV,并且如果您的盒子功率不足,则可能需要修改该选项。--help
(另请参阅-mx5g
。
memory
您将获得:
pip install pycorenlp
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"]))
可用于估计整个文本的情绪。0: 'I love you .': 3 Positive
1: 'I hate him .': 1 Negative
2: 'You are nice .': 3 Positive
3: 'He is dumb': 1 Negative
(2)和mean(1)之间,范围从sentimentValue
(0)到Neutral
(4),这似乎很少见。Negative
来VeryNegative
。 VeryPositive
是默认端口,您可以在启动服务器时使用stop the server选项对其进行更改。kill $(lsof -ti tcp:9000)
(以毫秒为单位)。9000
只是one注释者,有-port
,您可以请求多个,并用逗号分隔它们:timeout
。sentiment
)。PS。我不敢相信我添加了一个[[9th答案,但是我想我必须这样做,因为现有的答案都没有帮助我(以前的8个答案中的一些已被删除,另一些已经转换为注释了) )。
'annotators': 'sentiment,lemma'
。 要安装,您可以使用画中画:
the result is different depending on whether you mention David or Bill
要执行基本任务,您可以将原生Python界面与Python packaged结合使用:
编辑:到目前为止,库
不支持情绪分析,但我没有删除答案,因为它直接回答了问题的“ Stanford nlp for python”部分。
pip install stanfordnlp
是用many NLP algorithms编写的用于情感分析的出色软件包。您可以拥有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()
。通过检查单词及其对应的情感评分(情感),可以对任何给定的句子进行情感分析。您可以从开始Textblob
第一个pip install命令将为您提供通过Python
以来在(docs here)系统中安装的textblob的最新版本。接下来,将下载所需的所有数据,即$ pip install -U textblob $ python -m textblob.download_corpora
。
这里是我的脚本之一,您可以下载jar并运行它。
virtualenv
-U will upgrade the pip package its latest available version
的解决方案使用corpus
。>stanford_corenlp_py此存储库提供了一个Python界面,用于调用斯坦福大学CoreNLP Java软件包(自3.5.1版开始)的“情感”和“实体”注释器。它使用py4j与JVM进行交互;同样,为了运行脚本scripts / runGateway.py之类的脚本,必须首先编译并运行Java类,以创建JVM网关。