2020-10-30 18:06:02 +01:00
|
|
|
import pytest
|
2022-02-04 12:37:56 +01:00
|
|
|
import pandas as pd
|
2022-01-04 18:34:16 +01:00
|
|
|
from pathlib import Path
|
2020-10-30 18:06:02 +01:00
|
|
|
|
2021-10-29 13:52:28 +05:30
|
|
|
from haystack.utils.preprocessing import convert_files_to_dicts, tika_convert_files_to_dicts
|
|
|
|
from haystack.utils.cleaning import clean_wiki_text
|
2022-01-04 18:34:16 +01:00
|
|
|
from haystack.utils.augment_squad import augment_squad
|
|
|
|
from haystack.utils.squad_data import SquadData
|
2020-10-01 14:47:45 +02:00
|
|
|
|
2022-01-26 18:12:55 +01:00
|
|
|
from conftest import SAMPLES_PATH
|
|
|
|
|
|
|
|
|
2021-10-29 13:52:28 +05:30
|
|
|
def test_convert_files_to_dicts():
|
2022-02-03 13:43:18 +01:00
|
|
|
documents = convert_files_to_dicts(
|
|
|
|
dir_path=(SAMPLES_PATH).absolute(), clean_func=clean_wiki_text, split_paragraphs=True
|
|
|
|
)
|
2020-10-01 14:47:45 +02:00
|
|
|
assert documents and len(documents) > 0
|
|
|
|
|
2022-02-03 13:43:18 +01:00
|
|
|
|
2020-10-30 18:06:02 +01:00
|
|
|
@pytest.mark.tika
|
2021-10-29 13:52:28 +05:30
|
|
|
def test_tika_convert_files_to_dicts():
|
2022-01-26 18:12:55 +01:00
|
|
|
documents = tika_convert_files_to_dicts(dir_path=SAMPLES_PATH, clean_func=clean_wiki_text, split_paragraphs=True)
|
2020-10-01 14:47:45 +02:00
|
|
|
assert documents and len(documents) > 0
|
|
|
|
|
2022-02-03 13:43:18 +01:00
|
|
|
|
2022-01-04 18:34:16 +01:00
|
|
|
def test_squad_augmentation():
|
2022-02-03 13:43:18 +01:00
|
|
|
input_ = SAMPLES_PATH / "squad" / "tiny.json"
|
|
|
|
output = SAMPLES_PATH / "squad" / "tiny_augmented.json"
|
|
|
|
glove_path = SAMPLES_PATH / "glove" / "tiny.txt" # dummy glove file, will not even be use when augmenting tiny.json
|
2022-01-04 18:34:16 +01:00
|
|
|
multiplication_factor = 5
|
2022-02-03 13:43:18 +01:00
|
|
|
augment_squad(
|
|
|
|
model="distilbert-base-uncased",
|
|
|
|
tokenizer="distilbert-base-uncased",
|
|
|
|
squad_path=input_,
|
|
|
|
output_path=output,
|
|
|
|
glove_path=glove_path,
|
|
|
|
multiplication_factor=multiplication_factor,
|
|
|
|
)
|
2022-01-04 18:34:16 +01:00
|
|
|
original_squad = SquadData.from_file(input_)
|
|
|
|
augmented_squad = SquadData.from_file(output)
|
2022-01-20 12:18:32 +01:00
|
|
|
assert original_squad.count(unit="paragraph") == augmented_squad.count(unit="paragraph") * multiplication_factor
|
2022-02-04 12:37:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_squad_to_df():
|
|
|
|
df = pd.DataFrame(
|
|
|
|
[["title", "context", "question", "id", "answer", 1, False]],
|
|
|
|
columns=["title", "context", "question", "id", "answer_text", "answer_start", "is_impossible"],
|
|
|
|
)
|
|
|
|
|
|
|
|
expected_result = [
|
|
|
|
{
|
|
|
|
"title": "title",
|
|
|
|
"paragraphs": [
|
|
|
|
{
|
|
|
|
"context": "context",
|
|
|
|
"qas": [
|
|
|
|
{
|
|
|
|
"question": "question",
|
|
|
|
"id": "id",
|
|
|
|
"answers": [{"text": "answer", "answer_start": 1}],
|
|
|
|
"is_impossible": False,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
result = SquadData.df_to_data(df)
|
|
|
|
|
|
|
|
assert result == expected_result
|