gpt4 book ai didi

java - 从 RDF 节点拉出字符串

转载 作者:行者123 更新时间:2023-12-02 12:35:48 24 4
gpt4 key购买 nike

在 Jena 中使用 SPARQL 查询时,我试图以更易读的格式获取数据,但是我不知道如何以正确的方式提取数据。就目前而言,输出是:

"http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#SaucelitoCanyon "

只想将“SaucelitoCanyon”作为输出。

public JenaQuery() {

String wineRegion = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
+ "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
+ "PREFIX wine:<http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n"
+ "SELECT ?region ?winery \n"
+ "WHERE {?wine wine:locatedIn ?region . \n"
+ "?region wine:locatedIn wine:CaliforniaRegion . \n"
+ "?wine wine:hasMaker ?winery}";

String inputFileName = "wine.rdf";
// create an empty model

Model model = ModelFactory.createDefaultModel();
// use the FileManager to find the input file
InputStream in;
in = FileManager.get().open(inputFileName);
if (in == null) {
throw new IllegalArgumentException(
"File: " + inputFileName + " not found");
}
// read the RDF/XML file
model.read(in, null);
try (QueryExecution qexec = QueryExecutionFactory.create(wineRegion, model)) {

ResultSet results = qexec.execSelect();

while (results.hasNext()) {
QuerySolution row = results.next();
RDFNode winery = row.get("winery");
System.out.println(winery);
}

qexec.close();

}

}

最佳答案

由于您已经在 SPARQL 查询中获得了前缀,因此您可以使用 strafterstr 函数将 URI 转换为字符串,并在后面加上后缀字首。在下面,我使用 values ?winery { ... }?winery 绑定(bind)到特定的 URI,但您的查询已经处理了这个问题。重要的部分是之后的绑定(bind),它负责字符串处理。

PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>
SELECT ?winery ?localname
WHERE {
values ?winery { wine:SomeWinery }
bind( strafter(str(?winery),str(wine:)) as ?localname )
}
winery                                                            localname
<http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#SomeWinery> "SomeWinery"

也就是说,在结构良好的本体中,个人通常会有一个 rdfs:label 来为个人提供字符串标签。当它可用时,您可能会考虑简单地检索该值。例如,

SELECT ?winery ?name
WHERE {
#-- ...
?winery rdfs:label ?name
}

这之前已在 my answer 中描述过至retrieving the class name of a specific subclass in owl ,但该问题不涉及 Jena,因此它并不完全重复,即使相同的基于 SPARQL 的解决方案也有效。

关于java - 从 RDF 节点拉出字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26403868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com