Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Unicode string encoding issues for python 2/3. #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions pycorenlp/corenlp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json, requests
import json, requests, sys

class StanfordCoreNLP:

Expand All @@ -8,7 +8,7 @@ def __init__(self, server_url):
self.server_url = server_url

def annotate(self, text, properties=None):
assert isinstance(text, str)
assert isinstance(text, str), "text parameter is not 'str'"
if properties is None:
properties = {}
else:
Expand All @@ -22,11 +22,14 @@ def annotate(self, text, properties=None):
'$ cd stanford-corenlp-full-2015-12-09/ \n'
'$ java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer')

data = text.encode()
# ensure proper encoding of python 3 strings
if sys.version_info.major >= 3:
text = text.encode('utf-8')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not text.encode('utf-8') for python 2?


r = requests.post(
self.server_url, params={
'properties': str(properties)
}, data=data, headers={'Connection': 'close'})
}, data=text, headers={'Connection': 'close'})
output = r.text
if ('outputFormat' in properties
and properties['outputFormat'] == 'json'):
Expand Down