Skip to content

Commit 1e5c6dd

Browse files
committed
Adding Natural Language Entity for responses.
1 parent 094c071 commit 1e5c6dd

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
language-usage
157157
Client <language-client>
158158
language-document
159+
language-responses
159160

160161
.. toctree::
161162
:maxdepth: 0

docs/language-responses.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Natural Language Response Classes
2+
=================================
3+
4+
Entity
5+
~~~~~~
6+
7+
.. automodule:: gcloud.language.entity
8+
:members:
9+
:show-inheritance:

gcloud/language/entity.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Definition for Google Cloud Natural Language API entities.
16+
17+
An entity is used to describe a proper name extracted from text.
18+
"""
19+
20+
21+
class EntityType(object):
22+
"""List of possible entity types."""
23+
24+
UNKNOWN = 'UNKNOWN'
25+
"""Unknown entity type."""
26+
27+
PERSON = 'PERSON'
28+
"""Person entity type."""
29+
30+
LOCATION = 'LOCATION'
31+
"""Location entity type."""
32+
33+
ORGANIZATION = 'ORGANIZATION'
34+
"""Organization entity type."""
35+
36+
EVENT = 'EVENT'
37+
"""Event entity type."""
38+
39+
WORK_OF_ART = 'WORK_OF_ART'
40+
"""Work of art entity type."""
41+
42+
CONSUMER_GOOD = 'CONSUMER_GOOD'
43+
"""Consumer good entity type."""
44+
45+
OTHER = 'OTHER'
46+
"""Other entity type (i.e. known but not classified)."""
47+
48+
49+
50+
class Entity(object):
51+
"""A Google Cloud Natural Language API entity.
52+
53+
Represents a phrase in text that is a known entity, such as a person,
54+
an organization, or location. The API associates information, such as
55+
salience and mentions, with entities.
56+
57+
See:
58+
https://cloud.google.com/natural-language/reference/rest/v1beta1/Entity
59+
60+
:type name: str
61+
:param name: The name / phrase identified as the entity.
62+
63+
:type entity_type: str
64+
:param entity_type: The type of the entity. See
65+
https://cloud.google.com/natural-language/\
66+
reference/rest/v1beta1/Entity#Type
67+
68+
:type metadata: dict
69+
:param metadata: The metadata associated with the entity.
70+
71+
:type salience: float
72+
:param salience: The prominence of the entity / phrase within the text
73+
containing it.
74+
75+
:type mentions: list
76+
:param mentions: List of strings that mention the entity.
77+
"""
78+
79+
def __init__(self, name, entity_type, metadata, salience, mentions):
80+
self.name = name
81+
self.entity_type = entity_type
82+
self.metadata = metadata
83+
self.salience = salience
84+
self.mentions = mentions

gcloud/language/test_entity.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
17+
18+
class TestEntity(unittest.TestCase):
19+
20+
def _getTargetClass(self):
21+
from gcloud.language.entity import Entity
22+
return Entity
23+
24+
def _makeOne(self, *args, **kw):
25+
return self._getTargetClass()(*args, **kw)
26+
27+
def test_constructor_defaults(self):
28+
name = 'Italian'
29+
entity_type = 'LOCATION'
30+
metadata = {'wikipedia_url': 'http://en.wikipedia.org/wiki/Italy'}
31+
salience = 0.19960518
32+
mentions = ['Italian']
33+
entity = self._makeOne(name, entity_type, metadata,
34+
salience, mentions)
35+
self.assertEqual(entity.name, name)
36+
self.assertEqual(entity.entity_type, entity_type)
37+
self.assertEqual(entity.metadata, metadata)
38+
self.assertEqual(entity.salience, salience)
39+
self.assertEqual(entity.mentions, mentions)

0 commit comments

Comments
 (0)