JohnnyBoy00
commited on
Commit
•
50c21de
1
Parent(s):
77a3a56
Upload conversion.py
Browse files- conversion.py +130 -0
conversion.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import string
|
3 |
+
import math
|
4 |
+
import random
|
5 |
+
import xml.etree.ElementTree as et
|
6 |
+
import jsonlines
|
7 |
+
import uuid
|
8 |
+
import pandas as pd
|
9 |
+
|
10 |
+
# set random seed for shuffling
|
11 |
+
random.seed(1)
|
12 |
+
|
13 |
+
# column names of the reference answers file
|
14 |
+
FILE_NUMBER_COL = 'file_number'
|
15 |
+
REFERENCE_ANSWER_COL = 'reference_answer'
|
16 |
+
|
17 |
+
# column names of the files with the data
|
18 |
+
QUESTION_COL = 'Frage'
|
19 |
+
ANSWER_COL = 'Antwort'
|
20 |
+
SCORE_COL = 'Score'
|
21 |
+
ERROR_CLASS_COL = 'Fehlerklasse'
|
22 |
+
FEEDBACK_COL = 'Feedback'
|
23 |
+
|
24 |
+
# labels for verification_feedback
|
25 |
+
CORRECT_LABEL = 'Correct'
|
26 |
+
PARTIALLY_CORRECT_LABEL = 'Partially correct'
|
27 |
+
INCORRECT_LABEL = 'Incorrect'
|
28 |
+
|
29 |
+
def convert_xlsx_to_jsonl(
|
30 |
+
path_to_dataset,
|
31 |
+
path_to_reference_answers_file,
|
32 |
+
dir,
|
33 |
+
filename,
|
34 |
+
train_split=None):
|
35 |
+
"""
|
36 |
+
Utility function used for conversion of .xlsx files from the dataset into JSON lines
|
37 |
+
|
38 |
+
Params:
|
39 |
+
path_to_dataset (string): path to the folder containing the dataset (in .xlsx format)
|
40 |
+
path_to_reference_answers_file (string): path to the folder containing the reference answers (in .xlsx format)
|
41 |
+
dir (string): name of the directory where the JSON lines file will be stored
|
42 |
+
filename (string): name of the JSON lines file that will store the dataset
|
43 |
+
train_split (float or None): if not None, defines which percentage of the dataset to use for the train and validation splits
|
44 |
+
|
45 |
+
Returns:
|
46 |
+
None: the file is saved JSON lines format in the specified location
|
47 |
+
"""
|
48 |
+
def return_verification_feedback(score):
|
49 |
+
if math.isclose(score, 1.0):
|
50 |
+
return CORRECT_LABEL
|
51 |
+
elif math.isclose(score, 0.0):
|
52 |
+
return INCORRECT_LABEL
|
53 |
+
else:
|
54 |
+
return PARTIALLY_CORRECT_LABEL
|
55 |
+
|
56 |
+
data = []
|
57 |
+
|
58 |
+
# get reference answers from file
|
59 |
+
reference_answers_df = pd.read_excel(path_to_reference_answers_file)
|
60 |
+
# the keys of the dictionary are the number of the files padded with zeroes
|
61 |
+
# so that it has two digits, and the values are the reference answers themselves
|
62 |
+
reference_answers = {
|
63 |
+
f'{row[FILE_NUMBER_COL]:02}': row[REFERENCE_ANSWER_COL].strip()
|
64 |
+
for _, row in reference_answers_df.iterrows()}
|
65 |
+
|
66 |
+
# loop through all files in directory
|
67 |
+
for f in os.listdir(path_to_dataset):
|
68 |
+
if f.endswith('.xlsx'):
|
69 |
+
# read file
|
70 |
+
file_df = pd.read_excel(os.path.join(path_to_dataset, f))
|
71 |
+
# get question
|
72 |
+
question = file_df[QUESTION_COL].iat[0].strip()
|
73 |
+
# get reference answer based on file name
|
74 |
+
ref_answer = reference_answers[f.split('.')[0]]
|
75 |
+
|
76 |
+
# loop through all rows and store the appropriate fields in a list
|
77 |
+
for _, row in file_df.iterrows():
|
78 |
+
response = row[ANSWER_COL].strip()
|
79 |
+
score = float(row[SCORE_COL])
|
80 |
+
feedback = str(row[FEEDBACK_COL]).strip()
|
81 |
+
verification_feedback = return_verification_feedback(score)
|
82 |
+
error_class = row[ERROR_CLASS_COL].strip()
|
83 |
+
|
84 |
+
# create dictionary with the appropriate fields
|
85 |
+
data.append({
|
86 |
+
'id': uuid.uuid4().hex, # generate unique id in HEX format
|
87 |
+
'question': question,
|
88 |
+
'reference_answer': ref_answer,
|
89 |
+
'provided_answer': response,
|
90 |
+
'answer_feedback': feedback,
|
91 |
+
'verification_feedback': verification_feedback,
|
92 |
+
'error_class': error_class,
|
93 |
+
'score': score
|
94 |
+
})
|
95 |
+
|
96 |
+
if not os.path.exists(dir):
|
97 |
+
print('Creating directory where JSON file will be stored\n')
|
98 |
+
os.makedirs(dir)
|
99 |
+
|
100 |
+
if train_split is None:
|
101 |
+
with jsonlines.open(f'{os.path.join(dir, filename)}.jsonl', 'w') as writer:
|
102 |
+
writer.write_all(data)
|
103 |
+
else:
|
104 |
+
# shuffle data and divide it into train and validation splits
|
105 |
+
random.shuffle(data)
|
106 |
+
train_data = data[: int(train_split * (len(data) - 1))]
|
107 |
+
val_data = data[int(train_split * (len(data) - 1)) :]
|
108 |
+
|
109 |
+
# write JSON lines file with train data
|
110 |
+
with jsonlines.open(f'{os.path.join(dir, filename)}-train.jsonl', 'w') as writer:
|
111 |
+
writer.write_all(train_data)
|
112 |
+
|
113 |
+
# write JSON lines file with validation data
|
114 |
+
with jsonlines.open(f'{os.path.join(dir, filename)}-validation.jsonl', 'w') as writer:
|
115 |
+
writer.write_all(val_data)
|
116 |
+
|
117 |
+
if __name__ == '__main__':
|
118 |
+
# convert legal domain dataset (german) to JSON lines
|
119 |
+
convert_xlsx_to_jsonl(
|
120 |
+
'data/training', 'data/reference_answers.xlsx',
|
121 |
+
'data/json', 'saf-legal-domain-german',
|
122 |
+
train_split=0.8)
|
123 |
+
|
124 |
+
convert_xlsx_to_jsonl(
|
125 |
+
'data/unseen_answers', 'data/reference_answers.xlsx',
|
126 |
+
'data/json', 'saf-legal-domain-german-unseen-answers')
|
127 |
+
|
128 |
+
convert_xlsx_to_jsonl(
|
129 |
+
'data/unseen_questions', 'data/reference_answers.xlsx',
|
130 |
+
'data/json', 'saf-legal-domain-german-unseen-questions')
|