Jump to content

User:Jts1882/Wikidata

From Wikipedia, the free encyclopedia

Information and resources for using Wikidata

Visualising Wikidata

[edit]

Code for parents:

SELECT ?parent ?parentLabel (GROUP_CONCAT(DISTINCT ?rank_label_; separator=", ") AS ?rankList) WHERE {
  wd:Q25265 wdt:P171+ ?parent .
  ?parent wdt:P105 ?rank .
  OPTIONAL {
    ?rank rdfs:label ?rank_label_ . FILTER (LANG(?rank_label_) = 'en')
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} GROUP BY ?parent ?parentLabel

Code for graphic (parents+children):

#defaultView:Graph
SELECT
  ?child ?childLabel
  ?rgb 
  ?parent ?parentLabel
WITH {
  SELECT ?child ?rgb ?parent WHERE {
    { 
      # Parent taxons
      SELECT ?child ?rgb ?parent  WHERE {
        SERVICE gas:service {
          gas:program gas:gasClass "com.bigdata.rdf.graph.analytics.BFS" ;
                  gas:in wd:Q25265 ;
                  # We should not do undirected here because this could result
                  # in very big graphs.
                  gas:traversalDirection "Forward" ;
                  gas:out ?child ;
                  gas:out1 ?depth ;
                  gas:out2 ?parent1 ;
                  gas:linkType wdt:P171 ;
        }
        ?child wdt:P171 ?parent .
        BIND(IF(?child = wd:Q25265, "FF0000", "FFFFFF") AS ?rgb) 
      }
    }
    UNION 
    {
      # Child taxons
      SELECT ?child ?rgb ?parent WHERE {
        BIND(wd:Q25265 AS ?parent)
        ?child wdt:P171 ?parent . 
        BIND("DDDDDD" AS ?rgb)
      }
      LIMIT 100
    }
  }
} AS %results
WHERE {
  INCLUDE %results 
          
  ?child rdfs:label ?child_label . FILTER(LANG(?child_label) = 'en')
  ?parent rdfs:label ?parent_label . FILTER(LANG(?parent_label) = 'en')
  OPTIONAL {
    ?child wdt:P105 / rdfs:label ?child_rank_label . FILTER (LANG(?child_rank_label) = 'en')
  }
  OPTIONAL {
    ?parent wdt:P105 / rdfs:label ?parent_rank_label . FILTER (LANG(?parent_rank_label) = 'en')
  }
  BIND(CONCAT(?child_label, " - ", COALESCE(?child_rank_label, "???")) AS ?childLabel)
  BIND(CONCAT(?parent_label, " - ", COALESCE(?parent_rank_label, "???")) AS ?parentLabel)

}


Sparql searches

[edit]

https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples

  • Species of Felidae
# Species of cats
SELECT ?item ?taxonname WHERE {
  ?item wdt:P31 wd:Q16521 ;    # taxon
        wdt:P105 wd:Q7432 ;    # taxon rank = species
        wdt:P171* wd:Q25265 ;  # parent taxon = Felidae
        wdt:P225 ?taxonname    # taxon name
}
  • Taxa with parent Felidae
# Taxa with parent Felidae
SELECT ?item ?taxonname WHERE {
  ?item wdt:P31 wd:Q16521 ;    # instance of taxon
       # wdt:P105 wd:Q7432 ;    # taxon rank = species
        wdt:P171 wd:Q25265 ;  # parent = Felidae
        wdt:P225 ?taxonname
}
#added before 2016-10

#defaultView:Graph
SELECT ?item ?itemLabel ?pic ?linkTo
WHERE
{
  wd:Q25265 wdt:P171* ?item
  OPTIONAL { ?item wdt:P171 ?linkTo }
  OPTIONAL { ?item wdt:P18 ?pic }
  SERVICE wikibase:label {bd:serviceParam wikibase:language "en" }
}
#defaultView:Graph
PREFIX gas: <http://www.bigdata.com/rdf/gas#>

SELECT ?item ?itemLabel ?pic ?linkTo
WHERE
{
  SERVICE gas:service {
    gas:program gas:gasClass "com.bigdata.rdf.graph.analytics.SSSP" ;
                gas:in wd:Q25265;
                gas:traversalDirection "Reverse" ;
                gas:out ?item ;
                gas:out1 ?depth ;
                gas:maxIterations 3 ;
                gas:linkType wdt:P171 .
  }
  OPTIONAL { ?item wdt:P171 ?linkTo }
  OPTIONAL { ?item wdt:P18 ?pic }
  SERVICE wikibase:label {bd:serviceParam wikibase:language "en" }
}
  • Taxa with two or three parents

Find taxa with two or three parents.

SELECT ?taxon WHERE { ?taxon wdt:P171 ?parent1, ?parent2 . FILTER (?parent1 != ?parent2) } LIMIT 10
SELECT ?taxon WHERE { ?taxon wdt:P171 ?parent1, ?parent2, ?parent3 . 
                     FILTER (?parent1 != ?parent2) 
                     FILTER (?parent1 != ?parent3)
                     FILTER (?parent3 != ?parent2)
                    } LIMIT 10