def gather(webpage): kales = Kales(API_KEY) data = kales.analyze(webpage['content'].encode('utf-8')) terms = list() entity_tuples= [(entity['relevance'], entity['name'])for entity in data["entities"]] for entity in sorted(entity_tuples, key=lambda entity: entity[0], reverse=True)[:num_of_entities]: #sort by relevance terms.extend(get_terms(entity[1])) return {'terms': {'$in': terms}}
class KalesTest(TestCase): def setUp(self): self.calais = Kales("123456789") @patch.object(Kales, '_request') def test_analyze(self, mock): mock.return_value = MockResponse() data = self.calais.analyze("Barack Obama") self.assertTrue("entities" in data) self.assertEqual(len(data["entities"]), 1) def test_analyze_empty(self): data = self.calais.analyze(" ") self.assertEqual(data, None)
def setUp(self): self.calais = Kales("123456789")