Skip to content

Commit

Permalink
Map Aardvark.dct_spatial_sm to Locations@kind="Place Name" (#146)
Browse files Browse the repository at this point in the history
* Map Aardvark.dct_spatial_sm to Locations@kind="Place Name"

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 authored Mar 11, 2024
1 parent 599097f commit a019673
Show file tree
Hide file tree
Showing 2 changed files with 49 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
25 changes: 25 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,27 @@ 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 fields.get("subjects") is None:
return fields

spatial_subjects = [
subject
for subject in fields.get("subjects", []) # defaults to empty list
if subject.kind == "Dublin Core; Spatial" and subject.value is not None
]

for subject in spatial_subjects:
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 a019673

Please sign in to comment.