Skip to content

Commit 7730614

Browse files
committed
Adding system test for language Document.analyze_entities().
1 parent a34109d commit 7730614

File tree

4 files changed

+77
-5
lines changed

4 files changed

+77
-5
lines changed

docs/language-usage.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,17 @@ metadata and other properties.
179179
name: Michelangelo Caravaggio
180180
type: PERSON
181181
metadata: {'wikipedia_url': 'http://en.wikipedia.org/wiki/Caravaggio'}
182-
salience: 0.75942981
182+
salience: 0.7615959
183183
====================
184184
name: Italian
185185
type: LOCATION
186186
metadata: {'wikipedia_url': 'http://en.wikipedia.org/wiki/Italy'}
187-
salience: 0.20193423
187+
salience: 0.19960518
188188
====================
189189
name: The Calling of Saint Matthew
190-
type: WORK_OF_ART
191-
metadata: {'wikipedia_url': 'http://en.wikipedia.org/wiki/index.html?curid=2838808'}
192-
salience: 0.03863598
190+
type: EVENT
191+
metadata: {'wikipedia_url': 'http://en.wikipedia.org/wiki/The_Calling_of_St_Matthew_(Caravaggio)'}
192+
salience: 0.038798928
193193
194194
Analyze Sentiment
195195
-----------------

system_tests/attempt_system_tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
'storage',
3636
'bigquery',
3737
'pubsub',
38+
'language',
3839
'logging',
3940
'translate',
4041
'monitoring',

system_tests/language.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
from gcloud import language
18+
19+
from system_test_utils import unique_resource_id
20+
21+
22+
class Config(object):
23+
"""Run-time configuration to be modified at set-up.
24+
25+
This is a mutable stand-in to allow test set-up to modify
26+
global state.
27+
"""
28+
CLIENT = None
29+
30+
31+
def setUpModule():
32+
Config.CLIENT = language.Client()
33+
34+
35+
class TestLanguage(unittest.TestCase):
36+
37+
def test_analyze_entities(self):
38+
from gcloud.language.entity import EntityType
39+
40+
text_content = ("Michelangelo Caravaggio, Italian painter, is "
41+
"known for 'The Calling of Saint Matthew'.")
42+
document = Config.CLIENT.document_from_text(text_content)
43+
entities = document.analyze_entities()
44+
self.assertEqual(len(entities), 3)
45+
entity1, entity2, entity3 = entities
46+
# Verify entity 1.
47+
self.assertEqual(entity1.name, 'Michelangelo Caravaggio')
48+
self.assertEqual(entity1.entity_type, EntityType.PERSON)
49+
self.assertTrue(0.7 < entity1.salience < 0.8)
50+
self.assertEqual(entity1.mentions, [entity1.name])
51+
self.assertEqual(entity1.metadata, {
52+
'wikipedia_url': 'http://en.wikipedia.org/wiki/Caravaggio',
53+
})
54+
# Verify entity 2.
55+
self.assertEqual(entity2.name, 'Italian')
56+
self.assertEqual(entity2.entity_type, EntityType.LOCATION)
57+
self.assertTrue(0.15 < entity2.salience < 0.25)
58+
self.assertEqual(entity2.mentions, [entity2.name])
59+
self.assertEqual(entity2.metadata, {
60+
'wikipedia_url': 'http://en.wikipedia.org/wiki/Italy',
61+
})
62+
# Verify entity 3.
63+
self.assertEqual(entity3.name, 'The Calling of Saint Matthew')
64+
self.assertEqual(entity3.entity_type, EntityType.EVENT)
65+
self.assertTrue(0 < entity3.salience < 0.1)
66+
self.assertEqual(entity3.mentions, [entity3.name])
67+
wiki_url = ('http://en.wikipedia.org/wiki/'
68+
'The_Calling_of_St_Matthew_(Caravaggio)')
69+
self.assertEqual(entity3.metadata, {'wikipedia_url': wiki_url})

system_tests/run_system_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import bigquery
2020
import bigtable
2121
import datastore
22+
import language
2223
import logging_
2324
import monitoring
2425
import pubsub
@@ -33,6 +34,7 @@
3334
'pubsub': pubsub,
3435
'bigquery': bigquery,
3536
'bigtable': bigtable,
37+
'language': language,
3638
'logging': logging_,
3739
'monitoring': monitoring,
3840
'translate': translate,

0 commit comments

Comments
 (0)