여러 개의 명명 된 그래프/모델이 저장되어있는 RDF4J 기본 triplestore를 사용하고 있습니다. 내 Java 프로그램에서 SPARQL CONSTRUCT 쿼리 양식을 사용하여 명명 된 그래프에서 부분 그래프를 검색하려고합니다. 부분 그래프는 특정 정수 값으로 식별되어야합니다. 내 저장된 이름이 그래프의SPARQL 구조체를 사용하여 부분 RDF 그래프를 쿼리하십시오.
하나는 예를 들어, 다음과 같습니다
는@prefix nif: <http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix app: <http://example.org/> .
@prefix onto: <http://example.org/ontology/> .
app:context#char=0,54 a nif:Context , nif:RFC5147String , nif:String ;
nif:beginIndex "0"^^xsd:int ;
nif:endIndex "54"^^xsd:int ;
nif:isString "Barack Obama lives in Washington. He studied at Harvard" .
app:sentence#char=0,31 a nif:RFC5147String , nif:String , nif:Sentence ;
onto:index "0"^^xsd:int ;
nif:beginIndex "0"^^xsd:int ;
nif:endIndex "31"^^xsd:int ;
onto:entity app:entity#char=0,12 , app:entity#char=22,30 ;
nif:referenceContext app:context#char=0,54 ;
nif:nextSentence app:sentence#char=32,54 ;
nif:anchorOf "Barack Obama lives in Washington." .
app:sentence#char=32,54 a nif:RFC5147String , nif:String , nif:Sentence ;
onto:index "1"^^xsd:int ;
nif:beginIndex "32"^^xsd:int ;
nif:endIndex "54"^^xsd:int ;
onto:entity app:entity#char=46,53 ;
nif:referenceContext app:context#char=0,54 ;
nif:previousSentence app:sentence#char=0,31 ;
nif:anchorOf "He studied at Harvard." .
app:entity#char=0,12 a nif:RFC5147String , nif:String , nif:Phrase ;
nif:beginIndex "0"^^xsd:int ;
nif:endIndex "12"^^xsd:int ;
onto:type "PERSON" ;
nif:referenceContext app:context#char=0,54 ;
nif:sentence app:sentence#char=0,31 ;
nif:anchorOf "Barack Obama" .
app:entity#char=22,30 a nif:RFC5147String , nif:String , nif:Phrase ;
nif:beginIndex "22"^^xsd:int ;
nif:endIndex "30"^^xsd:int ;
onto:type "LOCATION" ;
nif:referenceContext app:context#char=0,54 ;
nif:sentence app:sentence#char=0,31 ;
nif:anchorOf "Washington" .
app:entity#char=46,53 a nif:RFC5147String , nif:String , nif:Phrase ;
nif:beginIndex "46"^^xsd:int ;
nif:endIndex "53"^^xsd:int ;
onto:type "ORGANIZATION" ;
nif:referenceContext app:context#char=0,54 ;
nif:sentence app:sentence#char=32,54 ;
nif:anchorOf "Harvard" .
위의 그래프는 NLP 도구로 주석 한 두 문장을 설명합니다. 문맥, 문장 자체 및 해당 문장의 모든 엔티티를 포함하여 이러한 문장 중 하나를 나타내는 하위 그래프를 쿼리하려고합니다.
모든 문장에는 특정 문장을 식별하기 위해 사용하려는 술어 onto:index
의 색인이 있습니다. 문장에 속한 엔티티는 onto:entity
으로 식별됩니다.
그래서, 원하는 서브 그래프는 과목 모두 각각의 술어와 객체와 app:context#char=0,54
, app:sentence#char=0,31
, app:entity#char=0,12
및 app:entity#char=22,30
로 구성되어야합니다. 따라서 이 아닌에는 app:sentence#char=32,54
및 app:entity#char=46,53
이라는 주제가 포함되어야합니다.
내 SPARQL 쿼리는 지금까지 다음과 같습니다
PREFIX nif: <http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX app: <http://example.org/location#>
PREFIX onto: <http://example.org/ontology/>
CONSTRUCT {
?s ?p ?o .
}
WHERE {
GRAPH app:12345 {
?s onto:index sentenceIndex .
}
}
자바에서는 다음과 같습니다
String nif: "http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#";
String xsd: "http://www.w3.org/2001/XMLSchema#";
String app = "http://example.org/location#" + modelID;
String onto = "http://example.org/ontology/";
String sparqlQuery = "PREFIX nif: <" + nif + "> \n";
sparqlQuery += "PREFIX xsd: <" + xsd + "> \n";
sparqlQuery += "PREFIX onto: <" + onto + "> \n";
sparqlQuery += "CONSTRUCT { \n";
sparqlQuery += " ?s ?p ?o . \n";
sparqlQuery += "} \n";
sparqlQuery += "WHERE { \n";
sparqlQuery += " GRAPH <" + app + "> { \n";
sparqlQuery += " ?s onto:index " + sentenceIndex + " . \n";
sparqlQuery += " } \n";
sparqlQuery += "}";
sentenceIndex
표현은 실제 onto:index
값을 제공하는 자바 INT 변수입니다. app:location#12345
은 우수한 명명 된 그래프입니다.
테스트 목적으로 위의 쿼리는 처음에는 문맥이나 엔터티가없는 지정된 인덱스로 전체 문장 제목을 반환해야합니다. 그러나이 쉬운 작업조차도 실패합니다. 그냥 빈 RDF4J 모델을 반환합니다.
이제 onto:index
술어로 식별되는 원하는 하위 그래프를 얻으려면 올바른 SPARQL 쿼리는 무엇입니까?
SPARQL에 익숙하지 않으므로 도움을 주시면 대단히 감사하겠습니다. 미리 감사드립니다!
이 쿼리는 문법적으로 올바르지 않으며 빈 모델을 반환하지 않습니다. 대신 접두사 선언 뒤의 전체 중단으로 인해 구문 오류가 발생하여 쿼리가 실패합니다. 아마도 복사 - 붙여 넣기 오류 일 뿐이며 쉽게 수정할 수 있습니다. 나중에 참조 할 수 있도록 게시중인 정확한 코드를 테스트 해보십시오. –
죄송합니다. 이것은 실제로 복사 및 붙여 넣기 오류입니다. 내 게시물을 편집하고 실제 Java 코드를 추가했습니다. 이 코드에는 컴파일 오류가 없지만 빈 모델이 반환됩니다. – phly