Bases: dict
Custom dictionary that overrides the setitem method
depending on the reduce_mode
Source code in edsnlp/pipelines/core/contextual_matcher/models.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 | class AssignDict(dict):
"""
Custom dictionary that overrides the __setitem__ method
depending on the reduce_mode
"""
def __init__(self, reduce_mode: dict):
super().__init__()
self.reduce_mode = reduce_mode
self._setitem_ = self.__setitem_options__()
def __missing__(self, key):
return (
{
"span": [],
"value_span": [],
"value_text": [],
}
if self.reduce_mode[key] is None
else {}
)
def __setitem__(self, key, value):
self._setitem_[self.reduce_mode[key]](key, value)
def __setitem_options__(self):
def keep_list(key, value):
old_values = self.__getitem__(key)
value["span"] = old_values["span"] + [value["span"]]
value["value_span"] = old_values["value_span"] + [value["value_span"]]
value["value_text"] = old_values["value_text"] + [value["value_text"]]
dict.__setitem__(self, key, value)
def keep_first(key, value):
old_values = self.__getitem__(key)
if (
old_values.get("span") is None
or value["span"].start <= old_values["span"].start
):
dict.__setitem__(self, key, value)
def keep_last(key, value):
old_values = self.__getitem__(key)
if (
old_values.get("span") is None
or value["span"].start >= old_values["span"].start
):
dict.__setitem__(self, key, value)
return {
None: keep_list,
"keep_first": keep_first,
"keep_last": keep_last,
}
|