SPARQL - 计算对象中子字符串的出现次数

SPARQL - Count occurrence of a substring in object

我对关联数据和 SPARQL 还很陌生,但我理解这个概念和一些查询,因为我确实了解 SQL。使用来自 rdfdata.org 的一些示例数据,我设法设置了一个带有 Elvis impersonator repo 的 GraphDB 实例。

使用一些基本查询,例如 SELECT * WHERE {?s ?p ?o} 并过滤对象值我能够在 table 中获得一些可见的基本数据。我有使用正则表达式的经验,所以我决定将其与 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 出现了 3 次,只算作 1 次。

这是用于 select 三元组和计算出现次数的 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) 但由于对象是字符串文字,因此 应该 无关紧要。

预期结果:

le table 有 4 列:| ?s | ?p | ?o | count |, 其中计数应为 "3"^^xsd:integer

SPARQL 计数用于统计RDF 数据中可能匹配的绑定数,或者简单地说,匹配行数。实际上只有一个对象与 REGEX 匹配,因此只有一行。 不幸的是,SPARQL 没有任何 explode 的概念来从一行中创建多行(或者更好地说,我不知道)。

作为解决方法,我使用 REGEX + String hacks 编写了一个 SPARQL 查询。思路是

  1. 用一些希望不会出现的特殊字符替换每次出现的 Elvis。我在这里选择 Å 进行演示。
  2. 删除文本中的其他字符
  3. 计算剩余字符串的长度

查询

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 |
+-----------------------------------------------+-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+

您可以通过获取输入字符串(例如,"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   |
-------------------------------------------------------

这里有几个注意事项。

  • 目标字符串必须是 "normal" 字符串。例如,如果它是一个真正的正则表达式模式,可以扩展到不同长度的文本,则此方法将不起作用。
  • 您需要了解它如何处理重叠字符串。例如,如果您的输入文本是 "daybydaybyday" 并且目标是 "daybyday",您是希望计数 一次 还是 两次[=28] =]?使用这种方法,您将只得到 一个,因为一旦替换了一个,剩下的字符串就没有了。