示例#1
0
    def parse_all_edges(self, clean_self_ref = False):
        '''
        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.

        TODO: FIXUP THIS STUPID API TO MAKE SURE WE CAN PARSE THE EDGE DATA!!!!!!!
        '''
        edges = []

        if not 'edges' in self.result:
            print_debug('This result does not have any edge! Printing raw result...', 'ResultTypeError')
            self.print_raw_result()
            return

        for edge_str in self.result['edges']:
            e = Edge(edge_str)
            if clean_self_ref:
                if e.start != e.end:
                    edges.append(e)
            else:
                edges.append(e)
        return edges
示例#2
0
 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
示例#3
0
 def __init__(self, **kwargs):
     query_args = {}
     for key, value in kwargs.iteritems():
         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.urlencode(query_args)
示例#4
0
 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
示例#5
0
 def __init__(self, lang = 'en', **kwargs):
     query_args = {}
     for key, value in kwargs.iteritems():
         if is_arg_valid(key, settings.SUPPORTED_ASSOCIATION_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.urlencode(query_args)
     self.lang = lang
示例#6
0
    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
示例#7
0
    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
示例#8
0
    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, url.encode('utf-8'))
        return json_data
示例#9
0
    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
示例#10
0
 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
示例#11
0
    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/c/%s/%s?' % (settings.BASE_LOOKUP_URL, self.lang, concept)]) + self.encoded_query_args
        print_debug(url, 'url')
        json_data = make_http_request(url, url.encode('utf-8'))
        return json_data
示例#12
0
    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/c/%s/%s?' % (settings.BASE_LOOKUP_URL, self.lang, concept)]) + self.encoded_query_args
        print_debug(url, 'url')
        json_data = make_http_request(url)
        return json_data
示例#13
0
def make_http_request(url):
    """
    Makes and http request to the 'url', if response to that
    'url' is not cached yet.

    Returns the response in json format.
    """
    request = urllib2.Request(url)
    try:
        data = urllib2.urlopen(request)
    except HTTPError, e:
        print_debug("Error code: %s" % e.code, "HTTPError")
        sys.exit()
示例#14
0
def make_http_request(url):
    '''
    Makes and http request to the 'url', if response to that
    'url' is not cached yet.

    Returns the response in json format.
    '''
    request = urllib2.Request(url)
    try:
        data = urllib2.urlopen(request)
    except HTTPError, e:
        print_debug('Error code: %s' % e.code, 'HTTPError')
        sys.exit()
示例#15
0
    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()
示例#16
0
    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()
示例#17
0
def make_http_request(url, urlenc):
    '''
    Makes and http request to the 'url', if response to that
    'url' is not cached yet.

    Returns the response in json format.
    '''
    request = urllib.request.Request(url)
    try:
        data = urllib.request.urlopen(request)
        datastr = data.readall().decode('utf-8')
    except HTTPError as e:
        print_debug('Error code: %s' % e.code, 'HTTPError')
        sys.exit()
    except URLError as e:
        print_debug('Reason: %s' % e.reason, 'URLError')
        sys.exit()
    return json.loads(datastr)
示例#18
0
try:
    import simplejson as json
except ImportError:
    import json

from urllib2 import HTTPError, URLError

from conceptnet5_client.utils.debug import print_debug
from conceptnet5_client.cache.file_cache import cache


@cache
def make_http_request(url):
    '''
    Makes and http request to the 'url', if response to that
    'url' is not cached yet.

    Returns the response in json format.
    '''
    request = urllib2.Request(url)
    try:
        data = urllib2.urlopen(request)
    except HTTPError, e:
        print_debug('Error code: %s' % e.code, 'HTTPError')
        sys.exit()
    except URLError, e:
        print_debug('Reason: %s' % e.reason, 'URLError')
        sys.exit()
    return json.load(data)
示例#19
0
 def print_raw_result(self):
     '''
     Prints the result of the query in key = value manner without any processing.
     '''
     for key, value in self.result.iteritems():
         print_debug('%s = %s' % (key, value))
示例#20
0
try:
    import simplejson as json
except ImportError:
    import json

from urllib2 import HTTPError, URLError

from conceptnet5_client.utils.debug import print_debug
from conceptnet5_client.cache.file_cache import cache


@cache
def make_http_request(url):
    """
    Makes and http request to the 'url', if response to that
    'url' is not cached yet.

    Returns the response in json format.
    """
    request = urllib2.Request(url)
    try:
        data = urllib2.urlopen(request)
    except HTTPError, e:
        print_debug("Error code: %s" % e.code, "HTTPError")
        sys.exit()
    except URLError, e:
        print_debug("Reason: %s" % e.reason, "URLError")
        sys.exit()
    return json.load(data)
示例#21
0
 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()))
示例#22
0
 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))
示例#23
0
 def print_assertion(self):
     '''
     Prints the lemmas of this edge with start, rel, end lemmas.
     '''
     print_debug('%s %s %s' %
                 (self.start_lemmas, self.rel, self.end_lemmas))
示例#24
0
 def print_assertion(self):
     '''
     Prints the lemmas of this edge with start, rel, end lemmas.
     '''
     print_debug('%s %s %s' % (self.start_lemmas, self.rel, self.end_lemmas))
示例#25
0
 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))
示例#26
0
 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()))
示例#27
0
 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))