Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 091b48e

Browse filesBrowse files
dizcologyJon Wayne Parrott
authored andcommitted
* copy classify_test samples and tests to v1 * flake * client library version
1 parent 5b34950 commit 091b48e
Copy full SHA for 091b48e

File tree

Expand file treeCollapse file tree

2 files changed

+71
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+71
-0
lines changed

‎language/cloud-client/v1/snippets.py

Copy file name to clipboardExpand all lines: language/cloud-client/v1/snippets.py
+53Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,61 @@ def entity_sentiment_file(gcs_uri):
253253
print(u'Sentiment: {}\n'.format(entity.sentiment))
254254

255255

256+
# [START def_classify_text]
257+
def classify_text(text):
258+
"""Classifies content categories of the provided text."""
259+
client = language.LanguageServiceClient()
260+
261+
if isinstance(text, six.binary_type):
262+
text = text.decode('utf-8')
263+
264+
document = types.Document(
265+
content=text.encode('utf-8'),
266+
type=enums.Document.Type.PLAIN_TEXT)
267+
268+
categories = client.classify_text(document).categories
269+
270+
for category in categories:
271+
print(u'=' * 20)
272+
print(u'{:<16}: {}'.format('name', category.name))
273+
print(u'{:<16}: {}'.format('confidence', category.confidence))
274+
# [END def_classify_text]
275+
276+
277+
# [START def_classify_file]
278+
def classify_file(gcs_uri):
279+
"""Classifies content categories of the text in a Google Cloud Storage
280+
file.
281+
"""
282+
client = language.LanguageServiceClient()
283+
284+
document = types.Document(
285+
gcs_content_uri=gcs_uri,
286+
type=enums.Document.Type.PLAIN_TEXT)
287+
288+
categories = client.classify_text(document).categories
289+
290+
for category in categories:
291+
print(u'=' * 20)
292+
print(u'{:<16}: {}'.format('name', category.name))
293+
print(u'{:<16}: {}'.format('confidence', category.confidence))
294+
# [END def_classify_file]
295+
296+
256297
if __name__ == '__main__':
257298
parser = argparse.ArgumentParser(
258299
description=__doc__,
259300
formatter_class=argparse.RawDescriptionHelpFormatter)
260301
subparsers = parser.add_subparsers(dest='command')
261302

303+
classify_text_parser = subparsers.add_parser(
304+
'classify-text', help=classify_text.__doc__)
305+
classify_text_parser.add_argument('text')
306+
307+
classify_text_parser = subparsers.add_parser(
308+
'classify-file', help=classify_file.__doc__)
309+
classify_text_parser.add_argument('gcs_uri')
310+
262311
sentiment_entities_text_parser = subparsers.add_parser(
263312
'sentiment-entities-text', help=entity_sentiment_text.__doc__)
264313
sentiment_entities_text_parser.add_argument('text')
@@ -309,3 +358,7 @@ def entity_sentiment_file(gcs_uri):
309358
entity_sentiment_text(args.text)
310359
elif args.command == 'sentiment-entities-file':
311360
entity_sentiment_file(args.gcs_uri)
361+
elif args.command == 'classify-text':
362+
classify_text(args.text)
363+
elif args.command == 'classify-file':
364+
classify_file(args.gcs_uri)

‎language/cloud-client/v1/snippets_test.py

Copy file name to clipboardExpand all lines: language/cloud-client/v1/snippets_test.py
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
BUCKET = os.environ['CLOUD_STORAGE_BUCKET']
2121
TEST_FILE_URL = 'gs://{}/text.txt'.format(BUCKET)
22+
LONG_TEST_FILE_URL = 'gs://{}/android_text.txt'.format(BUCKET)
2223

2324

2425
def test_sentiment_text(capsys):
@@ -77,3 +78,20 @@ def test_sentiment_entities_utf(capsys):
7778
'foo→bar')
7879
out, _ = capsys.readouterr()
7980
assert 'Begin Offset : 4' in out
81+
82+
83+
def test_classify_text(capsys):
84+
snippets.classify_text(
85+
'Android is a mobile operating system developed by Google, '
86+
'based on the Linux kernel and designed primarily for touchscreen '
87+
'mobile devices such as smartphones and tablets.')
88+
out, _ = capsys.readouterr()
89+
assert 'name' in out
90+
assert '/Computers & Electronics' in out
91+
92+
93+
def test_classify_file(capsys):
94+
snippets.classify_file(LONG_TEST_FILE_URL)
95+
out, _ = capsys.readouterr()
96+
assert 'name' in out
97+
assert '/Computers & Electronics' in out

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.