query-b51bcdf2f8030acd0efa8cfc5f08a81a

rq turtle/ttl

.on TwitterOriginally posted

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 ps: <http://www.wikidata.org/prop/statement/>
PREFIX pqv: <http://www.wikidata.org/prop/qualifier/value/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
#defaultView:ImageGrid
SELECT ?cocktail ?cocktailLabel (SAMPLE(?recipe) AS ?recipe) (SAMPLE(?image) AS ?image) WHERE {
  {
    SELECT ?cocktail ?cocktailLabel (GROUP_CONCAT(DISTINCT ?ingredient; separator=", ") AS ?ingredientsList) (GROUP_CONCAT(DISTINCT ?garnishLabel; separator=", ") AS ?garnishList) (COUNT(DISTINCT ?ingredient) + COUNT(DISTINCT ?garnishLabel) AS ?count) WHERE {
      ?cocktail wdt:P31/wdt:P279* wd:Q134768;
                p:P186 ?materialStat.
      MINUS { ?materialStat pq:P518/wdt:P279* wd:Q2453629. }
      MINUS { ?materialStat ps:P186 wd:Q488463; pq:P366 wd:Q26876981. }
      MINUS { ?materialStat pq:P366 wd:Q59541. }
      ?materialStat ps:P186/rdfs:label ?materialLabel.
      FILTER(LANG(?materialLabel) = "en")
      BIND(?materialLabel AS ?ingredientSolo)
      OPTIONAL {
        ?materialStat ps:P186/rdfs:label ?materialLabel;
                      pq:P1114 ?quantity.
        FILTER(LANG(?materialLabel) = "en")
        BIND(CONCAT(STR(?quantity), " ", ?materialLabel) AS ?ingredientWithQuantity).
        OPTIONAL {
          ?materialStat pq:P1114 ?quantity;
                        ps:P186/rdfs:label ?materialLabel;
                        pqv:P1114/wikibase:quantityUnit ?unit.
          FILTER(LANG(?materialLabel) = "en")
          FILTER(?unit != wd:Q199)
          ?unit rdfs:label ?unitLabel.
          FILTER(LANG(?unitLabel) = "en")
          BIND(CONCAT(STR(?quantity), " ", IF(?quantity = 1, ?unitLabel, CONCAT(?unitLabel, IF(STRENDS(?unitLabel, "sh"), "es", "s"))), " ", ?materialLabel) AS ?ingredientWithUnit)
        }
      }
      BIND(COALESCE(?ingredientWithUnit, ?ingredientWithQuantity, ?ingredientSolo) AS ?ingredient)
      OPTIONAL {
        ?cocktail p:P186 [ ps:P186 ?garnish; pq:P366 wd:Q59541 ].
        ?garnish rdfs:label ?garnishLabel.
        FILTER(LANG(?garnishLabel) = "en")
      }
      ?cocktail rdfs:label ?cocktailLabel.
      FILTER(LANG(?cocktailLabel) = "en")
    }
    GROUP BY ?cocktail ?cocktailLabel
  }
  BIND(
    IF(REGEX(?ingredientsList, ", .*,"),
       REPLACE(?ingredientsList, "(.*), (.*)", "$1, and $2"),
       REPLACE(?ingredientsList, "(.*), (.*)", "$1 and $2"))
    AS ?ingredients)
  BIND(
    IF(REGEX(?garnishList, ", .*,"),
       REPLACE(?garnishList, "(.*), (.*)", "$1, and $2"),
       REPLACE(?garnishList, "(.*), (.*)", "$1 and $2"))
    AS ?garnishes)
  OPTIONAL {
    ?cocktail p:P186 [ ps:P186 ?glass; pq:P518/wdt:P279* wd:Q2453629 ].
    ?glass rdfs:label ?glassLabel.
    FILTER(LANG(?glassLabel) = "en")
    BIND(IF(REGEX(?glassLabel, "^[AEIOUaeiou]"), "an", "a") AS ?article)
    BIND(CONCAT(" in ", ?article, " ", ?glassLabel) AS ?container)
    BIND(", served" AS ?served)
  }
  OPTIONAL {
    ?cocktail p:P186 [ ps:P186 wd:Q488463; pq:P366 wd:Q26876981 ].
    BIND(" on the rocks" AS ?onTheRocks)
    BIND(", served" AS ?served)
  }
  OPTIONAL {
    FILTER(STRLEN(STR(?garnishes)) > 1)
    BIND(CONCAT(" with ", ?garnishes) AS ?garnish)
    BIND(", served" AS ?served)
  }
  BIND(CONCAT(
    ?ingredients,
    COALESCE(?served, ""),
    COALESCE(?onTheRocks, ""),
    COALESCE(?garnish, ""),
    COALESCE(?container, ""))
    AS ?recipe)
  OPTIONAL { ?cocktail wdt:P18 ?image. }
}
GROUP BY ?cocktail ?cocktailLabel
ORDER BY DESC(MAX(?count))

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v16("?_anon_42b5416610844a34809f33a099ae9de1127403") v17("?_anon_42b5416610844a34809f33a099ae9de1127404") v29("?_anon_42b5416610844a34809f33a099ae9de1127416") v23("?article") v6("?cocktail"):::projected v3("?cocktailLabel"):::projected v24("?container") v20("?count") v27("?garnish") v4("?garnishLabel") v19("?garnishList") v22("?garnishes") v22("?glass") v2("?glassLabel") v30("?image"):::projected v14("?ingredient") v8("?ingredientSolo") v12("?ingredientWithQuantity") v13("?ingredientWithUnit") v21("?ingredients") v18("?ingredientsList") v5("?materialLabel") v7("?materialStat") v26("?onTheRocks") v11("?quantity") v30("?recipe"):::projected v27("?served") v9("?unit") v10("?unitLabel") a2((" ")) a3((" ")) a4((" ")) a5((" ")) a6((" ")) a7((" ")) a8((" ")) a9((" ")) a10((" ")) a11((" ")) a1((" ")) c5(["wd:Q134768"]):::iri c13(["wd:Q59541"]):::iri c12(["wd:Q26876981"]):::iri c10(["wd:Q488463"]):::iri c8(["wd:Q2453629"]):::iri f0[["?cocktailLabel = 'en'"]] f0 --> v3 f1[["?materialLabel = 'en'"]] f1 --> v5 v6 --"p:direct/P31"--> a2 a2 --"p:direct/P279"--> c5 v6 --"p:P186"--> v7 subgraph minus2["MINUS"] style minus2 stroke-width:6px,fill:pink,stroke:red; v7 --"p:qualifier/P518"--> a3 a3 --"p:direct/P279"--> c8 end subgraph minus3["MINUS"] style minus3 stroke-width:6px,fill:pink,stroke:red; v7 --"p:statement/P186"--> c10 v7 --"p:qualifier/P366"--> c12 end subgraph minus4["MINUS"] style minus4 stroke-width:6px,fill:pink,stroke:red; v7 --"p:qualifier/P366"--> c13 end v7 --"p:statement/P186"--> a4 a4 --"rdfs:label"--> v5 bind5[/"?materialLabel"/] v5 --o bind5 bind5 --as--o v8 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v7 -."p:statement/P186".-> a5 a5 --"rdfs:label"--> v5 v7 --"p:qualifier/P1114"--> v11 bind6[/"concat(str(?quantity),' ',?materialLabel)"/] v11 --o bind6 v5 --o bind6 bind6 --as--o v12 subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v7 -."p:qualifier/P1114".-> v11 v7 --"p:statement/P186"--> a6 a6 --"rdfs:label"--> v5 v7 --"p:qualifier/value/P1114"--> a7 a7 --"wikibase:quantityUnit"--> v9 v9 --"rdfs:label"--> v10 bind7[/"concat(str(?quantity),' ',if(?quantity = '1^^xsd:integer',?unitLabel,concat(?unitLabel,if(ends-with(?unitLabel,'sh'),'es','s'))),' ',?materialLabel)"/] v11 --o bind7 v10 --o bind7 v5 --o bind7 bind7 --as--o v13 end end bind8[/"?ingredientWithUnit?ingredientWithQuantity?ingredientSolo"/] v13 --o bind8 v12 --o bind8 v8 --o bind8 bind8 --as--o v14 subgraph optional2["(optional)"] style optional2 fill:#bbf,stroke-dasharray: 5 5; a8 -."p:statement/P186".-> v27 a8 --"p:qualifier/P366"--> c13 v6 --"p:P186"--> a8 v27 --"rdfs:label"--> v4 end v6 --"rdfs:label"--> v3 bind13[/"count(?ingredient)"/] v14 --o bind13 bind13 --as--o v16 bind14[/"count(?garnishLabel)"/] v4 --o bind14 bind14 --as--o v17 bind15[/"?ingredient"/] v14 --o bind15 bind15 --as--o v18 bind16[/"?garnishLabel"/] v4 --o bind16 bind16 --as--o v19 bind17[/" + "/] null --o bind17 null --o bind17 bind17 --as--o v20 bind18[/"if(regex(?ingredientsList,', .*,'),replace(?ingredientsList,'(.*), (.*)','$1, and $2'),replace(?ingredientsList,'(.*), (.*)','$1 and $2'))"/] v18 --o bind18 bind18 --as--o v21 bind19[/"if(regex(?garnishList,', .*,'),replace(?garnishList,'(.*), (.*)','$1, and $2'),replace(?garnishList,'(.*), (.*)','$1 and $2'))"/] v19 --o bind19 bind19 --as--o v22 subgraph optional3["(optional)"] style optional3 fill:#bbf,stroke-dasharray: 5 5; a9 -."p:statement/P186".-> v22 a9 --"p:qualifier/P518"--> a10 a10 --"p:direct/P279"--> c8 v6 --"p:P186"--> a9 v22 --"rdfs:label"--> v2 bind20[/"if(regex(?glassLabel,'^#91;AEIOUaeiou#93;'),'an','a')"/] v2 --o bind20 bind20 --as--o v23 bind21[/"concat(' in ',?article,' ',?glassLabel)"/] v23 --o bind21 v2 --o bind21 bind21 --as--o v24 bind22[/"', served'"/] bind22 --as--o v27 end subgraph optional4["(optional)"] style optional4 fill:#bbf,stroke-dasharray: 5 5; a11 -."p:statement/P186".-> c10 a11 --"p:qualifier/P366"--> c12 v6 --"p:P186"--> a11 bind23[/"' on the rocks'"/] bind23 --as--o v26 bind24[/"', served'"/] bind24 --as--o v27 end subgraph optional5["(optional)"] style optional5 fill:#bbf,stroke-dasharray: 5 5; bind25[/"concat(' with ',?garnishes)"/] v22 --o bind25 bind25 --as--o v27 bind26[/"', served'"/] bind26 --as--o v27 end bind27[/"concat(?ingredients,?served'',?onTheRocks'',?garnish'',?container'')"/] v21 --o bind27 v27 --o bind27 v26 --o bind27 v27 --o bind27 v24 --o bind27 bind27 --as--o v30 subgraph optional6["(optional)"] style optional6 fill:#bbf,stroke-dasharray: 5 5; v6 -."p:direct/P18".-> v30 end bind31[/"max(?count)"/] v20 --o bind31 bind31 --as--o v29 bind32[/"sample(?recipe)"/] v30 --o bind32 bind32 --as--o v30 bind33[/"sample(?image)"/] v30 --o bind33 bind33 --as--o v30