-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #614 from flairNLP/implement-xpath-search-for-lds
Implement XPath queries for LDs
- Loading branch information
Showing
4 changed files
with
139 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,41 @@ | ||
import json | ||
from typing import Dict, Sequence, Union | ||
from typing import Any, Callable, Dict, Sequence, TypeVar, Union | ||
|
||
from typing_extensions import TypeAlias | ||
|
||
JSONVal: TypeAlias = Union[None, bool, str, float, int, Sequence["JSONVal"], Dict[str, "JSONVal"]] | ||
|
||
_T = TypeVar("_T") | ||
|
||
|
||
def is_jsonable(x): | ||
try: | ||
json.dumps(x) | ||
return True | ||
except (TypeError, OverflowError): | ||
return False | ||
|
||
|
||
def replace_keys_in_nested_dict(data: Dict[str, _T], transformation: Callable[[str], str]) -> Dict[str, _T]: | ||
"""Recursively replace all keys in a nested dictionary with <transformation>. | ||
Args: | ||
data: The dictionary to transform | ||
transformation: The transformation to use | ||
Returns: | ||
The transformed dictionary | ||
""" | ||
|
||
def process(element) -> Any: | ||
if isinstance(element, dict): | ||
# Apply transformation to keys and recurse into values | ||
return {transformation(k): process(v) for k, v in element.items()} | ||
elif isinstance(element, list): | ||
# Recursively apply to elements in a list | ||
return [process(i) for i in element] | ||
else: | ||
# Base case: return the value as is if it's not a dict or list | ||
return element | ||
|
||
return {transformation(k): process(v) for k, v in data.items()} |
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,24 @@ | ||
from typing import Any, Dict, List | ||
|
||
from lxml.etree import XPath | ||
|
||
from fundus.parser.data import LinkedDataMapping | ||
|
||
lds: List[Dict[str, Any]] = [ | ||
{"@type": "Example1", "value": 1, "data": {"inner": "value", "nested": {"dict": True}}}, | ||
{"@type": "Example2", "value": 2, "_:*@": "Howdy"}, | ||
{"this": "should", "be": {"of": "type", "__UNKNOWN_TYPE__": True, "value": 3}}, | ||
] | ||
|
||
|
||
class TestLinkedDataMapping: | ||
def test_constructor(self): | ||
LinkedDataMapping(lds) | ||
|
||
def test_xpath_search(self): | ||
ld = LinkedDataMapping(lds) | ||
assert ld.xpath_search(XPath("//value")) == ["1", "2", "3"] | ||
assert ld.xpath_search(XPath("//UNKNOWN_TYPE//value")) == ["3"] | ||
assert ld.xpath_search(XPath("//_U003AU002AU0040")) == ["Howdy"] | ||
assert ld.xpath_search(XPath("//dict")) == ["True"] | ||
assert ld.xpath_search(XPath("//Example2")) == [{"@type": "Example2", "value": "2", "_:*@": "Howdy"}] |