2017-01-24 1 views
0

나는 2 개의 SPARQL 쿼리를 사용하여 DBpedia를 쿼리하고 각각의 쿼리에 대해 결과를 목록에 넣는 python 스크립트를 가지고 있습니다. 그런 다음이 목록 집합을 만들어 중복을 제거하고 필요한 결과를 얻었습니다. 이 프로세스를 가속화하기 위해 쿼리를 결합 할 가능성이있는 경우 비효율적 인 방법으로 보입니다.2 쿼리의 결과 합치기

SQL SPARQL에서 경험이 많은 사람이 이러한 쿼리를 결합하여 파이썬 스크립트의 속도를 높일 수 있습니까?

컨텍스트의 경우 :이 함수 (쿼리 1)에주는 쿼리 워드의 DBpedia 페이지에 대한 모든 속성의 속성과 값의 레이블 이름과 숫자/텍스트 값이 아닌 값으로 레이블 지정 (쿼리 2).

def querydbpedia(queryword): 
mylist = list() 
sparql = SPARQLWrapper("http://dbpedia.org/sparql") 
sparql.setQuery(""" 
    prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> 

    SELECT DISTINCT ?pLabel ?oLabel ?o WHERE { 
    <http://dbpedia.org/resource/""" + queryword + """> ?p ?o. 
    ?p rdfs:label ?pLabel . 
    ?o rdfs:label ?oLabel . 
    FILTER(LANG(?pLabel) = "" || LANGMATCHES(LANG(?pLabel), "en")) 
    FILTER(LANG(?oLabel) = "" || LANGMATCHES(LANG(?oLabel), "en")) 
    } 
""") 

sparql.setReturnFormat(JSON) 
results = sparql.query().convert() 

for result in results["results"]["bindings"]: 
    mystr = (result["pLabel"]["value"] + " - " + result["oLabel"]["value"]).lower() 
    mylist.append(mystr) 

sparql.setQuery(""" 
    prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> 

    SELECT DISTINCT ?pLabel ?oLabel ?o WHERE { 
    <http://dbpedia.org/resource/""" + queryword + """> ?p ?o. 
    ?p rdfs:label ?pLabel . 
    OPTIONAL {?o rdfs:label ?oLabel} . 
    FILTER(LANG(?pLabel) = "" || LANGMATCHES(LANG(?pLabel), "en")) 
    FILTER(LANG(?o) = "" || LANGMATCHES(LANG(?o), "en")) 
    } 
""") 

sparql.setReturnFormat(JSON) 
results = sparql.query().convert() 

for result in results["results"]["bindings"]: 
    mystr = (result["pLabel"]["value"] + " - " + result["o"]["value"]).lower() 
    if not ("abstract" in mystr): 
    mylist.append(mystr) 

mylist = list(set(mylist)) 
return mylist 

답변

1

SPARQL UNION을 사용할 수 있습니다.

또는 SPARQL 1.1 VALUES을 사용하여 동일한 쿼리를 여러 리소스에 적용 할 수 있습니다. 예 : -

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
    SELECT DISTINCT ?s ?pLabel ?oLabel ?o WHERE { 
    ?s ?p ?o. 
    VALUES ?s {<http://dbpedia.org/resource/Leipzig> <http://dbpedia.org/resource/Dresden>} 
    ?p rdfs:label ?pLabel . 
    ?o rdfs:label ?oLabel . 
    FILTER(LANG(?pLabel) = "" || LANGMATCHES(LANG(?pLabel), "en")) 
    FILTER(LANG(?oLabel) = "" || LANGMATCHES(LANG(?oLabel), "en")) 
    } 
+0

나는 UNION이 내가 찾고있는 것이지, 나는 이것을 스스로 해보려고 노력했으나 올바르게 할 수는 없다. 이러한 쿼리를 UNION으로 사용하면 원본 2 쿼리와 동일한 결과가 인쇄 된 것처럼 보일 수 없습니다. 이러한 쿼리에 대해 UNION을 어떻게 수행해야합니까? – Nick

+0

왜 대신 VALUES를 사용할 수 없습니까? 이것은 훨씬 더 편리합니다. 작동하지 않습니까? – AKSW

+0

그리고 시도한 UNION 쿼리를 표시 할 수 있습니까? 무엇이 그것으로 작동하지 않습니까? 이 경우 주체의 변수를 유지해야합니다. 그렇지 않으면 두 리소스를 구분할 수 없습니다. – AKSW