Datasets:
lmqg
/

Languages:
German
Multilinguality:
monolingual
Size Categories:
10K<n<100K
Source Datasets:
deepset/germanquad
ArXiv:
Tags:
question-generation
License:
asahi417 commited on
Commit
f71a702
1 Parent(s): 3a113b7
Files changed (3) hide show
  1. README.md +0 -0
  2. process.py +125 -0
  3. qg_dequad.py +0 -0
README.md ADDED
File without changes
process.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ gsplit -l 1500 -d --additional-suffix=.jsonl test.jsonl test
3
+ gsplit -l 1500 -d --additional-suffix=.jsonl train.jsonl train
4
+ gsplit -l 1500 -d --additional-suffix=.jsonl validation.jsonl validation
5
+ rm -rf test.jsonl
6
+ rm -rf train.jsonl
7
+ rm -rf validation.jsonl
8
+ """
9
+ import json
10
+ import os
11
+ import re
12
+ import spacy
13
+ from random import seed, shuffle
14
+ from tqdm import tqdm
15
+ from datasets import load_dataset
16
+
17
+ DATASET_NAME = "deepset/germanquad"
18
+ DATASET_TYPES = None
19
+ HIGHLIGHT_TOKEN = '<hl>'
20
+ GENERATE_TEST_SPLIT = True
21
+ SPLITTER = spacy.load('de_core_news_sm')
22
+
23
+
24
+ def get_sentence(document: str): return [str(sent) for sent in SPLITTER(document).sents]
25
+
26
+
27
+ def process_single_data(question: str, paragraph: str, answer: str):
28
+ """ Convert single raw json data into QG format """
29
+ example = {'question': question, 'paragraph': paragraph, 'answer': answer}
30
+ start = example['paragraph'].find(example['answer'])
31
+ end = start + len(answer)
32
+ assert paragraph[start:end] == answer
33
+ # get sentence
34
+ before_tmp = get_sentence(example['paragraph'][:start])
35
+ if len(before_tmp) == 0:
36
+ before = ''
37
+ before_sentence = ''
38
+ else:
39
+ if before_tmp[-1].endswith('.'):
40
+ before = ' '.join(before_tmp)
41
+ before_sentence = ''
42
+ else:
43
+ before = ' '.join(before_tmp[:-1])
44
+ before_sentence = before_tmp[-1]
45
+ before_sentence = before_sentence if before_sentence.endswith(' ') else f'{before_sentence} '
46
+ after_tmp = get_sentence(example['paragraph'][start + len(example['answer']):])
47
+ if len(after_tmp) == 0:
48
+ after = ''
49
+ after_sentence = ''
50
+ else:
51
+ after = ' '.join(after_tmp[1:])
52
+ after_sentence = after_tmp[0]
53
+ after_sentence = after_sentence if after_sentence.startswith(' ') else f' {after_sentence}'
54
+ example['sentence'] = f"{before_sentence}{example['answer']}{after_sentence}"
55
+
56
+ # get paragraph_sentence
57
+ before = '' if before == '' else f'{before} '
58
+ after = '' if after == '' else f' {after}'
59
+ source_text = '{0}{1} {2} {1}{3}'.format(before, HIGHLIGHT_TOKEN, example['sentence'], after)
60
+ example['paragraph_sentence'] = re.sub(r'\s+', ' ', source_text)
61
+
62
+ # get paragraph_answer
63
+ source_text = '{0}{1} {2} {1}{3}'.format(
64
+ example['paragraph'][:start], HIGHLIGHT_TOKEN, example['answer'],
65
+ example['paragraph'][start + len(example['answer']):])
66
+ example['paragraph_answer'] = re.sub(r'\s+', ' ', source_text)
67
+
68
+ # get sentence_answer
69
+ if len(before_tmp) == 0 or before_tmp[-1].endswith('.'):
70
+ before = ''
71
+ else:
72
+ before = before_tmp[-1] if before_tmp[-1].endswith(' ') else f'{before_tmp[-1]} '
73
+ if len(after_tmp) == 0:
74
+ after = ''
75
+ else:
76
+ after = after_tmp[0] if after_tmp[0].startswith(' ') else f' {after_tmp[0]}'
77
+ source_text = '{0}{1} {2} {1}{3}'.format(before, HIGHLIGHT_TOKEN, example['answer'], after)
78
+ example['sentence_answer'] = re.sub(r'\s+', ' ', source_text)
79
+
80
+ return example
81
+
82
+
83
+ if __name__ == '__main__':
84
+ output = './data/processed'
85
+ os.makedirs(output, exist_ok=True)
86
+ if DATASET_TYPES is not None:
87
+ dataset = load_dataset(DATASET_NAME, DATASET_TYPES)
88
+ else:
89
+ dataset = load_dataset(DATASET_NAME)
90
+ for _split in dataset.keys():
91
+ tmp_dataset = dataset[_split]
92
+ with open(f'{output}/{_split}.jsonl', 'w') as f:
93
+ for single_data in tqdm(tmp_dataset):
94
+ question_str = single_data['question']
95
+ paragraph_str = single_data['context']
96
+ answer_str = single_data['answers']['text']
97
+ if type(answer_str) == list:
98
+ answer_str = answer_str[0]
99
+ assert type(answer_str) is str, answer_str
100
+ assert type(question_str) is str, question_str
101
+ assert type(paragraph_str) is str, paragraph_str
102
+ tmp_data = process_single_data(question=question_str, paragraph=paragraph_str, answer=answer_str)
103
+ tmp_data['paragraph_id'] = single_data['id']
104
+ f.write(json.dumps(tmp_data) + '\n')
105
+ if GENERATE_TEST_SPLIT:
106
+ # randomly sample for test set
107
+ with open(f'{output}/train.jsonl') as f:
108
+ lines_train = [json.loads(i) for i in f.read().split('\n') if len(i) > 0]
109
+ with open(f'{output}/test.jsonl') as f:
110
+ size = len([i for i in f.read().split('\n') if len(i) > 0])
111
+ paragraph_ids = list(set([i['paragraph_id'] for i in lines_train]))
112
+ data_train = {p: [i for i in lines_train if i['paragraph_id'] == p] for p in paragraph_ids}
113
+ seed(0)
114
+ shuffle(paragraph_ids)
115
+ data_test = []
116
+ data_train_new = []
117
+ for i in paragraph_ids:
118
+ if len(data_test) < size:
119
+ data_test += data_train[i]
120
+ else:
121
+ data_train_new += data_train[i]
122
+ with open(f'{output}/train.jsonl', 'w') as f:
123
+ f.write('\n'.join([json.dumps(i) for i in data_train_new]))
124
+ with open(f'{output}/validation.jsonl', 'w') as f:
125
+ f.write('\n'.join([json.dumps(i) for i in data_test]))
qg_dequad.py ADDED
File without changes