-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add body
to unit tests
#338
Merged
Merged
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9ce8448
add `body` to unit tests
MaxDall b191ada
add test cases
MaxDall 033e759
Merge branch 'master' into add_additional_attributes_to_unit_tests
dobbersc d8f3297
Rename `transformation` -> `deserialization_functions` for extraction…
dobbersc 63a462d
Refactor `TextSequenceTree` (de)serialization from transformation dic…
dobbersc 493b734
sort parser test case attributes
MaxDall 5ead10b
Merge branch 'master' into add_additional_attributes_to_unit_tests
MaxDall 3a85059
Merge branch 'master' into add_additional_attributes_to_unit_tests
MaxDall 49d9a6c
update test files to cover attribute order
MaxDall 996d8b2
sort test case keys after insert and don't overwrite unless specified
MaxDall d885e01
add missing test cases
MaxDall decaa9b
Merge branch 'master' into add_additional_attributes_to_unit_tests
MaxDall f561af8
update test cases
MaxDall def1825
black
MaxDall d584d20
Merge branch 'master' into add_additional_attributes_to_unit_tests
MaxDall bab5b3d
update test cases and `WAZ`
MaxDall 9e6868e
update bi paragraph/subheadline selector
addie9800 607503d
reextract bi test data
addie9800 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
from dataclasses import dataclass, fields | ||
from typing import ( | ||
Any, | ||
Callable, | ||
ClassVar, | ||
Collection, | ||
Dict, | ||
Iterable, | ||
|
@@ -188,9 +190,33 @@ def __repr__(self) -> str: | |
def __str__(self) -> str: | ||
return "\n".join(self) | ||
|
||
def __eq__(self, other: object) -> bool: | ||
if not isinstance(other, TextSequence): | ||
return NotImplemented | ||
return self._data == other._data | ||
|
||
|
||
@dataclass | ||
class TextSequenceTree(ABC): | ||
"""Base class to traverse and build trees of TextSequence. | ||
|
||
The ClassVar __transformation__ is needed to serialize the tree. There must be an entry with type | ||
Tuple[Callable, Callable] for each field with the first callable beeing the serialization and | ||
the second the deserialization of that specific field. | ||
|
||
Examples: | ||
>>> import datetime | ||
>>> class Tree(TextSequenceTree): | ||
>>> head: TextSequence | ||
>>> tail: TextSequence | ||
>>> | ||
>>> __transformation__ = {"head": (list, TextSequence), "tail": (list, TextSequence)} | ||
|
||
|
||
""" | ||
|
||
__transformation__: ClassVar[Dict[str, Tuple[Callable[[Any], Any], Callable[[Any], Any]]]] | ||
|
||
def as_text_sequence(self) -> TextSequence: | ||
texts = [text for tl in self.df_traversal() for text in tl] | ||
return TextSequence(texts) | ||
|
@@ -215,7 +241,22 @@ def recursion(o: object): | |
for value in self: | ||
yield from recursion(value) | ||
|
||
def __iter__(self): | ||
def serialize(self) -> Dict[str, Any]: | ||
serialized = {} | ||
for field in fields(self): | ||
name = field.name | ||
serialized[name] = self.__transformation__[name][0](getattr(self, name)) | ||
return serialized | ||
|
||
@classmethod | ||
def deserialize(cls, obj: Dict[str, Any]) -> "TextSequenceTree": | ||
kwargs = {} | ||
for name, value in obj.items(): | ||
kwargs[name] = cls.__transformation__[name][1](value) | ||
# noinspection PyArgumentList | ||
return cls(**kwargs) | ||
|
||
def __iter__(self) -> Iterator[Any]: | ||
field_values = [getattr(self, f.name) for f in fields(self)] | ||
yield from field_values | ||
|
||
|
@@ -231,8 +272,18 @@ class ArticleSection(TextSequenceTree): | |
headline: TextSequence | ||
paragraphs: TextSequence | ||
|
||
__transformation__ = {"headline": (list, TextSequence), "paragraphs": (list, TextSequence)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also make the TextSequence it self serializable as well and call the serialize function here. Would this also be a desired feature? |
||
|
||
|
||
@dataclass | ||
class ArticleBody(TextSequenceTree): | ||
summary: TextSequence | ||
sections: List[ArticleSection] | ||
|
||
__transformation__ = { | ||
"summary": (list, TextSequence), | ||
"sections": ( | ||
lambda sections: [section.serialize() for section in sections], | ||
lambda sections: [ArticleSection.deserialize(section) for section in sections], | ||
), | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a quick question: Why is the headline a TextSequence again? It would be intuitive to me if it was just a single string without any sequential structure like a section with multiple paragraphs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Headline was originally intended to be a TextSequence to catch any errors resulting from consequent headlines.