Skip to content

Commit

Permalink
Don't ignore children when the parent-child border isn't crossed
Browse files Browse the repository at this point in the history
If the parent is being processed, this shouldn't change anything because
the child wouldn't be processed either way, whether because it's
integral or because the border isn't crossed.

If the child is being processed, the old behavior was that the parent
wasn't processed because of the border, but the child was still ignored
for being integral. With this change, the child is no longer ignored in
that case.
  • Loading branch information
dseomn committed Jun 29, 2024
1 parent 4941e85 commit 942db48
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 27 deletions.
56 changes: 29 additions & 27 deletions rock_paper_sand/wikidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,33 @@ def _is_ignored(
or item_classes & ignored_classes_from_request
)

def _should_cross_parent_child_border(
self, parent: wikidata_value.ItemRef, child: wikidata_value.ItemRef
) -> bool:
"""Returns whether to cross the parent-child border for related media.
Some parent-child pairs cross the border between generally unrelated
sets of media. E.g., somebody interested in watching a series of
anthology films might want to know all the films in the series. But
there could be many items related to stories in the individual
anthologies, and those items don't have much of a connection to the
series of anthologies that the user is interested in. Or from the other
side, if the user is interested in a book that was adapted into a part
of an anthology movie, they might be interested in that part of the
anthology movie, but not necessarily in the entire anthology series.
Args:
parent: Parent.
child: Child.
"""
del child # Unused.
parent_classes = self._api.entity_classes(parent)
parent_forms = self._api.forms_of_creative_work(parent)
return (
not parent_classes & self._anthology_classes
and not parent_forms & self._anthology_classes
)

def _integral_child_classes(
self,
) -> Iterable[
Expand Down Expand Up @@ -727,6 +754,8 @@ def _is_integral_child(
parent: Parent.
child: Child.
"""
if not self._should_cross_parent_child_border(parent, child):
return False
parent_classes = self._api.entity_classes(parent)
parent_classes_and_forms = (
parent_classes | self._api.forms_of_creative_work(parent)
Expand Down Expand Up @@ -794,33 +823,6 @@ def _integral_children(
if self._is_integral_child(item_ref, child)
)

def _should_cross_parent_child_border(
self, parent: wikidata_value.ItemRef, child: wikidata_value.ItemRef
) -> bool:
"""Returns whether to cross the parent-child border for related media.
Some parent-child pairs cross the border between generally unrelated
sets of media. E.g., somebody interested in watching a series of
anthology films might want to know all the films in the series. But
there could be many items related to stories in the individual
anthologies, and those items don't have much of a connection to the
series of anthologies that the user is interested in. Or from the other
side, if the user is interested in a book that was adapted into a part
of an anthology movie, they might be interested in that part of the
anthology movie, but not necessarily in the entire anthology series.
Args:
parent: Parent.
child: Child.
"""
del child # Unused.
parent_classes = self._api.entity_classes(parent)
parent_forms = self._api.forms_of_creative_work(parent)
return (
not parent_classes & self._anthology_classes
and not parent_forms & self._anthology_classes
)

def _related_item_priority(
self, item_ref: wikidata_value.ItemRef
) -> _RelatedMediaPriority:
Expand Down
42 changes: 42 additions & 0 deletions rock_paper_sand/wikidata_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,48 @@ def setUp(self) -> None:
},
),
),
dict(
testcase_name="related_media_includes_integral_child_of_collection",
filter_config={"relatedMedia": {}},
item={"name": "foo", "wikidata": "Q1"},
api_entities={
"Q21": {"labels": {}, "descriptions": {}},
},
api_entity_classes={
"Q1": set(),
"Q2": {wikidata_value.Q_FILM, wikidata_value.Q_ANTHOLOGY_FILM},
"Q21": {wikidata_value.Q_FILM},
},
api_forms_of_creative_work={
"Q1": set(),
"Q2": set(),
"Q21": set(),
},
api_related_media={
"Q1": wikidata.RelatedMedia(
parents=set(),
siblings={wikidata_value.ItemRef("Q21")},
children=set(),
loose=set(),
),
"Q21": wikidata.RelatedMedia(
parents={wikidata_value.ItemRef("Q2")},
siblings=set(),
children=set(),
loose=set(),
),
},
expected_result=media_filter.FilterResult(
True,
extra={
media_filter.ResultExtra(
human_readable=(
"related item: <https://www.wikidata.org/wiki/Q21>"
),
),
},
),
),
dict(
testcase_name="related_media_includes_label_and_description",
filter_config={"languages": ["en"], "relatedMedia": {}},
Expand Down

0 comments on commit 942db48

Please sign in to comment.