Skip to content

Instantly share code, notes, and snippets.

@bearloga
Created April 21, 2017 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bearloga/24cdba3490b5e0a3643dec5bcf2f3d3c to your computer and use it in GitHub Desktop.
Save bearloga/24cdba3490b5e0a3643dec5bcf2f3d3c to your computer and use it in GitHub Desktop.
Fetches cocktails and their ingredients from Wikidata using SPARQL and Wikidata Query Service
cocktails <- WikidataQueryServiceR::query_wikidata('
SELECT DISTINCT ?cocktailLabel ?ingredientLabel ?instanceOfLabel ?subclassLabel
WHERE
{
?cocktail wdt:P31/wdt:P279* wd:Q134768 .
?cocktail wdt:P186 ?ingredient .
OPTIONAL {
?ingredient wdt:P279 ?subclass .
}
OPTIONAL {
?ingredient wdt:P31 ?instanceOf .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}')
library(magrittr)
cocktails %<>%
dplyr::rename(
cocktail = cocktailLabel,
ingredient = ingredientLabel,
instance_of = instanceOfLabel,
subclass = subclassLabel
) %>%
dplyr::mutate(
cocktail = stringi::stri_trans_general(cocktail, "Latin-ASCII"),
ingredient = stringi::stri_trans_general(ingredient, "Latin-ASCII")
) %>%
dplyr::arrange(cocktail, ingredient)
cocktails[cocktails == ""] <- NA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment