diff --git a/docs/index.rst b/docs/index.rst index 38a12478ae0ba..f504faaa572ee 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -156,6 +156,7 @@ language-usage Client language-document + language-responses .. toctree:: :maxdepth: 0 diff --git a/docs/language-responses.rst b/docs/language-responses.rst new file mode 100644 index 0000000000000..ee6b30e9f3b3b --- /dev/null +++ b/docs/language-responses.rst @@ -0,0 +1,9 @@ +Natural Language Response Classes +================================= + +Entity +~~~~~~ + +.. automodule:: gcloud.language.entity + :members: + :show-inheritance: diff --git a/gcloud/language/entity.py b/gcloud/language/entity.py new file mode 100644 index 0000000000000..2c4a16648b257 --- /dev/null +++ b/gcloud/language/entity.py @@ -0,0 +1,84 @@ +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Definition for Google Cloud Natural Language API entities. + +An entity is used to describe a proper name extracted from text. +""" + + +class EntityType(object): + """List of possible entity types.""" + + UNKNOWN = 'UNKNOWN' + """Unknown entity type.""" + + PERSON = 'PERSON' + """Person entity type.""" + + LOCATION = 'LOCATION' + """Location entity type.""" + + ORGANIZATION = 'ORGANIZATION' + """Organization entity type.""" + + EVENT = 'EVENT' + """Event entity type.""" + + WORK_OF_ART = 'WORK_OF_ART' + """Work of art entity type.""" + + CONSUMER_GOOD = 'CONSUMER_GOOD' + """Consumer good entity type.""" + + OTHER = 'OTHER' + """Other entity type (i.e. known but not classified).""" + + + +class Entity(object): + """A Google Cloud Natural Language API entity. + + Represents a phrase in text that is a known entity, such as a person, + an organization, or location. The API associates information, such as + salience and mentions, with entities. + + See: + https://cloud.google.com/natural-language/reference/rest/v1beta1/Entity + + :type name: str + :param name: The name / phrase identified as the entity. + + :type entity_type: str + :param entity_type: The type of the entity. See + https://cloud.google.com/natural-language/\ + reference/rest/v1beta1/Entity#Type + + :type metadata: dict + :param metadata: The metadata associated with the entity. + + :type salience: float + :param salience: The prominence of the entity / phrase within the text + containing it. + + :type mentions: list + :param mentions: List of strings that mention the entity. + """ + + def __init__(self, name, entity_type, metadata, salience, mentions): + self.name = name + self.entity_type = entity_type + self.metadata = metadata + self.salience = salience + self.mentions = mentions diff --git a/gcloud/language/test_entity.py b/gcloud/language/test_entity.py new file mode 100644 index 0000000000000..3ba644669de1e --- /dev/null +++ b/gcloud/language/test_entity.py @@ -0,0 +1,39 @@ +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + + +class TestEntity(unittest.TestCase): + + def _getTargetClass(self): + from gcloud.language.entity import Entity + return Entity + + def _makeOne(self, *args, **kw): + return self._getTargetClass()(*args, **kw) + + def test_constructor_defaults(self): + name = 'Italian' + entity_type = 'LOCATION' + metadata = {'wikipedia_url': 'http://en.wikipedia.org/wiki/Italy'} + salience = 0.19960518 + mentions = ['Italian'] + entity = self._makeOne(name, entity_type, metadata, + salience, mentions) + self.assertEqual(entity.name, name) + self.assertEqual(entity.entity_type, entity_type) + self.assertEqual(entity.metadata, metadata) + self.assertEqual(entity.salience, salience) + self.assertEqual(entity.mentions, mentions)