Skip to content

Commit

Permalink
Improve Label and MultiLabel __str__ and __repr__ (#2202)
Browse files Browse the repository at this point in the history
* Add proper annotation to MultiLabel and slightly improve __str__ for MultiLabel and Label

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ZanSara and github-actions[bot] committed Feb 17, 2022
1 parent 542d7b8 commit c93b365
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions haystack/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,11 +481,20 @@ def __repr__(self):
return str(self.to_dict())

def __str__(self):
return str(self.to_dict())
return f"<Label: {self.to_dict()}>"


@dataclass
class MultiLabel:
labels: List[Label]
query: str
answers: List[str]
no_answer: bool
document_ids: List[str]
document_contents: List[str]
gold_offsets_in_contexts: List[Dict]
gold_offsets_in_documents: List[Dict]

def __init__(self, labels: List[Label], drop_negative_labels=False, drop_no_answers=False):
"""
There are often multiple `Labels` associated with a single query. For example, there can be multiple annotated
Expand All @@ -499,7 +508,6 @@ def __init__(self, labels: List[Label], drop_negative_labels=False, drop_no_answ
:param drop_negative_labels: Whether to drop negative labels from that group (e.g. thumbs down feedback from UI)
:param drop_no_answers: Whether to drop labels that specify the answer is impossible
"""

# drop duplicate labels and remove negative labels if needed.
labels = list(set(labels))
if drop_negative_labels:
Expand Down Expand Up @@ -588,7 +596,7 @@ def __repr__(self):
return str(self.to_dict())

def __str__(self):
return str(self.to_dict())
return f"<MultiLabel: {self.to_dict()}>"


def _pydantic_dataclass_from_dict(dict: dict, pydantic_dataclass_type) -> Any:
Expand Down

0 comments on commit c93b365

Please sign in to comment.