def parse_all_edges(self, clean_self_ref=True): ''' Parses the edges for this result object, initiates an edge object for each and returns list of these edge objects. :param clean_self_ref: If it is set, the self referencing edges with any kind of relation, such as '/c/en/see_movie /r/Causes/ /c/en/see_move', will not be included in the returned list. ''' edges = [] if 'edges' not in self.result: print_debug( 'This result does not have any edge! Printing raw result...', 'ResultTypeError') self.print_raw_result() return intered_conception = '/c/' + intered_lang for edge_str in self.result['edges']: e = Edge(edge_str) # I care only Chinese. you can do as you like if not e.start.startswith( intered_conception) or not e.end.startswith( intered_conception): continue if clean_self_ref: if e.start != e.end: edges.append(e) else: edges.append(e) return edges
def print_specific_attr(self, attr): """ :param attr: attribute you want to look up :return: key related value """ print_debug('{}'.format(getattr(self, attr)))
def search(self): ''' Constructs the search url for this instance of search object with specified query args and returns the result of the request in json format. ''' url = ''.join(['%s%s' % (settings.BASE_SEARCH_URL, '?')]) + self.encoded_query_args print_debug(url, 'url') json_data = make_http_request(url) return json_data
def __init__(self, **kwargs): query_args = {} for key, value in kwargs.items(): if is_arg_valid(key, settings.SUPPORTED_SEARCH_ARGS): query_args[key] = value # print_debug('%s : %s' % (key, value), 'arg') else: print_debug('%s : %s -- THIS ARG IS NOT SUPPORTED!' % (key, value), 'ArgError') self.encoded_query_args = urllib.parse.urlencode(query_args)
def get_similar_concepts(self, concept): ''' Returns the similar concepts with similarity scores for this passed 'concept' in json format. :param concept: a concept word or phrase, e.g. 'toast', 'see movie' etc. ''' url = ''.join(['%s/c/%s/%s?' % (settings.BASE_ASSOCIATION_URL, self.lang, concept)]) + self.encoded_query_args print_debug(url, 'url') json_data = make_http_request(url) return json_data
def get_similar_concepts_by_term_list(self, term_list): ''' Returns the similar concepts with similarity scores for this term_list in json format. :param term_list: a list of concepts. ''' terms = ','.join(term_list) url = ''.join(['%s/list/%s/%s?' % (settings.BASE_ASSOCIATION_URL, self.lang, terms)]) + self.encoded_query_args print_debug(url, 'url') json_data = make_http_request(url) return json_data
def __init__(self, json_data): result = {} for key, value in json_data.items(): if is_arg_valid(key, SUPPORTED_KEYS): result[key] = value # print_debug('%s : %s' % (key, value), 'arg') else: print_debug( '%s : %s -- THIS KEY IS NOT SUPPORTED!' % (key, value), 'KeyError') self.result = result
def search_concept(self, concept): ''' Constructs the search url for this instance of lookup object with specified query args and specified concept and finally returns the result of the request in json format. :param concept: a concept word or phrase, e.g. 'toast', 'see movie' etc. ''' concept = concept.replace(' ', '_') url = ''.join( ['%s/%s/%s/%s?' % (settings.BASE_LOOKUP_URL, self.Type, self.lang, concept)]) + self.encoded_query_args print_debug(url, 'url') json_data = make_http_request(url) return json_data
def search_source(self, source_uri=None): ''' Constructs the search url for this instance of lookup object with specified query args and specified uri and finally returns the 50 statements submitted by this source. :param source_uri: a uri specifying the source, e.g. '/s/contributor/omcs/rspeer', '/s/wordnet/3.0', '/s/rule/sum_edges' etc. ''' if source_uri: url = ''.join(['%s%s' % (settings.BASE_LOOKUP_URL, source_uri)]) print(url) json_data = make_http_request(url) return json_data else: print_debug('You should pass argument \'source\'.', 'ArgError') sys.exit()
def print_raw_result(self): ''' Prints the result of the query in key = value manner without any processing. ''' for key, value in self.result.items(): print_debug('%s = %s' % (key, value))
def print_all_attrs(self): ''' Prints all attributes regarding to this edge. ''' attrs = vars(self) print_debug('\n'.join('%s: %s' % item for item in attrs.items()))
def print_edge(self): ''' Prints the normalized edge data with start node, rel, end node. ''' print_debug('(%s -> %s -> %s)' % (self.start, self.rel, self.end))
def print_assertion(self): ''' Prints the lemmas of this edge with start, rel, end lemmas. ''' print_debug('%s %s %s' % (self.start, self.rel, self.end))