简体   繁体   English

SPARQL,RDF和处理数据

[英]SPARQL, RDF & processing Data

I'm new to this entire semantics stuff, and i've been plunged into a project which requires me to use the SPARQL endpoint from dbpedia to retrieve information about certain things such as a City for example. 我对这整个语义都很陌生,而且我已经投入到一个项目中,这个项目要求我使用dbpedia的SPARQL端点来检索有关某些事物的信息,例如City。 The idea is that my application collects a few of these keywords ( which are predefined, such as City, Country & Region ) and rely on dbpedia to get some sensible information about this. 我的想法是,我的应用程序收集了一些这些关键字( 预定义的,如城市,国家和地区 ),并依靠dbpedia获取有关此问题的一些明智信息。

After fiddling around with the SPARQL endpoint for some time, I came up with the following query: 在摆弄SPARQL端点一段时间之后,我想出了以下查询:

PREFIX dbo: <http://dbpedia.org/ontology/> 
PREFIX prop: <http://dbpedia.org/property/>

SELECT ?city
WHERE {
?city prop:name "X"@en; 
a dbo:PopulatedPlace
}

Where X stands for the name of the city that I'm trying to get information about. 其中X代表我试图获取信息的城市名称。 When I run this query against the SPARQL endpoint, I receive a URI in a RDF structure that points to a RDF store on DBPedia ( at least that's how I interpret it, please correct me if I'm wrong ) 当我针对SPARQL端点运行此查询时,我在RDF结构中收到一个指向DBPedia上的RDF存储的URI( 至少我是如何解释它的,如果我错了请纠正我

The returned result for the City "Antwerpen" looks like this: City“Antwerpen”的返回结果如下所示:

<rdf:RDF><rdf:Description rdf:nodeID="rset"><rdf:type rdf:resource="http://www.w3.org/2005/sparql-results#ResultSet"/><res:resultVariable>city</res:resultVariable><res:solution rdf:nodeID="r0"><res:binding rdf:nodeID="r0c0"><res:variable>city</res:variable><res:value rdf:resource="http://dbpedia.org/resource/Antwerp"/></res:binding></res:solution></rdf:Description></rdf:RDF>

I'm using Ruby on Rails with the RDF for Ruby gem to work with RDF data. 我正在使用Ruby on RailsRuby gem的RDF来处理RDF数据。 But honestly, I'm clueless on how to actually work with this information. 但老实说,我对如何真正处理这些信息毫无头绪。 When I follow the link, I seem to be receiving an RDF store that contains all information about the city I queried. 当我按照链接时,我似乎收到一个RDF商店,其中包含有关我查询的城市的所有信息。

Am I right in that I probably need to point my Code to the URI received and parse our the information I desire? 我是对的,我可能需要将我的代码指向收到的URI并解析我想要的信息吗? Or should it be possible for example to select some information directly through the SPARQL endpoint? 或者是否可以直接通过SPARQL端点选择一些信息? Like for example the description & demographic data? 例如描述和人口统计数据?

I know this is a pretty vague question, but I'm trying my best to get the hang of this technology, and looking for some examples to help me better understand it. 我知道这是一个非常模糊的问题,但我正在尽我所能来掌握这项技术,并寻找一些例子来帮助我更好地理解它。

It's probably easier to get started by getting all the info you want directly from the SPARQL endpoint. 通过直接从SPARQL端点获取所需的所有信息,可能更容易入手。 You can use a query like this: 您可以使用如下查询:

SELECT ?city ?property ?value
WHERE {
    ?city prop:name "X"@en; 
          a dbo:PopulatedPlace;
          ?property ?value .
}

This will return a bunch of properties and values that describe the city in some detail and it will give you a flavour for the kind of information that you can get. 这将返回一组属性和值,用于详细描述城市,它将为您提供您可以获得的信息类型。

A next step would be to query specifically for the properties you're interested in: 下一步是专门查询您感兴趣的属性:

SELECT *
WHERE {
    ?city prop:name "Galway"@en; 
          a dbo:PopulatedPlace .
    OPTIONAL { ?city dbo:populationUrban ?pop }
    OPTIONAL { ?city foaf:homepage ?homepage }
    OPTIONAL { ?city geo:lat ?lat }
    OPTIONAL { ?city geo:long ?long }
}

I'm putting each of the triple patterns into an OPTIONAL block because some cities may not have all of the information available. 我将每个三重模式放入一个OPTIONAL区块,因为有些城市可能没有所有可用的信息。 Without the OPTIONAL block, you'd only get a result if all the triple patterns match for the city. 如果没有OPTIONAL块,只有当所有三重模式与城市匹配时才会得到结果。

yes, you can use the URI received and parse out the information, or, you can use "bind" in your SPARQL to get the information only. 是的,您可以使用收到的URI并解析信息,或者,您可以在SPARQL中使用“bind”来获取信息。 For example, in the following SPARQL code: 例如,在以下SPARQL代码中:

PREFIX dbo: <http://dbpedia.org/ontology/> 
PREFIX prop: <http://dbpedia.org/property/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos/>

SELECT ?city, ?latitude, ?longitude
WHERE {
?city prop:name "Edinburgh"@en; 
    a dbo:PopulatedPlace;
    geo:lat ?lat;
    geo:long ?long.
    bind(strafter(str(?lat), "http://www.w3.org/2003/01/geo/wgs84_pos/") as ?latitude)
    bind(strafter(str(?lat), "http://www.w3.org/2003/01/geo/wgs84_pos/") as ?longitude)
}

instead of get " http://www.w3.org/2003/01/geo/wgs84_pos/55.93 " for latitude, you will get "55.93" only. 而不是获得纬度的“ http://www.w3.org/2003/01/geo/wgs84_pos/55.93 ”,你只会获得“55.93”。

Of course, if you do not have to use SPARQL endpoint, Jena (for java) and rdflib (for python) are very good choices to handle SPARQL queries and edit ontology. 当然,如果您不必使用SPARQL端点,则Jena(对于java)和rdflib(对于python)是处理SPARQL查询和编辑本体的非常好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM