From 491bba9daf0d1d022167aab82a62b83124673f2c Mon Sep 17 00:00:00 2001 From: florencep Date: Fri, 28 Apr 2017 17:44:16 -0700 Subject: [PATCH 1/2] remove hardcoded encoding since the client lib (not like the API) requires the document encoding, we should not hardcoded UTF8 by default. Added code used in other sample applications. Tested --- language/cloud-client/v1beta2/snippets.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/language/cloud-client/v1beta2/snippets.py b/language/cloud-client/v1beta2/snippets.py index 2e6745d2c94..aaff155dfa4 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 @@ -29,6 +30,12 @@ from google.cloud.proto.language.v1beta2 import language_service_pb2 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.""" @@ -153,7 +160,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 +184,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)) From 305185fa79537308fb2b908e75ddad36639894dd Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Mon, 1 May 2017 09:20:38 -0700 Subject: [PATCH 2/2] Fix lint issues --- language/cloud-client/v1beta2/snippets.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/language/cloud-client/v1beta2/snippets.py b/language/cloud-client/v1beta2/snippets.py index aaff155dfa4..5e34daeb4d3 100644 --- a/language/cloud-client/v1beta2/snippets.py +++ b/language/cloud-client/v1beta2/snippets.py @@ -30,6 +30,7 @@ from google.cloud.proto.language.v1beta2 import language_service_pb2 import six + def get_native_encoding_type(): """Returns the encoding type that matches Python's native strings.""" if sys.maxunicode == 65535: @@ -37,6 +38,7 @@ def get_native_encoding_type(): else: return 'UTF32' + def sentiment_text(text): """Detects sentiment in the text.""" language_client = language.Client(api_version='v1beta2')