Skip to content

Commit 48c75b1

Browse files
authored
Merge pull request #2798 from jmdobry/language-v1
Switch NL from v1beta1 to v1. Fixes #2747.
2 parents f0fed3f + 031abbe commit 48c75b1

File tree

7 files changed

+158
-32
lines changed

7 files changed

+158
-32
lines changed

packages/google-cloud-language/google/cloud/language/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Connection(_http.JSONConnection):
2323
API_BASE_URL = 'https://language.googleapis.com'
2424
"""The base of the API call URL."""
2525

26-
API_VERSION = 'v1beta1'
26+
API_VERSION = 'v1'
2727
"""The version of the API, used in building the API call's URL."""
2828

2929
API_URL_TEMPLATE = '{api_base_url}/{api_version}/documents:{path}'

packages/google-cloud-language/google/cloud/language/document.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def analyze_entities(self):
155155
other properties.
156156
157157
.. _analyzeEntities: https://cloud.google.com/natural-language/\
158-
reference/rest/v1beta1/documents/analyzeEntities
158+
reference/rest/v1/documents/analyzeEntities
159159
160160
See `analyzeEntities`_.
161161
@@ -176,7 +176,7 @@ def analyze_sentiment(self):
176176
"""Analyze the sentiment in the current document.
177177
178178
.. _analyzeSentiment: https://cloud.google.com/natural-language/\
179-
reference/rest/v1beta1/documents/analyzeSentiment
179+
reference/rest/v1/documents/analyzeSentiment
180180
181181
See `analyzeSentiment`_.
182182
@@ -188,6 +188,27 @@ def analyze_sentiment(self):
188188
method='POST', path='analyzeSentiment', data=data)
189189
return Sentiment.from_api_repr(api_response['documentSentiment'])
190190

191+
def analyze_syntax(self):
192+
"""Analyze the syntax in the current document.
193+
194+
.. _analyzeSyntax: https://cloud.google.com/natural-language/\
195+
reference/rest/v1/documents/analyzeSyntax
196+
197+
See `analyzeSyntax`_.
198+
199+
:rtype: list
200+
:returns: A list of :class:`~.language.syntax.Token` returned from
201+
the API.
202+
"""
203+
data = {
204+
'document': self._to_dict(),
205+
'encodingType': self.encoding,
206+
}
207+
api_response = self.client._connection.api_request(
208+
method='POST', path='analyzeSyntax', data=data)
209+
return [Token.from_api_repr(token)
210+
for token in api_response.get('tokens', ())]
211+
191212
def annotate_text(self, include_syntax=True, include_entities=True,
192213
include_sentiment=True):
193214
"""Advanced natural language API: document syntax and other features.
@@ -205,7 +226,7 @@ def annotate_text(self, include_syntax=True, include_entities=True,
205226
learning and need in-depth text features to build upon.
206227
207228
.. _annotateText: https://cloud.google.com/natural-language/\
208-
reference/rest/v1beta1/documents/annotateText
229+
reference/rest/v1/documents/annotateText
209230
210231
See `annotateText`_.
211232

packages/google-cloud-language/google/cloud/language/entity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ class Entity(object):
5858
and put in its own property.
5959
6060
.. _Entity message: https://cloud.google.com/natural-language/\
61-
reference/rest/v1beta1/Entity
61+
reference/rest/v1/Entity
6262
.. _EntityType enum: https://cloud.google.com/natural-language/\
63-
reference/rest/v1beta1/Entity#Type
63+
reference/rest/v1/Entity#Type
6464
6565
See `Entity message`_.
6666

packages/google-cloud-language/google/cloud/language/sentiment.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,24 @@ class Sentiment(object):
2222
"""A Google Cloud Natural Language API sentiment object.
2323
2424
.. _Sentiment message: https://cloud.google.com/natural-language/\
25-
reference/rest/v1beta1/Sentiment
25+
reference/rest/v1/Sentiment
2626
.. _Sentiment basics: https://cloud.google.com/natural-language/\
2727
docs/basics#sentiment-analysis-values
2828
2929
See `Sentiment message`_ and `Sentiment basics`_.
3030
31-
:type polarity: float
32-
:param polarity: Polarity of the sentiment in the ``[-1.0, 1.0]`` range.
31+
:type score: float
32+
:param score: Score of the sentiment in the ``[-1.0, 1.0]`` range.
3333
Larger numbers represent more positive sentiments.
3434
3535
:type magnitude: float
3636
:param magnitude: A non-negative number in the ``[0, +inf)`` range, which
3737
represents the absolute magnitude of sentiment
38-
regardless of polarity (positive or negative).
38+
regardless of score (positive or negative).
3939
"""
4040

41-
def __init__(self, polarity, magnitude):
42-
self.polarity = polarity
41+
def __init__(self, score, magnitude):
42+
self.score = score
4343
self.magnitude = magnitude
4444

4545
@classmethod
@@ -52,6 +52,6 @@ def from_api_repr(cls, payload):
5252
:rtype: :class:`Sentiment`
5353
:returns: The sentiment parsed from the API representation.
5454
"""
55-
polarity = payload['polarity']
55+
score = payload['score']
5656
magnitude = payload['magnitude']
57-
return cls(polarity, magnitude)
57+
return cls(score, magnitude)

packages/google-cloud-language/google/cloud/language/syntax.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ class Token(object):
103103
"""A Google Cloud Natural Language API token object.
104104
105105
.. _Token message: https://cloud.google.com/natural-language/reference\
106-
/rest/v1beta1/documents/annotateText#Token
106+
/rest/v1/documents/annotateText#Token
107107
.. _Lemma: https://en.wikipedia.org/wiki/Lemma_(morphology)
108108
.. _Label enum: https://cloud.google.com/natural-language/reference/\
109-
rest/v1beta1/documents/annotateText#Label
109+
rest/v1/documents/annotateText#Label
110110
111111
See `Token message`_.
112112
@@ -148,7 +148,7 @@ def __init__(self, text_content, text_begin, part_of_speech,
148148

149149
@classmethod
150150
def from_api_repr(cls, payload):
151-
"""Convert a token from the JSON API into a :class:`Sentiment`.
151+
"""Convert a token from the JSON API into a :class:`Token`.
152152
153153
:param payload: dict
154154
:type payload: The value from the backend.
@@ -172,7 +172,7 @@ class Sentence(object):
172172
"""A Google Cloud Natural Language API sentence object.
173173
174174
.. _Sentence message: https://cloud.google.com/natural-language/reference\
175-
/rest/v1beta1/documents/annotateText#Sentence
175+
/rest/v1/documents/annotateText#Sentence
176176
177177
See `Sentence message`_.
178178

packages/google-cloud-language/unit_tests/test_document.py

Lines changed: 113 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
ANNOTATE_NAME = 'Moon'
1919
ANNOTATE_CONTENT = 'A cow jumped over the %s.' % (ANNOTATE_NAME,)
20-
ANNOTATE_POLARITY = 1
20+
ANNOTATE_SCORE = 1
2121
ANNOTATE_MAGNITUDE = 0.2
2222
ANNOTATE_SALIENCE = 0.11793101
2323
ANNOTATE_WIKI_URL = 'http://en.wikipedia.org/wiki/Natural_satellite'
@@ -286,20 +286,20 @@ def test_analyze_entities(self):
286286
client._connection.api_request.assert_called_once_with(
287287
path='analyzeEntities', method='POST', data=expected)
288288

289-
def _verify_sentiment(self, sentiment, polarity, magnitude):
289+
def _verify_sentiment(self, sentiment, score, magnitude):
290290
from google.cloud.language.sentiment import Sentiment
291291

292292
self.assertIsInstance(sentiment, Sentiment)
293-
self.assertEqual(sentiment.polarity, polarity)
293+
self.assertEqual(sentiment.score, score)
294294
self.assertEqual(sentiment.magnitude, magnitude)
295295

296296
def test_analyze_sentiment(self):
297297
content = 'All the pretty horses.'
298-
polarity = 1
298+
score = 1
299299
magnitude = 0.6
300300
response = {
301301
'documentSentiment': {
302-
'polarity': polarity,
302+
'score': score,
303303
'magnitude': magnitude,
304304
},
305305
'language': 'en-US',
@@ -308,13 +308,118 @@ def test_analyze_sentiment(self):
308308
document = self._make_one(client, content)
309309

310310
sentiment = document.analyze_sentiment()
311-
self._verify_sentiment(sentiment, polarity, magnitude)
311+
self._verify_sentiment(sentiment, score, magnitude)
312312

313313
# Verify the request.
314314
expected = self._expected_data(content)
315315
client._connection.api_request.assert_called_once_with(
316316
path='analyzeSentiment', method='POST', data=expected)
317317

318+
def _verify_token(self, token, text_content, part_of_speech, lemma):
319+
from google.cloud.language.syntax import Token
320+
321+
self.assertIsInstance(token, Token)
322+
self.assertEqual(token.text_content, text_content)
323+
self.assertEqual(token.part_of_speech, part_of_speech)
324+
self.assertEqual(token.lemma, lemma)
325+
326+
def test_analyze_syntax(self):
327+
from google.cloud.language.document import Encoding
328+
from google.cloud.language.syntax import PartOfSpeech
329+
330+
name1 = 'R-O-C-K'
331+
name2 = 'USA'
332+
content = name1 + ' in the ' + name2
333+
response = {
334+
'sentences': [
335+
{
336+
'text': {
337+
'content': 'R-O-C-K in the USA',
338+
'beginOffset': -1,
339+
},
340+
'sentiment': None,
341+
}
342+
],
343+
'tokens': [
344+
{
345+
'text': {
346+
'content': 'R-O-C-K',
347+
'beginOffset': -1,
348+
},
349+
'partOfSpeech': {
350+
'tag': 'NOUN',
351+
},
352+
'dependencyEdge': {
353+
'headTokenIndex': 0,
354+
'label': 'ROOT',
355+
},
356+
'lemma': 'R-O-C-K',
357+
},
358+
{
359+
'text': {
360+
'content': 'in',
361+
'beginOffset': -1,
362+
},
363+
'partOfSpeech': {
364+
'tag': 'ADP',
365+
},
366+
'dependencyEdge': {
367+
'headTokenIndex': 0,
368+
'label': 'PREP',
369+
},
370+
'lemma': 'in',
371+
},
372+
{
373+
'text': {
374+
'content': 'the',
375+
'beginOffset': -1,
376+
},
377+
'partOfSpeech': {
378+
'tag': 'DET',
379+
},
380+
'dependencyEdge': {
381+
'headTokenIndex': 3,
382+
'label': 'DET',
383+
},
384+
'lemma': 'the',
385+
},
386+
{
387+
'text': {
388+
'content': 'USA',
389+
'beginOffset': -1,
390+
},
391+
'partOfSpeech': {
392+
'tag': 'NOUN',
393+
},
394+
'dependencyEdge': {
395+
'headTokenIndex': 1,
396+
'label': 'POBJ',
397+
},
398+
'lemma': 'USA',
399+
},
400+
],
401+
'language': 'en-US',
402+
}
403+
client = make_mock_client(response)
404+
document = self._make_one(client, content)
405+
406+
tokens = document.analyze_syntax()
407+
self.assertEqual(len(tokens), 4)
408+
token1 = tokens[0]
409+
self._verify_token(token1, name1, PartOfSpeech.NOUN, name1)
410+
token2 = tokens[1]
411+
self._verify_token(token2, 'in', PartOfSpeech.ADPOSITION, 'in')
412+
token3 = tokens[2]
413+
self._verify_token(token3, 'the', PartOfSpeech.DETERMINER, 'the')
414+
token4 = tokens[3]
415+
self._verify_token(token4, name2, PartOfSpeech.NOUN, name2)
416+
417+
# Verify the request.
418+
expected = self._expected_data(
419+
content, encoding_type=Encoding.UTF8)
420+
client._connection.api_request.assert_called_once_with(
421+
path='analyzeSyntax', method='POST', data=expected)
422+
318423
def _verify_sentences(self, include_syntax, annotations):
319424
from google.cloud.language.syntax import Sentence
320425

@@ -357,7 +462,7 @@ def _annotate_text_helper(self, include_sentiment,
357462
}
358463
if include_sentiment:
359464
response['documentSentiment'] = {
360-
'polarity': ANNOTATE_POLARITY,
465+
'score': ANNOTATE_SCORE,
361466
'magnitude': ANNOTATE_MAGNITUDE,
362467
}
363468

@@ -375,7 +480,7 @@ def _annotate_text_helper(self, include_sentiment,
375480
# Sentiment
376481
if include_sentiment:
377482
self._verify_sentiment(annotations.sentiment,
378-
ANNOTATE_POLARITY, ANNOTATE_MAGNITUDE)
483+
ANNOTATE_SCORE, ANNOTATE_MAGNITUDE)
379484
else:
380485
self.assertIsNone(annotations.sentiment)
381486
# Entity

packages/google-cloud-language/unit_tests/test_sentiment.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ def _make_one(self, *args, **kw):
2626
return self._get_target_class()(*args, **kw)
2727

2828
def test_constructor(self):
29-
polarity = 1
29+
score = 1
3030
magnitude = 2.3
31-
sentiment = self._make_one(polarity, magnitude)
32-
self.assertEqual(sentiment.polarity, polarity)
31+
sentiment = self._make_one(score, magnitude)
32+
self.assertEqual(sentiment.score, score)
3333
self.assertEqual(sentiment.magnitude, magnitude)
3434

3535
def test_from_api_repr(self):
3636
klass = self._get_target_class()
37-
polarity = -1
37+
score = -1
3838
magnitude = 5.55
3939
payload = {
40-
'polarity': polarity,
40+
'score': score,
4141
'magnitude': magnitude,
4242
}
4343
sentiment = klass.from_api_repr(payload)
44-
self.assertEqual(sentiment.polarity, polarity)
44+
self.assertEqual(sentiment.score, score)
4545
self.assertEqual(sentiment.magnitude, magnitude)

0 commit comments

Comments
 (0)