# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os import io import datasets import DiscoEvalConstants import pickle import logging # General Constants: LABEL_NAME = 'label' numbers = { 1: "first", 2: "second", 3: "third", 4: "fourth", 5: "fifth", 6: "sixth", 7: "seventh", 8: "eighth", 9: "ninth" } TEXT_COLUMN_NAME = [f"{numbers[i]}_sentence" for i in range(1, 10)] # SSPabs Constants: SSPABS = 'SSPabs' SSPABS_TRAIN_NAME = 'train.txt' SSPABS_VALID_NAME = 'valid.txt' SSPABS_TEST_NAME = 'test.txt' SSPABS_DATA_DIR = 'data/SSP/abs' SSPABS_LABELS = { "0": "Does not belong to abstract", "1": "Belongs to abstract", } SSPABS_TEXT_COLUMNS = 1 # PDTB Constants: PDTB_I = 'PDTB-I' PDTB_E = 'PDTB-E' PDTB_TRAIN_NAME = 'train.txt' PDTB_VALID_NAME = 'valid.txt' PDTB_TEST_NAME = 'test.txt' PDTB_DATA_DIR = 'data/PDTB' PDTB_DIRS = {PDTB_E: 'Explicit', PDTB_I: 'Implicit'} PDTB_E_LABELS = [ 'Comparison.Concession', 'Comparison.Contrast', 'Contingency.Cause', 'Contingency.Condition', 'Contingency.Pragmatic condition', 'Expansion.Alternative', 'Expansion.Conjunction', 'Expansion.Instantiation', 'Expansion.List', 'Expansion.Restatement', 'Temporal.Asynchronous', 'Temporal.Synchrony', ] PDTB_I_LABELS = [ 'Comparison.Concession', 'Comparison.Contrast', 'Contingency.Cause', 'Contingency.Pragmatic cause', 'Expansion.Alternative', 'Expansion.Conjunction', 'Expansion.Instantiation', 'Expansion.List', 'Expansion.Restatement', 'Temporal.Asynchronous', 'Temporal.Synchrony', ] PDTB_E_TEXT_COLUMNS = 2 PDTB_I_TEXT_COLUMNS = 2 # SP Constants: SPARXIV = 'SParxiv' SPROCSTORY = 'SProcstory' SPWIKI = 'SPwiki' SP_TRAIN_NAME = 'train.txt' SP_VALID_NAME = 'valid.txt' SP_TEST_NAME = 'test.txt' SP_DATA_DIR = 'data/SP' SP_DIRS = {SPARXIV: 'arxiv', SPROCSTORY: 'rocstory', SPWIKI: 'wiki'} SP_LABELS = { "0": 'First sentence', "1": 'Second sentence', "2": 'Third sentence', "3": "Fourth sentence", "4": "Fifth sentence", } SP_TEXT_COLUMNS = 5 # BSO Constants: BSOARXIV = 'BSOarxiv' BSOROCSTORY = 'BSOrocstory' BSOWIKI = 'BSOwiki' BSO_TRAIN_NAME = 'train.txt' BSO_VALID_NAME = 'valid.txt' BSO_TEST_NAME = 'test.txt' BSO_DATA_DIR = 'data/BSO' BSO_DIRS = {BSOARXIV: 'arxiv', BSOROCSTORY: 'rocstory', BSOWIKI: 'wiki'} BSO_LABELS = { "0": 'Incorrect order', "1": 'Correct order', } BSO_TEXT_COLUMNS = 2 # DC Constants: DCCHAT = 'DCchat' DCWIKI = 'DCwiki' DC_TRAIN_NAME = 'train.txt' DC_VALID_NAME = 'valid.txt' DC_TEST_NAME = 'test.txt' DC_DATA_DIR = 'data/DC' DC_DIRS = {DCCHAT: 'chat', DCWIKI: 'wiki'} DC_LABELS = { "0": "Incoherent", "1": "Coherent", } DC_TEXT_COLUMNS = 6 # RST Constants: RST = 'RST' RST_TRAIN_NAME = 'RST_TRAIN.pkl' RST_VALID_NAME = 'RST_DEV.pkl' RST_TEST_NAME = 'RST_TEST.pkl' RST_DATA_DIR = 'data/RST' RST_LABELS = [ 'NS-Explanation', 'NS-Evaluation', 'NN-Condition', 'NS-Summary', 'SN-Cause', 'SN-Background', 'NS-Background', 'SN-Summary', 'NS-Topic-Change', 'NN-Explanation', 'SN-Topic-Comment', 'NS-Elaboration', 'SN-Attribution', 'SN-Manner-Means', 'NN-Evaluation', 'NS-Comparison', 'NS-Contrast', 'SN-Condition', 'NS-Temporal', 'NS-Enablement', 'SN-Evaluation', 'NN-Topic-Comment', 'NN-Temporal', 'NN-Textual-organization', 'NN-Same-unit', 'NN-Comparison', 'NN-Topic-Change', 'SN-Temporal', 'NN-Joint', 'SN-Enablement', 'SN-Explanation', 'NN-Contrast', 'NN-Cause', 'SN-Contrast', 'NS-Attribution', 'NS-Topic-Comment', 'SN-Elaboration', 'SN-Comparison', 'NS-Cause', 'NS-Condition', 'NS-Manner-Means' ] RST_TEXT_COLUMNS = 2 DATASET_NAMES = [ SSPABS, PDTB_I, PDTB_E, SPARXIV, SPROCSTORY, SPWIKI, BSOARXIV, BSOROCSTORY, BSOWIKI, DCCHAT, DCWIKI, RST, ] _CITATION = """\ @InProceedings{mchen-discoeval-19, title = {Evaluation Benchmarks and Learning Criteria for Discourse-Aware Sentence Representations}, author = {Mingda Chen and Zewei Chu and Kevin Gimpel}, booktitle = {Proc. of {EMNLP}}, year={2019} } """ _DESCRIPTION = """\ This dataset contains all tasks of the DiscoEval benchmark for sentence representation learning. """ _HOMEPAGE = "https://github.com/ZeweiChu/DiscoEval" class DiscoEvalSentence(datasets.GeneratorBasedBuilder): """DiscoEval Benchmark""" VERSION = datasets.Version("1.1.0") BUILDER_CONFIGS = [ datasets.BuilderConfig( name=DiscoEvalConstants.SPARXIV, version=VERSION, description="Sentence positioning dataset from arXiv", ), datasets.BuilderConfig( name=DiscoEvalConstants.SPROCSTORY, version=VERSION, description="Sentence positioning dataset from ROCStory", ), datasets.BuilderConfig( name=DiscoEvalConstants.SPWIKI, version=VERSION, description="Sentence positioning dataset from Wikipedia", ), datasets.BuilderConfig( name=DiscoEvalConstants.DCCHAT, version=VERSION, description="Discourse Coherence dataset from chat", ), datasets.BuilderConfig( name=DiscoEvalConstants.DCWIKI, version=VERSION, description="Discourse Coherence dataset from Wikipedia", ), datasets.BuilderConfig( name=DiscoEvalConstants.RST, version=VERSION, description="The RST Discourse Treebank dataset ", ), datasets.BuilderConfig( name=DiscoEvalConstants.PDTB_E, version=VERSION, description="The Penn Discourse Treebank - Explicit dataset.", ), datasets.BuilderConfig( name=DiscoEvalConstants.PDTB_I, version=VERSION, description="The Penn Discourse Treebank - Implicit dataset.", ), datasets.BuilderConfig( name=DiscoEvalConstants.SSPABS, version=VERSION, description="The SSP dataset.", ), datasets.BuilderConfig( name=DiscoEvalConstants.BSOARXIV, version=VERSION, description="The BSO Task with the arxiv dataset.", ), datasets.BuilderConfig( name=DiscoEvalConstants.BSOWIKI, version=VERSION, description="The BSO Task with the wiki dataset.", ), datasets.BuilderConfig( name=DiscoEvalConstants.BSOROCSTORY, version=VERSION, description="The BSO Task with the rocstory dataset.", ), ] def _info(self): if self.config.name in [DiscoEvalConstants.SPARXIV, DiscoEvalConstants.SPROCSTORY, DiscoEvalConstants.SPWIKI]: features_dict = { DiscoEvalConstants.TEXT_COLUMN_NAME[i]: datasets.Value('string') for i in range(DiscoEvalConstants.SP_TEXT_COLUMNS) } features_dict[DiscoEvalConstants.LABEL_NAME] = datasets.ClassLabel( names=list(DiscoEvalConstants.SP_LABELS.values()), ) features = datasets.Features(features_dict) elif self.config.name in [DiscoEvalConstants.BSOARXIV, DiscoEvalConstants.BSOWIKI, DiscoEvalConstants.BSOROCSTORY]: features_dict = { DiscoEvalConstants.TEXT_COLUMN_NAME[i]: datasets.Value('string') for i in range(DiscoEvalConstants.BSO_TEXT_COLUMNS) } features_dict[DiscoEvalConstants.LABEL_NAME] = datasets.ClassLabel( names=list(DiscoEvalConstants.BSO_LABELS.values()) ) features = datasets.Features(features_dict) elif self.config.name in [DiscoEvalConstants.DCCHAT, DiscoEvalConstants.DCWIKI]: features_dict = { DiscoEvalConstants.TEXT_COLUMN_NAME[i]: datasets.Value('string') for i in range(DiscoEvalConstants.DC_TEXT_COLUMNS) } features_dict[DiscoEvalConstants.LABEL_NAME] = datasets.ClassLabel( names=list(DiscoEvalConstants.DC_LABELS.values()) ) features = datasets.Features(features_dict) elif self.config.name in [DiscoEvalConstants.RST]: features_dict = { DiscoEvalConstants.TEXT_COLUMN_NAME[i]: [datasets.Value('string')] for i in range(DiscoEvalConstants.RST_TEXT_COLUMNS) } features_dict[DiscoEvalConstants.LABEL_NAME] = datasets.ClassLabel( names=list(DiscoEvalConstants.RST_LABELS.values()) ) features = datasets.Features(features_dict) elif self.config.name in [DiscoEvalConstants.PDTB_E]: features_dict = { DiscoEvalConstants.TEXT_COLUMN_NAME[i]: datasets.Value('string') for i in range(DiscoEvalConstants.PDTB_E_TEXT_COLUMNS) } features_dict[DiscoEvalConstants.LABEL_NAME] = datasets.ClassLabel( names=DiscoEvalConstants.PDTB_E_LABELS ) features = datasets.Features(features_dict) elif self.config.name in [DiscoEvalConstants.PDTB_I]: features_dict = { DiscoEvalConstants.TEXT_COLUMN_NAME[i]: datasets.Value('string') for i in range(DiscoEvalConstants.PDTB_I_TEXT_COLUMNS) } features_dict[DiscoEvalConstants.LABEL_NAME] = datasets.ClassLabel( names=DiscoEvalConstants.PDTB_I_LABELS ) features = datasets.Features(features_dict) elif self.config.name in [DiscoEvalConstants.SSPABS]: features_dict = { DiscoEvalConstants.TEXT_COLUMN_NAME[i]: datasets.Value('string') for i in range(DiscoEvalConstants.SSPABS_TEXT_COLUMNS) } features_dict[DiscoEvalConstants.LABEL_NAME] = datasets.ClassLabel( names=list(DiscoEvalConstants.SSPABS_LABELS.values()) ) features = datasets.Features(features_dict) return datasets.DatasetInfo( description=_DESCRIPTION, features=features, homepage=_HOMEPAGE, citation=_CITATION, ) def _split_generators(self, dl_manager): if self.config.name in [DiscoEvalConstants.SPARXIV, DiscoEvalConstants.SPROCSTORY, DiscoEvalConstants.SPWIKI]: data_dir = DiscoEvalConstants.SP_DATA_DIR + "/" + DiscoEvalConstants.SP_DIRS[self.config.name] train_name = DiscoEvalConstants.SP_TRAIN_NAME valid_name = DiscoEvalConstants.SP_VALID_NAME test_name = DiscoEvalConstants.SP_TEST_NAME elif self.config.name in [DiscoEvalConstants.BSOARXIV, DiscoEvalConstants.BSOWIKI, DiscoEvalConstants.BSOROCSTORY]: data_dir = DiscoEvalConstants.BSO_DATA_DIR + "/" + DiscoEvalConstants.BSO_DIRS[self.config.name] train_name = DiscoEvalConstants.BSO_TRAIN_NAME valid_name = DiscoEvalConstants.BSO_VALID_NAME test_name = DiscoEvalConstants.BSO_TEST_NAME elif self.config.name in [DiscoEvalConstants.DCCHAT, DiscoEvalConstants.DCWIKI]: data_dir = DiscoEvalConstants.DC_DATA_DIR + "/" + DiscoEvalConstants.DC_DIRS[self.config.name] train_name = DiscoEvalConstants.DC_TRAIN_NAME valid_name = DiscoEvalConstants.DC_VALID_NAME test_name = DiscoEvalConstants.DC_TEST_NAME elif self.config.name in [DiscoEvalConstants.RST]: data_dir = DiscoEvalConstants.RST_DATA_DIR train_name = DiscoEvalConstants.RST_TRAIN_NAME valid_name = DiscoEvalConstants.RST_VALID_NAME test_name = DiscoEvalConstants.RST_TEST_NAME elif self.config.name in [DiscoEvalConstants.PDTB_E, DiscoEvalConstants.PDTB_I]: data_dir = os.path.join(DiscoEvalConstants.PDTB_DATA_DIR, DiscoEvalConstants.PDTB_DIRS[self.config.name]) train_name = DiscoEvalConstants.PDTB_TRAIN_NAME valid_name = DiscoEvalConstants.PDTB_VALID_NAME test_name = DiscoEvalConstants.PDTB_TEST_NAME elif self.config.name in [DiscoEvalConstants.SSPABS]: data_dir = DiscoEvalConstants.SSPABS_DATA_DIR train_name = DiscoEvalConstants.SSPABS_TRAIN_NAME valid_name = DiscoEvalConstants.SSPABS_VALID_NAME test_name = DiscoEvalConstants.SSPABS_TEST_NAME urls_to_download = { "train": data_dir + "/" + train_name, "valid": data_dir + "/" + valid_name, "test": data_dir + "/" + test_name, } logger = logging.getLogger(__name__) data_dirs = dl_manager.download_and_extract(urls_to_download) logger.info(f"Data directories: {data_dirs}") downloaded_files = dl_manager.download_and_extract(data_dirs) logger.info(f"Downloading Completed") return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "filepath": downloaded_files['train'], "split": "train", }, ), datasets.SplitGenerator( name=datasets.Split.VALIDATION, gen_kwargs={ "filepath": downloaded_files['valid'], "split": "dev", }, ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={ "filepath": downloaded_files['test'], "split": "test" }, ), ] def _generate_examples(self, filepath, split): logger = logging.getLogger(__name__) logger.info(f"Current working dir: {os.getcwd()}") logger.info("generating examples from = %s", filepath) if self.config.name == DiscoEvalConstants.RST: data = pickle.load(open(filepath, "rb")) for key, line in enumerate(data): example = {DiscoEvalConstants.TEXT_COLUMN_NAME[i]: sent for i, sent in enumerate(line[1:])} example[DiscoEvalConstants.LABEL_NAME] = DiscoEvalConstants.RST_DATA_DIR[line[0]] yield key, example else: with io.open(filepath, mode='r', encoding='utf-8') as f: for key, line in enumerate(f): line = line.strip().split("\t") example = {DiscoEvalConstants.TEXT_COLUMN_NAME[i]: sent for i, sent in enumerate(line[1:])} if self.config.name == DiscoEvalConstants.PDTB_E: example[DiscoEvalConstants.LABEL_NAME] = DiscoEvalConstants.PDTB_E_LABELS[int(line[0])] if self.config.name == DiscoEvalConstants.PDTB_I: example[DiscoEvalConstants.LABEL_NAME] = DiscoEvalConstants.PDTB_I_LABELS[int(line[0])] elif self.config.name in (DiscoEvalConstants.DCCHAT, DiscoEvalConstants.DCWIKI): example[DiscoEvalConstants.LABEL_NAME] = DiscoEvalConstants.DC_LABELS[line[0]] elif self.config.name == DiscoEvalConstants.SSPABS: example[DiscoEvalConstants.LABEL_NAME] = DiscoEvalConstants.SSPABS_LABELS[line[0]] elif self.config.name in (DiscoEvalConstants.SPWIKI, DiscoEvalConstants.SPROCSTORY, DiscoEvalConstants.SPARXIV): example[DiscoEvalConstants.LABEL_NAME] = DiscoEvalConstants.SP_LABELS[line[0]] elif self.config.name in (DiscoEvalConstants.BSOARXIV, DiscoEvalConstants.BSOWIKI, DiscoEvalConstants.BSOROCSTORY): example[DiscoEvalConstants.LABEL_NAME] = DiscoEvalConstants.BSO_LABELS[line[0]] yield key, example