From b1a025aecdcfc6d7217dd926161bb1cfcf9c4193 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Mon, 6 Nov 2017 13:31:20 -0800 Subject: [PATCH 1/3] copy classify_test samples and tests to v1 --- language/cloud-client/v1/snippets.py | 53 +++++++++++++++++++++++ language/cloud-client/v1/snippets_test.py | 18 ++++++++ 2 files changed, 71 insertions(+) diff --git a/language/cloud-client/v1/snippets.py b/language/cloud-client/v1/snippets.py index e13fc7dd6c8..b1c070f4e54 100644 --- a/language/cloud-client/v1/snippets.py +++ b/language/cloud-client/v1/snippets.py @@ -254,12 +254,61 @@ def entity_sentiment_file(gcs_uri): print(u'Sentiment: {}\n'.format(entity.sentiment)) +# [START def_classify_text] +def classify_text(text): + """Classifies content categories of the provided text.""" + client = language.LanguageServiceClient() + + if isinstance(text, six.binary_type): + text = text.decode('utf-8') + + document = types.Document( + content=text.encode('utf-8'), + type=enums.Document.Type.PLAIN_TEXT) + + categories = client.classify_text(document).categories + + for category in categories: + print(u'=' * 20) + print(u'{:<16}: {}'.format('name', category.name)) + print(u'{:<16}: {}'.format('confidence', category.confidence)) +# [END def_classify_text] + + +# [START def_classify_file] +def classify_file(gcs_uri): + """Classifies content categories of the text in a Google Cloud Storage + file. + """ + client = language.LanguageServiceClient() + + document = types.Document( + gcs_content_uri=gcs_uri, + type=enums.Document.Type.PLAIN_TEXT) + + categories = client.classify_text(document).categories + + for category in categories: + print(u'=' * 20) + print(u'{:<16}: {}'.format('name', category.name)) + print(u'{:<16}: {}'.format('confidence', category.confidence)) +# [END def_classify_file] + + if __name__ == '__main__': parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) subparsers = parser.add_subparsers(dest='command') + classify_text_parser = subparsers.add_parser( + 'classify-text', help=classify_text.__doc__) + classify_text_parser.add_argument('text') + + classify_text_parser = subparsers.add_parser( + 'classify-file', help=classify_file.__doc__) + classify_text_parser.add_argument('gcs_uri') + sentiment_entities_text_parser = subparsers.add_parser( 'sentiment-entities-text', help=entity_sentiment_text.__doc__) sentiment_entities_text_parser.add_argument('text') @@ -310,3 +359,7 @@ def entity_sentiment_file(gcs_uri): entity_sentiment_text(args.text) elif args.command == 'sentiment-entities-file': entity_sentiment_file(args.gcs_uri) + elif args.command == 'classify-text': + classify_text(args.text) + elif args.command == 'classify-file': + classify_file(args.gcs_uri) diff --git a/language/cloud-client/v1/snippets_test.py b/language/cloud-client/v1/snippets_test.py index 168701dc666..27fbee24d92 100644 --- a/language/cloud-client/v1/snippets_test.py +++ b/language/cloud-client/v1/snippets_test.py @@ -19,6 +19,7 @@ BUCKET = os.environ['CLOUD_STORAGE_BUCKET'] TEST_FILE_URL = 'gs://{}/text.txt'.format(BUCKET) +LONG_TEST_FILE_URL = 'gs://{}/android_text.txt'.format(BUCKET) def test_sentiment_text(capsys): @@ -77,3 +78,20 @@ def test_sentiment_entities_utf(capsys): 'foo→bar') out, _ = capsys.readouterr() assert 'Begin Offset : 4' in out + + +def test_classify_text(capsys): + snippets.classify_text( + 'Android is a mobile operating system developed by Google, ' + 'based on the Linux kernel and designed primarily for touchscreen ' + 'mobile devices such as smartphones and tablets.') + out, _ = capsys.readouterr() + assert 'name' in out + assert '/Computers & Electronics' in out + + +def test_classify_file(capsys): + snippets.classify_file(LONG_TEST_FILE_URL) + out, _ = capsys.readouterr() + assert 'name' in out + assert '/Computers & Electronics' in out From c32bf7f6e306a1ae4c447be7f0cde1f125fa606b Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Mon, 6 Nov 2017 13:37:39 -0800 Subject: [PATCH 2/3] flake --- language/cloud-client/v1/snippets.py | 1 - 1 file changed, 1 deletion(-) diff --git a/language/cloud-client/v1/snippets.py b/language/cloud-client/v1/snippets.py index b1c070f4e54..30b591a4037 100644 --- a/language/cloud-client/v1/snippets.py +++ b/language/cloud-client/v1/snippets.py @@ -27,7 +27,6 @@ from google.cloud import language from google.cloud.language import enums from google.cloud.language import types - import six From f367450a030d812ab4d81ab4c2c6c7ce011458fc Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Mon, 13 Nov 2017 09:14:38 -0800 Subject: [PATCH 3/3] client library version --- language/cloud-client/v1/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/language/cloud-client/v1/requirements.txt b/language/cloud-client/v1/requirements.txt index 5a82efc96e7..b5848a34cec 100644 --- a/language/cloud-client/v1/requirements.txt +++ b/language/cloud-client/v1/requirements.txt @@ -1 +1 @@ -google-cloud-language==0.31.0 +google-cloud-language==1.0.0