Stanford nlp for python py-corenlp

问题描述 投票:22回答:8

我要做的就是找到任何给定字符串的情绪(正/负/中性)。在研究中,我遇到了斯坦福大学NLP。但是可悲的是它在Java中。关于如何使它适用于python的任何想法?

python stanford-nlp sentiment-analysis
8个回答
53
投票

使用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

注意:

  1. 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传递给服务器,则应增加它。
  2. server,您可以用cd stanford-corenlp-full-2018-10-05 java -mx5g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -timeout 10000 列出它们。
  3. [timeout应该分配足够的more options,但是YMMV,并且如果您的盒子功率不足,则可能需要修改该选项。

安装python软件包

--help

(另请参阅-mx5g

使用它

memory

您将获得:

pip install pycorenlp

注意

  1. 您将整个文本传递到服务器,然后将其拆分为句子。它还将句子拆分为标记。
  2. 情感归因于每个句子,而不是全文。句子之间的the official list 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"])) 可用于估计整个文本的情绪。
  3. 句子的平均情感在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),这似乎很少见。
  4. 您可以通过在启动它的终端上键入Ctrl-C或使用shell命令NegativeVeryNegativeVeryPositive是默认端口,您可以在启动服务器时使用stop the server选项对其进行更改。
  5. 如果收到超时错误,则在服务器或客户端中增加kill $(lsof -ti tcp:9000)(以毫秒为单位)。
  6. 9000只是one注释者,有-port,您可以请求多个,并用逗号分隔它们:timeout
  7. [请注意,情感模型有点特质(例如sentiment)。

PS。我不敢相信我添加了一个[[9th答案,但是我想我必须这样做,因为现有的答案都没有帮助我(以前的8个答案中的一些已被删除,另一些已经转换为注释了) )。


5
投票
斯坦福大学NLP工具的本地Python实现

最近,斯坦福大学发布了一种新的many more,用于实现基于神经网络(NN)的算法,用于最重要的NLP任务:

    令牌化
  • 多字令牌(MWT)扩展
  • 词法化
  • 词性(POS)和形态特征标记
  • 依赖关系解析
  • 它在Python中实现,并使用PyTorch作为NN库。该软件包包含的准确模型超过'annotators': 'sentiment,lemma'

    要安装,您可以使用画中画:

    the result is different depending on whether you mention David or Bill

    要执行基本任务,您可以将原生Python界面与Python packaged结合使用:

    50 languages

    编辑:

    到目前为止,库

    不支持情绪分析,但我没有删除答案,因为它直接回答了问题的“ Stanford nlp for python”部分。


  • 1
    投票
    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
    

    0
    投票
    我也面临类似的情况。我的大多数项目都在Python中,情感部分是Java。幸运的是,学习如何使用斯坦福CoreNLP jar很容易。

    这里是我的脚本之一,您可以下载jar并运行它。

    virtualenv


    0
    投票
    我正面临着同样的问题:@roopalgarg指出,也许使用-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网关。


    0
    投票
    使用stanfordcore-nlp python库

    0
    投票
    我建议使用TextBlob库。一个示例实现如下所示:

    0
    投票
    在这个问题上有非常新的进展:
    © www.soinside.com 2019 - 2024. All rights reserved.