query-1eeb17708eb5b81b0b64b5c121338d60

rq turtle/ttl

TODO

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT DISTINCT ?item
  (GROUP_CONCAT(DISTINCT lcase(?common_name); separator=", ") as ?common_names)
  (SAMPLE(?scientific_name) AS ?scientific_name)
  (GROUP_CONCAT(DISTINCT lcase(?taxon_common); separator=", ") as ?taxon_common)
  (MIN(?score) AS ?matchscore)
WHERE {
  BIND ("cat" AS ?searchstr) # Fantastic cats and where to find them
  ?item wdt:P31 wd:Q16521;
    wdt:P171+ wd:Q7377;
    wdt:P225 ?scientific_name.
  OPTIONAL {
    ?item wdt:P1843 ?taxon_common.
    FILTER(LANGMATCHES(LANG(?taxon_common), "en"))
  }
  OPTIONAL {
    ?item wdt:P366 ?common.
    ?common rdfs:label ?common_name.
    FILTER(LANGMATCHES(LANG(?common_name), "en"))
  }
  FILTER(BOUND(?common_name)|| BOUND(?taxon_common))
  FILTER( CONTAINS(lcase(?common_name), ?searchstr) || CONTAINS(lcase(?taxon_common), ?searchstr) )
  BIND((STRLEN(?common_name) - STRLEN(?searchstr)) AS ?ldiff_a)
  BIND((STRLEN(?taxon_common) - STRLEN(?searchstr)) AS ?ldiff_b)
  BIND(
    IF(BOUND(?ldiff_a) && !BOUND(?ldiff_b), ?ldiff_a,
      IF(!BOUND(?ldiff_a) && BOUND(?ldiff_b), ?ldiff_b,
        IF(!BOUND(?ldiff_a) && !BOUND(?ldiff_b), 9999,
          IF(?ldiff_a < ?ldiff_b, ?ldiff_a, ?ldiff_b))))
  AS ?score)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?item
ORDER BY ?matchscore

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v7("?common") v2("?common_name"):::projected v11("?common_names") v5("?item"):::projected v8("?ldiff_a") v9("?ldiff_b") v12("?matchscore") v12("?scientific_name"):::projected v10("?score"):::projected v5("?searchstr") v12("?taxon_common"):::projected c5(["wd:Q7377"]):::iri c11(["bd:serviceParam"]):::iri c1(["en"]):::literal c3(["wd:Q16521"]):::iri f0[["(contains(lower-case(?common_name),?searchstr) || contains(lower-case(?taxon_common),?searchstr))"]] f0 --> v2 f0 --> v5 f0 --> v12 f1[["(bound(?common_name) || bound(?taxon_common))"]] f1 --> v2 f1 --> v12 bind2[/"'cat'"/] bind2 --as--o v5 v5 --"wdt:P31"--> c3 v5 --"wdt:P171"--> c5 v5 --"wdt:P225"--> v12 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v5 -."wdt:P1843".-> v12 end subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v5 -."wdt:P366".-> v7 v7 --"rdfs:label"--> v2 end bind3[/"string-length(?common_name) - string-length(?searchstr)"/] v2 --o bind3 v5 --o bind3 bind3 --as--o v8 bind4[/"string-length(?taxon_common) - string-length(?searchstr)"/] v12 --o bind4 v5 --o bind4 bind4 --as--o v9 bind5[/"if(bound(?ldiff_a)not bound(?ldiff_b),?ldiff_a,if(not bound(?ldiff_a)bound(?ldiff_b),?ldiff_b,if(not bound(?ldiff_a)not bound(?ldiff_b),'9999^^xsd:integer',if(?ldiff_a < ?ldiff_b,?ldiff_a,?ldiff_b))))"/] v8 --o bind5 v9 --o bind5 bind5 --as--o v10 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c11 --"wikibase:language"--> c1 end bind10[/"lower-case(?common_name)"/] v2 --o bind10 bind10 --as--o v11 bind11[/"sample(?scientific_name)"/] v12 --o bind11 bind11 --as--o v12 bind12[/"lower-case(?taxon_common)"/] v12 --o bind12 bind12 --as--o v12 bind13[/"min(?score)"/] v10 --o bind13 bind13 --as--o v12