-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
120 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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
unit tests: | ||
* serialization and deserialization | ||
see copyright/license https://huggingface.co/spaces/DerwenAI/textgraphs/blob/main/README.md | ||
""" | ||
|
||
from os.path import abspath, dirname | ||
import json | ||
import pathlib | ||
import sys | ||
|
||
import deepdiff # pylint: disable=E0401 | ||
|
||
sys.path.insert(0, str(pathlib.Path(dirname(dirname(abspath(__file__)))))) | ||
import textgraphs # pylint: disable=C0413 | ||
|
||
|
||
def test_load_minimal ( | ||
) -> None: | ||
""" | ||
Construct a _lemma graph_ from a minimal example, then compare | ||
serialized and deserialized data to ensure no fields get corrupted | ||
in the conversions. | ||
""" | ||
text: str = """ | ||
See Spot run. | ||
""" | ||
|
||
tg: textgraphs.TextGraphs = textgraphs.TextGraphs() # pylint: disable=C0103 | ||
pipe: textgraphs.Pipeline = tg.create_pipeline(text.strip()) | ||
|
||
# serialize into node-link format | ||
tg.collect_graph_elements(pipe) | ||
tg.construct_lemma_graph() | ||
tg.calc_phrase_ranks() | ||
|
||
json_str: str = tg.dump_lemma_graph() | ||
exp_graph = json.loads(json_str) | ||
|
||
# deserialize from node-link format | ||
tg = textgraphs.TextGraphs() # pylint: disable=C0103 | ||
tg.load_lemma_graph(json_str) | ||
tg.construct_lemma_graph() | ||
|
||
obs_graph: dict = json.loads(tg.dump_lemma_graph()) | ||
|
||
# compare | ||
diff: deepdiff.diff.DeepDiff = deepdiff.DeepDiff(exp_graph, obs_graph) | ||
|
||
if len(diff) > 0: | ||
print(json.dumps(json.loads(diff.to_json()), indent = 2)) | ||
|
||
assert len(diff) == 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
test_load_minimal() |
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