Skip to content

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

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 11 additions & 2 deletions language/cloud-client/v1beta2/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"""

import argparse
import sys

from google.cloud import language
from google.cloud.gapic.language.v1beta2 import enums
Expand All @@ -30,6 +31,14 @@
import six


def get_native_encoding_type():
"""Returns the encoding type that matches Python's native strings."""
Copy link
Contributor

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.

Copy link

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.

if sys.maxunicode == 65535:
return 'UTF16'
Copy link
Contributor

Choose a reason for hiding this comment

The 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')
Expand Down Expand Up @@ -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())
Copy link
Contributor

@gguuss gguuss May 2, 2017

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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: ')
Expand All @@ -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))
Expand Down