Exemple #1
0
# -*- coding: utf-8 -*-
"""
Examples for accessing the KnowledgeStore as shown in Session 03.

Created on Tue Nov  6 13:27:24 2018

@author: lbechberger
"""

import sys
sys.path.append(".")
import knowledgestore.ks as ks

query_string = "SELECT DISTINCT ?e WHERE {?m dbo:starring ?e . ?m dbo:genre dbpedia:Comedy . ?m dbo:starring dbpedia:Charlie_Sheen . ?e rdf:type dbo:PlayboyPlaymate . ?e dbo:birthPlace dbpedia:Canada}"
sparql_result = ks.run_sparql_query(query_string)
print(sparql_result)
print("")

graph_string = """SELECT ?label 
WHERE {
GRAPH <http://www.newsreader-project.eu/modules/dbpedia-en> 
{ dbpedia:Angela_Merkel rdfs:label ?label } 
}"""
graph_result = ks.run_sparql_query(graph_string)
print(graph_result)
print("")

merkel_string = """SELECT ?label 
WHERE {
dbpedia:Angela_Merkel rdfs:label ?label
}"""
Exemple #2
0
# first question
article_uri = "http://en.wikinews.org/wiki/'Worst_song_of_all_time'_becomes_YouTube_sensation"
mention_uris = ks.run_resource_query(article_uri, "ks:hasMention")

counter = 0
for mention_uri in mention_uris:
    types = ks.run_mention_query(mention_uri, "@type")
    if "nwr:RelationMention" in types:
        counter += 1

print("Answer for question 1: {0}".format(counter))

# second question
sparql_query = "SELECT DISTINCT ?m WHERE { dbpedia:Angela_Merkel gaf:denotedBy ?m }"
sparql_result = ks.run_sparql_query(sparql_query)

resource_uris = []
for binding in sparql_result:
    mention_uri = binding['m']
    resource_uri = ks.mention_uri_to_resource_uri(mention_uri)
    if resource_uri not in resource_uris:
        resource_uris.append(resource_uri)

counter = 0
all_mappings = ks.get_all_resource_category_mappings(
    ks.top_level_category_names)
for resource_uri in resource_uris:
    if "Economy and business" in all_mappings[resource_uri]:
        counter += 1
Exemple #3
0
# efficient SPARQL queries
print('\nSPARQL')
mentions = ks.run_resource_query(train[0][0], 'ks:hasMention')
print("#Mentions:", len(mentions))
sparql_naive_first = "SELECT ?e WHERE { ?e gaf:denotedBy <"
sparql_naive_second = "> . ?e rdf:type sem:Event }"
sparql_better_first = "SELECT ?e WHERE { VALUES ?m { "
sparql_better_second = " } . ?e gaf:denotedBy ?m . ?e rdf:type sem:Event}"

# start_naive = time.time()
# naive_results = []
# for mention in mentions:
#     naive_results += ks.run_sparql_query(sparql_naive_first + mention + sparql_naive_second)
# end_naive = time.time()
# print(len(naive_results), end_naive - start_naive)

start_better = time.time()
preformatted = list(map(lambda x: '<' + x + '>', mentions))
counter = 0
slice_size = 50
better_results = []
while counter < len(mentions):
    better_results += ks.run_sparql_query(
        sparql_better_first +
        ' '.join(preformatted[counter:counter + slice_size]) +
        sparql_better_second)
    counter += slice_size
end_better = time.time()
print(len(better_results), end_better - start_better)