在Python中使用Stanford CoreNLP进行情感分析 - python

我正在学习NLP,并且刚刚安装了Stanford CoreNLP。我使用Windows10,并且已将Python3与Anaconda3一起安装。我还安装了pycorenlp-0.3。

我在文件已下载和提取的目录中使用以下命令运行CoreNLP。

java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000

在Python中使用Stanford CoreNLP进行情感分析 - python

在我的Jupyter笔记本中,我运行以下在网络上找到的代码:

import json, requests

class StanfordCoreNLP:

        """
        Modified from https://github.com/smilli/py-corenlp (https://github.com/smilli/py-corenlp)
        """
        def __init__(self, server_url):
            # TODO: Error handling? More checking on the url?
            if server_url[-1] == '/':
                server_url = server_url[:-1]
            self.server_url = server_url

        def annotate(self, text, properties=None):
            assert isinstance(text, str)

            if properties is None:
                properties = {}
            else:
                assert isinstance(properties, dict)

            # Checks that the Stanford CoreNLP server is started.
            try:
                requests.get(self.server_url)
            except requests.exceptions.ConnectionError:
                raise Exception('Check whether you have started the CoreNLP server e.g.\n'
                                    '$ cd <path_to_core_nlp_folder>/stanford-corenlp-full-2016-10-31/ \n'
                    '$ java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port <port>' )

            data = text.encode()
            r = requests.post(
                self.server_url, params={
                        'properties': str(properties)
                }, data=data, headers={'Connection': 'close'})

            output = r.text

            if ('outputFormat' in properties
                and properties['outputFormat'] == 'json'):
                try:
                    output = json.loads(output, encoding='utf-8', strict=True)
                except:
                    pass
            return output

def sentiment_analysis_on_sentence(sentence):
            # The StanfordCoreNLP server is running on http://127.0.0.1:9000 (http://127.0.0.1:9000)
            nlp = StanfordCoreNLP('http://127.0.0.1:9000 (http://127.0.0.1:9000)')
                    # Json response of all the annotations
            output = nlp.annotate(sentence, properties={
                    "annotators": "tokenize,ssplit,parse,sentiment",
                    "outputFormat": "json",
                    # Only split the sentence at End Of Line. We assume that this method only takes in one single sentence.
                    "ssplit.eolonly": "true",
                    # Setting enforceRequirements to skip some annotators and make the process faster
                    "enforceRequirements": "false"
                    })
            # Only care about the result of the first sentence because we assume we only annotate a single sentence 

            return int(output['sentences'][0]['sentimentValue'])

但是,当我在Jupyter Notebook中运行时:

sentiment_analysis_on_sentence('I like the service.')

我有一个例外:

---------------------------------------------------------------------------
LocationParseError                        Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\requests\models.py in prepare_url(self, url, params)
    370         try:
--> 371             scheme, auth, host, port, path, query, fragment = parse_url(url)
    372         except LocationParseError as e:

C:\ProgramData\Anaconda3\lib\site-packages\urllib3\util\url.py in parse_url(url)
    198             if not port.isdigit():
--> 199                 raise LocationParseError(url)
    200             try:

LocationParseError: Failed to parse: 127.0.0.1:9000 (http:

During handling of the above exception, another exception occurred:

InvalidURL                                Traceback (most recent call last)
<ipython-input-142-e4763a0324a6> in <module>()
----> 1 sentiment_analysis_on_sentence('I like the service.')

<ipython-input-141-9cf27500efe3> in sentiment_analysis_on_sentence(sentence)
     54                     "ssplit.eolonly": "true",
     55                     # Setting enforceRequirements to skip some annotators and make the process faster
---> 56                     "enforceRequirements": "false"
     57                     })
     58             # Only care about the result of the first sentence because we assume we only annotate a single sentence

<ipython-input-141-9cf27500efe3> in annotate(self, text, properties)
     22             # Checks that the Stanford CoreNLP server is started.
     23             try:
---> 24                 requests.get(self.server_url)
     25             except requests.exceptions.ConnectionError:
     26                 raise Exception('Check whether you have started the CoreNLP server e.g.\n'

C:\ProgramData\Anaconda3\lib\site-packages\requests\api.py in get(url, params, **kwargs)
     70 
     71     kwargs.setdefault('allow_redirects', True)
---> 72     return request('get', url, params=params, **kwargs)
     73 
     74 

C:\ProgramData\Anaconda3\lib\site-packages\requests\api.py in request(method, url, **kwargs)
     56     # cases, and look like a memory leak in others.
     57     with sessions.Session() as session:
---> 58         return session.request(method=method, url=url, **kwargs)
     59 
     60 

C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    492             hooks=hooks,
    493         )
--> 494         prep = self.prepare_request(req)
    495 
    496         proxies = proxies or {}

C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py in prepare_request(self, request)
    435             auth=merge_setting(auth, self.auth),
    436             cookies=merged_cookies,
--> 437             hooks=merge_hooks(request.hooks, self.hooks),
    438         )
    439         return p

C:\ProgramData\Anaconda3\lib\site-packages\requests\models.py in prepare(self, method, url, headers, files, data, params, auth, cookies, hooks, json)
    303 
    304         self.prepare_method(method)
--> 305         self.prepare_url(url, params)
    306         self.prepare_headers(headers)
    307         self.prepare_cookies(cookies)

C:\ProgramData\Anaconda3\lib\site-packages\requests\models.py in prepare_url(self, url, params)
    371             scheme, auth, host, port, path, query, fragment = parse_url(url)
    372         except LocationParseError as e:
--> 373             raise InvalidURL(*e.args)
    374 
    375         if not scheme:

InvalidURL: Failed to parse: 127.0.0.1:9000 (http:

我该如何解决?

参考方案

在第48行

nlp = StanfordCoreNLP('http://127.0.0.1:9000 (http://127.0.0.1:9000)')

您应该在带有端口的第一个URL之后删除此(http://127.0.0.1:9000)

附加步骤:

正如您在命令行日志中看到的那样,Stanford NLP使用lexparser而不是ShiftReduce Parser。
这很好,因为情感分析和Shiftreduce解析器目前具有可以协同工作的探针。
为确保两者均正常工作,您应该添加

    "parse.model": "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz"

到调用属性(请参阅第50 f。行)。

对于德国用户:
如果Stanford CoreNLP服务器以错误的语言环境启动,并且收到无效的JSON文件,则最后可能仍然会出错。
然后,应将以下paramters添加到服务器启动中:

"-Duser.language=en -Duser.country=US Default"

您如何在列表内部调用一个字符串位置? - python

我一直在做迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。我试过放在列表内和列表外。maze = ["o","*","*","*","*","*",…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

字符串文字中的正斜杠表现异常 - python

为什么S1和S2在撇号位置方面表现不同?S1="1/282/03/10" S2="4/107/03/10" R1="".join({"N\'" ,S1,"\'" }) R2="".join({"N\'…