Skip to content

Instantly share code, notes, and snippets.

@rhunter
Created June 30, 2014 07:59
Show Gist options
  • Save rhunter/1cd43eab7caceb67da58 to your computer and use it in GitHub Desktop.
Save rhunter/1cd43eab7caceb67da58 to your computer and use it in GitHub Desktop.
International number one songs and DBpedia

The scenario we were exploring, helping make a music playlist for a road trip, focused on the following:

  • reminiscibility
    • time and place
    • high exposure at an impressionable age
      • tied to life events
      • social music, nightclubs
        • ->teens, esp. late teens (16-20)
    • nothing to do with "goodness" of music
  • singalongability

Solutions based on DBpedia

There are a bunch of ways to get online access to DBpedia. The particular (bare-bones) query interface to we were using was SNORQL.

The fancier query builder seems to work fine if you build the query using the clicky-clicky circles-and-lines interface, but when we skipped straight to pasting in our own SPARQL, it showed the results in non-obvious ways.

There's also a relatively nice non-SPARQL exploration tool -- for an example, check out Believe (Cher song) in Faceted Search.

Find a category that both these things share

PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?catX ?catY ?catA
WHERE {
  <http://dbpedia.org/resource/Not_Many> ?p1 ?catX .
  <http://dbpedia.org/resource/Believe_(Cher_song)> ?p1 ?catY .
  ?catX skos:broader+ ?catA .
  ?catY skos:broader+ ?catA
}

This would probably be a lot more useful if DBpedia imported the full category hierarchy, instead of just those directly tagged in the wikitext.

DBpedia's extraction framework currently allocates "broader topics" based on a category linking directly to another category (and, curiously, allocates "related topics" based on linking directly to fully-qualified category).

The MediaWiki API does provide a template expansion service:

https://en.wikipedia.org/w/api.php?action=expandtemplates&title=:Category:2003_Singles&text={{Year%20by%20category|m=2|c=0|d=0|y=3|cat=singles|subcat=Singles%20by%20year|align=none}}&prop=wikitext

Find number one singles that had labels of New Zealand and UK

PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX dbpont: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://purl.org/dc/terms/>
SELECT ?regional_no_1s ?song ?songlabel ?artistname  WHERE {
  GRAPH ?g {
    ?song rdf:type dbpont:Single  .
    ?song dcterms:subject ?regional_no_1s .
    ?regional_no_1s skos:broader <http://dbpedia.org/resource/Category:Number-one_singles> .
    ?song dbpedia2:region "New Zealand"@en .
    ?song dbpedia2:region "United Kingdom"@en 
    # <http://dbpedia.org/resource/Believe_(Cher_song)> dbpont:musicalArtist <http://dbpedia.org/resource/Cher>   
    OPTIONAL {
     ?song foaf:name ?songlabel .
     ?song dbpont:musicalArtist ?artist .
     ?song dbpont:releaseDate ?releaseDate .
     ?artist rdfs:label ?artistname
    }
    FILTER (
      IF(BOUND(?songlabel), langMatches(LANG(?songlabel), "en" ), 1 = 1)
      &&
      IF(BOUND(?artistname), langMatches(LANG(?artistname), "en" ), 1 = 1)
      # &&
      #(IF(BOUND(?releaseDate), (YEAR(?releaseDate) = 1998), 1 = 1))
    )
  }
}
LIMIT 500
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment