def test_one_way_relation(_connect_to_conceptnet): russian = Language.get(name='ru') heaven_concepts = Label.get(text='рай', language=russian).concepts hell_concepts = Label.get(text='ад', language=russian).concepts result = list(edges_between(heaven_concepts, hell_concepts, two_way=False)) assert len(result) == 1 and result[0].relation.uri == Relation( name="antonym").uri
def test_two_ways_relation(_connect_to_conceptnet): russian = Language.get(name='ru') heaven_concepts = Label.get(text='рай', language=russian).concepts hell_concepts = Label.get(text='ад', language=russian).concepts result = list(edges_between(heaven_concepts, hell_concepts, two_way=True)) assert len(result) == 2 and [i.relation.uri for i in result] == [ Relation(name="antonym").uri, Relation(name="antonym").uri ]
def get_surface_text(self, phrase_1, phrase_2, relation): try: node1 = self._get_node(phrase_1).concepts node2 = self._get_node(phrase_2).concepts except DoesNotExist: return '' for e in edges_between(node1, node2, two_way=False): if camelize(e.relation.name) == relation: text: str = e.etc['surfaceText'] if text: return text.replace('[[', '').replace(']]', '').replace('*', '') return ''
def get_relations_between(self, phrase_1, phrase_2): relations = [] try: concept_1 = self._get_node(phrase_1).concepts concept_2 = self._get_node(phrase_2).concepts except DoesNotExist: return relations for e in edges_between(concept_1, concept_2, two_way=False): relations.append(camelize(e.relation.name)) return relations
import conceptnet_lite from conceptnet_lite import Label, Language, edges_for, edges_between base_dir_path = Path('~/conceptnet-lite-data') conceptnet_lite.connect( db_path=base_dir_path / 'conceptnet.db', dump_dir_path=base_dir_path, ) russian = Language.get(name='ru') print("All edges between 'рай' и 'ад':") heaven_concepts = Label.get(text='рай', language=russian).concepts hell_concepts = Label.get(text='ад', language=russian).concepts for e in edges_between(heaven_concepts, hell_concepts, two_way=True): print(" Edge URI:", e.uri) print(" Relation:", e.relation.name) english = Language.get(name='en') print("Get edges for 'introvert':") introvert_concepts = Label.get(text='introvert', language=english).concepts for e in edges_for(introvert_concepts, same_language=True): print(" Edge URI:", e.uri) print("Traversing Russian:") for l in russian.labels: print(" Label:", l.text) for c in l.concepts: print(" Concept URI:", c.uri)