def distance_one_query(id1, distance_one):
    query = (
        ' SELECT distinct ?p ?id2 WHERE { <http://dbpedia.org/resource/' +
        id1 + '> ?p ?id2\
     . ' + suffixes_dbpedia_0 +
        ' FILTER (!regex(str(?pl), "Wikipage","i")) . FILTER (!regex(str(?pl), \
     "abstract","i")) . }')
    query_date = (
        ' SELECT distinct ?p ?id2 WHERE { <http://dbpedia.org/resource/' +
        id1 + '> ?p ?id2\
     . ' + suffixes_dbpedia_0 +
        ' FILTER (!regex(str(?pl), "Wikipage","i")) . FILTER (!regex(str(?pl), \
     "abstract","i")) . FILTER (!regex(str(?pl), "Date","i")). FILTER (!regex(str(?pl), "Year","i"))}'
    )
    # print query
    try:
        result = sparql.query(sparql_endpoint, query)
        q1_values = [sparql.unpack_row(row_result) for row_result in result]
        # print query
    except:
        result = sparql.query(sparql_endpoint, query_date)
        q1_values = [sparql.unpack_row(row_result) for row_result in result]
        # print query_date
    if q1_values:
        # print q1_values
        for vals in q1_values:
            vals_0 = vals[0].split('/')[-1]
            if vals_0 not in unwanted_predicates:
                if isinstance(vals[1], basestring):
                    if '/' in vals[1]:
                        distance_one.append(
                            [id1, vals_0, vals[1].split('/')[-1]])
                    else:
                        distance_one.append([id1, vals_0, vals[1]])
    return distance_one
Esempio n. 2
0
def get_description(entity_type):
    query = 'SELECT distinct ?label WHERE { <http://dbpedia.org/' + entity_type + '> rdfs:comment ?label . \
        FILTER langMatches( lang(?label), "EN" ) . }'

    result = sparql.query(sparql_dbpedia, query)
    type_comment = [sparql.unpack_row(row_result) for row_result in result]
    query = 'SELECT distinct ?label WHERE { <http://dbpedia.org/' + entity_type + '> rdfs:label ?label . \
    FILTER langMatches( lang(?label), "EN" ) . }'

    result = sparql.query(sparql_dbpedia, query)
    type_label = [sparql.unpack_row(row_result) for row_result in result]
    return type_comment, type_label
Esempio n. 3
0
def get_rank(id, entity_1, entity_2, label):
    query_1 = prefix + entity_1 + '>' + suffix
    query_2 = prefix + entity_2 + '>' + suffix
    # print query_1, query_2
    # try:
    result_1 = sparql.query(sparql_endpoint, query_1)
    result_2 = sparql.query(sparql_endpoint, query_2)
    value_1 = [sparql.unpack_row(row_result) for row_result in result_1][0][0]
    value_2 = [sparql.unpack_row(row_result) for row_result in result_2][0][0]
    score = (value_1 + value_2)
    return [
        id,
        entity_1.encode('utf-8'),
        entity_2.encode('utf-8'), label, score
    ]
Esempio n. 4
0
def DBPEDIA_request_movie_byName(in_name, in_metadata_mappings,
                                 in_metadata_content):

    print '\n' + '*' * 40
    print 'DBPEDIA_request_movie_byName'
    print '*' * 40

    print 'Searching in DBpedia for: ', in_name

    movies = []
    # This will use the name of the movie provided by the user
    # to get all the dbpedia_resultspossible URIs that match.
    dbpedia_results = dbpedia.getURIs(in_name)

    property_URI = sourceField_2_globalSchemaURI(
        'http://xmlns.com/foaf/0.1/name', 'DBPEDIA_source',
        in_metadata_mappings, in_metadata_content)
    if property_URI != SOURCE_ITEM_NOT_IN_GLOBAL_SCHEMA:
        for row in dbpedia_results:
            values = sparql.unpack_row(row)
            movies.append((values[0], property_URI, values[1]))

        for movie in movies:
            print movie

    return movies
Esempio n. 5
0
def kgminer_training_data(poi, q_part):
    q_ts = 'PREFIX dbo: <http://dbpedia.org/ontology/> select distinct ?url1 ?url2 where { \
    { ?url2 <http://dbpedia.org/'                                  + poi + '> ?url1 } . ' + q_part + \
           ' FILTER(?url1 != ?url2).} '

    print q_ts
    result = sparql.query(sparql_dbpedia, q_ts)
    training_set = [sparql.unpack_row(row_result) for row_result in result]
    if not training_set:
        q_ts = 'PREFIX dbo: <http://dbpedia.org/ontology/> select distinct ?url1 ?url2 where { \
        {?url1 <http://dbpedia.org/'                                     + poi + '> ?url2} . ' + q_part + \
               ' FILTER(?url1 != ?url2).} '
        result = sparql.query(sparql_dbpedia, q_ts)
        training_set = [sparql.unpack_row(row_result) for row_result in result]
    print q_ts
    return training_set
def get_confidence_rudik(numerator_query, denominator_query, pos_neg, examples):
    rule_true = 0
    body_true = 0
    i = 0
    for example in examples:
        # print i, example
        # i += 1
        # numerator_query_new = numerator_query.replace('?subject','<http://dbpedia.org/resource/'+example[0]+'>')
        denominator_query_new = denominator_query.replace('?subject','<http://dbpedia.org/resource/'+example[0]+'>')
        denominator_query_new = denominator_query_new.replace('?object','<http://dbpedia.org/resource/'+example[1]+'>')
        # numerator_query_new = numerator_query_new.replace('?object','<http://dbpedia.org/resource/'+example[1]+'>')
        # try:
        #     result = sparql.query(sparql_endpoint, numerator_query_new)
        #     numerator_value = [sparql.unpack_row(row_result) for row_result in result][0][0]
        # except:
        #     numerator_value = 0
        try:
            result = sparql.query(sparql_endpoint, denominator_query_new)
            denominator_value = [sparql.unpack_row(row_result) for row_result in result][0][0]
        except:
            denominator_value = 0
        # print numerator_value, denominator_value
        # if numerator_value > 0:
        #     rule_true += 1
        if denominator_value > 0:
            body_true += 1
    # print numerator_query_new
    # print denominator_query_new
    # print "-----------------"
    # print float(body_true),float(len(examples))
    confidence = float(body_true)/float(len(examples))
    return confidence
Esempio n. 7
0
def resolve_title_as_artist_dbpedia(artist_name, sim_threshold=0.5):
	try:
		logging.info("Resolving %s as artist using DBpedia" % artist_name)
		query = sparql.query('http://dbpedia.org/sparql', '''
			select ?artist where {
				?artist rdf:type ?type . 
				?type rdf:subClassOf* <http://schema.org/MusicGroup> . 
				?artist foaf:name "''' + artist_name.decode('utf-8') + '''"@en . 
			} LIMIT 10''')

		results = query.fetchall()

		if len(results) < 1:
			logging.warning("Request to DBpedia returned no results for %s, skipping resolution" % artist_name)
			return (artist_name, None)

		title = None
		maxSimilarity = 0.0
		for result in results:
			resolved_name = (sparql.unpack_row(result)[0].encode('utf-8')
					.replace('http://dbpedia.org/resource/', '')
					.replace('_', ' '))
			similarity = sentence_similarity(artist_name, urllib.unquote(resolved_name))
			if similarity > maxSimilarity:
				title = resolved_name
				maxSimilarity = similarity
	except urllib2.HTTPError, sparql.SparqlException:
		logging.warning("Request to DBpedia failed for %s, skipping resolution" % artist_name)
		return (artist_name, None)
Esempio n. 8
0
def print_result(offset,f):
   endpoint='http://130.235.17.116:8000/openrdf-sesame/repositories/AAOT'
   statement=("""PREFIX aaot:<http://cs.lth.se/ontologies/aaot.owl#>
   	select * WHERE {
   	 ?name aaot:age ?age . 
   	 ?name aaot:age_donor ?age_donor .
         ?name aaot:operation_year ?operation_year 
   	 ?name aaot:gender ?gen . 
         ?name aaot:survival_time ?survival_time .
        } limit 1000
          offset """
          +str(offset)
       )
   result = sparql.query(endpoint,statement)
   variables = result.variables
   for row in result.fetchall():
      values=sparql.unpack_row(row)
      i=1
      while i<len(values):
         if variables[i]=='gen':
            if values[i]=='F':
               values[i]=1
            else:
               values[i]=0
         print >> f,values[i],'\t',
         i+=1
      print >> f
Esempio n. 9
0
def predicate_finder(triple_dict):
    pval_list = []
    for k in triple_dict.keys():
        q_comment = 'SELECT distinct ?uri ?comment WHERE { ?uri rdfs:comment ?comment . \
        FILTER langMatches( lang(?comment), "EN" ).  ?comment bif:contains "' + k.split(
        )[1] + '" .}'
        q_label = 'SELECT distinct ?uri ?label WHERE { ?uri rdfs:label ?label . ?uri rdf:type rdf:Property . \
        FILTER langMatches( lang(?label), "EN" ). ?label bif:contains "' + k.split(
        )[1] + '" .}'
        predicate_result = sparql.query(sparql_dbpedia, q_comment)
        p_values = [sparql.unpack_row(row) for row in predicate_result]
        if not p_values:
            predicate_result = sparql.query(sparql_dbpedia, q_label)
            p_values = [sparql.unpack_row(row) for row in predicate_result]
        pval_list.append(p_values)
    return pval_list
Esempio n. 10
0
def distance_one_query(id1, distance_one):
    print "Distance One Query"
    sparql_endpoint = sparql_dbpedia
    query = (
        prefixes_dbpedia +
        ' SELECT distinct ?p ?id2 WHERE { <http://dbpedia.org/resource/' +
        id1 + '> ?p ?id2\
     . ' + suffixes_dbpedia_0 +
        ' FILTER (!regex(str(?pl), "Wikipage","i")) . FILTER (!regex(str(?pl), \
     "abstract","i")) . }')
    print query
    result = sparql.query(sparql_endpoint, query)
    q1_values = [sparql.unpack_row(row_result) for row_result in result]
    print len(q1_values)
    if q1_values:
        print len(q1_values)
        for vals in q1_values:
            vals_0 = vals[0].split('/')[-1]
            if vals_0 not in unwanted_predicates:
                if isinstance(vals[1], basestring):
                    if '/' in vals[1]:
                        distance_one.append(
                            [id1, vals_0, vals[1].split('/')[-1]])
                    else:
                        distance_one.append([id1, vals_0, vals[1]])
    return distance_one
Esempio n. 11
0
def get_evidence(resource, rules):
    evidence = []
    for rule in rules.split('\n'):
        if rule:
            body = rule.split(':-')[1]
            relations = re.findall(r"(\w*?)\(", body)
            vars = re.findall(r"\((.*?)\)", body)
            query = get_evidence_query(relations, vars, resource)
            # print query
            result = sparql.query(sparql_endpoint, query)
            q1_values = [
                sparql.unpack_row(row_result) for row_result in result
            ]
            # print q1_values
            if q1_values:
                for vals in q1_values:
                    try:
                        vals = [
                            val.split('/')[-1] if '/' in val else val
                            for val in vals
                        ]
                        evid = [vals[i:i + 3] for i in xrange(0, len(vals), 3)]
                        evidence.extend(evid)
                    except:
                        pass
    return evidence
Esempio n. 12
0
def resource_extractor(entity):
    db_resource = dict()
    wiki_resource = dict()
    resource_ids = dict()
    result = []
    query = 'PREFIX dbo: <http://dbpedia.org/ontology/> SELECT distinct ?uri ?label WHERE { ?uri rdfs:label ?label . \
    FILTER langMatches( lang(?label), "EN" ) . ?label bif:contains "' + entity + '" . }'
    print query
    try:
        result = sparql.query(sparql_dbpedia, query)
    except:
        pass
    if result:
        resources = [sparql.unpack_row(row_result) for row_result in result]
        for resource in resources:
            if 'wikidata' in resource[0]:
                if resource[1] not in wiki_resource.keys():
                    wiki_resource[resource[1]] = [resource[0]]
                else:
                    if resource[0] not in sum(wiki_resource.values(), []):
                        wiki_resource[resource[1]].append(resource[0])
            else:
                if resource[1] not in db_resource.keys(
                ) and 'Category' not in resource[0]:
                    db_resource[resource[1]] = [resource[0]]
                else:
                    if resource[0] not in sum(db_resource.values(), []):
                        db_resource.get(resource[1], []).append(resource[0])
        resource_ids['dbpedia_id'] = db_resource.get(entity)[0].split('/')[-1]
        resource_ids['wikidata_id'] = wiki_resource.get(entity)[0].split(
            '/')[-1]
    return resource_ids
Esempio n. 13
0
def get_resource_wikidata_sameas(resource: str):
    q = "SELECT ?x WHERE { <http://dbpedia.org/resource/" + resource + "> <http://www.w3.org/2002/07/owl#sameAs> ?x. FILTER regex(str(?x), \"wikidata\")}"
    # print(resource)
    result = sparql.query(DBPEDIA_ENDPOINT, q)
    if result.hasresult():
        return sparql.unpack_row(result[0])[0]
    else:
        return None
Esempio n. 14
0
 def test_custom_mapping(self):
     convert_datetime = Mock()
     row = list(self.result.fetchall())[0]
     unpacked_row = sparql.unpack_row(row, convert_type={
         sparql.XSD_DATETIME: convert_datetime,
     })
     self.assertTrue(unpacked_row[2] is convert_datetime.return_value)
     convert_datetime.assert_called_with("2009-11-02 14:31:40")
Esempio n. 15
0
def dbpedia_wikidata_equivalent(dbpedia_url):
    query = 'PREFIX owl:<http://www.w3.org/2002/07/owl#> PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> SELECT \
    ?WikidataProp WHERE { <' + dbpedia_url + '>  owl:sameAs ?WikidataProp . FILTER (CONTAINS \
    (str(?WikidataProp) , "wikidata.org")) .} '

    result = sparql.query(sparql_dbpedia, query)
    resources = [sparql.unpack_row(row_result) for row_result in result]
    return resources
Esempio n. 16
0
def dbpedia_things(query):
    things = []
    result = sparql.query('http://dbpedia.org/sparql', query)
    for row in result.fetchall():
        values = sparql.unpack_row(row)
        name = values[1]
        things.append(name)
    return things
Esempio n. 17
0
def sparql_example():
    q = ('SELECT DISTINCT ?station, ?orbits WHERE { '
    '?station a <http://dbpedia.org/ontology/SpaceStation> . '
    '?station <http://dbpedia.org/property/orbits> ?orbits . '
    'FILTER(?orbits > 50000) } ORDER BY DESC(?orbits)')
    result = sparql.query('http://dbpedia.org/sparql', q)
    for row in result:
        print 'row:', row
        values = sparql.unpack_row(row)
        print values[0], "-", values[1], "orbits"
Esempio n. 18
0
 def test_custom_mapping(self):
     convert_datetime = Mock()
     row = list(self.result.fetchall())[0]
     unpacked_row = sparql.unpack_row(row,
                                      convert_type={
                                          sparql.XSD_DATETIME:
                                          convert_datetime,
                                      })
     self.assertTrue(unpacked_row[2] is convert_datetime.return_value)
     convert_datetime.assert_called_with("2009-11-02 14:31:40")
Esempio n. 19
0
 def test_custom_function(self):
     convert = Mock()
     no_default_converters = patch('sparql._types', {})
     no_default_converters.start()
     try:
         row0 = sparql.unpack_row(list(self.result.fetchall())[0], convert)
         self.assertTrue(row0[2] is convert.return_value)
         convert.assert_called_with("18:58:21", sparql.XSD_TIME)
     finally:
         no_default_converters.stop()
Esempio n. 20
0
 def test_custom_function(self):
     convert = Mock()
     no_default_converters = patch('sparql._types', {})
     no_default_converters.start()
     try:
         row0 = sparql.unpack_row(list(self.result.fetchall())[0], convert)
         self.assertTrue(row0[2] is convert.return_value)
         convert.assert_called_with("18:58:21", sparql.XSD_TIME)
     finally:
         no_default_converters.stop()
def getQueryResults(repo_name,query):
	s=sparql.Service(sesame_url+repo_name+"/query")
	result = s.query(query)
	data = [] 
	for row in result:
		values = sparql.unpack_row(row)
		d = {}
		for i, v in enumerate(values):
			d[result.variables[i]] = v
		data.append(d)
	return data
Esempio n. 22
0
def get_predicate_wikidata_sameas(predicate: str):
    q = "ASK WHERE { <http://dbpedia.org/ontology/" + predicate + "> ?p ?o. }"
    result = sparql.query(DBPEDIA_ENDPOINT, q)
    is_ontology = result.hasresult()
    predicate_uri = f'<http://dbpedia.org/{"ontology" if is_ontology else "property"}/{predicate}>'
    q = "SELECT * WHERE { " + predicate_uri + " <http://www.w3.org/2002/07/owl#equivalentProperty> ?o. FILTER regex(str(?o), \"wikidata\")}"
    result = sparql.query(DBPEDIA_ENDPOINT, q)
    if result.hasresult():
        return sparql.unpack_row(result[0])[0]
    else:
        return None
Esempio n. 23
0
def get_all_entity(predicate):
    # q_ts = 'PREFIX dbo:  <http://dbpedia.org/ontology/> select distinct ?url1 ?url2 where { ?url1 rdf:type dbo:Person .\
    #  ?url1 dbo:' + predicate + ' ?url2  .} '
    q_ts = 'PREFIX dbo:  <http://dbpedia.org/ontology/> select distinct ?url1 where { ?url1 rdf:type dbo:Person .} '
    print q_ts
    result = sparql.query(sparql_dbpedia, q_ts, timeout=30000)
    training_set = [sparql.unpack_row(row_result) for row_result in result]
    print len(training_set)
    print training_set[:2]
    return training_set
    sys.exit(0)
Esempio n. 24
0
def positive_relations(predicate):
    query = 'select distinct ?a where {?a <http://dbpedia.org/property/' + predicate + '> ?b. ?b rdf:type \
    <http://dbpedia.org/ontology/Person> . ?a rdf:type <http://dbpedia.org/ontology/Company> .}'

    sparql_endpoint = sparql_dbpedia
    print query
    try:
        result = sparql.query(sparql_endpoint, query)
        q1_values = [sparql.unpack_row(row_result) for row_result in result]
    except:
        q1_values = []
    return q1_values
Esempio n. 25
0
def query_dbpedia(uri):
    statement = ('select ?name ?abstract where {'
                 'OPTIONAL {<' + uri + '> foaf:name ?name.}.'
                                       'OPTIONAL {<' + uri + '> dbo:abstract ?abstract.}.'
                                                             'FILTER (lang(?name) = "en" && lang(?abstract)= "en" )} '
                                                             'ORDER BY DESC(strlen(str(?name))) LIMIT 1')
    result = s.query(query=statement)
    for row in result.fetchone():
        values = sparql.unpack_row(row)
        name = values[0]
        abstract = str(next(nlp(values[1]).sents))
        return name, abstract
Esempio n. 26
0
def get_all_person():
    q_ts = 'PREFIX foaf:  <http://xmlns.com/foaf/0.1/> PREFIX dbo:  <http://dbpedia.org/ontology/> \
    PREFIX dbp: <http://dbpedia.org/property/> select ?url1  ?url2 ?url3 ?url4 ?url5 ?url6 ?url7 ?url8 ?url9 ?url10 ?url11\
     ?url12 ?url13 ?url14 where  { ?url1  dbo:birthPlace ?url6 .  ?url1  foaf:gender ?url2 . optional { ?url1 \
      dbp:nationality ?url3} . optional { ?url1  dbo:deathPlace ?url5 }. \
      optional {   ?url1  dbo:profession ?url4 }. optional { ?url1  dbo:residence ?url7} . optional { ?url1  dbo:almaMater \
      ?url8 }. optional { ?url1  dbo:deathCause ?url9 }. optional { ?url1  dbo:religion ?url10 } .  optional { ?url1  dbo:parent ?url11} \
      . optional { ?url1  dbo:child ?url12} . optional { ?url1  dbo:ethnicity ?url13} .  optional { ?url1 dbo:spouse ?url14} .  }'

    print q_ts
    result = sparql.query(sparql_dbpedia, q_ts)
    training_set = [sparql.unpack_row(row_result) for row_result in result]
    return training_set
Esempio n. 27
0
def dbpedia_wikidata_mapping():
    resource_dict = dict()
    query = "PREFIX owl:<http://www.w3.org/2002/07/owl#> PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> SELECT \
    ?itemLabel ?WikidataProp WHERE { ?DBpediaProp  owl:equivalentProperty  ?WikidataProp . FILTER ( \
    CONTAINS ( str(?WikidataProp) , 'wikidata')) . ?DBpediaProp  rdfs:label  ?itemLabel . FILTER (lang(?itemLabel) \
    = 'en') .} ORDER BY  ?DBpediaProp"

    result = sparql.query(sparql_dbpedia, query)
    resources = [sparql.unpack_row(row_result) for row_result in result]
    for resource in resources:
        resource_dict[resource[1].split('/')[-1]] = resource[0]
    with open('LPmln/predicate_dict.json', 'w') as fp:
        json.dump(resource_dict, fp, default=json_serial)
Esempio n. 28
0
def getLabel (uri):

    labelQuery = "SELECT ?label WHERE { <"+uri+"> <http://www.w3.org/2000/01/rdf-schema#label> ?label} limit 1"
    result = sparql.query(endpoint, labelQuery)

    for row in result:
        return  sparql.unpack_row(row)[0]

    label = str(urlparse(uri).fragment)
    if not label:
        label = uri.rsplit('/', 1)[-1]
        if not label:
            return uri

    return label
Esempio n. 29
0
def negative_relations(predicate):
    query = "SELECT DISTINCT ?subject <notfounders> ?object WHERE { ?object <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> \
    <http://dbpedia.org/ontology/Person>. ?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> \
    <http://dbpedia.org/ontology/Company>. {{?subject ?targetRelation ?realObject.} UNION  \
    {?realSubject ?targetRelation ?object.}} ?subject ?otherRelation ?object. \
    FILTER (?targetRelation = <http://dbpedia.org/property/" + predicate + ">) \
    FILTER (?otherRelation != <http://dbpedia.org/property/" + predicate + ">) \
    FILTER NOT EXISTS {?subject <http://dbpedia.org/property/" + predicate + "> ?object.} }"
    sparql_endpoint = sparql_dbpedia
    print query
    try:
        result = sparql.query(sparql_endpoint, query)
        q1_values = [sparql.unpack_row(row_result) for row_result in result]
    except:
        q1_values = []
    return q1_values
Esempio n. 30
0
def sparql_query(query, endpoint):
    """
    Runs the given (string) query against the given endpoint,
    returns a list of dicts with the variables' values.
    """
    result = sparql.query(endpoint, query)

    data = []
    for row in result:
        values = sparql.unpack_row(row)
        d = {}
        for i, v in enumerate(values):
            d[result.variables[i]] = v
        data.append(d)

    return data
Esempio n. 31
0
def get_result(searchable_name, lang):
    endpoint = 'http://dbpedia.org/sparql'
    abstract_page_query = (f"""
             SELECT ?label
             WHERE {{
             <http://dbpedia.org/resource/{searchable_name}>
            <http://www.w3.org/2000/01/rdf-schema#comment> ?label
            . FILTER (lang(?label) = '{lang}')}}
             """)

    query_result = sparql.query(endpoint, abstract_page_query)
    value = None  # stores the abstract ontology(object description)  of the page
    for row in query_result:  # loops once dont know how to get the values in the query_result so loop
        values = sparql.unpack_row(row)
        value = values[0]
        return value
    return value
Esempio n. 32
0
def get_kgminer_predicates(type_set, triple_dict):
    predicate_list = []
    for triples_k, triples_v in triple_dict.iteritems():
        sort_list = dict()
        for triple_v in triples_v:
            item1_v = type_set.get(triple_v[0], [])
            item2_v = type_set.get(triple_v[1], [])
            if item1_v and item2_v:
                for it1 in item1_v:
                    for it2 in item2_v:
                        if it1 != it2:
                            if it2 in sort_list.keys(
                            ) and it1 in sort_list.get(it2, []):
                                q_pp = ''
                            else:
                                if it1 not in sort_list.keys():
                                    sort_list[it1] = [it2]
                                else:
                                    sort_list[it1].append(it2)
                                q_pp = 'SELECT distinct ?p WHERE { ?url1 rdf:type <http://dbpedia.org/ontology/'+it1+'>\
                                 . ?url2 rdf:type <http://dbpedia.org/ontology/'                                                                                 + it2 + '> . {?url1 ?p ?url2 } UNION {?url2 ?p ?url1 } \
                                                            . FILTER(STRSTARTS(STR(?p), "http://dbpedia.org/")).' \
                                                            '} '
                        else:
                            q_pp = 'SELECT distinct ?p WHERE { ?url1 rdf:type <http://dbpedia.org/ontology/' + it1 + '> . \
                            ?url2 rdf:type <http://dbpedia.org/ontology/' + it2 + '> . ?url1 ?p ?url2 . \
                            FILTER(STRSTARTS(STR(?p), "http://dbpedia.org/")).}'

                        # try:
                        if q_pp:
                            result = sparql.query(sparql_dbpedia_on, q_pp)
                            pred_values = [
                                sparql.unpack_row(row_result)
                                for row_result in result
                            ]
                            if pred_values:
                                pred_vals = [
                                    val[0].replace('http://dbpedia.org/', '')
                                    for val in pred_values
                                ]
                                predicate_list.extend(pred_vals)
                        # except Exception as e:
                        #     print e
    return list(set(predicate_list))
 def query(self, query):
     # launchVIRTUOSO_doDataLakeTransformations():
     try:
         result = sparql.query(self.VIRTUOSO_SPARQL_ENDPOINT, query)
         print query
         print "query succeded, result: " + result.variables[0]
         
         triples = []
         for row in result:
             values = sparql.unpack_row(row)
             triples.append([values[0], values[1], values[2]])
         return triples
         
     except sparql.SparqlException as e:
         print 'Exception: '
         print e.message
         print 'Query: '
         print query
         pass
    def query(self, query):
        # launchVIRTUOSO_doDataLakeTransformations():
        try:
            result = sparql.query(self.VIRTUOSO_SPARQL_ENDPOINT, query)
            print query
            print "query succeded, result: " + result.variables[0]

            triples = []
            for row in result:
                values = sparql.unpack_row(row)
                triples.append([values[0], values[1], values[2]])
            return triples

        except sparql.SparqlException as e:
            print 'Exception: '
            print e.message
            print 'Query: '
            print query
            pass
def get_confidence_amie(denominator_query, examples):
    body_true = 0
    for i,example in enumerate(examples):
        denominator_query_new = denominator_query.replace('?a','<http://dbpedia.org/resource/'+example[0].decode('utf-8')+'>')
        denominator_query_new = denominator_query_new.replace('?b','<http://dbpedia.org/resource/'+example[1].decode('utf-8')+'>')
        try:
            result = sparql.query(sparql_endpoint, denominator_query_new)
            denominator_value = [sparql.unpack_row(row_result) for row_result in result][0][0]
        except:
            # print denominator_query_new,i
            denominator_value = 0
        if denominator_value > 0:
            body_true += 1
            # print denominator_query_new
    print body_true
    print "-----------------"
    print float(body_true),float(len(examples))
    confidence = float(body_true)/float(len(examples))
    return confidence
Esempio n. 36
0
def distance_two_query(entity, distance_two):
    sparql_endpoint = sparql_dbpedia
    print "Distance Two Query"
    query = ' SELECT distinct ?p ?id2 ?p1 ?id3 WHERE { <http://dbpedia.org/resource/' + \
            entity + '> ?p ?id2 .  ?id2 ?p1 ?id3 . FILTER (!regex(str(?p1), \
                "owl","i")) .}'

    print query
    try:
        result = sparql.query(sparql_endpoint, query)
        q1_values = [sparql.unpack_row(row_result) for row_result in result]
    except:
        q1_values = []
        pass
    if q1_values:
        print len(q1_values)
        for vals in q1_values:
            vals_0 = vals[0].split('/')[-1]
            vals_2 = vals[2].split('/')[-1]
            if vals_0 not in unwanted_predicates:
                if not isinstance(vals[1], basestring):
                    distance_two.append([entity, vals_0, vals[1]])
                else:
                    distance_two.append(
                        [entity, vals_0, vals[1].split('/')[-1]])
            if vals_2 not in unwanted_predicates:
                if not isinstance(vals[3], basestring) and not isinstance(
                        vals[1], basestring):
                    distance_two.append([vals[1], vals_2, vals[3]])
                elif isinstance(vals[3], basestring) and not isinstance(
                        vals[1], basestring):
                    distance_two.append(
                        [vals[1], vals_2, vals[3].split('/')[-1]])
                elif not isinstance(vals[3], basestring) and isinstance(
                        vals[1], basestring):
                    distance_two.append(
                        [vals[1].split('/')[-1], vals_2, vals[3]])
                else:
                    distance_two.append([
                        vals[1].split('/')[-1], vals_2, vals[3].split('/')[-1]
                    ])
    return distance_two
def get_examples(query):
    print "Processing Examples"
    examples = []
    try:
        result = sparql.query(sparql_endpoint, query)
        examples = [sparql.unpack_row(row_result) for row_result in result]
        examples = [
            map(lambda x: x.split('/')[-1].encode('utf-8'), vals)
            for vals in examples
        ]
    except Exception as e:
        print(e)
    if examples:
        size = len(examples)
        # train_size = size / 5
        return examples[:200], examples[200:]
        # return [], examples
    else:
        print "Examples Not Found"
    return [], []
Esempio n. 38
0
def count_result_from_query(subset):
    value = 0;
    endpoint = 'http://130.235.17.116:8000/openrdf-sesame/repositories/AAOT'  
    statement_beginning = """PREFIX aaot:<http://cs.lth.se/ontologies/aaot.owl#>
   	select (count(*) as ?count) WHERE { \n"""
    statement_end = """ ?name aaot:survival_time ?survival_time .
            }"""
    statement_query = ""

    for i in subset:
        statement_query = statement_query + "?name aaot:"+i+" ?"+i+" .\n"

    statement = statement_beginning + statement_query + statement_end

    result = sparql.query(endpoint, statement)

    for row in result.fetchall():
        values = sparql.unpack_row(row)
        value = values[0]

    return value
Esempio n. 39
0
def DBPEDIA_request_movie_byName(in_name, in_metadata_mappings, in_metadata_content):        
    
    print '\n' + '*'*40    
    print 'DBPEDIA_request_movie_byName'
    print '*'*40
    
    print 'Searching in DBpedia for: ', in_name    

    movies = []   
    # This will use the name of the movie provided by the user
    # to get all the dbpedia_resultspossible URIs that match.    
    dbpedia_results = dbpedia.getURIs(in_name)  
    
    property_URI = sourceField_2_globalSchemaURI('http://xmlns.com/foaf/0.1/name', 'DBPEDIA_source', in_metadata_mappings, in_metadata_content)
    if property_URI != SOURCE_ITEM_NOT_IN_GLOBAL_SCHEMA:    
        for row in dbpedia_results:
            values = sparql.unpack_row(row)
            movies.append((values[0], property_URI, values[1]))
            
        for movie in movies:                                   
            print movie
    
    return movies
Esempio n. 40
0
def print_result(attributes,offset,f):
   endpoint='http://130.235.17.116:8000/openrdf-sesame/repositories/AAOT'
   statement_beginning="""PREFIX aaot:<http://cs.lth.se/ontologies/aaot.owl#>
   	select * WHERE { """
              
   statement_ending=""" ?name aaot:survival_time ?survival_time .
            } limit 1000
            offset"""

   statement_query = ""
   for i in attributes:
      statement_query = statement_query + "?name aaot:"+i+" ?"+i+" ."

   statement = (statement_beginning + statement_query + statement_ending + str(offset))

   result = sparql.query(endpoint,statement)
   variables = result.variables
   for row in result.fetchall():
      values=sparql.unpack_row(row)
      i=1
      while i<len(values):
         print >> f,values[i],'\t',
         i+=1
      print >> f
Esempio n. 41
0
def run_query(method_ob, **kwargs):
    result = method_ob(**kwargs)
    return [dict(zip(result.var_names, unpack_row(row))) for row in result.rdfterm_rows]
Esempio n. 42
0
def multiple_query():

    endpoint = 'http://130.235.17.116:8000/openrdf-sesame/repositories/AAOT'
    
    f_properties = open('listProperties.txt','r')
    
    statement_beginning = """PREFIX aaot:<http://cs.lth.se/ontologies/aaot.owl#>
   	select (count(*) as ?count) WHERE { \n"""
    
    statement_end = """ ?name aaot:survival_time ?survival_time .
            }"""

    offset = 0;
    max_list_attributes = []
    treshold = 10000
    line=f_properties.readlines()
    for i in range(0, len(line)):
        list_attributes = []
        list_attributes.append(line[i].strip())
        statement_query = ""
        statement_query = statement_query + "?name aaot:"+line[i].strip()+" ?"+line[i].strip()+" ."
        statement = statement_beginning + statement_query + statement_end +"\n"
        result = sparql.query(endpoint,statement)
        for row in result.fetchall():
            values = sparql.unpack_row(row)
            if (values[0] > 50000):
                print values[0],
                print list_attributes
            '''
            if ((values[0] > treshold) and (len(list_attributes) > len(max_list_attributes))):
                max_list_attributes = list_attributes
                print str(values[0]), 
                print max_list_attributes
                '''
        for j in range(i+1, len(line)):
            latest_value = 0
            list_attributes.append(line[j].strip())
            statement_query = statement_query + "?name aaot:"+line[j].strip()+" ?"+line[j].strip()+" ."
            statement = statement_beginning + statement_query + statement_end+ "\n"
            result = sparql.query(endpoint,statement)
            for row in result.fetchall():
                values = sparql.unpack_row(row)
                latest_value = values[0]
                if (latest_value >= 50000):
                    print values[0],
                    print list_attributes
                else:
                    break
            if (latest_value < 50000):
                break
                '''
                latest_value = values[0]
                print values[0],
                print len(list_attributes),
                print len(max_list_attributes)
                if ((values[0] > treshold) and (len(list_attributes) > len(max_list_attributes))):
                    print "condition checked"
                    max_list_attributes = list_attributes
                    print str(values[0]),
                    print list_attributes
            if(latest_value == 0):
                break
                '''

    return max_list_attributes
    def addTSSInfo(self, vcfInputFile):
        vcf_reader = vcf.Reader(open(vcfInputFile, 'r'))
        vcf_reader.infos['TSSOL'] = VcfInfo('TSSOL', vcf_field_counts['A'], 'String',
                                            'Info indicates whether the variant overlapping with the'
                                            ' transcription start site(TSS)')

        vcf_writer = vcf.VCFWriter(open('output.vcf', 'w'), vcf_reader)

        query = SPARQLQueries.sparqlQueries()

        totalVar = 0
        tssOLVar = 0

        lo = LiftOver('hg38ToHg19.over.chain.gz')

        for record in vcf_reader:
            variantStart = record.start
            variantEnd = record.end
            variantChromosome = record.CHROM
            variantSubType = record.var_subtype
            isOverlapping = False



            # Adding chr prefix to the chromosome
            if "chr" not in variantChromosome:
                variantChromosome = "chr"+str(record.CHROM)

            #liftover from hg20 to hg19
            data = lo.convert_coordinate(variantChromosome, variantStart)

            #print variantChromosome
            print variantStart
            print variantEnd


            if ((data != None)):
                data2 = data.pop()

                variantChromosomehg19 = data2[0]
                variantStarthg19 = data2[1]



                data = lo.convert_coordinate(variantChromosome, variantEnd)
                data2 = data.pop()

                variantEndhg19 = data2[1]



                # SPARQL query
                result = query.getTSS('http://ep.dbcls.jp/fantom5/sparql', variantStarthg19, variantEndhg19, variantChromosomehg19)

                for row in result:

                    values = sparql.unpack_row(row)
                    cageStart = values[1]
                    cageEnd = values[2]

                    if ((variantSubType == 'ins') & ( variantStart > cageStart )):
                        isOverlapping = True
                        tssOLVar = tssOLVar+1
                        break
                    elif ((variantSubType != 'ins') & (cageStart > 0)):
                       isOverlapping = True
                       tssOLVar = tssOLVar+1
                    break

                totalVar = totalVar+1
                record.add_info('TSSOL', [isOverlapping])
            else:
                print "No liftover found for this pos = "+record.ID

            vcf_writer.write_record(record)

            print "No of variants = "+str(totalVar)
            print "No of tss overlapping variants = "+str(tssOLVar)
Esempio n. 44
0
 def __iter__(self):
     return (sparql.unpack_row(r, convert_type=sparql_converters)
             for r in self.rdfterm_rows)
Esempio n. 45
0
 def __getitem__(self, n):
     return sparql.unpack_row(self.rdfterm_rows[n],
                              convert_type=sparql_converters)
Esempio n. 46
0
def query(query_str):
    result = sparql.query(localhost_endpoint, query_str)
    row_tuple = namedtuple('row', ' '.join(result.variables))
    return [row_tuple(*sparql.unpack_row(row)) for row in result]
Esempio n. 47
0
 def _execute(self, query):
     res = sparql.query(self.endpoint, query)
     return (sparql.unpack_row(r) for r in res)
Esempio n. 48
0
def rowToList(result):
	resultList = []
	for row in result:
		values = sparql.unpack_row(row)
		resultList.append(values)
	return resultList
Esempio n. 49
0
 def __init__(self, raw_result):
     self.variables = raw_result.variables
     _row_type = namedtuple('ResultRow', ' '.join(self.variables))
     self.rows = (_row_type(*sparql.unpack_row(r)) for r in raw_result)