-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Natural Language Entity for responses.
- Loading branch information
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |