Created
April 21, 2017 18:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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