I query occupations in Wikidata OPTIONAL { ?item wdt:P106 ?occupation }
. No problem in grouping those by label ?occupation rdfs:label ?occupationLabel FILTER((LANG(?occupationLabel)) = "en") .
So far, so good. But now I want to further group certain values together, such as: "lawyer", "jurist" ... (e.g. here all "law" related occupations). E.g. judge, jurist, lawyer -> law | physicist, mathematician, computer scientist -> MINT ....
Best solution I have found to do it with IF
bind ( IF(CONTAINS(?_occupationLabel, "law") || CONTAINS(?_occupationLabel, "jurist"), "law!",
IF(CONTAINS(?_occupationLabel, "chemi") || CONTAINS(?_occupationLabel, "physicist"), "MINT!", ?_occupationLabel)
) as ?occupationLabelE)
Considered my use case I would end up with a deeply nested statement. Probably I could shorten it a bit by using RegExp, but with 10-15 categories not very elegant. Full query (shortening failed, had to add full link).
Anything cleaner/smarter?
Edit: Here I have found a trick way to avoid the nested statement. This IMHO is still some kind of hack, but much easier to write (as I can just add the various conditions).
BIND (
COALESCE(
IF(CONTAINS(?occupationLabel, "law"), "law!", 1/0),
IF(CONTAINS(?occupationLabel, "jurist"), "law!", 1/0),
IF(CONTAINS(?occupationLabel, "chemi"), "MINT!", 1/0),
?occupationLabel
) AS ?occupationLabelE