-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hybrid traiter/GPT reconciler for event date
- Loading branch information
1 parent
35d62b8
commit 67616d2
Showing
12 changed files
with
1,347 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import Callable | ||
|
||
|
||
class Template: | ||
def __init__(self): | ||
self._actions = [] | ||
|
||
@property | ||
def reconcile(self) -> list[Callable]: | ||
return self._actions | ||
|
||
@reconcile.setter | ||
def reconcile(self, actions: list[Callable]): | ||
self._actions += actions | ||
|
||
|
||
TEMPLATE = Template() | ||
|
||
|
||
class Base: | ||
def __init__(self, *args): | ||
TEMPLATE.reconcile = args | ||
|
||
@staticmethod | ||
def search(other, keys: list[str], default=None): | ||
for key in keys: | ||
if other.get(key): | ||
return other[key] | ||
return default | ||
|
||
@staticmethod | ||
def case(*args) -> list[str]: | ||
keys = " ".join(args).split() | ||
keys += [k.lower() for k in keys] | ||
return sorted(set(keys)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from typing import Any | ||
|
||
from .base import Base | ||
|
||
|
||
class Date(Base): | ||
date = "dwc:eventDate" | ||
verbatim = "dwc:verbatimEventDate" | ||
match_verb = Base.case(verbatim) | ||
match = Base.case( | ||
date, "dwc:collectionDate dwc:earliestDateCollected dwc:latestDateCollected" | ||
) | ||
|
||
def __init__(self): | ||
super().__init__(self.event_date) | ||
|
||
def event_date( | ||
self, traiter: dict[str, Any], other: dict[str, Any] | ||
) -> dict[str, str]: | ||
t_val = traiter.get(self.date) | ||
o_val = self.search(other, self.match) | ||
|
||
if t_val and not o_val: | ||
raise ValueError(f"MISSING in OpenAI output {self.date} = {t_val}") | ||
|
||
if not o_val: | ||
return {} | ||
|
||
if not t_val or t_val == o_val or o_val in t_val: | ||
reconciled = {self.date: o_val} | ||
if verb := self.search(other, self.match_verb, traiter.get(self.verbatim)): | ||
reconciled[self.verbatim] = verb | ||
return reconciled | ||
|
||
if o_val == traiter.get(self.verbatim): | ||
return { | ||
self.date: traiter[self.date], | ||
self.verbatim: traiter[self.verbatim], | ||
} | ||
|
||
if t_val != o_val: | ||
raise ValueError(f"MISMATCH {self.date}: {o_val} != {t_val}") | ||
|
||
raise ValueError(f"UNKNOWN error in {self.date}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from . import base | ||
|
||
|
||
class Elevation(base.Base): | ||
keys = [ | ||
"dwc:decimalElevation", | ||
"dwc:elevation", | ||
"dwc:elevationAccuracy", | ||
"dwc:elevationInMeters", | ||
"dwc:maxElevationInMeters", | ||
"dwc:maximumElevationInFeet", | ||
"dwc:maximumElevationInFeet", | ||
"dwc:maximumElevationInMeters", | ||
"dwc:minElevationInMeters", | ||
"dwc:minimumElevationInMeters", | ||
"dwc:verbatimElevation", | ||
] | ||
|
||
def __init__(self): | ||
super().__init__(self.elevation) | ||
|
||
def elevation(self): | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.