Skip to content

Commit 499ffa4

Browse files
authored
Merge pull request #631 from flairNLP/handle-multuple-ld-types
Handle multiple ld types
2 parents 001703e + 403be63 commit 499ffa4

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

src/fundus/parser/data.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from lxml.etree import XPath, tostring
2626
from typing_extensions import Self, TypeAlias, deprecated
2727

28-
from fundus.utils.serialization import replace_keys_in_nested_dict
28+
from fundus.utils.serialization import JSONVal, replace_keys_in_nested_dict
2929

3030
LDMappingValue: TypeAlias = Union[List[Dict[str, Any]], Dict[str, Any]]
3131

@@ -64,23 +64,25 @@ def __init__(self, lds: Iterable[Dict[str, Any]] = ()):
6464
def serialize(self) -> Dict[str, Any]:
6565
return {attribute: value for attribute, value in self.__dict__.items() if "__" not in attribute}
6666

67+
def _add(self, ld: Dict[str, JSONVal], ld_type: str) -> None:
68+
if value := self.__dict__.get(ld_type):
69+
if not isinstance(value, list):
70+
self.__dict__[ld_type] = [value]
71+
self.__dict__[ld_type].append(ld)
72+
else:
73+
self.__dict__[ld_type] = ld
74+
6775
def add_ld(self, ld: Dict[str, Any], name: Optional[str] = None) -> None:
68-
if ld_type := ld.get("@type", name):
69-
if isinstance(ld_type, list):
70-
if len(ld_type) == 1:
71-
ld_type = ld_type[0]
72-
else:
73-
raise TypeError(f"Unable tp parse ld_type '{ld_type}' of type {list} with length != 1")
74-
if value := self.__dict__.get(ld_type):
75-
if not isinstance(value, list):
76-
self.__dict__[ld_type] = [value]
77-
self.__dict__[ld_type].append(ld)
76+
if ld_type := (name or ld.get("@type")):
77+
if isinstance(ld_type, str):
78+
self._add(ld, ld_type)
79+
elif isinstance(ld_type, list):
80+
for t in ld_type:
81+
self._add(ld, t)
7882
else:
79-
self.__dict__[ld_type] = ld
83+
raise NotImplementedError(f"Unexpected LD type {type(ld_type)}")
8084
else:
81-
if not self.__dict__.get(self.__UNKNOWN_TYPE__):
82-
self.__dict__[self.__UNKNOWN_TYPE__] = []
83-
self.__dict__[self.__UNKNOWN_TYPE__].append(ld)
85+
self._add(ld, self.__UNKNOWN_TYPE__)
8486

8587
@deprecated("Use xpath_search() instead")
8688
def get_value_by_key_path(self, key_path: List[str], default: Any = None) -> Optional[Any]:

0 commit comments

Comments
 (0)