gpt4 book ai didi

java - 从 SPARQL 结果中的文字中删除数据类型

转载 作者:搜寻专家 更新时间:2023-11-01 02:08:45 24 4
gpt4 key购买 nike

我正在使用 SPARQL 查询 OWL 本体,但结果显示链接和数据。我只想要数据。如何删除链接并显示在数据上?我得到的输出是:

---------------------------------------------------------------------------------------------------------------------------
| Email | ind | ind1 |
===========================================================================================================================
| "5"^^<http://www.w3.org/2001/XMLSchema#decimal> | my:HotelCityPark | "Hotel"^^<http://www.w3.org/2001/XMLSchema#string> |
---------------------------------------------------------------------------------------------------------------------------

我想要的输出是(注意 ^^<http://www.w3.org/2001/XMLSchema#decimal> 等部分不再存在。

---------------------------------
| Email | ind | ind1 |
=================================
| 5 | HotelCityPark | Hotel |
---------------------------------

这是我的 Java 代码和我的 OWL 本体:

class jena_testing
{ public static void main(String[] args) {

String filename="modified2.owl";
Model model=ModelFactory.createDefaultModel();
OntModel model1=ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM,model);

try
{
File file=new File(filename);
FileInputStream reader=new FileInputStream(file);
model.read(reader,null);

String query1="PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
"PREFIX my: <http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#>"+
"SELECT ?Email ?ind ?ind1"+
" WHERE {"+
" ?ind rdf:type my:Accomodation ."+
"?ind my:hasStarRating ?Email."+
"?ind my:hasType ?ind1."+
"FILTER regex(str(?ind),'HotelCityPark')"+
"}";
com.hp.hpl.jena.query.Query query=QueryFactory.create(query1);
QueryExecution exe=QueryExecutionFactory.create(query, model1);
ResultSet RES=exe.execSelect();
ResultSetFormatter.out(System.out, RES, query);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
<?xml version="1.0"?>


<!DOCTYPE rdf:RDF [
<!ENTITY owl "http://www.w3.org/2002/07/owl#" >
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
<!ENTITY untitled-ontology-20 "http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#" >
]>


<rdf:RDF xmlns="http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#"
xml:base="http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:untitled-ontology-20="http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#">
<owl:Ontology rdf:about="http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20"/>



<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Data properties
//
///////////////////////////////////////////////////////////////////////////////////////
-->




<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#hasLatitude -->

<owl:DatatypeProperty rdf:about="&untitled-ontology-20;hasLatitude"/>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#hasLongitude -->

<owl:DatatypeProperty rdf:about="&untitled-ontology-20;hasLongitude"/>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#hasStarRating -->

<owl:DatatypeProperty rdf:about="&untitled-ontology-20;hasStarRating"/>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#hasStreet -->

<owl:DatatypeProperty rdf:about="&untitled-ontology-20;hasStreet"/>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#hasType -->

<owl:DatatypeProperty rdf:about="&untitled-ontology-20;hasType"/>



<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Classes
//
///////////////////////////////////////////////////////////////////////////////////////
-->




<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Accomodation -->

<owl:Class rdf:about="&untitled-ontology-20;Accomodation"/>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Attractions -->

<owl:Class rdf:about="&untitled-ontology-20;Attractions"/>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Restuarants -->

<owl:Class rdf:about="&untitled-ontology-20;Restuarants"/>



<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Individuals
//
///////////////////////////////////////////////////////////////////////////////////////
-->




<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#AkkalkotSwamiSamarthMaharaj_temple -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;AkkalkotSwamiSamarthMaharaj_temple">
<rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Ambassador_Executive -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;Ambassador_Executive">
<rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
<hasStarRating rdf:datatype="&xsd;decimal">3</hasStarRating>
<hasType rdf:datatype="&xsd;string">Hotel</hasType>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#BalajiSarovarPremier -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;BalajiSarovarPremier">
<rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
<hasStarRating rdf:datatype="&xsd;decimal">3.5</hasStarRating>
<hasType rdf:datatype="&xsd;string">Hotel</hasType>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#BhagwatBigCinemas -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;BhagwatBigCinemas">
<rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Bhuikotfort -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;Bhuikotfort">
<rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#DhruvaHotel -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;DhruvaHotel">
<rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
<hasStarRating rdf:datatype="&xsd;decimal">3</hasStarRating>
<hasType rdf:datatype="&xsd;string">Hotel</hasType>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Durga_Mangesh_Garden_Restuarant -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;Durga_Mangesh_Garden_Restuarant">
<rdf:type rdf:resource="&untitled-ontology-20;Restuarants"/>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#GreatIndianBustardSancturay -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;GreatIndianBustardSancturay">
<rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#HotelCityPark -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;HotelCityPark">
<rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
<hasStarRating rdf:datatype="&xsd;decimal">5</hasStarRating>
<hasType rdf:datatype="&xsd;string">Hotelcitypark</hasType>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#HotelLotus -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;HotelLotus">
<rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
<hasStarRating rdf:datatype="&xsd;decimal">3</hasStarRating>
<hasType rdf:datatype="&xsd;string">Hotel</hasType>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#HotelNila -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;HotelNila">
<rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
<hasStarRating rdf:datatype="&xsd;decimal">1.2</hasStarRating>
<hasType rdf:datatype="&xsd;string">Hotel</hasType>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#HotelTripursundari -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;HotelTripursundari">
<rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
<hasStarRating rdf:datatype="&xsd;decimal">1.6</hasStarRating>
<hasType rdf:datatype="&xsd;string">Hotel</hasType>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#HotelVaishnavi -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;HotelVaishnavi">
<rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
<hasStarRating rdf:datatype="&xsd;decimal">4.5</hasStarRating>
<hasType rdf:datatype="&xsd;string">Hotel</hasType>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#KRAVINGS-THE-FOOD-WORLD -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;KRAVINGS-THE-FOOD-WORLD">
<rdf:type rdf:resource="&untitled-ontology-20;Restuarants"/>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Kamat -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;Kamat">
<rdf:type rdf:resource="&untitled-ontology-20;Restuarants"/>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#NikitaPurVeg -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;NikitaPurVeg">
<rdf:type rdf:resource="&untitled-ontology-20;Restuarants"/>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Pandurang_temple -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;Pandurang_temple">
<rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Parasnath_Jain_Temple -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;Parasnath_Jain_Temple">
<rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#PrathamHotel -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;PrathamHotel">
<rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
<hasStarRating rdf:datatype="&xsd;decimal">1.6</hasStarRating>
<hasType rdf:datatype="&xsd;string">Hotel</hasType>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#RevanSiddeshwar_temple -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;RevanSiddeshwar_temple">
<rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#RiteshHotel -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;RiteshHotel">
<rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
<hasStarRating rdf:datatype="&xsd;decimal">3.5</hasStarRating>
<hasType rdf:datatype="&xsd;string">Hotel1</hasType>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Rukmini_temple -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;Rukmini_temple">
<rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Shivparvati_International_Hotel -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;Shivparvati_International_Hotel">
<rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
<hasStarRating rdf:datatype="&xsd;decimal">4</hasStarRating>
<hasType rdf:datatype="&xsd;string">Hotel</hasType>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#ShriSiddheshwar_temple -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;ShriSiddheshwar_temple">
<rdf:type rdf:resource="&untitled-ontology-20;Attractions"/>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Sips_and_Bites -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;Sips_and_Bites">
<rdf:type rdf:resource="&untitled-ontology-20;Restuarants"/>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Spice_n_Ice -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;Spice_n_Ice">
<rdf:type rdf:resource="&untitled-ontology-20;Restuarants"/>
<hasStarRating rdf:datatype="&xsd;decimal">3.5</hasStarRating>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Srikamal_International_Hotel -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;Srikamal_International_Hotel">
<rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
<hasStarRating rdf:datatype="&xsd;decimal">4.8</hasStarRating>
<hasType rdf:datatype="&xsd;string">Hotel</hasType>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Supraja_Pav_Bhaji -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;Supraja_Pav_Bhaji">
<rdf:type rdf:resource="&untitled-ontology-20;Restuarants"/>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#SuryaInternational -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;SuryaInternational">
<rdf:type rdf:resource="&untitled-ontology-20;Accomodation"/>
<hasStarRating rdf:datatype="&xsd;decimal">5</hasStarRating>
<hasType rdf:datatype="&xsd;string">Hotel</hasType>
</owl:NamedIndividual>



<!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#Temptations -->

<owl:NamedIndividual rdf:about="&untitled-ontology-20;Temptations">
<rdf:type rdf:resource="&untitled-ontology-20;Restuarants"/>
</owl:NamedIndividual>
</rdf:RDF>

最佳答案

您可能希望更加熟悉 RDF 的一些概念。 W3C 的 RDF Primer可能是一个很好的起点。我这样说是因为您提到结果的方式表明您对 RDF 不熟悉。让我们来看看它们。在您显示的结果中,您将返回这些值:

"5"^^<http://www.w3.org/2001/XMLSchema#decimal>
my:HotelCityPark
"Hotel"^^<http://www.w3.org/2001/XMLSchema#string>

第一个是数据类型为 xsd:decimal 的文字 5。那是一个数字。在 RDF 1.0 中,文字可以是纯文字(只是一个字符串)、语言标记字符串(带有语言标签的字符串)或数据类型文字(字符串(词汇形式)和数据类型)。文字是用来表示具体数据的。 "5"^^xsd:decimal对它是什么非常具体。您可以提取数据类型文字的词法部分,以保留普通文字 "5"。 ,但请注意,这不再是数字。如果您想要一个 Java 数字,最好处理为文字并获取其解释值。

第二个是缩写为 my:HotelCityPark 的 URI .根据您的前缀,这是 URI http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#HotelCityPark . URI 在 RDF 中用作标识符。您可以将该 URI 转换为普通文字,以获得 "http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#HotelCityPark" ,然后执行一些字符串操作以删除所有内容,包括 # , 留下普通文字 "HotelCityPark" .

第三个是数据类型文字(数据类型为 xsd:string )。就像第一个一样,您可以提取其词法形式以保留普通文字 "Hotel"。 ,这并没有真正丢失太多信息。 (在 RDF 1.1 中,没有纯文字;没有显式数据类型的字符串使用 xsd:string 隐式类型化。)

您请求的值

5
HotelCityPark
Hotel

确实没有足够的信息告诉我们您想要什么数据。那些应该是字符串吗?如果是这样,那么你应该使用

"5"
"HotelCityPark"
"Hotel"

如果您希望第一个是数字,您也可以将其转换为 xsd:integer ,因为形式为 "5"^^xsd:integer 的文字通常写成 5 .例如,Turtle 和 N3 语法支持这一点。

您可以使用 xsd:integer 进行转换, 字符串提取 str功能,并删除 my:使用 strafter 作为前缀功能。因此你会有这样的查询:

prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix my: <http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-20#>

select ?Email ?ind ?ind1 where {
#-- The values block just binds ?_Email, ?_ind, and ?_ind1
#-- to the original output that you were getting. You'd still
#-- have your original query text here.
values (?_Email ?_ind ?_ind1) {
("5"^^xsd:decimal my:HotelCityPark "Hotel"^^xsd:string)
}

bind( xsd:integer(?_Email) as ?Email )
bind( strafter(str(?_ind),str(my:)) as ?ind )
bind( str(?_ind1) as ?ind1 )
}
-------------------------------------
| Email | ind | ind1 |
=====================================
| 5 | "HotelCityPark" | "Hotel" |
-------------------------------------

关于java - 从 SPARQL 结果中的文字中删除数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23626194/

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