diff --git a/language/cloud-client/v1beta2/snippets.py b/language/cloud-client/v1beta2/snippets.py index 2e6745d2c94..5e34daeb4d3 100644 --- a/language/cloud-client/v1beta2/snippets.py +++ b/language/cloud-client/v1beta2/snippets.py @@ -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' + 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()) 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))