|
import os |
|
import string |
|
import math |
|
import random |
|
import xml.etree.ElementTree as et |
|
import jsonlines |
|
import uuid |
|
import pandas as pd |
|
|
|
|
|
random.seed(1) |
|
|
|
|
|
FILE_NUMBER_COL = 'file_number' |
|
REFERENCE_ANSWER_COL = 'reference_answer' |
|
|
|
|
|
QUESTION_COL = 'Frage' |
|
ANSWER_COL = 'Antwort' |
|
SCORE_COL = 'Score' |
|
ERROR_CLASS_COL = 'Fehlerklasse' |
|
FEEDBACK_COL = 'Feedback' |
|
|
|
|
|
CORRECT_LABEL = 'Correct' |
|
PARTIALLY_CORRECT_LABEL = 'Partially correct' |
|
INCORRECT_LABEL = 'Incorrect' |
|
|
|
def convert_xlsx_to_jsonl( |
|
path_to_dataset, |
|
path_to_reference_answers_file, |
|
dir, |
|
filename, |
|
train_split=None): |
|
""" |
|
Utility function used for conversion of .xlsx files from the dataset into JSON lines |
|
|
|
Params: |
|
path_to_dataset (string): path to the folder containing the dataset (in .xlsx format) |
|
path_to_reference_answers_file (string): path to the folder containing the reference answers (in .xlsx format) |
|
dir (string): name of the directory where the JSON lines file will be stored |
|
filename (string): name of the JSON lines file that will store the dataset |
|
train_split (float or None): if not None, defines which percentage of the dataset to use for the train and validation splits |
|
|
|
Returns: |
|
None: the file is saved JSON lines format in the specified location |
|
""" |
|
def return_verification_feedback(score): |
|
if math.isclose(score, 1.0): |
|
return CORRECT_LABEL |
|
elif math.isclose(score, 0.0): |
|
return INCORRECT_LABEL |
|
else: |
|
return PARTIALLY_CORRECT_LABEL |
|
|
|
data = [] |
|
|
|
|
|
reference_answers_df = pd.read_excel(path_to_reference_answers_file) |
|
|
|
|
|
reference_answers = { |
|
f'{row[FILE_NUMBER_COL]:02}': row[REFERENCE_ANSWER_COL].strip() |
|
for _, row in reference_answers_df.iterrows()} |
|
|
|
|
|
for f in os.listdir(path_to_dataset): |
|
if f.endswith('.xlsx'): |
|
|
|
file_df = pd.read_excel(os.path.join(path_to_dataset, f)) |
|
|
|
question = file_df[QUESTION_COL].iat[0].strip() |
|
|
|
ref_answer = reference_answers[f.split('.')[0]] |
|
|
|
|
|
for _, row in file_df.iterrows(): |
|
response = row[ANSWER_COL].strip() |
|
score = float(row[SCORE_COL]) |
|
feedback = str(row[FEEDBACK_COL]).strip() |
|
verification_feedback = return_verification_feedback(score) |
|
error_class = row[ERROR_CLASS_COL].strip() |
|
|
|
|
|
data.append({ |
|
'id': uuid.uuid4().hex, |
|
'question': question, |
|
'reference_answer': ref_answer, |
|
'provided_answer': response, |
|
'answer_feedback': feedback, |
|
'verification_feedback': verification_feedback, |
|
'error_class': error_class, |
|
'score': score |
|
}) |
|
|
|
if not os.path.exists(dir): |
|
print('Creating directory where JSON file will be stored\n') |
|
os.makedirs(dir) |
|
|
|
if train_split is None: |
|
with jsonlines.open(f'{os.path.join(dir, filename)}.jsonl', 'w') as writer: |
|
writer.write_all(data) |
|
else: |
|
|
|
random.shuffle(data) |
|
train_data = data[: int(train_split * (len(data) - 1))] |
|
val_data = data[int(train_split * (len(data) - 1)) :] |
|
|
|
|
|
with jsonlines.open(f'{os.path.join(dir, filename)}-train.jsonl', 'w') as writer: |
|
writer.write_all(train_data) |
|
|
|
|
|
with jsonlines.open(f'{os.path.join(dir, filename)}-validation.jsonl', 'w') as writer: |
|
writer.write_all(val_data) |
|
|
|
if __name__ == '__main__': |
|
|
|
convert_xlsx_to_jsonl( |
|
'data/training', 'data/reference_answers.xlsx', |
|
'data/json', 'saf-legal-domain-german', |
|
train_split=0.8) |
|
|
|
convert_xlsx_to_jsonl( |
|
'data/unseen_answers', 'data/reference_answers.xlsx', |
|
'data/json', 'saf-legal-domain-german-unseen-answers') |
|
|
|
convert_xlsx_to_jsonl( |
|
'data/unseen_questions', 'data/reference_answers.xlsx', |
|
'data/json', 'saf-legal-domain-german-unseen-questions') |