Skip to content
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

fix: strip whitespaces safely from FARMReader's answers #3526

Merged
merged 9 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions haystack/modeling/model/predictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,18 @@ def _span_to_string(self, token_offsets: List[int], clear_text: str) -> Tuple[st
# final_text can be an empty string if start_t points to the very final token of the passage
# final_text can be a whitespace if there is a whitespace token in the text, e.g.,
# if the original text contained multiple consecutive whitespaces
if len(final_text.strip()) > 0:
final_text = final_text.strip()
else:
cleaned_final_text = final_text.strip()
if not cleaned_final_text:
return "", 0, 0
end_ch = int(start_ch + len(final_text))

return final_text, start_ch, end_ch
# Adjust the offsets in case of whitespace at the beginning of the answer
right_offset = len(final_text.rstrip()) != len(final_text)
ZanSara marked this conversation as resolved.
Show resolved Hide resolved
if right_offset:
start_ch = start_ch + right_offset

end_ch = int(start_ch + len(cleaned_final_text))
ZanSara marked this conversation as resolved.
Show resolved Hide resolved

return cleaned_final_text, start_ch, end_ch

def to_doc_level(self, start: int, end: int):
"""
Expand Down
34 changes: 19 additions & 15 deletions test/modeling/test_question_answering.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@
from haystack.modeling.data_handler.inputs import QAInput, Question


DOC_TEXT = """Twilight Princess was released to universal critical acclaim and commercial success. \
It received perfect scores from major publications such as 1UP.com, Computer and Video Games, \
Electronic Gaming Monthly, Game Informer, GamesRadar, and GameSpy. On the review aggregators \
GameRankings and Metacritic, Twilight Princess has average scores of 95% and 95 for the Wii \
version and scores of 95% and 96 for the GameCube version. GameTrailers in their review called \
it one of the greatest games ever created."""


@pytest.fixture()
def span_inference_result(bert_base_squad2, caplog=None):
if caplog:
caplog.set_level(logging.CRITICAL)
obj_input = [
QAInput(
doc_text="Twilight Princess was released to universal critical acclaim and commercial success. It received perfect scores from major publications such as 1UP.com, Computer and Video Games, Electronic Gaming Monthly, Game Informer, GamesRadar, and GameSpy. On the review aggregators GameRankings and Metacritic, Twilight Princess has average scores of 95% and 95 for the Wii version and scores of 95% and 96 for the GameCube version. GameTrailers in their review called it one of the greatest games ever created.",
questions=Question("Who counted the game among the best ever made?", uid="best_id_ever"),
doc_text=DOC_TEXT, questions=Question("Who counted the game among the best ever made?", uid="best_id_ever")
)
]
result = bert_base_squad2.inference_from_objects(obj_input, return_json=False)[0]
Expand All @@ -27,7 +34,13 @@ def no_answer_inference_result(bert_base_squad2, caplog=None):
caplog.set_level(logging.CRITICAL)
obj_input = [
QAInput(
doc_text='The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain "Amazonas" in their names. The Amazon represents over half of the planet\'s remaining rainforests, and comprises the largest and most biodiverse tract of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species.',
doc_text="""\
The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by
Peru with 13%, Colombia with 10%, and with minor amounts in Venezuela, Ecuador, Bolivia, Guyana,
Suriname and French Guiana. States or departments in four nations contain "Amazonas" in their names.
The Amazon represents over half of the planet\'s remaining rainforests, and comprises the largest
and most biodiverse tract of tropical rainforest in the world, with an estimated 390 billion individual
trees divided into 16,000 species.""",
questions=Question(
"The Amazon represents less than half of the planets remaining what?", uid="best_id_ever"
),
Expand All @@ -38,17 +51,9 @@ def no_answer_inference_result(bert_base_squad2, caplog=None):


def test_inference_different_inputs(bert_base_squad2):
qa_format_1 = [
{
"questions": ["Who counted the game among the best ever made?"],
"text": "Twilight Princess was released to universal critical acclaim and commercial success. It received perfect scores from major publications such as 1UP.com, Computer and Video Games, Electronic Gaming Monthly, Game Informer, GamesRadar, and GameSpy. On the review aggregators GameRankings and Metacritic, Twilight Princess has average scores of 95% and 95 for the Wii version and scores of 95% and 96 for the GameCube version. GameTrailers in their review called it one of the greatest games ever created.",
}
]
qa_format_1 = [{"questions": ["Who counted the game among the best ever made?"], "text": DOC_TEXT}]
q = Question(text="Who counted the game among the best ever made?")
qa_format_2 = QAInput(
questions=[q],
doc_text="Twilight Princess was released to universal critical acclaim and commercial success. It received perfect scores from major publications such as 1UP.com, Computer and Video Games, Electronic Gaming Monthly, Game Informer, GamesRadar, and GameSpy. On the review aggregators GameRankings and Metacritic, Twilight Princess has average scores of 95% and 95 for the Wii version and scores of 95% and 96 for the GameCube version. GameTrailers in their review called it one of the greatest games ever created.",
)
qa_format_2 = QAInput(questions=[q], doc_text=DOC_TEXT)

result1 = bert_base_squad2.inference_from_dicts(dicts=qa_format_1)
result2 = bert_base_squad2.inference_from_objects(objects=[qa_format_2])
Expand All @@ -60,8 +65,7 @@ def test_span_inference_result_ranking_by_confidence(bert_base_squad2, caplog=No
caplog.set_level(logging.CRITICAL)
obj_input = [
QAInput(
doc_text="Twilight Princess was released to universal critical acclaim and commercial success. It received perfect scores from major publications such as 1UP.com, Computer and Video Games, Electronic Gaming Monthly, Game Informer, GamesRadar, and GameSpy. On the review aggregators GameRankings and Metacritic, Twilight Princess has average scores of 95% and 95 for the Wii version and scores of 95% and 96 for the GameCube version. GameTrailers in their review called it one of the greatest games ever created.",
questions=Question("Who counted the game among the best ever made?", uid="best_id_ever"),
doc_text=DOC_TEXT, questions=Question("Who counted the game among the best ever made?", uid="best_id_ever")
)
]

Expand Down