|
| 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}) |
0 commit comments