Skip to content

Commit

Permalink
Handle metaclasses in subclass query
Browse files Browse the repository at this point in the history
  • Loading branch information
dseomn committed Nov 14, 2023
1 parent b20b430 commit a672c4d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
12 changes: 10 additions & 2 deletions rock_paper_sand/wikidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,18 @@ def transitive_subclasses(
) -> Set[wikidata_value.ItemRef]:
"""Returns transitive subclasses of the given class."""
if class_ref not in self._transitive_subclasses:
subclass_of = wikidata_value.P_SUBCLASS_OF.id
subclass_predicate = "|".join(
(
f"wdt:{wikidata_value.P_SUBCLASS_OF.id}",
(
f"(wdt:{wikidata_value.P_INSTANCE_OF.id}/"
f"wdt:{wikidata_value.P_IS_METACLASS_FOR.id})"
),
)
)
results = self.sparql(
"SELECT REDUCED ?class WHERE { "
f"?class wdt:{subclass_of}* wd:{class_ref.id}. "
f"?class ({subclass_predicate})* wd:{class_ref.id}. "
"?class wikibase:sitelinks []. "
"}"
)
Expand Down
1 change: 1 addition & 0 deletions rock_paper_sand/wikidata_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ def human_readable_url_prefix(cls) -> str:
P_HAS_PARTS = _p("https://www.wikidata.org/wiki/Property:P527")
P_HAS_SPIN_OFF = _p("https://www.wikidata.org/wiki/Property:P2512")
P_INSTANCE_OF = _p("https://www.wikidata.org/wiki/Property:P31")
P_IS_METACLASS_FOR = _p("https://www.wikidata.org/wiki/Property:P8225")
P_MANIFESTATION_OF = _p("https://www.wikidata.org/wiki/Property:P1557")
P_MEDIA_FRANCHISE = _p("https://www.wikidata.org/wiki/Property:P8345")
P_MODIFIED_VERSION_OF = _p("https://www.wikidata.org/wiki/Property:P5059")
Expand Down

0 comments on commit a672c4d

Please sign in to comment.