def test_related_concepts(self): related_concepts = fac.concept(num=2) c = Concept('Argos') concept_associations = [ConceptConceptAssociation(concept, 1.0) for concept in related_concepts] c.concept_associations = concept_associations for concept in c.concepts: self.assertTrue(c in concept.from_concepts) self.assertTrue(concept in c.concepts)
def test_generates_related_concepts_when_concepts_getter_called(self): c = Concept('Some Concept') c.summary = 'this is a summary Clinton' # At first there should not be any concept assocations. self.assertEqual(len(c.concept_associations), 0) # But when you call the `concepts` getter, # the concept associations are generated. self.assertEqual(len(c.concepts), 1) self.assertEqual(len(c.concept_associations), 1) self.assertEqual(c.concepts[0].names, ['Clinton'])
def test_sets_downloaded_image_url(self): # Check that the concept's image url is # set to the url our mock S3 returns. self._patch_knowledge_for() self._patch_uri_for_name(self.uri) c = Concept('Argos') self.assertEqual(c.image, 'https://s3.amazon.com/fakeimage.jpg')
def test_sets_properties_from_knowledge(self): self._patch_knowledge_for() self._patch_uri_for_name(self.uri) c = Concept('Argos') self.assertEqual(c.summary, 'this is a fake.summary for uri {0}'.format(self.uri)) self.assertEqual(c.name, 'Canonical name')
def test_creates_alias(self): # Patch these methods so # tests don't require the Fuseki server # or a network connection to Wikipedia. self._patch_knowledge_for() self._patch_uri_for_name(self.uri) c = Concept('Argos') self.db.session.add(c) save() self.assertEqual(len(c.aliases), 1) self.assertEqual(Alias.query.count(), 1) self.assertEqual(Alias.query.first().name, 'Argos')
def conceptize(self): """ Process the article text for concepts, and add the appropriate mentions. """ concepts = [] for c_name in gx.concepts(self.text): # Search for the concept. uri = knowledge.uri_for_name(c_name) if uri: slug = uri.split('/')[-1] else: slug = slugify(c_name) c = Concept.query.get(slug) # If an concept is found... if c: # Add this name as a new alias, if necessary. alias = Alias.query.filter_by(name=c_name, concept=c).first() if not alias: alias = Alias(c_name) c.aliases.append(alias) # Avoid duplicate aliases. if alias not in self.mentions: self.mentions.append(alias) # If one doesn't exist, create a new one. if not c: c = Concept(c_name) self.mentions.append(c.aliases[0]) db.session.add(c) db.session.commit() concepts.append(c) # Score the concepts' importance. total_found = len(concepts) counter = Counter(concepts) uniq_concepts = set(concepts) assocs = [] for concept in uniq_concepts: score = counter[concept] / total_found assoc = ArticleConceptAssociation(concept, score) assocs.append(assoc) self.concept_associations = assocs
def test_sets_commonness(self): self._patch_knowledge_for() self._patch_uri_for_name(self.uri) c = Concept('Argos') self.assertEqual(c.commonness, 0.0)
def test_sets_slug_from_uri(self): self._patch_knowledge_for() self._patch_uri_for_name(self.uri) c = Concept('Argos') self.assertEqual(c.slug, 'argos_argos')