|
import os |
|
import string |
|
import math |
|
import random |
|
import xml.etree.ElementTree as et |
|
import jsonlines |
|
import uuid |
|
|
|
|
|
random.seed(1) |
|
|
|
def convert_xml_to_jsonl(path_to_dataset, dir, filename, train_split=None): |
|
""" |
|
Utility function used for conversion of XML files from the dataset into JSON lines |
|
|
|
Params: |
|
path_to_dataset (string): path to the folder containing the dataset (in XML 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 in JSON lines format in the specified location |
|
""" |
|
data = [] |
|
|
|
|
|
for f in os.listdir(path_to_dataset): |
|
if f.endswith('.xml'): |
|
root = et.parse(os.path.join(path_to_dataset, f)).getroot() |
|
|
|
question = root.find('questionText').text.replace('\n', ' ') |
|
|
|
ref_answers = [x for x in root.find('referenceAnswers')] |
|
student_answers = [x for x in root.find('studentAnswers')] |
|
|
|
if len(ref_answers) == 1: |
|
|
|
ref_answer = ref_answers[0].text.strip() |
|
|
|
|
|
for answer in student_answers: |
|
response = answer.find('response').text.strip() |
|
score = float(answer.find('score').text) |
|
feedback = answer.find('response_feedback').text.strip() |
|
verification_feedback = answer.find('verification_feedback').text.strip() |
|
|
|
|
|
data.append({ |
|
'id': uuid.uuid4().hex, |
|
'question': question, |
|
'reference_answer': ref_answer, |
|
'provided_answer': response, |
|
'answer_feedback': feedback, |
|
'verification_feedback': verification_feedback, |
|
'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_xml_to_jsonl( |
|
'data/training/german', |
|
'data/json', |
|
'saf-micro-job-german', |
|
train_split=0.8) |
|
|
|
convert_xml_to_jsonl( |
|
'data/unseen_answers/german', |
|
'data/json', |
|
'saf-micro-job-german-unseen-answers') |
|
|
|
convert_xml_to_jsonl( |
|
'data/unseen_questions/german', |
|
'data/json', |
|
'saf-micro-job-german-unseen-questions') |