JohnnyBoy00 commited on
Commit
1897ad8
1 Parent(s): 978ecd7

Upload conversion.py

Browse files
Files changed (1) hide show
  1. conversion.py +97 -0
conversion.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ def convert_xml_to_jsonl(path_to_dataset, dir, filename, train_split=None):
14
+ """
15
+ Utility function used for conversion of XML files from the dataset into JSON lines
16
+
17
+ Params:
18
+ path_to_dataset (string): path to the folder containing the dataset (in XML format)
19
+ dir (string): name of the directory where the JSON lines file will be stored
20
+ filename (string): name of the JSON lines file that will store the dataset
21
+ train_split (float or None): if not None, defines which percentage of the dataset to use for the train and validation splits
22
+
23
+ Returns:
24
+ None: the file is saved in JSON lines format in the specified location
25
+ """
26
+ data = []
27
+
28
+ # loop through all files in directory
29
+ for f in os.listdir(path_to_dataset):
30
+ if f.endswith('.xml'):
31
+ root = et.parse(os.path.join(path_to_dataset, f)).getroot()
32
+ # get question
33
+ question = root.find('questionText').text.replace('\n', ' ')
34
+ # get reference and student answers
35
+ ref_answers = [x for x in root.find('referenceAnswers')]
36
+ student_answers = [x for x in root.find('studentAnswers')]
37
+
38
+ if len(ref_answers) == 1:
39
+ # get reference answer and clear all spaces
40
+ ref_answer = ref_answers[0].text.strip()
41
+
42
+ # loop through all student answers and store the appropriate fields in a list
43
+ for answer in student_answers:
44
+ response = answer.find('response').text.strip()
45
+ score = float(answer.find('score').text)
46
+ feedback = answer.find('response_feedback').text.strip()
47
+ verification_feedback = answer.find('verification_feedback').text.strip()
48
+
49
+ # create dictionary with the appropriate fields
50
+ data.append({
51
+ 'id': uuid.uuid4().hex, # generate unique id in HEX format
52
+ 'question': question,
53
+ 'reference_answer': ref_answer,
54
+ 'provided_answer': response,
55
+ 'answer_feedback': feedback,
56
+ 'verification_feedback': verification_feedback,
57
+ 'score': score
58
+ })
59
+
60
+ if not os.path.exists(dir):
61
+ print('Creating directory where JSON file will be stored\n')
62
+ os.makedirs(dir)
63
+
64
+ if train_split is None:
65
+ with jsonlines.open(f'{os.path.join(dir, filename)}.jsonl', 'w') as writer:
66
+ writer.write_all(data)
67
+ else:
68
+ # shuffle data and divide it into train and validation splits
69
+ random.shuffle(data)
70
+ train_data = data[: int(train_split * (len(data) - 1))]
71
+ val_data = data[int(train_split * (len(data) - 1)) :]
72
+
73
+ # write JSON lines file with train data
74
+ with jsonlines.open(f'{os.path.join(dir, filename)}-train.jsonl', 'w') as writer:
75
+ writer.write_all(train_data)
76
+
77
+ # write JSON lines file with validation data
78
+ with jsonlines.open(f'{os.path.join(dir, filename)}-validation.jsonl', 'w') as writer:
79
+ writer.write_all(val_data)
80
+
81
+ if __name__ == '__main__':
82
+ # convert micro job dataset (german) to JSON lines
83
+ convert_xml_to_jsonl(
84
+ 'data/training/german',
85
+ 'data/json',
86
+ 'saf-micro-job-german',
87
+ train_split=0.8)
88
+
89
+ convert_xml_to_jsonl(
90
+ 'data/unseen_answers/german',
91
+ 'data/json',
92
+ 'saf-micro-job-german-unseen-answers')
93
+
94
+ convert_xml_to_jsonl(
95
+ 'data/unseen_questions/german',
96
+ 'data/json',
97
+ 'saf-micro-job-german-unseen-questions')