Search code examples
sparqlgraphdb

SPARQL - Count occurrence of a substring in object


I'm fairly new to Linked-Data and SPARQL but I understand the concept and some of the querying as I do have knowledge of SQL. Using some example data from rdfdata.org I managed to setup a GraphDB instance with an Elvis impersonator repo.

Using some basic queries like SELECT * WHERE {?s ?p ?o} and filtering on object values I was able to get some basic data visible in tables. I have experience using regular expressions so I decided to use this with SPARQL to count the occurrence of Elvis within the object. However, whatever I do I am not able to get this above one.

This is a problem as I have triples that contain a form of elvis more than once:

 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.

As you can see there are three occurrences of Elvis which are only counted as 1.

Here is the SPARQL query used to select the triple and to count the occurrences:

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

How is it possible that these occurrences are not counted? I tried using str(?o) but as the object is a string literal to begin with that should not matter.

Expected result:

le table with 4 columns: | ?s | ?p | ?o | count |, where count should be "3"^^xsd:integer


Solution

  • SPARQL count is used to count the number of matching possible bindings in the RDF data or simply said, the number of matching rows. Indeed there is only one object that matches the REGEX, thus, only one row. Unfortunately, SPARQL doesn't have any concept of explode to create multiple rows out of a single row (or better said, I'm not aware of).

    As a workaround, I wrote a SPARQL query with REGEX + String hacks. The idea is to

    1. replace each occurrence of Elvis with some special character that hopefully doesn't occur. I've chosen Å here for demonstration.
    2. delete each other character in the text
    3. compute the length of the remaining string

    Query

    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) 
    }
    

    Output (sample)

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