Я изучаю NLP и только что установил Stanford CoreNLP. Я использую Windows10 и установил Python3 с Anaconda3. Я также установил pycorenlp - 0.3.
Я запускаю CoreNLP, используя следующую команду в каталоге, где файлы были загружены и извлечены.
java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000
В моем ноутбуке 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:
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:
Как я могу это исправить?