1
1
#!/usr/bin/env python
2
2
3
- # Copyright 2016 Google, Inc .
3
+ # Copyright 2018 Google, LLC .
4
4
#
5
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
6
# you may not use this file except in compliance with the License.
24
24
import argparse
25
25
import sys
26
26
27
- from google .cloud import language
28
- from google .cloud .language import enums
29
- from google .cloud .language import types
30
- import six
31
27
28
+ def sentiment_text ():
29
+ # [START language_sentiment_text]
30
+ import six
31
+ from google .cloud import language
32
+ from google .cloud .language import enums
33
+ from google .cloud .language import types
34
+
35
+ text = 'Hello, world!'
32
36
33
- # [START language_sentiment_text]
34
- def sentiment_text (text ):
35
- """Detects sentiment in the text."""
36
37
client = language .LanguageServiceClient ()
37
38
38
39
if isinstance (text , six .binary_type ):
@@ -51,12 +52,17 @@ def sentiment_text(text):
51
52
print ('Score: {}' .format (sentiment .score ))
52
53
print ('Magnitude: {}' .format (sentiment .magnitude ))
53
54
# [END language_python_migration_sentiment_text]
54
- # [END language_sentiment_text]
55
+ # [END language_sentiment_text]
56
+
57
+
58
+ def sentiment_file ():
59
+ # [START language_sentiment_gcs]
60
+ from google .cloud import language
61
+ from google .cloud .language import enums
62
+ from google .cloud .language import types
55
63
64
+ gcs_uri = 'gs://cloud-samples-data/language/hello.txt'
56
65
57
- # [START language_sentiment_gcs]
58
- def sentiment_file (gcs_uri ):
59
- """Detects sentiment in the file located in Google Cloud Storage."""
60
66
client = language .LanguageServiceClient ()
61
67
62
68
# Instantiates a plain text document.
@@ -72,12 +78,18 @@ def sentiment_file(gcs_uri):
72
78
73
79
print ('Score: {}' .format (sentiment .score ))
74
80
print ('Magnitude: {}' .format (sentiment .magnitude ))
75
- # [END language_sentiment_gcs]
81
+ # [END language_sentiment_gcs]
82
+
83
+
84
+ def entities_text ():
85
+ # [START language_entities_text]
86
+ import six
87
+ from google .cloud import language
88
+ from google .cloud .language import enums
89
+ from google .cloud .language import types
76
90
91
+ text = 'President Kennedy spoke at the White House.'
77
92
78
- # [START language_entities_text]
79
- def entities_text (text ):
80
- """Detects entities in the text."""
81
93
client = language .LanguageServiceClient ()
82
94
83
95
if isinstance (text , six .binary_type ):
@@ -105,12 +117,17 @@ def entities_text(text):
105
117
print (u'{:<16}: {}' .format ('wikipedia_url' ,
106
118
entity .metadata .get ('wikipedia_url' , '-' )))
107
119
# [END language_python_migration_entities_text]
108
- # [END language_entities_text]
120
+ # [END language_entities_text]
109
121
110
122
111
- # [START language_entities_gcs]
112
- def entities_file (gcs_uri ):
113
- """Detects entities in the file located in Google Cloud Storage."""
123
+ def entities_file ():
124
+ # [START language_entities_gcs]
125
+ from google .cloud import language
126
+ from google .cloud .language import enums
127
+ from google .cloud .language import types
128
+
129
+ gcs_uri = 'gs://cloud-samples-data/language/president.txt'
130
+
114
131
client = language .LanguageServiceClient ()
115
132
116
133
# Instantiates a plain text document.
@@ -131,12 +148,18 @@ def entities_file(gcs_uri):
131
148
print (u'{:<16}: {}' .format ('salience' , entity .salience ))
132
149
print (u'{:<16}: {}' .format ('wikipedia_url' ,
133
150
entity .metadata .get ('wikipedia_url' , '-' )))
134
- # [END language_entities_gcs]
151
+ # [END language_entities_gcs]
152
+
135
153
154
+ def syntax_text ():
155
+ # [START language_syntax_text]
156
+ import six
157
+ from google .cloud import language
158
+ from google .cloud .language import enums
159
+ from google .cloud .language import types
160
+
161
+ text = 'President Kennedy spoke at the White House.'
136
162
137
- # [START language_syntax_text]
138
- def syntax_text (text ):
139
- """Detects syntax in the text."""
140
163
client = language .LanguageServiceClient ()
141
164
142
165
if isinstance (text , six .binary_type ):
@@ -157,12 +180,17 @@ def syntax_text(text):
157
180
print (u'{}: {}' .format (part_of_speech_tag .name ,
158
181
token .text .content ))
159
182
# [END language_python_migration_syntax_text]
160
- # [END language_syntax_text]
183
+ # [END language_syntax_text]
184
+
161
185
186
+ def syntax_file ():
187
+ # [START language_syntax_gcs]
188
+ from google .cloud import language
189
+ from google .cloud .language import enums
190
+ from google .cloud .language import types
191
+
192
+ gcs_uri = 'gs://cloud-samples-data/language/president.txt'
162
193
163
- # [START language_syntax_gcs]
164
- def syntax_file (gcs_uri ):
165
- """Detects syntax in the file located in Google Cloud Storage."""
166
194
client = language .LanguageServiceClient ()
167
195
168
196
# Instantiates a plain text document.
@@ -178,12 +206,18 @@ def syntax_file(gcs_uri):
178
206
part_of_speech_tag = enums .PartOfSpeech .Tag (token .part_of_speech .tag )
179
207
print (u'{}: {}' .format (part_of_speech_tag .name ,
180
208
token .text .content ))
181
- # [END language_syntax_gcs]
209
+ # [END language_syntax_gcs]
210
+
211
+
212
+ def entity_sentiment_text ():
213
+ # [START language_entity_sentiment_text]
214
+ import six
215
+ from google .cloud import language
216
+ from google .cloud .language import enums
217
+ from google .cloud .language import types
182
218
219
+ text = 'President Kennedy spoke at the White House.'
183
220
184
- # [START language_entity_sentiment_text]
185
- def entity_sentiment_text (text ):
186
- """Detects entity sentiment in the provided text."""
187
221
client = language .LanguageServiceClient ()
188
222
189
223
if isinstance (text , six .binary_type ):
@@ -211,12 +245,17 @@ def entity_sentiment_text(text):
211
245
print (u' Type : {}' .format (mention .type ))
212
246
print (u'Salience: {}' .format (entity .salience ))
213
247
print (u'Sentiment: {}\n ' .format (entity .sentiment ))
214
- # [END language_entity_sentiment_text]
248
+ # [END language_entity_sentiment_text]
249
+
250
+
251
+ def entity_sentiment_file ():
252
+ # [START language_entity_sentiment_gcs]
253
+ from google .cloud import language
254
+ from google .cloud .language import enums
255
+ from google .cloud .language import types
215
256
257
+ gcs_uri = 'gs://cloud-samples-data/language/president.txt'
216
258
217
- # [START language_entity_sentiment_gcs]
218
- def entity_sentiment_file (gcs_uri ):
219
- """Detects entity sentiment in a Google Cloud Storage file."""
220
259
client = language .LanguageServiceClient ()
221
260
222
261
document = types .Document (
@@ -240,12 +279,20 @@ def entity_sentiment_file(gcs_uri):
240
279
print (u' Type : {}' .format (mention .type ))
241
280
print (u'Salience: {}' .format (entity .salience ))
242
281
print (u'Sentiment: {}\n ' .format (entity .sentiment ))
243
- # [END language_entity_sentiment_gcs]
282
+ # [END language_entity_sentiment_gcs]
244
283
245
284
246
- # [START language_classify_text]
247
- def classify_text (text ):
248
- """Classifies content categories of the provided text."""
285
+ def classify_text ():
286
+ # [START language_classify_text]
287
+ import six
288
+ from google .cloud import language
289
+ from google .cloud .language import enums
290
+ from google .cloud .language import types
291
+
292
+ text = 'Android is a mobile operating system developed by Google, ' \
293
+ 'based on the Linux kernel and designed primarily for ' \
294
+ 'touchscreen mobile devices such as smartphones and tablets.'
295
+
249
296
client = language .LanguageServiceClient ()
250
297
251
298
if isinstance (text , six .binary_type ):
@@ -261,14 +308,17 @@ def classify_text(text):
261
308
print (u'=' * 20 )
262
309
print (u'{:<16}: {}' .format ('name' , category .name ))
263
310
print (u'{:<16}: {}' .format ('confidence' , category .confidence ))
264
- # [END language_classify_text]
311
+ # [END language_classify_text]
312
+
313
+
314
+ def classify_file ():
315
+ # [START language_classify_gcs]
316
+ from google .cloud import language
317
+ from google .cloud .language import enums
318
+ from google .cloud .language import types
265
319
320
+ gcs_uri = 'gs://cloud-samples-data/language/android.txt'
266
321
267
- # [START language_classify_gcs]
268
- def classify_file (gcs_uri ):
269
- """Classifies content categories of the text in a Google Cloud Storage
270
- file.
271
- """
272
322
client = language .LanguageServiceClient ()
273
323
274
324
document = types .Document (
@@ -281,7 +331,7 @@ def classify_file(gcs_uri):
281
331
print (u'=' * 20 )
282
332
print (u'{:<16}: {}' .format ('name' , category .name ))
283
333
print (u'{:<16}: {}' .format ('confidence' , category .confidence ))
284
- # [END language_classify_gcs]
334
+ # [END language_classify_gcs]
285
335
286
336
287
337
if __name__ == '__main__' :
@@ -292,63 +342,53 @@ def classify_file(gcs_uri):
292
342
293
343
classify_text_parser = subparsers .add_parser (
294
344
'classify-text' , help = classify_text .__doc__ )
295
- classify_text_parser .add_argument ('text' )
296
345
297
346
classify_text_parser = subparsers .add_parser (
298
347
'classify-file' , help = classify_file .__doc__ )
299
- classify_text_parser .add_argument ('gcs_uri' )
300
348
301
349
sentiment_entities_text_parser = subparsers .add_parser (
302
350
'sentiment-entities-text' , help = entity_sentiment_text .__doc__ )
303
- sentiment_entities_text_parser .add_argument ('text' )
304
351
305
352
sentiment_entities_file_parser = subparsers .add_parser (
306
353
'sentiment-entities-file' , help = entity_sentiment_file .__doc__ )
307
- sentiment_entities_file_parser .add_argument ('gcs_uri' )
308
354
309
355
sentiment_text_parser = subparsers .add_parser (
310
356
'sentiment-text' , help = sentiment_text .__doc__ )
311
- sentiment_text_parser .add_argument ('text' )
312
357
313
358
sentiment_file_parser = subparsers .add_parser (
314
359
'sentiment-file' , help = sentiment_file .__doc__ )
315
- sentiment_file_parser .add_argument ('gcs_uri' )
316
360
317
361
entities_text_parser = subparsers .add_parser (
318
362
'entities-text' , help = entities_text .__doc__ )
319
- entities_text_parser .add_argument ('text' )
320
363
321
364
entities_file_parser = subparsers .add_parser (
322
365
'entities-file' , help = entities_file .__doc__ )
323
- entities_file_parser .add_argument ('gcs_uri' )
324
366
325
367
syntax_text_parser = subparsers .add_parser (
326
368
'syntax-text' , help = syntax_text .__doc__ )
327
- syntax_text_parser .add_argument ('text' )
328
369
329
370
syntax_file_parser = subparsers .add_parser (
330
371
'syntax-file' , help = syntax_file .__doc__ )
331
- syntax_file_parser .add_argument ('gcs_uri' )
332
372
333
373
args = parser .parse_args ()
334
374
335
375
if args .command == 'sentiment-text' :
336
- sentiment_text (args . text )
376
+ sentiment_text ()
337
377
elif args .command == 'sentiment-file' :
338
- sentiment_file (args . gcs_uri )
378
+ sentiment_file ()
339
379
elif args .command == 'entities-text' :
340
- entities_text (args . text )
380
+ entities_text ()
341
381
elif args .command == 'entities-file' :
342
- entities_file (args . gcs_uri )
382
+ entities_file ()
343
383
elif args .command == 'syntax-text' :
344
- syntax_text (args . text )
384
+ syntax_text ()
345
385
elif args .command == 'syntax-file' :
346
- syntax_file (args . gcs_uri )
386
+ syntax_file ()
347
387
elif args .command == 'sentiment-entities-text' :
348
- entity_sentiment_text (args . text )
388
+ entity_sentiment_text ()
349
389
elif args .command == 'sentiment-entities-file' :
350
- entity_sentiment_file (args . gcs_uri )
390
+ entity_sentiment_file ()
351
391
elif args .command == 'classify-text' :
352
- classify_text (args . text )
392
+ classify_text ()
353
393
elif args .command == 'classify-file' :
354
- classify_file (args . gcs_uri )
394
+ classify_file ()
0 commit comments