|
|
|
import os |
|
|
|
import json |
|
|
|
import datasets |
|
from typing import Any |
|
|
|
import sys |
|
|
|
|
|
|
|
_CITATION = """\ |
|
@inproceedings{lecorve2022sparql2text, |
|
title={Coqar: Question rewriting on coqa}, |
|
author={Lecorv\'e, Gw\'enol\'e and Veyret, Morgan and Brabant, Quentin and Rojas-Barahona, Lina M.}, |
|
journal={Proceedings of the Conference of the Asia-Pacific Chapter of the Association for Computational Linguistics and the International Joint Conference on Natural Language Processing (AACL-IJCNLP)}, |
|
year={2022} |
|
} |
|
""" |
|
|
|
_HOMEPAGE = "" |
|
|
|
_DESCRIPTION = """\ |
|
Special version of CSQA for the SPARQL-to-Text task |
|
""" |
|
|
|
_URLS = { |
|
"all": "json/csqa_sparql_to_text.tar.gz" |
|
} |
|
|
|
class CSQA(datasets.GeneratorBasedBuilder): |
|
""" |
|
Complex Sequential Question Answering dataset |
|
""" |
|
|
|
VERSION = datasets.Version("1.0.0") |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
|
|
description=_DESCRIPTION, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
features=datasets.Features( |
|
{ |
|
"id": datasets.Value("string"), |
|
"turns": [ |
|
{ |
|
"id": datasets.Value("int64"), |
|
"ques_type_id": datasets.Value("int64"), |
|
"question-type": datasets.Value("string"), |
|
"description": datasets.Value("string"), |
|
"entities_in_utterance": [datasets.Value("string")], |
|
"relations": [datasets.Value("string")], |
|
"type_list": [datasets.Value("string")], |
|
"speaker": datasets.Value("string"), |
|
"utterance": datasets.Value("string"), |
|
"all_entities": [datasets.Value("string")], |
|
"active_set": [datasets.Value("string")], |
|
'sec_ques_sub_type': datasets.Value("int64"), |
|
'sec_ques_type': datasets.Value("int64"), |
|
'set_op_choice': datasets.Value("int64"), |
|
'is_inc': datasets.Value("int64"), |
|
'count_ques_sub_type': datasets.Value("int64"), |
|
'count_ques_type': datasets.Value("int64"), |
|
'is_incomplete': datasets.Value("int64"), |
|
'inc_ques_type': datasets.Value("int64"), |
|
'set_op': datasets.Value("int64"), |
|
'bool_ques_type': datasets.Value("int64"), |
|
'entities': [datasets.Value("string")], |
|
"clarification_step": datasets.Value("int64"), |
|
"gold_actions": [[datasets.Value("string")]], |
|
"is_spurious": datasets.Value("bool"), |
|
"masked_verbalized_answer": datasets.Value("string"), |
|
"parsed_active_set": [datasets.Value("string")], |
|
"sparql_query": datasets.Value("string"), |
|
"verbalized_all_entities": [datasets.Value("string")], |
|
"verbalized_answer": datasets.Value("string"), |
|
"verbalized_entities_in_utterance": [datasets.Value("string")], |
|
"verbalized_gold_actions": [[datasets.Value("string")]], |
|
"verbalized_parsed_active_set": [datasets.Value("string")], |
|
"verbalized_sparql_query": datasets.Value("string"), |
|
"verbalized_triple": datasets.Value("string"), |
|
"verbalized_type_list": [datasets.Value("string")] |
|
} |
|
] |
|
} |
|
), |
|
|
|
|
|
|
|
supervised_keys=None, |
|
|
|
homepage=_HOMEPAGE, |
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
"""Returns SplitGenerators.""" |
|
|
|
|
|
|
|
downloaded_files = dl_manager.download_and_extract(_URLS) |
|
train_path = os.path.join(downloaded_files['all'],'csqa_sparql_to_text/train/') |
|
test_path = os.path.join(downloaded_files['all'],'csqa_sparql_to_text/test/') |
|
valid_path = os.path.join(downloaded_files['all'],'csqa_sparql_to_text/valid/') |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={"filepath": train_path, |
|
"split": "train"} |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
gen_kwargs={"filepath": test_path, |
|
"split": "test"} |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
gen_kwargs={"filepath": valid_path, |
|
"split": "valid"} |
|
), |
|
] |
|
|
|
def _generate_examples(self, filepath, split): |
|
"""Yields examples.""" |
|
|
|
def _transform(x): |
|
pattern = { |
|
"id": None, |
|
"ques_type_id": None, |
|
"question-type": "", |
|
"description": "", |
|
"entities_in_utterance": [], |
|
"relations": [], |
|
"type_list": [], |
|
"speaker": "", |
|
"utterance": "", |
|
"all_entities": [], |
|
"active_set": [], |
|
'sec_ques_sub_type': None, |
|
'sec_ques_type': None, |
|
'set_op_choice': None, |
|
'is_inc': None, |
|
'count_ques_sub_type': None, |
|
'count_ques_type': None, |
|
'is_incomplete': None, |
|
'inc_ques_type': None, |
|
'set_op': None, |
|
'bool_ques_type': None, |
|
'entities': [], |
|
"clarification_step": None, |
|
"gold_actions": [], |
|
"is_spurious": None, |
|
"masked_verbalized_answer": None, |
|
"parsed_active_set": [], |
|
"sparql_query": None, |
|
"verbalized_all_entities": [], |
|
"verbalized_answer": None, |
|
"verbalized_entities_in_utterance": [], |
|
"verbalized_gold_actions": [], |
|
"verbalized_parsed_active_set": [], |
|
"verbalized_sparql_query": None, |
|
"verbalized_triple": [], |
|
"verbalized_type_list": [] |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
pattern.update(x) |
|
|
|
|
|
|
|
|
|
return pattern |
|
data_keys = {} |
|
for root, dirs, files in os.walk(filepath): |
|
dialog_id = root.split('/')[-1] |
|
for i,filename in enumerate(files): |
|
sample_id = "%s:%s"%(dialog_id,i) |
|
with open(os.path.join(root,filename),'r') as f: |
|
data = json.load(f) |
|
|
|
for x in data: |
|
for k,v in x.items(): |
|
if not k in data_keys: |
|
data_keys[k] = type(v) |
|
new_data = list() |
|
for i,_ in enumerate(data): |
|
|
|
|
|
|
|
|
|
|
|
new_data.append(data[i]) |
|
data = [ _transform(x) for x in data] |
|
yield sample_id, { |
|
"id": sample_id, |
|
"turns": data |
|
} |
|
|
|
|