Example #1
0
    def get_predicate_in_dbpedia(term, root_sub, root_sub_type):
        # in order to get the predicate, you need the term and the sub to query with
        if root_sub_type == 'class':
            if type(root_sub) == type({}):
                query = '''PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                           SELECT DISTINCT ?p WHERE { ?s rdf:type <%s> . ?s ?p ?o} ''' % root_sub[
                    'uri']
                sparqlEngine = SPARQLEngine()
                result = sparqlEngine.fire_query(query)
                predicates = sparqlEngine.get_predicates_by_query(result)
                return {
                    'uri':
                    LookUpService.rank_uri_basing_on_label(term, predicates),
                    'type': 'property',
                    'from': 'dbpedia'
                }
            else:

                query = '''PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                           SELECT DISTINCT ?p WHERE { ?s rdf:type <%s> . ?s ?p ?o}  ''' % root_sub
                sparqlEngine = SPARQLEngine()
                result = sparqlEngine.fire_query(query)
                predicates = sparqlEngine.get_predicates_by_query(result)
                return {
                    'uri':
                    LookUpService.rank_uri_basing_on_label(term, predicates),
                    'type': 'property',
                    'from': 'dbpedia'
                }

        elif root_sub_type == 'instance':
            query = '''SELECT DISTINCT ?p WHERE { <%s> ?p ?o} ''' % root_sub
            sparqlEngine = SPARQLEngine()
            result = sparqlEngine.fire_query(query)
            predicates = sparqlEngine.get_predicates_by_query(result)
            return {
                'uri':
                LookUpService.rank_uri_basing_on_label(term, predicates),
                'type': 'property',
                'from': 'dbpedia'
            }
Example #2
0
    def NIN(self, tree):
        print('Entering NIN')

        # ========================= Stage 1 =============================
        tree = toolbox.remove_qi(tree)
        root_np_tree = PatternProcessor.get_root_sub(tree)
        root_np = toolbox.np_processor(root_np_tree)
        if 'FUNC_ALL' in root_np['funcs']:
            root_np.update(LookUpService.get_sub_class_uri(root_np['term']))
        else:
            root_np.update(LookUpService.get_sub_instance_uri(root_np['term']))
        root_np['variable'] = SPARQLEngine.construct_variable_name(root_np['term'])
        root_np['component'] = PatternProcessor.construct_components(root_np, root_np['term'])
        print(root_np['component'])
        # ========================= Stage 2 =============================
        tree = toolbox.get_the_only_sub_tree(tree)
        tree.remove(root_np_tree)
        next_np = toolbox.get_the_only_sub_tree(tree)
        next_np = toolbox.np_processor(next_np)
        print('next_np ', next_np)

        print('next np funcs', next_np['funcs'])
        if 'FUNC_ALL' in next_np['funcs']:
            uri = LookUpService.get_sub_class_uri(next_np['term'])
            next_np['type'] = 'class'
            next_np.update(uri)
        else:
            uri = LookUpService.get_predicate_uri(next_np['term'], root_np['uri'], root_np['type'])
            next_np.update(uri)

        print('next np', next_np)
        next_np['variable'] = SPARQLEngine.construct_variable_name(next_np['term'])

        query_result = QueryConstructor.construct_standard_nin_query(root_np, next_np)
        query = query_result['query']
        domain = query_result['from']

        sparqlEngine = SPARQLEngine()
        print('========= query =========')
        print(query)
        print('=========================')
        if domain == 'ols':
            print('=================== Searching ols ===================')
            result = sparqlEngine.fire_mix_query(query)
            return result
        else:
            print('=================== Searching dbpedia ===================')
            result = sparqlEngine.fire_query(query)
            return result