Skip to content

Commit

Permalink
Map Aardvark.dct_spatial_sm to Locations@kind="Place Name"
Browse files Browse the repository at this point in the history
Why these changes are being introduced:
* Support place names for location-based searching

How this addresses that need:
* Add global Transformer method to create Location objects from spatial subjects

Side effects of this change:
* None

Relevant ticket(s):
* https://mitlibraries.atlassian.net/browse/GDT-217
  • Loading branch information
jonavellecuerdo committed Mar 11, 2024
1 parent 599097f commit 95349bd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/sources/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ def test_create_dates_and_locations_from_publishers_success():
}


def test_create_locations_from_spatial_subjects():
fields = {
"subjects": [
timdex.Subject(
value=["Some city, Some country"], kind="Dublin Core; Spatial"
),
timdex.Subject(value=["City 1", "City 2"], kind="Dublin Core; Spatial"),
]
}
assert Transformer.create_locations_from_spatial_subjects(fields) == {
"subjects": [
timdex.Subject(
value=["Some city, Some country"], kind="Dublin Core; Spatial"
),
timdex.Subject(value=["City 1", "City 2"], kind="Dublin Core; Spatial"),
],
"locations": [
timdex.Location(value="Some city, Some country", kind="Place Name"),
timdex.Location(value="City 1", kind="Place Name"),
timdex.Location(value="City 2", kind="Place Name"),
],
}


def test_xmltransformer_initializes_with_expected_attributes(oai_pmh_records):
transformer = XMLTransformer("cool-repo", oai_pmh_records)
assert transformer.source == "cool-repo"
Expand Down
31 changes: 31 additions & 0 deletions transmogrifier/sources/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def _transform(
}

fields = self.create_dates_and_locations_from_publishers(fields)
fields = self.create_locations_from_spatial_subjects(fields)

# If citation field was not present, generate citation from other fields
if fields.get("citation") is None:
Expand Down Expand Up @@ -392,3 +393,33 @@ def create_dates_and_locations_from_publishers(fields: dict) -> dict:
if publisher_location not in fields.setdefault("locations", []):
fields["locations"].append(publisher_location)
return fields

@final
@staticmethod
def create_locations_from_spatial_subjects(fields: dict) -> dict:
"""Add Location objects for spatial subjects.
Args:
fields: A dict of fields representing a TIMDEX record.
"""
if not (subjects := fields.get("subjects")):
return fields

if not (
spatial_subjects := list(
filter(
lambda subject: subject.kind == "Dublin Core; Spatial", subjects # type: ignore[arg-type]
)
)
):
return fields

for subject in spatial_subjects:
if not subject.value:
continue

for place_name in subject.value:
subject_location = timdex.Location(value=place_name, kind="Place Name")
if subject_location not in fields.setdefault("locations", []):
fields["locations"].append(subject_location)
return fields

0 comments on commit 95349bd

Please sign in to comment.