我对Linked-Data和SPARQL
相当新,但我理解这个概念和一些查询,因为我对SQL有所了解。使用rdfdata.org中的一些示例数据,我设法使用Elvis impersonator repo设置GraphDB实例。
使用一些基本的查询
SELECT * WHERE {?s ?p ?o}
并过滤对象值我能够在表格中看到一些基本数据。我有使用正则表达式的经验,所以我决定使用SPARQL来计算对象中Elvis
的出现次数。但是,无论我做什么,我都无法得到这个。
这是一个问题,因为我的三元组包含elvis
不止一次的形式:
s: http://www.gigmasters.com/elvis/bobjames/
p: ep:influences
o: Elvis Elvis Elvis! I also do a Neil Diamond tribute as well, and have
been a DJ, MC, and musician for many years.
正如您所看到的,Elvis
出现了三次,只计为1次。
以下是用于选择三元组并计算出现次数的SPARQL查询:
SELECT ?s ?p ?o (count(regex( ?o ,"[Ee]lvis")) as ?count)
WHERE {
?s ?p ?o.
filter(regex( ?o ,"([Ee]lvis.){3}")) //only return the triple above
}
GROUP BY ?s ?p ?o
这些事件怎么可能不计算在内?我尝试使用str(?o)
,但因为对象是一个字符串文字,首先 无关紧要。
预期结果:
包含4列的表:| ?s | ?p | ?o | count |
,
其中count应为"3"^^xsd:integer
答案 0 :(得分:4)
您可以通过获取输入字符串(例如"A B A C"
),用空字符串("A"
)替换目标的出现(例如,""
)来实现此目的更新的字符串(例如," B C"
)。然后,计算更新的字符串与输入字符串的长度之间的差异。将其除以目标的长度,以及目标在输入中出现的次数。例如:
@prefix : <urn:ex:>
:a :hasString "I like Elvis." .
:b :hasString "Elvis's name was Elvis." .
:c :hasString "Not mentioned here" .
:d :hasString "daybydaybyday" .
prefix : <urn:ex:>
select ?x ?s ?t ?count where {
values ?t { "Elvis" "daybyday" }
?x :hasString ?s .
bind(((strlen(?s) - strlen(replace(?s, ?t, ""))) / strlen(?t)) as ?count)
}
-------------------------------------------------------
| x | s | t | count |
=======================================================
| :a | "I like Elvis." | "Elvis" | 1.0 |
| :b | "Elvis's name was Elvis." | "Elvis" | 2.0 |
| :c | "Not mentioned here" | "Elvis" | 0.0 |
| :d | "daybydaybyday" | "Elvis" | 0.0 |
| :a | "I like Elvis." | "daybyday" | 0.0 |
| :b | "Elvis's name was Elvis." | "daybyday" | 0.0 |
| :c | "Not mentioned here" | "daybyday" | 0.0 |
| :d | "daybydaybyday" | "daybyday" | 1.0 |
-------------------------------------------------------
这里有几点需要注意。
答案 1 :(得分:3)
SPARQL计数用于计算RDF数据中匹配的可能绑定的数量,或简单地说,匹配行的数量。实际上只有一个对象与REGEX匹配,因此只有一行。 不幸的是,SPARQL没有 explode 的任何概念来创建单行中的多行(或者更好地说,我不知道)。
作为一种解决方法,我使用REGEX + String hacks编写了一个SPARQL查询。想法是
Elvis
的每个匹配项替换为希望不会发生的特殊字符。我已在此选择Å
进行演示。PREFIX ep: <http://www.snee.com/ns/ep>
SELECT ?s ?p ?o ?cnt
WHERE {
?s ?p ?o.
filter(regex( str(?o) ,"([Ee]lvis.)"))
bind(
strlen(
replace(
replace(str(?o), "([Ee]lvis.)", "Å")
, "[^Å]", ""
)
) as ?cnt)
}
+-----------------------------------------------+-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+
| s | p | o | cnt |
+-----------------------------------------------+-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+
| http://www.gigmasters.com/elvis/ChuckBaril/ | http://www.snee.com/ns/epinfluences | Elvis, Donny Osmond, Barry Manilow, Pebo Bryson, James Ingram, George Benson, and George Strait | 1 |
| http://www.gigmasters.com/elvis/DukeHicks/ | dc:description | Been performing Elvis tribute shows for 10 yrs. Having been in the music business for twenty years Duke knows how to please the audience. Duke started doing his tribute shows after several request from the audience members to do more and more of Elvis' songs and a request for him to do an Elvis Tribute Show. Duke has been asked several times if he is lip-syching to Elvis' songs and the answer is absolutely NO. The sound and stage presence is so close to 'The King' that it has startled many. | 4 |
| http://www.gigmasters.com/elvis/DukeHicks/ | http://www.snee.com/ns/epcategory | Elvis Impersonator, Tribute Band | 1 |
| http://www.gigmasters.com/elvis/DukeHicks/ | http://www.snee.com/ns/epinfluences | Elvis Presley | 1 |
| http://www.gigmasters.com/elvis/ElvisByDano/ | dc:description | For a great time at your next event, how about ELVIS by Dano? His main goal is to provide a show that reflects the raw energy, passion, and humor that The King once shared with us. Dano, being a huge Elvis fan since his eleventh year, has loved singing along with The Man his entire adult life. He started to impersonate Elvis in public about 1995, and his first long solo performance, with a full set of songs, was at a church social in 2002. Dano was also a seven year member of a classic rock band and often contributed an Elvis act that audiences always truly enjoyed. Starting in February, 2004 he has performed in many solo shows for benefits, auctions, various parties , a Theme Park, as well as much time donated to entertain the elderly. He uses quality audio equipment with great sounding background tracks. Longer travel distances will be considered. Contact Dano today if you want your next party 'all shook up'!!! | 3 |
+-----------------------------------------------+-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+