def get_all_tuples_of_relations(self): ''' Returns list of tuples of relations which connects 'self.concepts' sequence in the path. ''' relations_tuples = [] for index, (concept1, concept2) in enumerate(pairwise(self.concepts)): search = Search(start=concept1, end=concept2) data = search.search() result = Result(data) if result.get_num_found() > 0: edges = result.parse_all_edges() if index == 0: for e in edges: if not [e.rel] in relations_tuples: relations_tuples.append([e.rel]) else: for relation_tuple in relations_tuples: if len(relation_tuple) == index: for e in edges: relation_tuple_copy = copy.deepcopy( relation_tuple) relation_tuple_copy.append(e.rel) if not relation_tuple_copy in relations_tuples: relations_tuples.append( relation_tuple_copy) for relation_tuple in relations_tuples: if len(relation_tuple) != len(self.concepts) - 1: relations_tuples.remove(relation_tuple) return relations_tuples
def get_all_tuples_of_relations(self): ''' Returns list of tuples of relations which connects 'self.concepts' sequence in the path. ''' relations_tuples = [] for index, (concept1, concept2) in enumerate(pairwise(self.concepts)): search = Search(start=concept1, end=concept2) data = search.search() result = Result(data) if result.get_num_found() > 0: edges = result.parse_all_edges() if index == 0: for e in edges: if not [e.rel] in relations_tuples: relations_tuples.append([e.rel]) else: for relation_tuple in relations_tuples: if len(relation_tuple) == index: for e in edges: relation_tuple_copy = copy.deepcopy(relation_tuple) relation_tuple_copy.append(e.rel) if not relation_tuple_copy in relations_tuples: relations_tuples.append(relation_tuple_copy) for relation_tuple in relations_tuples: if len(relation_tuple) != len(self.concepts)-1: relations_tuples.remove(relation_tuple) return relations_tuples
def demonstrate_search(): print 'Demonstrating Search API' s = Search(rel='/c/en/be_often_compare_to') data = s.search() r = Result(data) r.print_raw_result() print s = Search(text='mariah carey', surfaceText='dion', something='anything') data = s.search() r = Result(data) r.print_raw_result() print
def get_all_tuples_of_concepts(self): ''' Returns list of tuples of concepts connected by 'self.relations' sequence in the path. ''' graph = nx.DiGraph() for relation in self.relations: search = Search(rel=relation, limit=1000) data = search.search() result = Result(data) edges = result.parse_all_edges() for e in edges: graph.add_node(e.start) graph.add_node(e.end) graph.add_edge(e.start, e.end) graph[e.start][e.end]['relation'] = relation concepts_tuples = [] for index, r in enumerate(self.relations): for edge in graph.edges(data=True): start = edge[0] end = edge[1] relation = edge[2]['relation'] if relation == r: if index == 0: concept_tuple = [start, end] concepts_tuples.append(concept_tuple) else: for concept_tuple in concepts_tuples: if len(concept_tuple) == index + 1: concepts_tuples.remove(concept_tuple) next_candidates = [] for next_edge in graph.edges(data=True): next_edge_start = next_edge[0] next_edge_end = next_edge[1] next_edge_relation = next_edge[2][ 'relation'] if next_edge_relation == r and concept_tuple[ -1] == next_edge_start: next_candidates.append(next_edge_end) for candidate in next_candidates: concept_tuple_copy = copy.deepcopy( concept_tuple) concept_tuple_copy.append(candidate) concepts_tuples.append(concept_tuple_copy) return concepts_tuples
def does_exist(self, print_where_breaks=False): ''' Checks whether this path exists in ConceptNet5. :param print_where_breaks: prints the assertion, if the assertion does not exist in the path. ''' for assertion in self.assertions: search = Search(start=assertion.start, rel=assertion.relation, end=assertion.end, limit=1000) data = search.search() result = Result(data) if result.get_num_found() == 0: if print_where_breaks == True: print('Assertion breaking the path: [ %s --> (%s) --> %s) ]' % ( assertion.start, assertion.relation, assertion.end)) return False return True
def get_all_tuples_of_concepts(self): ''' Returns list of tuples of concepts connected by 'self.relations' sequence in the path. ''' graph = nx.DiGraph() for relation in self.relations: search = Search(rel=relation, limit=1000) data = search.search() result = Result(data) edges = result.parse_all_edges() for e in edges: graph.add_node(e.start) graph.add_node(e.end) graph.add_edge(e.start, e.end) graph[e.start][e.end]['relation'] = relation concepts_tuples = [] for index, r in enumerate(self.relations): for edge in graph.edges(data=True): start = edge[0] end = edge[1] relation = edge[2]['relation'] if relation == r: if index == 0: concept_tuple = [start, end] concepts_tuples.append(concept_tuple) else: for concept_tuple in concepts_tuples: if len(concept_tuple) == index+1: concepts_tuples.remove(concept_tuple) next_candidates = [] for next_edge in graph.edges(data=True): next_edge_start = next_edge[0] next_edge_end = next_edge[1] next_edge_relation = next_edge[2]['relation'] if next_edge_relation == r and concept_tuple[-1] == next_edge_start: next_candidates.append(next_edge_end) for candidate in next_candidates: concept_tuple_copy = copy.deepcopy(concept_tuple) concept_tuple_copy.append(candidate) concepts_tuples.append(concept_tuple_copy) return concepts_tuples
def does_exist(self, print_where_breaks=False): ''' Checks whether this path exists in ConceptNet5. :param print_where_breaks: prints the assertion, if the assertion does not exist in the path. ''' for assertion in self.assertions: search = Search(start=assertion.start, rel=assertion.relation, end=assertion.end, limit=1000) data = search.search() result = Result(data) if result.get_num_found() == 0: if print_where_breaks == True: print 'Assertion breaking the path: [ %s --> (%s) --> %s) ]' % ( assertion.start, assertion.relation, assertion.end) return False return True
from conceptnet5_client.web.api import Search # Search for the edges whose relation is 'be often compared to' s = Search(rel='/c/en/be_often_compare_to') data = s.search() r = Result(data) # print results in key = value format r.print_raw_results() # Search for any edge whose any of 'startLemmas', 'endLemmas' or 'relLemmas' matches # 'mariah carey' and whose 'surfaceText' matches 'dion'. Here the arg'something' is # not supported, so it will be ignored constructing query URL. s = Search(text='mariah carey', surfaceText='dion', something='anything') data = s.search() r = Result(data) # print results in key = value format r.print_raw_results()