sparql-examples

A set of SPARQL examples that are used in different TGX resources

View the Project on GitHub BiGCAT-UM/sparql-examples

089

rq turtle/ttl

Event aspect: related events timelocation

Use at

PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://wikidata.org/entity/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX target: <http://www.wikidata.org/entity/Q133457282>

SELECT
  (ROUND(1000 * ?score_) / 1000 AS ?score)
  (ROUND(1000 * ?time_score_) / 1000 AS ?time_score)
  (ROUND(1000 * ?location_score_) / 1000 AS ?location_score) 
  ?event ?eventLabel
WITH {
  SELECT ?event_type WHERE {
    VALUES ?event_type { wd:Q2020153 wd:Q46855 wd:Q40444998 }
  }
} AS %event_types
WITH {
  SELECT ?event WHERE {
    INCLUDE %event_types
    ?event wdt:P31 / wdt:P279* ?event_type .
  }
} AS %events
WITH {
  SELECT
    (SUM(?scores) AS ?score_)
    (MAX(?time_scores) AS ?time_score_)
    (MAX(?location_scores) AS ?location_score_)
    ?event
  WHERE {
    INCLUDE %events 
          
    { 
      # Around the same time
      target: wdt:P580 | wdt:P585 ?time0 .
      ?event wdt:P580 | wdt:P585 ?time .
      BIND((365.25 * YEAR(?time0) + 30.5 * MONTH(?time0) + DAY(?time0)) AS ?day0)
      BIND((365.25 * YEAR(?time) + 30.5 * MONTH(?time) + DAY(?time)) AS ?day)
      
      # TODO: multiple days cause double counting
      BIND(20 / (1 + ABS(?day - ?day0)) AS ?time_scores)
    }
    # UNION
    {
      SELECT ?event (MAX(?inverse_distance) AS ?location_scores) WHERE {
        INCLUDE %events 
                
        # geocoordinate of the query event
        target: wdt:P276? / wdt:P159? / wdt:P625 ?geo0 .
        
        # geocoordinate of other events
        ?event wdt:P276? / wdt:P625 ?geo .
        
        # inverse distance
        BIND((200 / (1 + geof:distance(?geo, ?geo0))) AS ?inverse_distance)
      }
      GROUP BY ?event
    }

    # Should online event be listed? 
    # UNION
    # {
    #   VALUES ?online_event { wd:Q1543677 wd:Q98381855 wd:Q98381912 }
    #   ?event wdt:P31 ?online_event .
    #   BIND(200 AS ?location_scores)
    # }
  
    BIND((?time_scores * ?location_scores) AS ?scores)
  }
  GROUP BY ?event
  
} AS %results
WHERE {
  INCLUDE %results
  FILTER (?event != target:)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],mul,en" . }
}
ORDER BY DESC(?score)

graph TD