import os import ast import datasets import pandas as pd from pprint import pprint _CITATION = """ @article{sarti-etal-2022-divemt, title={{DivEMT}: Neural Machine Translation Post-Editing Effort Across Typologically Diverse Languages}, author={Sarti, Gabriele and Bisazza, Arianna and Guerberof Arenas, Ana and Toral, Antonio}, journal={ArXiv preprint 2205.12215}, url={https://arxiv.org/abs/2205.12215}, year={2022}, month={may} } """ _DESCRIPTION = """\ DivEMT is the first publicly available post-editing study of Neural Machine Translation (NMT) over a typologically diverse set of target languages. Using a strictly controlled setup, 18 professional translators were instructed to translate or post-edit the same set of English documents into Arabic, Dutch, Italian, Turkish, Ukrainian, and Vietnamese. During the process, their edits, keystrokes, editing times, pauses, and perceived effort were logged, enabling an in-depth, cross-lingual evaluation of NMT quality and its post-editing process. """ _HOMEPAGE = "https://github.com/gsarti/divemt" _LICENSE = "GNU General Public License v3.0" _ROOT_PATH = "https://huggingface.co/datasets/GroNLP/divemt/raw/main" _PATHS = { "main": os.path.join(_ROOT_PATH, "main.tsv"), "warmup": os.path.join(_ROOT_PATH, "warmup.tsv"), } _ALL_FIELDS = ['unit_id', 'flores_id', 'item_id', 'subject_id', 'lang_id', 'doc_id', 'task_type', 'translation_type', 'src_len_chr', 'mt_len_chr', 'tgt_len_chr', 'src_len_wrd', 'mt_len_wrd', 'tgt_len_wrd', 'edit_time', 'k_total', 'k_letter', 'k_digit', 'k_white', 'k_symbol', 'k_nav', 'k_erase', 'k_copy', 'k_cut', 'k_paste', 'k_do', 'n_pause_geq_300', 'len_pause_geq_300', 'n_pause_geq_1000', 'len_pause_geq_1000', 'event_time', 'num_annotations', 'last_modification_time', 'n_insert', 'n_delete', 'n_substitute', 'n_shift', 'tot_shifted_words', 'tot_edits', 'hter', 'cer', 'bleu', 'chrf', 'time_s', 'time_m', 'time_h', 'time_per_char', 'time_per_word', 'key_per_char', 'words_per_hour', 'words_per_minute', 'per_subject_visit_order', 'src_text', 'mt_text', 'tgt_text', 'aligned_edit', 'src_tokens', 'src_annotations', 'mt_tokens', 'mt_annotations', 'tgt_tokens', 'tgt_annotations' ] _FLOAT_FIELDS = ["edit_time", "bleu", "chrf", "hter", "n_insert", "n_delete", "n_substitute", "n_shift", "time_s", "time_m", "time_h", 'time_per_char', 'time_per_word', 'key_per_char', 'words_per_hour', 'words_per_minute', 'tot_shifted_words', 'tot_edits', "mt_len_chr", "mt_len_wrd", "cer" ] _STR_FIELDS = ["unit_id", "item_id", "subject_id", "lang_id", "task_type", "translation_type", "src_text", "mt_text", "tgt_text", "aligned_edit" ] _STR_SEQ_FIELDS = ['src_tokens', 'mt_tokens', 'tgt_tokens'] _LANG_ANNOTATIONS_FIELDS = ['src_annotations', 'mt_annotations', 'tgt_annotations'] class DivEMTConfig(datasets.BuilderConfig): """BuilderConfig for the DivEMT Dataset.""" def __init__( self, features, **kwargs, ): """ Args: features: `list[string]`, list of the features that will appear in the feature dict. Should not include "label". **kwargs: keyword arguments forwarded to super. """ super().__init__(version=datasets.Version("1.0.0"), **kwargs) self.features = features class DivEMT(datasets.GeneratorBasedBuilder): VERSION = datasets.Version("1.0.0") BUILDER_CONFIGS = [ DivEMTConfig( name=name, features=_ALL_FIELDS, ) for name in ["warmup", "main"] ] DEFAULT_CONFIG_NAME = "main" def _info(self): features = {feature: datasets.Value("int32") for feature in self.config.features} for field in _ALL_FIELDS: if field in self.config.features: if field in _STR_FIELDS: features[field] = datasets.Value("string") if field in _FLOAT_FIELDS: features[field] = datasets.Value("float32") if field in _STR_SEQ_FIELDS: features[field] = datasets.Sequence(datasets.Value("string")) if field in _LANG_ANNOTATIONS_FIELDS: features[field] = datasets.features.Sequence( datasets.features.Sequence( { "id": datasets.Value("int8"), "text": datasets.Value("string"), "lemma": datasets.Value("string"), "upos": datasets.Value("string"), "xpos": datasets.Value("string"), "feats": datasets.Value("string"), "head": datasets.Value("int8"), "deprel": datasets.Value("string"), "start_char": datasets.Value("int8"), "end_char": datasets.Value("int8"), "ner": datasets.Value("string"), } ) ) return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features(features), homepage=_HOMEPAGE, license=_LICENSE, citation=_CITATION, ) def _split_generators(self, dl_manager): """Returns SplitGenerators.""" dl_dir = dl_manager.download_and_extract(_PATHS[self.config.name]) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "filepath": dl_dir, "features": self.config.features, }, ) ] def _generate_examples(self, filepath: str): """Yields examples as (key, example) tuples.""" data = pd.read_csv(filepath, sep="\t") for id_, row in data.iterrows(): row_dic = row.to_dict() for field in _STR_SEQ_FIELDS + _LANG_ANNOTATIONS_FIELDS: if isinstance(row_dic[field], float) and math.isnan(row_dic[field]): row_dic[field] = [] else: row_dic[field] = ast.literal_eval(row_dic[field]) yield id_, row_dic