Skip to content

Commit

Permalink
Adding Natural Language Entity for responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Aug 23, 2016
1 parent 094c071 commit 1e5c6dd
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
language-usage
Client <language-client>
language-document
language-responses

.. toctree::
:maxdepth: 0
Expand Down
9 changes: 9 additions & 0 deletions docs/language-responses.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Natural Language Response Classes
=================================

Entity
~~~~~~

.. automodule:: gcloud.language.entity
:members:
:show-inheritance:
84 changes: 84 additions & 0 deletions gcloud/language/entity.py
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions gcloud/language/test_entity.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 1e5c6dd

Please sign in to comment.