User:TweetsFactsAndQueries/Queries/major and minor tonalities by genre
Jump to navigation
Jump to search
Try it!
Originally posted on Twitter.
# which genres are more likely to be in major key, which in minor key, which are about equally distributed?
# note: some tonalities are neither major nor minor, e. g. Q211745 atonality
SELECT ?genre ?genreLabel ?total ?major ?minor (?major / ?total AS ?majorRatio)
WITH {
SELECT DISTINCT ?tonality WHERE {
[] wdt:P826 ?tonality.
}
} AS %tonalities
WITH {
SELECT ?tonality ?isMajor ?isMinor WHERE {
INCLUDE %tonalities.
?tonality rdfs:label ?tonalityLabel.
FILTER(LANG(?tonalityLabel) = "en")
# TODO https://www.wikidata.org/wiki/Wikidata_talk:WikiProject_Music#Items_for_major_and_minor_keys
BIND(STRENDS(?tonalityLabel, " major") AS ?isMajor)
BIND(STRENDS(?tonalityLabel, " minor") AS ?isMinor)
}
} AS %tonalitiesWithMode
WITH {
SELECT ?genre ?tonality (COUNT(DISTINCT ?composition) AS ?count) WHERE {
?composition wdt:P31/wdt:P279* wd:Q207628;
wdt:P136/wdt:P279* ?genre;
wdt:P826 ?tonality.
?genre wdt:P31 wd:Q188451.
}
GROUP BY ?genre ?tonality
} AS %genresAndTonalities WITH {
SELECT ?genre (SUM(?count) AS ?total) (SUM(IF(?isMajor, ?count, 0)) AS ?major) (SUM(IF(?isMinor, ?count, 0)) AS ?minor) WHERE {
INCLUDE %genresAndTonalities.
INCLUDE %tonalitiesWithMode.
}
GROUP BY ?genre
} AS %results WHERE {
INCLUDE %results.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY DESC(?total) DESC(?major)
The detection of major and minor keys can hopefully be improved in the future, see Wikidata talk:WikiProject Music#Items for major and minor keys.