-
Notifications
You must be signed in to change notification settings - Fork 6.6k
remove hardcoded encoding #921
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
""" | ||
|
||
import argparse | ||
import sys | ||
|
||
from google.cloud import language | ||
from google.cloud.gapic.language.v1beta2 import enums | ||
|
@@ -30,6 +31,14 @@ | |
import six | ||
|
||
|
||
def get_native_encoding_type(): | ||
"""Returns the encoding type that matches Python's native strings.""" | ||
if sys.maxunicode == 65535: | ||
return 'UTF16' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why aren't you using the enumeration type? |
||
else: | ||
return 'UTF32' | ||
|
||
|
||
def sentiment_text(text): | ||
"""Detects sentiment in the text.""" | ||
language_client = language.Client(api_version='v1beta2') | ||
|
@@ -153,7 +162,7 @@ def entity_sentiment_text(text): | |
document.type = enums.Document.Type.PLAIN_TEXT | ||
|
||
result = language_client.analyze_entity_sentiment( | ||
document, enums.EncodingType.UTF8) | ||
document, get_native_encoding_type()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For better snippets, please update this to be an input to the function as opposed to something that will not appear in devsite. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @monattar Furthermore, what is broken and how does this fix it? |
||
|
||
for entity in result.entities: | ||
print('Mentions: ') | ||
|
@@ -177,7 +186,7 @@ def entity_sentiment_file(gcs_uri): | |
document.type = enums.Document.Type.PLAIN_TEXT | ||
|
||
result = language_client.analyze_entity_sentiment( | ||
document, enums.EncodingType.UTF8) | ||
document, get_native_encoding_type()) | ||
|
||
for entity in result.entities: | ||
print(u'Name: "{}"'.format(entity.name)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason UTF8 isn't here? Below, the input is explicitly encoded as UTF8.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is identified what Python natively does, regardless of any explicit encoding/decoding.