From 4f46537f191ce78fc25d28b1f718977586311587 Mon Sep 17 00:00:00 2001 From: Daniel Valdivia Date: Mon, 29 May 2017 16:21:17 -0700 Subject: [PATCH 01/11] Adding support for all the data returned by the API for PartOfSpeech inside Token --- language/google/cloud/language/syntax.py | 108 ++++++++++++++++++----- 1 file changed, 88 insertions(+), 20 deletions(-) diff --git a/language/google/cloud/language/syntax.py b/language/google/cloud/language/syntax.py index 9bad2116b332..da2ae127f83d 100644 --- a/language/google/cloud/language/syntax.py +++ b/language/google/cloud/language/syntax.py @@ -20,7 +20,45 @@ class PartOfSpeech(object): - """Part of speech of a :class:`Token`.""" + """A Google Cloud Natural Language API Part of speech object. Rich data about + the Part of Speech of a token returned by the API + + :type aspect: str + :param aspect: Aspect + + :type reciprocity: str + :param reciprocity: Reciprocity + + :type case: str + :param case: Case + + :type mood: str + :param mood: Mood + + :type tag: str + :param tag: Tag + + :type person: str + :param person: Person + + :type number: str + :param number: Number + + :type tense: str + :param tense: Tense + + :type form: str + :param form: Form + + :type proper: str + :param proper: Proper + + :type voice: str + :param voice: Voice + + :type gender: str + :param gender: Gender + """ UNKNOWN = 'UNKNOWN' """Unknown part of speech.""" @@ -81,22 +119,52 @@ class PartOfSpeech(object): 'AFFIX': 'AFFIX', } - @classmethod - def reverse(cls, tag): - """Reverses the API's enum name for the one on this class. - - For example:: + def __init__(self, aspect, reciprocity, case, mood, tag, person, number, tense, form, proper, voice, gender): + self.aspect = aspect + self.reciprocity = reciprocity + self.case = case + self.mood = mood + self.tag = tag + self.person = person + self.number = number + self.tense = tense + self.form = form + self.proper = proper + self.voice = voice + self.gender = gender - >>> PartOfSpeech.OTHER - 'X' - >>> PartOfSpeech.reverse('X') - 'OTHER' - - :rtype: str - :returns: The attribute name corresponding to the API part of - speech enum. - """ - return cls._REVERSE_MAP[tag] + @classmethod + def from_api_repr(cls, payload): + return PartOfSpeech(aspect=payload['aspect'], + reciprocity=payload['reciprocity'], + case=payload['case'], + mood=payload['mood'], + tag=payload['tag'], + person=payload['person'], + number=payload['number'], + tense=payload['tense'], + form=payload['form'], + proper=payload['proper'], + voice=payload['voice'], + gender=payload['gender']) + + +@classmethod +def reverse(cls, tag): + """Reverses the API's enum name for the one on this class. + + For example:: + + >>> PartOfSpeech.OTHER + 'X' + >>> PartOfSpeech.reverse('X') + 'OTHER' + + :rtype: str + :returns: The attribute name corresponding to the API part of + speech enum. + """ + return cls._REVERSE_MAP[tag] class Token(object): @@ -118,9 +186,9 @@ class Token(object): document according to the encoding type specified in the API request. - :type part_of_speech: str - :param part_of_speech: The part of speech of the token. See - :class:`PartOfSpeech` for possible values. + :type part_of_speech: PartOfSpeech + :param part_of_speech: An object representing the Part of Speech of the token + with it's properties. :type edge_index: int :param edge_index: The head of this token in the dependency tree. This is @@ -159,7 +227,7 @@ def from_api_repr(cls, payload): text_span = payload['text'] text_content = text_span['content'] text_begin = text_span['beginOffset'] - part_of_speech = payload['partOfSpeech']['tag'] + part_of_speech = PartOfSpeech.from_api_repr(payload['partOfSpeech']) edge = payload['dependencyEdge'] edge_index = edge['headTokenIndex'] edge_label = edge['label'] From fdba1be96994b98e6c7fea6dcd1fb2e0b68961cd Mon Sep 17 00:00:00 2001 From: Daniel Valdivia Date: Mon, 29 May 2017 16:24:29 -0700 Subject: [PATCH 02/11] Fixing auto-formatting leaving a function outside of the PartOfSpeech class --- language/google/cloud/language/syntax.py | 27 ++++++++++++------------ 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/language/google/cloud/language/syntax.py b/language/google/cloud/language/syntax.py index da2ae127f83d..779df57c981b 100644 --- a/language/google/cloud/language/syntax.py +++ b/language/google/cloud/language/syntax.py @@ -148,23 +148,22 @@ def from_api_repr(cls, payload): voice=payload['voice'], gender=payload['gender']) + @classmethod + def reverse(cls, tag): + """Reverses the API's enum name for the one on this class. -@classmethod -def reverse(cls, tag): - """Reverses the API's enum name for the one on this class. - - For example:: + For example:: - >>> PartOfSpeech.OTHER - 'X' - >>> PartOfSpeech.reverse('X') - 'OTHER' + >>> PartOfSpeech.OTHER + 'X' + >>> PartOfSpeech.reverse('X') + 'OTHER' - :rtype: str - :returns: The attribute name corresponding to the API part of - speech enum. - """ - return cls._REVERSE_MAP[tag] + :rtype: str + :returns: The attribute name corresponding to the API part of + speech enum. + """ + return cls._REVERSE_MAP[tag] class Token(object): From d12defa4349ae9e1bf3f1c56659f5f8380a4af33 Mon Sep 17 00:00:00 2001 From: Daniel Valdivia Date: Mon, 29 May 2017 16:39:36 -0700 Subject: [PATCH 03/11] Adding unit tests for the new code to comply with code coverage tests --- language/tests/unit/test_syntax.py | 77 +++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/language/tests/unit/test_syntax.py b/language/tests/unit/test_syntax.py index 8c1f994da5ae..c66dd16b6863 100644 --- a/language/tests/unit/test_syntax.py +++ b/language/tests/unit/test_syntax.py @@ -16,7 +16,6 @@ class TestPartOfSpeech(unittest.TestCase): - @staticmethod def _get_target_class(): from google.cloud.language.syntax import PartOfSpeech @@ -34,9 +33,83 @@ def test_reverse(self): result = klass.reverse(value) self.assertEqual(result, attr) + def _make_one(self, *args, **kw): + return self._get_target_class()(*args, **kw) -class TestToken(unittest.TestCase): + def test_constructor(self): + + aspect = 'ASPECT_UNKNOWN' + reciprocity = 'RECIPROCITY_UNKNOWN' + case = 'NOMINATIVE' + mood = 'MOOD_UNKNOWN' + tag = 'PRON' + person = 'FIRST' + number = 'SINGULAR' + tense = 'TENSE_UNKNOWN' + form = 'FORM_UNKNOWN' + proper = 'PROPER_UNKNOWN' + voice = 'VOICE_UNKNOWN' + gender = 'GENDER_UNKNOWN' + + pos = self._make_one(aspect, reciprocity, case, mood, tag, person, + number, tense, form, proper, voice, gender) + self.assertEqual(pos.aspect, aspect) + self.assertEqual(pos.reciprocity, reciprocity) + self.assertEqual(pos.case, case) + self.assertEqual(pos.mood, mood) + self.assertEqual(pos.tag, tag) + self.assertEqual(pos.person, person) + self.assertEqual(pos.number, number) + self.assertEqual(pos.tense, tense) + self.assertEqual(pos.form, form) + self.assertEqual(pos.proper, proper) + self.assertEqual(pos.voice, voice) + self.assertEqual(pos.gender, gender) + def test_from_api_repr(self): + klass = self._get_target_class() + aspect = 'ASPECT_UNKNOWN' + reciprocity = 'RECIPROCITY_UNKNOWN' + case = 'NOMINATIVE' + mood = 'MOOD_UNKNOWN' + tag = 'PRON' + person = 'FIRST' + number = 'SINGULAR' + tense = 'TENSE_UNKNOWN' + form = 'FORM_UNKNOWN' + proper = 'PROPER_UNKNOWN' + voice = 'VOICE_UNKNOWN' + gender = 'GENDER_UNKNOWN' + payload = { + 'aspect': aspect, + 'reciprocity': reciprocity, + 'case': case, + 'mood': mood, + 'tag': tag, + 'person': person, + 'number': number, + 'tense': tense, + 'form': form, + 'proper': proper, + 'voice': voice, + 'gender': gender + } + pos = klass.from_api_repr(payload) + self.assertEqual(pos.aspect, aspect) + self.assertEqual(pos.reciprocity, reciprocity) + self.assertEqual(pos.case, case) + self.assertEqual(pos.mood, mood) + self.assertEqual(pos.tag, tag) + self.assertEqual(pos.person, person) + self.assertEqual(pos.number, number) + self.assertEqual(pos.tense, tense) + self.assertEqual(pos.form, form) + self.assertEqual(pos.proper, proper) + self.assertEqual(pos.voice, voice) + self.assertEqual(pos.gender, gender) + + +class TestToken(unittest.TestCase): @staticmethod def _get_target_class(): from google.cloud.language.syntax import Token From ef9b2de5d18545b26438ef8b35c07fab36cc0252 Mon Sep 17 00:00:00 2001 From: Daniel Valdivia Date: Mon, 29 May 2017 16:51:04 -0700 Subject: [PATCH 04/11] Updated test for Token --- language/tests/unit/test_syntax.py | 72 +++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 7 deletions(-) diff --git a/language/tests/unit/test_syntax.py b/language/tests/unit/test_syntax.py index c66dd16b6863..92ec1cfa1573 100644 --- a/language/tests/unit/test_syntax.py +++ b/language/tests/unit/test_syntax.py @@ -124,7 +124,20 @@ def test_constructor(self): text_content = 'All' text_begin = -1 - part_of_speech = PartOfSpeech.DETERMINER + aspect = 'ASPECT_UNKNOWN' + reciprocity = 'RECIPROCITY_UNKNOWN' + case = 'NOMINATIVE' + mood = 'MOOD_UNKNOWN' + tag = 'PRON' + person = 'FIRST' + number = 'SINGULAR' + tense = 'TENSE_UNKNOWN' + form = 'FORM_UNKNOWN' + proper = 'PROPER_UNKNOWN' + voice = 'VOICE_UNKNOWN' + gender = 'GENDER_UNKNOWN' + part_of_speech = PartOfSpeech(aspect, reciprocity, case, mood, tag, person, + number, tense, form, proper, voice, gender) edge_index = 3 edge_label = 'PREDET' lemma = text_content @@ -132,7 +145,18 @@ def test_constructor(self): edge_index, edge_label, lemma) self.assertEqual(token.text_content, text_content) self.assertEqual(token.text_begin, text_begin) - self.assertEqual(token.part_of_speech, part_of_speech) + self.assertEqual(token.part_of_speech.aspect, part_of_speech.aspect) + self.assertEqual(token.part_of_speech.reciprocity, part_of_speech.reciprocity) + self.assertEqual(token.part_of_speech.case, part_of_speech.case) + self.assertEqual(token.part_of_speech.mood, part_of_speech.mood) + self.assertEqual(token.part_of_speech.tag, part_of_speech.tag) + self.assertEqual(token.part_of_speech.person, part_of_speech.person) + self.assertEqual(token.part_of_speech.number, part_of_speech.number) + self.assertEqual(token.part_of_speech.tense, part_of_speech.tense) + self.assertEqual(token.part_of_speech.form, part_of_speech.form) + self.assertEqual(token.part_of_speech.proper, part_of_speech.proper) + self.assertEqual(token.part_of_speech.voice, part_of_speech.voice) + self.assertEqual(token.part_of_speech.gender, part_of_speech.gender) self.assertEqual(token.edge_index, edge_index) self.assertEqual(token.edge_label, edge_label) self.assertEqual(token.lemma, lemma) @@ -143,7 +167,32 @@ def test_from_api_repr(self): klass = self._get_target_class() text_content = 'pretty' text_begin = -1 - part_of_speech = PartOfSpeech.ADJECTIVE + aspect = 'ASPECT_UNKNOWN' + reciprocity = 'RECIPROCITY_UNKNOWN' + case = 'NOMINATIVE' + mood = 'MOOD_UNKNOWN' + tag = 'PRON' + person = 'FIRST' + number = 'SINGULAR' + tense = 'TENSE_UNKNOWN' + form = 'FORM_UNKNOWN' + proper = 'PROPER_UNKNOWN' + voice = 'VOICE_UNKNOWN' + gender = 'GENDER_UNKNOWN' + part_of_speech = { + 'aspect': aspect, + 'reciprocity': reciprocity, + 'case': case, + 'mood': mood, + 'tag': tag, + 'person': person, + 'number': number, + 'tense': tense, + 'form': form, + 'proper': proper, + 'voice': voice, + 'gender': gender + } edge_index = 3 edge_label = 'AMOD' lemma = text_content @@ -152,9 +201,7 @@ def test_from_api_repr(self): 'content': text_content, 'beginOffset': text_begin, }, - 'partOfSpeech': { - 'tag': part_of_speech, - }, + 'partOfSpeech': part_of_speech, 'dependencyEdge': { 'headTokenIndex': edge_index, 'label': edge_label, @@ -164,7 +211,18 @@ def test_from_api_repr(self): token = klass.from_api_repr(payload) self.assertEqual(token.text_content, text_content) self.assertEqual(token.text_begin, text_begin) - self.assertEqual(token.part_of_speech, part_of_speech) + self.assertEqual(token.part_of_speech.aspect, part_of_speech['aspect']) + self.assertEqual(token.part_of_speech.reciprocity, part_of_speech['reciprocity']) + self.assertEqual(token.part_of_speech.case, part_of_speech['case']) + self.assertEqual(token.part_of_speech.mood, part_of_speech['mood']) + self.assertEqual(token.part_of_speech.tag, part_of_speech['tag']) + self.assertEqual(token.part_of_speech.person, part_of_speech['person']) + self.assertEqual(token.part_of_speech.number, part_of_speech['number']) + self.assertEqual(token.part_of_speech.tense, part_of_speech['tense']) + self.assertEqual(token.part_of_speech.form, part_of_speech['form']) + self.assertEqual(token.part_of_speech.proper, part_of_speech['proper']) + self.assertEqual(token.part_of_speech.voice, part_of_speech['voice']) + self.assertEqual(token.part_of_speech.gender, part_of_speech['gender']) self.assertEqual(token.edge_index, edge_index) self.assertEqual(token.edge_label, edge_label) self.assertEqual(token.lemma, lemma) From 343c2d9faa5d70f21488cc0794c88130be0aa0f4 Mon Sep 17 00:00:00 2001 From: Daniel Valdivia Date: Mon, 29 May 2017 16:56:16 -0700 Subject: [PATCH 05/11] lint --- language/tests/unit/test_syntax.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/language/tests/unit/test_syntax.py b/language/tests/unit/test_syntax.py index 92ec1cfa1573..387257353ccb 100644 --- a/language/tests/unit/test_syntax.py +++ b/language/tests/unit/test_syntax.py @@ -162,8 +162,6 @@ def test_constructor(self): self.assertEqual(token.lemma, lemma) def test_from_api_repr(self): - from google.cloud.language.syntax import PartOfSpeech - klass = self._get_target_class() text_content = 'pretty' text_begin = -1 From 92e6ca7e37ec3cf79556ee74dc66735e3fa74ea7 Mon Sep 17 00:00:00 2001 From: Daniel Valdivia Date: Mon, 29 May 2017 17:06:35 -0700 Subject: [PATCH 06/11] Update API Response tests --- language/google/cloud/language/syntax.py | 11 +++--- language/tests/unit/test_api_responses.py | 41 ++++++++++++++++++++--- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/language/google/cloud/language/syntax.py b/language/google/cloud/language/syntax.py index 779df57c981b..622bd084584d 100644 --- a/language/google/cloud/language/syntax.py +++ b/language/google/cloud/language/syntax.py @@ -20,8 +20,8 @@ class PartOfSpeech(object): - """A Google Cloud Natural Language API Part of speech object. Rich data about - the Part of Speech of a token returned by the API + """A Google Cloud Natural Language API Part of speech object. + Rich data about the Part of Speech of a token returned by the API :type aspect: str :param aspect: Aspect @@ -119,7 +119,8 @@ class PartOfSpeech(object): 'AFFIX': 'AFFIX', } - def __init__(self, aspect, reciprocity, case, mood, tag, person, number, tense, form, proper, voice, gender): + def __init__(self, aspect, reciprocity, case, mood, tag, person, + number, tense, form, proper, voice, gender): self.aspect = aspect self.reciprocity = reciprocity self.case = case @@ -186,8 +187,8 @@ class Token(object): in the API request. :type part_of_speech: PartOfSpeech - :param part_of_speech: An object representing the Part of Speech of the token - with it's properties. + :param part_of_speech: An object representing the Part of Speech of the + token with it's properties. :type edge_index: int :param edge_index: The head of this token in the dependency tree. This is diff --git a/language/tests/unit/test_api_responses.py b/language/tests/unit/test_api_responses.py index bc04522acb06..de542d5f3a77 100644 --- a/language/tests/unit/test_api_responses.py +++ b/language/tests/unit/test_api_responses.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from copy import copy import unittest +from copy import copy class TestEntityResponse(unittest.TestCase): @@ -115,6 +115,18 @@ def _verify_sentiment_response(self, sentiment_response): class TestSyntaxResponse(unittest.TestCase): SENTENCE_DICT = copy(TestSentimentResponse.SENTENCE_DICT) + aspect = 'ASPECT_UNKNOWN' + reciprocity = 'RECIPROCITY_UNKNOWN' + case = 'NOMINATIVE' + mood = 'MOOD_UNKNOWN' + tag = 'PRON' + person = 'FIRST' + number = 'SINGULAR' + tense = 'TENSE_UNKNOWN' + form = 'FORM_UNKNOWN' + proper = 'PROPER_UNKNOWN' + voice = 'VOICE_UNKNOWN' + gender = 'GENDER_UNKNOWN' TOKEN_DICT = { 'dependencyEdge': { 'headTokenIndex': 0, @@ -122,7 +134,18 @@ class TestSyntaxResponse(unittest.TestCase): }, 'lemma': 'it', 'partOfSpeech': { - 'tag': 'PRON', + 'aspect': aspect, + 'reciprocity': reciprocity, + 'case': case, + 'mood': mood, + 'tag': tag, + 'person': person, + 'number': number, + 'tense': tense, + 'form': form, + 'proper': proper, + 'voice': voice, + 'gender': gender }, 'text': { 'beginOffset': 0, @@ -156,7 +179,6 @@ def test_api_repr_factory(self): def _verify_syntax_response(self, syntax_response): from google.cloud.language.sentiment import Sentiment - from google.cloud.language.syntax import PartOfSpeech self.assertEqual(syntax_response.language, 'en') @@ -169,7 +191,18 @@ def _verify_syntax_response(self, syntax_response): token = syntax_response.tokens[0] self.assertEqual(token.text_content, 'It') self.assertEqual(token.text_begin, 0) - self.assertEqual(token.part_of_speech, PartOfSpeech.PRONOUN) + self.assertEqual(token.part_of_speech.aspect, 'ASPECT_UNKNOWN') + self.assertEqual(token.part_of_speech.reciprocity, 'RECIPROCITY_UNKNOWN') + self.assertEqual(token.part_of_speech.case, 'NOMINATIVE') + self.assertEqual(token.part_of_speech.mood, 'MOOD_UNKNOWN') + self.assertEqual(token.part_of_speech.tag, 'PRON') + self.assertEqual(token.part_of_speech.person, 'FIRST') + self.assertEqual(token.part_of_speech.number, 'SINGULAR') + self.assertEqual(token.part_of_speech.tense, 'TENSE_UNKNOWN') + self.assertEqual(token.part_of_speech.form, 'FORM_UNKNOWN') + self.assertEqual(token.part_of_speech.proper, 'PROPER_UNKNOWN') + self.assertEqual(token.part_of_speech.voice, 'VOICE_UNKNOWN') + self.assertEqual(token.part_of_speech.gender, 'GENDER_UNKNOWN') self.assertEqual(token.edge_index, 0) self.assertEqual(token.edge_label, 'NSUBJ') self.assertEqual(token.lemma, 'it') From de34fce37bac70f6954b673db52e6863506a73de Mon Sep 17 00:00:00 2001 From: Daniel Valdivia Date: Mon, 29 May 2017 17:10:42 -0700 Subject: [PATCH 07/11] update tests for document --- language/tests/unit/test_document.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/language/tests/unit/test_document.py b/language/tests/unit/test_document.py index c30d13b6f15e..dcdf715c7015 100644 --- a/language/tests/unit/test_document.py +++ b/language/tests/unit/test_document.py @@ -14,7 +14,6 @@ import unittest - ANNOTATE_NAME = 'Moon' ANNOTATE_CONTENT = 'A cow jumped over the %s.' % (ANNOTATE_NAME,) ANNOTATE_SCORE = 1 @@ -29,7 +28,20 @@ def _make_token_json(name, part_of_speech, head, edge_label): 'content': name, 'beginOffset': -1, }, - 'partOfSpeech': {'tag': part_of_speech}, + 'partOfSpeech': { + 'aspect': 'ASPECT_UNKNOWN', + 'reciprocity': 'RECIPROCITY_UNKNOWN', + 'case': 'NOMINATIVE', + 'mood': 'MOOD_UNKNOWN', + 'tag': part_of_speech, + 'person': 'FIRST', + 'number': 'SINGULAR', + 'tense': 'TENSE_UNKNOWN', + 'form': 'FORM_UNKNOWN', + 'proper': 'PROPER_UNKNOWN', + 'voice': 'VOICE_UNKNOWN', + 'gender': 'GENDER_UNKNOWN', + }, 'dependencyEdge': { 'headTokenIndex': head, 'label': edge_label, @@ -120,7 +132,6 @@ def test_default_low_maxunicode(self): class TestDocument(unittest.TestCase): - @staticmethod def _get_target_class(): from google.cloud.language.document import Document From 49cf4bf11c695a7283b3bbe73ee07ec9ad0d6f19 Mon Sep 17 00:00:00 2001 From: Daniel Valdivia Date: Mon, 29 May 2017 17:19:28 -0700 Subject: [PATCH 08/11] Updated Document Tests --- language/tests/unit/test_document.py | 54 +++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/language/tests/unit/test_document.py b/language/tests/unit/test_document.py index dcdf715c7015..83e061d96d02 100644 --- a/language/tests/unit/test_document.py +++ b/language/tests/unit/test_document.py @@ -440,7 +440,7 @@ def _verify_token(self, token, text_content, part_of_speech, lemma): self.assertIsInstance(token, Token) self.assertEqual(token.text_content, text_content) - self.assertEqual(token.part_of_speech, part_of_speech) + self.assertEqual(token.part_of_speech['tag'], part_of_speech) self.assertEqual(token.lemma, lemma) def test_analyze_syntax(self): @@ -468,7 +468,18 @@ def test_analyze_syntax(self): 'beginOffset': -1, }, 'partOfSpeech': { - 'tag': 'NOUN', + 'aspect' : 'ASPECT_UNKNOWN', + 'reciprocity' : 'RECIPROCITY_UNKNOWN', + 'case' : 'CASE_UNKNOWN', + 'mood' : 'MOOD_UNKNOWN', + 'tag' : 'NOUN', + 'person' : 'PERSON_UNKNOWN', + 'number' : 'SINGULAR', + 'tense' : 'TENSE_UNKNOWN', + 'form' : 'FORM_UNKNOWN', + 'proper' : 'PROPER', + 'voice' : 'VOICE_UNKNOWN', + 'gender' : 'GENDER_UNKNOWN' }, 'dependencyEdge': { 'headTokenIndex': 0, @@ -482,7 +493,18 @@ def test_analyze_syntax(self): 'beginOffset': -1, }, 'partOfSpeech': { - 'tag': 'ADP', + 'aspect' : 'ASPECT_UNKNOWN', + 'reciprocity' : 'RECIPROCITY_UNKNOWN', + 'case' : 'CASE_UNKNOWN', + 'mood' : 'MOOD_UNKNOWN', + 'tag' : 'ADP', + 'person' : 'PERSON_UNKNOWN', + 'number' : 'NUMBER_UNKNOWN', + 'tense' : 'TENSE_UNKNOWN', + 'form' : 'FORM_UNKNOWN', + 'proper' : 'PROPER_UNKNOWN', + 'voice' : 'VOICE_UNKNOWN', + 'gender' : 'GENDER_UNKNOWN' }, 'dependencyEdge': { 'headTokenIndex': 0, @@ -496,7 +518,18 @@ def test_analyze_syntax(self): 'beginOffset': -1, }, 'partOfSpeech': { - 'tag': 'DET', + 'aspect' : 'ASPECT_UNKNOWN', + 'reciprocity' : 'RECIPROCITY_UNKNOWN', + 'case' : 'CASE_UNKNOWN', + 'mood' : 'MOOD_UNKNOWN', + 'tag' : 'DET', + 'person' : 'PERSON_UNKNOWN', + 'number' : 'NUMBER_UNKNOWN', + 'tense' : 'TENSE_UNKNOWN', + 'form' : 'FORM_UNKNOWN', + 'proper' : 'PROPER_UNKNOWN', + 'voice' : 'VOICE_UNKNOWN', + 'gender' : 'GENDER_UNKNOWN' }, 'dependencyEdge': { 'headTokenIndex': 3, @@ -510,7 +543,18 @@ def test_analyze_syntax(self): 'beginOffset': -1, }, 'partOfSpeech': { - 'tag': 'NOUN', + 'aspect' : 'ASPECT_UNKNOWN', + 'reciprocity' : 'RECIPROCITY_UNKNOWN', + 'case' : 'CASE_UNKNOWN', + 'mood' : 'MOOD_UNKNOWN', + 'tag' : 'NOUN', + 'person' : 'PERSON_UNKNOWN', + 'number' : 'SINGULAR', + 'tense' : 'TENSE_UNKNOWN', + 'form' : 'FORM_UNKNOWN', + 'proper' : 'PROPER', + 'voice' : 'VOICE_UNKNOWN', + 'gender' : 'GENDER_UNKNOWN' }, 'dependencyEdge': { 'headTokenIndex': 1, From 6b196cb82ceadf2160261ebb2d63294a73799595 Mon Sep 17 00:00:00 2001 From: Daniel Valdivia Date: Mon, 29 May 2017 17:21:58 -0700 Subject: [PATCH 09/11] Test Document change getattr to object property access --- language/tests/unit/test_document.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/language/tests/unit/test_document.py b/language/tests/unit/test_document.py index 83e061d96d02..289ce9e09022 100644 --- a/language/tests/unit/test_document.py +++ b/language/tests/unit/test_document.py @@ -435,12 +435,12 @@ def test_analyze_sentiment(self): client._connection.api_request.assert_called_once_with( path='analyzeSentiment', method='POST', data=expected) - def _verify_token(self, token, text_content, part_of_speech, lemma): + def _verify_token(self, token, text_content, part_of_speech_tag, lemma): from google.cloud.language.syntax import Token self.assertIsInstance(token, Token) self.assertEqual(token.text_content, text_content) - self.assertEqual(token.part_of_speech['tag'], part_of_speech) + self.assertEqual(token.part_of_speech.tag, part_of_speech_tag) self.assertEqual(token.lemma, lemma) def test_analyze_syntax(self): From a5393a48f0579753a8b655fe85cd5f671de6a97e Mon Sep 17 00:00:00 2001 From: Daniel Valdivia Date: Mon, 29 May 2017 17:24:03 -0700 Subject: [PATCH 10/11] Document Tests --- language/tests/unit/test_document.py | 98 ++++++++++++++-------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/language/tests/unit/test_document.py b/language/tests/unit/test_document.py index 289ce9e09022..0b12d0955309 100644 --- a/language/tests/unit/test_document.py +++ b/language/tests/unit/test_document.py @@ -468,18 +468,18 @@ def test_analyze_syntax(self): 'beginOffset': -1, }, 'partOfSpeech': { - 'aspect' : 'ASPECT_UNKNOWN', - 'reciprocity' : 'RECIPROCITY_UNKNOWN', - 'case' : 'CASE_UNKNOWN', - 'mood' : 'MOOD_UNKNOWN', - 'tag' : 'NOUN', - 'person' : 'PERSON_UNKNOWN', - 'number' : 'SINGULAR', - 'tense' : 'TENSE_UNKNOWN', - 'form' : 'FORM_UNKNOWN', - 'proper' : 'PROPER', - 'voice' : 'VOICE_UNKNOWN', - 'gender' : 'GENDER_UNKNOWN' + 'aspect': 'ASPECT_UNKNOWN', + 'reciprocity': 'RECIPROCITY_UNKNOWN', + 'case': 'CASE_UNKNOWN', + 'mood': 'MOOD_UNKNOWN', + 'tag': 'NOUN', + 'person': 'PERSON_UNKNOWN', + 'number': 'SINGULAR', + 'tense': 'TENSE_UNKNOWN', + 'form': 'FORM_UNKNOWN', + 'proper': 'PROPER', + 'voice': 'VOICE_UNKNOWN', + 'gender': 'GENDER_UNKNOWN' }, 'dependencyEdge': { 'headTokenIndex': 0, @@ -493,18 +493,18 @@ def test_analyze_syntax(self): 'beginOffset': -1, }, 'partOfSpeech': { - 'aspect' : 'ASPECT_UNKNOWN', - 'reciprocity' : 'RECIPROCITY_UNKNOWN', - 'case' : 'CASE_UNKNOWN', - 'mood' : 'MOOD_UNKNOWN', - 'tag' : 'ADP', - 'person' : 'PERSON_UNKNOWN', - 'number' : 'NUMBER_UNKNOWN', - 'tense' : 'TENSE_UNKNOWN', - 'form' : 'FORM_UNKNOWN', - 'proper' : 'PROPER_UNKNOWN', - 'voice' : 'VOICE_UNKNOWN', - 'gender' : 'GENDER_UNKNOWN' + 'aspect': 'ASPECT_UNKNOWN', + 'reciprocity': 'RECIPROCITY_UNKNOWN', + 'case': 'CASE_UNKNOWN', + 'mood': 'MOOD_UNKNOWN', + 'tag': 'ADP', + 'person': 'PERSON_UNKNOWN', + 'number': 'NUMBER_UNKNOWN', + 'tense': 'TENSE_UNKNOWN', + 'form': 'FORM_UNKNOWN', + 'proper': 'PROPER_UNKNOWN', + 'voice': 'VOICE_UNKNOWN', + 'gender': 'GENDER_UNKNOWN' }, 'dependencyEdge': { 'headTokenIndex': 0, @@ -518,18 +518,18 @@ def test_analyze_syntax(self): 'beginOffset': -1, }, 'partOfSpeech': { - 'aspect' : 'ASPECT_UNKNOWN', - 'reciprocity' : 'RECIPROCITY_UNKNOWN', - 'case' : 'CASE_UNKNOWN', - 'mood' : 'MOOD_UNKNOWN', - 'tag' : 'DET', - 'person' : 'PERSON_UNKNOWN', - 'number' : 'NUMBER_UNKNOWN', - 'tense' : 'TENSE_UNKNOWN', - 'form' : 'FORM_UNKNOWN', - 'proper' : 'PROPER_UNKNOWN', - 'voice' : 'VOICE_UNKNOWN', - 'gender' : 'GENDER_UNKNOWN' + 'aspect': 'ASPECT_UNKNOWN', + 'reciprocity': 'RECIPROCITY_UNKNOWN', + 'case': 'CASE_UNKNOWN', + 'mood': 'MOOD_UNKNOWN', + 'tag': 'DET', + 'person': 'PERSON_UNKNOWN', + 'number': 'NUMBER_UNKNOWN', + 'tense': 'TENSE_UNKNOWN', + 'form': 'FORM_UNKNOWN', + 'proper': 'PROPER_UNKNOWN', + 'voice': 'VOICE_UNKNOWN', + 'gender': 'GENDER_UNKNOWN' }, 'dependencyEdge': { 'headTokenIndex': 3, @@ -543,18 +543,18 @@ def test_analyze_syntax(self): 'beginOffset': -1, }, 'partOfSpeech': { - 'aspect' : 'ASPECT_UNKNOWN', - 'reciprocity' : 'RECIPROCITY_UNKNOWN', - 'case' : 'CASE_UNKNOWN', - 'mood' : 'MOOD_UNKNOWN', - 'tag' : 'NOUN', - 'person' : 'PERSON_UNKNOWN', - 'number' : 'SINGULAR', - 'tense' : 'TENSE_UNKNOWN', - 'form' : 'FORM_UNKNOWN', - 'proper' : 'PROPER', - 'voice' : 'VOICE_UNKNOWN', - 'gender' : 'GENDER_UNKNOWN' + 'aspect': 'ASPECT_UNKNOWN', + 'reciprocity': 'RECIPROCITY_UNKNOWN', + 'case': 'CASE_UNKNOWN', + 'mood': 'MOOD_UNKNOWN', + 'tag': 'NOUN', + 'person': 'PERSON_UNKNOWN', + 'number': 'SINGULAR', + 'tense': 'TENSE_UNKNOWN', + 'form': 'FORM_UNKNOWN', + 'proper': 'PROPER', + 'voice': 'VOICE_UNKNOWN', + 'gender': 'GENDER_UNKNOWN' }, 'dependencyEdge': { 'headTokenIndex': 1, @@ -608,7 +608,7 @@ def _verify_tokens(self, annotations, token_info): self.assertIsInstance(token, Token) self.assertEqual(token.text_content, info[0]) self.assertEqual(token.text_begin, -1) - self.assertEqual(token.part_of_speech, info[1]) + self.assertEqual(token.part_of_speech.tag, info[1]) self.assertEqual(token.edge_index, info[2]) self.assertEqual(token.edge_label, info[3]) self.assertEqual(token.lemma, info[0]) From 5b181da1e6c59c6337f46e2d08b4349c7b32d098 Mon Sep 17 00:00:00 2001 From: Daniel Valdivia Date: Wed, 31 May 2017 13:47:18 -0700 Subject: [PATCH 11/11] Improved docstrings. Rearraged imports --- language/google/cloud/language/syntax.py | 43 ++++++++++++++++------- language/tests/unit/test_api_responses.py | 2 +- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/language/google/cloud/language/syntax.py b/language/google/cloud/language/syntax.py index 622bd084584d..037a6a74f298 100644 --- a/language/google/cloud/language/syntax.py +++ b/language/google/cloud/language/syntax.py @@ -21,43 +21,60 @@ class PartOfSpeech(object): """A Google Cloud Natural Language API Part of speech object. - Rich data about the Part of Speech of a token returned by the API + + These are the grammatical categories of the matched token in + the sentence. https://cloud.google.com/natural-language/docs\ + /reference/rest/v1/Token#PartOfSpeech :type aspect: str - :param aspect: Aspect + :param aspect: The grammatical aspect. https://cloud.google\ + .com/natural-language/docs/reference/rest/v1/\ + Token#Aspect :type reciprocity: str - :param reciprocity: Reciprocity + :param reciprocity: The grammatical reciprocity. https://\ + cloud.google.com/natural-language/docs/reference\ + /rest/v1/Token#Reciprocity :type case: str - :param case: Case + :param case: The grammatical case. https://cloud.google.com/\ + natural-language/docs/reference/rest/v1/Token#Case :type mood: str - :param mood: Mood + :param mood: The grammatical mood. https://cloud.google.com/\ + natural-language/docs/reference/rest/v1/Token#Mood :type tag: str - :param tag: Tag + :param tag: The part of speech tag. https://cloud.google.com/natural\ + -language/docs/reference/rest/v1/Token#Tag :type person: str - :param person: Person + :param person: The grammatical person. https://cloud.google.com/\ + natural-language/docs/reference/rest/v1/Token#Person :type number: str - :param number: Number + :param number: The grammatical number. https://cloud.google.com/natural\ + -language/docs/reference/rest/v1/Token#Number :type tense: str - :param tense: Tense + :param tense: The grammatical tense. https://cloud.google.com/natural\ + -language/docs/reference/rest/v1/Token#Tense :type form: str - :param form: Form + :param form: The grammatical form. https://cloud.google.com/natural\ + -language/docs/reference/rest/v1/Token#Form :type proper: str - :param proper: Proper + :param proper: The grammatical properness. https://cloud.google.com/\ + natural-language/docs/reference/rest/v1/Token#Proper :type voice: str - :param voice: Voice + :param voice: The grammatical voice. https://cloud.google.com/\ + natural-language/docs/reference/rest/v1/Token#Voice :type gender: str - :param gender: Gender + :param gender: The grammatical gender. https://cloud.google.com/\ + natural-language/docs/reference/rest/v1/Token#Gender """ UNKNOWN = 'UNKNOWN' diff --git a/language/tests/unit/test_api_responses.py b/language/tests/unit/test_api_responses.py index de542d5f3a77..4b79ec923fee 100644 --- a/language/tests/unit/test_api_responses.py +++ b/language/tests/unit/test_api_responses.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import unittest from copy import copy +import unittest class TestEntityResponse(unittest.TestCase):