Skip to content

Commit 3399655

Browse files
dizcologybusunkim96
authored andcommitted
Natural Language GAPIC client library [(#1018)](#1018)
1 parent 945ad11 commit 3399655

13 files changed

+216
-110
lines changed
6 KB
Binary file not shown.

language/snippets/cloud-client/v1/README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Google Cloud Natural Language API Python Samples
55

66
This directory contains samples for Google Cloud Natural Language API. The `Google Cloud Natural Language API`_ provides natural language understanding technologies to developers, including sentiment analysis, entity recognition, and syntax analysis. This API is part of the larger Cloud Machine Learning API.
77

8+
- See the `migration guide`_ for information about migrating to Python client library v0.26.1.
9+
10+
.. _migration guide: https://cloud.google.com/natural-language/docs/python-client-migration
11+
812

913

1014

language/snippets/cloud-client/v1/README.rst.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ product:
1010
entity recognition, and syntax analysis. This API is part of the larger
1111
Cloud Machine Learning API.
1212

13+
14+
- See the `migration guide`_ for information about migrating to Python client library v0.26.1.
15+
16+
17+
.. _migration guide: https://cloud.google.com/natural-language/docs/python-client-migration
18+
1319
setup:
1420
- auth
1521
- install_deps

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,25 @@
1818
def run_quickstart():
1919
# [START language_quickstart]
2020
# Imports the Google Cloud client library
21+
# [START migration_import]
2122
from google.cloud import language
23+
from google.cloud.language import enums
24+
from google.cloud.language import types
25+
# [END migration_import]
2226

2327
# Instantiates a client
24-
language_client = language.Client()
28+
# [START migration_client]
29+
client = language.LanguageServiceClient()
30+
# [END migration_client]
2531

2632
# The text to analyze
27-
text = 'Hello, world!'
28-
document = language_client.document_from_text(text)
33+
text = u'Hello, world!'
34+
document = types.Document(
35+
content=text,
36+
type=enums.Document.Type.PLAIN_TEXT)
2937

3038
# Detects the sentiment of the text
31-
sentiment = document.analyze_sentiment().sentiment
39+
sentiment = client.analyze_sentiment(document=document).document_sentiment
3240

3341
print('Text: {}'.format(text))
3442
print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
google-cloud-language==0.25.0
1+
google-cloud-language==0.26.1

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

Lines changed: 74 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,118 +24,164 @@
2424
import argparse
2525

2626
from google.cloud import language
27+
from google.cloud.language import enums
28+
from google.cloud.language import types
2729
import six
2830

2931

32+
# [START def_sentiment_text]
3033
def sentiment_text(text):
3134
"""Detects sentiment in the text."""
32-
language_client = language.Client()
35+
client = language.LanguageServiceClient()
3336

3437
if isinstance(text, six.binary_type):
3538
text = text.decode('utf-8')
3639

3740
# Instantiates a plain text document.
38-
document = language_client.document_from_text(text)
41+
# [START migration_document_text]
42+
# [START migration_analyze_sentiment]
43+
document = types.Document(
44+
content=text,
45+
type=enums.Document.Type.PLAIN_TEXT)
46+
# [END migration_document_text]
3947

4048
# Detects sentiment in the document. You can also analyze HTML with:
41-
# document.doc_type == language.Document.HTML
42-
sentiment = document.analyze_sentiment().sentiment
49+
# document.type == enums.Document.Type.HTML
50+
sentiment = client.analyze_sentiment(document).document_sentiment
4351

4452
print('Score: {}'.format(sentiment.score))
4553
print('Magnitude: {}'.format(sentiment.magnitude))
54+
# [END migration_analyze_sentiment]
55+
# [END def_sentiment_text]
4656

4757

58+
# [START def_sentiment_file]
4859
def sentiment_file(gcs_uri):
4960
"""Detects sentiment in the file located in Google Cloud Storage."""
50-
language_client = language.Client()
61+
client = language.LanguageServiceClient()
5162

5263
# Instantiates a plain text document.
53-
document = language_client.document_from_url(gcs_uri)
64+
# [START migration_document_gcs_uri]
65+
document = types.Document(
66+
gcs_content_uri=gcs_uri,
67+
type=enums.Document.Type.PLAIN_TEXT)
68+
# [END migration_document_gcs_uri]
5469

5570
# Detects sentiment in the document. You can also analyze HTML with:
56-
# document.doc_type == language.Document.HTML
57-
sentiment = document.analyze_sentiment().sentiment
71+
# document.type == enums.Document.Type.HTML
72+
sentiment = client.analyze_sentiment(document).document_sentiment
5873

5974
print('Score: {}'.format(sentiment.score))
6075
print('Magnitude: {}'.format(sentiment.magnitude))
76+
# [END def_sentiment_file]
6177

6278

79+
# [START def_entities_text]
6380
def entities_text(text):
6481
"""Detects entities in the text."""
65-
language_client = language.Client()
82+
client = language.LanguageServiceClient()
6683

6784
if isinstance(text, six.binary_type):
6885
text = text.decode('utf-8')
6986

7087
# Instantiates a plain text document.
71-
document = language_client.document_from_text(text)
88+
# [START migration_analyze_entities]
89+
document = types.Document(
90+
content=text,
91+
type=enums.Document.Type.PLAIN_TEXT)
7292

7393
# Detects entities in the document. You can also analyze HTML with:
74-
# document.doc_type == language.Document.HTML
75-
entities = document.analyze_entities().entities
94+
# document.type == enums.Document.Type.HTML
95+
entities = client.analyze_entities(document).entities
7696

7797
for entity in entities:
7898
print('=' * 20)
7999
print(u'{:<16}: {}'.format('name', entity.name))
80-
print(u'{:<16}: {}'.format('type', entity.entity_type))
100+
print(u'{:<16}: {}'.format('type', entity.type))
81101
print(u'{:<16}: {}'.format('metadata', entity.metadata))
82102
print(u'{:<16}: {}'.format('salience', entity.salience))
83103
print(u'{:<16}: {}'.format('wikipedia_url',
84104
entity.metadata.get('wikipedia_url', '-')))
105+
# [END migration_analyze_entities]
106+
# [END def_entities_text]
85107

86108

109+
# [START def_entities_file]
87110
def entities_file(gcs_uri):
88111
"""Detects entities in the file located in Google Cloud Storage."""
89-
language_client = language.Client()
112+
client = language.LanguageServiceClient()
90113

91114
# Instantiates a plain text document.
92-
document = language_client.document_from_url(gcs_uri)
115+
document = types.Document(
116+
gcs_content_uri=gcs_uri,
117+
type=enums.Document.Type.PLAIN_TEXT)
93118

94119
# Detects sentiment in the document. You can also analyze HTML with:
95-
# document.doc_type == language.Document.HTML
96-
entities = document.analyze_entities().entities
120+
# document.type == enums.Document.Type.HTML
121+
entities = client.analyze_entities(document).entities
97122

98123
for entity in entities:
99124
print('=' * 20)
100125
print(u'{:<16}: {}'.format('name', entity.name))
101-
print(u'{:<16}: {}'.format('type', entity.entity_type))
126+
print(u'{:<16}: {}'.format('type', entity.type))
102127
print(u'{:<16}: {}'.format('metadata', entity.metadata))
103128
print(u'{:<16}: {}'.format('salience', entity.salience))
104129
print(u'{:<16}: {}'.format('wikipedia_url',
105130
entity.metadata.get('wikipedia_url', '-')))
131+
# [END def_entities_file]
106132

107133

134+
# [START def_syntax_text]
108135
def syntax_text(text):
109136
"""Detects syntax in the text."""
110-
language_client = language.Client()
137+
client = language.LanguageServiceClient()
111138

112139
if isinstance(text, six.binary_type):
113140
text = text.decode('utf-8')
114141

115142
# Instantiates a plain text document.
116-
document = language_client.document_from_text(text)
143+
# [START migration_analyze_syntax]
144+
document = types.Document(
145+
content=text,
146+
type=enums.Document.Type.PLAIN_TEXT)
117147

118148
# Detects syntax in the document. You can also analyze HTML with:
119-
# document.doc_type == language.Document.HTML
120-
tokens = document.analyze_syntax().tokens
149+
# document.type == enums.Document.Type.HTML
150+
tokens = client.analyze_syntax(document).tokens
151+
152+
# part-of-speech tags from enums.PartOfSpeech.Tag
153+
pos_tag = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM',
154+
'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX')
121155

122156
for token in tokens:
123-
print(u'{}: {}'.format(token.part_of_speech.tag, token.text_content))
157+
print(u'{}: {}'.format(pos_tag[token.part_of_speech.tag],
158+
token.text.content))
159+
# [END migration_analyze_syntax]
160+
# [END def_syntax_text]
124161

125162

163+
# [START def_syntax_file]
126164
def syntax_file(gcs_uri):
127165
"""Detects syntax in the file located in Google Cloud Storage."""
128-
language_client = language.Client()
166+
client = language.LanguageServiceClient()
129167

130168
# Instantiates a plain text document.
131-
document = language_client.document_from_url(gcs_uri)
169+
document = types.Document(
170+
gcs_content_uri=gcs_uri,
171+
type=enums.Document.Type.PLAIN_TEXT)
132172

133173
# Detects syntax in the document. You can also analyze HTML with:
134-
# document.doc_type == language.Document.HTML
135-
tokens = document.analyze_syntax().tokens
174+
# document.type == enums.Document.Type.HTML
175+
tokens = client.analyze_syntax(document).tokens
176+
177+
# part-of-speech tags from enums.PartOfSpeech.Tag
178+
pos_tag = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM',
179+
'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX')
136180

137181
for token in tokens:
138-
print(u'{}: {}'.format(token.part_of_speech.tag, token.text_content))
182+
print(u'{}: {}'.format(pos_tag[token.part_of_speech.tag],
183+
token.text.content))
184+
# [END def_syntax_file]
139185

140186

141187
if __name__ == '__main__':

language/snippets/cloud-client/v1beta2/README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Google Cloud Natural Language API Python Samples
55

66
This directory contains samples for Google Cloud Natural Language API. The `Google Cloud Natural Language API`_ provides natural language understanding technologies to developers, including sentiment analysis, entity recognition, and syntax analysis. This API is part of the larger Cloud Machine Learning API.
77

8+
- See the `migration guide`_ for information about migrating to Python client library v0.26.1.
9+
10+
.. _migration guide: https://cloud.google.com/natural-language/docs/python-client-migration
11+
812

913

1014

language/snippets/cloud-client/v1beta2/README.rst.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ product:
1010
entity recognition, and syntax analysis. This API is part of the larger
1111
Cloud Machine Learning API.
1212

13+
14+
- See the `migration guide`_ for information about migrating to Python client library v0.26.1.
15+
16+
17+
.. _migration guide: https://cloud.google.com/natural-language/docs/python-client-migration
18+
1319
setup:
1420
- auth
1521
- install_deps

language/snippets/cloud-client/v1beta2/quickstart.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,25 @@
1818
def run_quickstart():
1919
# [START language_quickstart]
2020
# Imports the Google Cloud client library
21-
from google.cloud import language
21+
# [START beta_import_client]
22+
# [START beta_import]
23+
from google.cloud import language_v1beta2
24+
from google.cloud.language_v1beta2 import enums
25+
from google.cloud.language_v1beta2 import types
26+
# [END beta_import]
2227

23-
# Instantiates a client with they v1beta2 version
24-
language_client = language.Client(api_version='v1beta2')
28+
# Instantiates a client with the v1beta2 version
29+
client = language_v1beta2.LanguageServiceClient()
30+
# [END beta_import_client]
2531

2632
# The text to analyze
27-
text = 'Hallo Welt!'
28-
document = language_client.document_from_text(text, language='DE')
29-
33+
text = u'Hallo Welt!'
34+
document = types.Document(
35+
content=text,
36+
type=enums.Document.Type.PLAIN_TEXT,
37+
language='de')
3038
# Detects the sentiment of the text
31-
sentiment = document.analyze_sentiment().sentiment
39+
sentiment = client.analyze_sentiment(document).document_sentiment
3240

3341
print('Text: {}'.format(text))
3442
print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
gapic-google-cloud-language-v1beta2==0.15.3
2-
google-cloud-language==0.25.0
1+
google-cloud-language==0.26.1

0 commit comments

Comments
 (0)