ikcest2022 / ikcest2022.py
miugod's picture
Update ikcest2022.py
a9e9382 verified
# coding=utf-8
# 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.
"""IWSLT 2017 dataset """
import os
import datasets
_HOMEPAGE = "https://aistudio.baidu.com/competition/detail/477/0/datasets"
_DESCRIPTION = """\
The IKCEST 2022 Multilingual Task addresses text translation, including zero-shot translation, with a single MT system across all directions including English, German, Dutch, Italian and Romanian. As unofficial task, conventional bilingual text translation is offered between English and Arabic, French, Japanese, Chinese, German and Korean.
"""
_CITATION = """\
@
"""
# REPO_URL = "https://huggingface.co/datasets/ikcest2022"
# MULTI_URL = REPO_URL + "data/2017-01-trnmted/texts/DeEnItNlRo/DeEnItNlRo/DeEnItNlRo-DeEnItNlRo.zip"
# BI_URL = REPO_URL + "data/2017-01-trnted/texts/{source}/{target}/{source}-{target}.zip"
URL = "data/ZhFrRuThArEn.zip"
# ikcest2022/data/ZhFrRuThArEn
class IKCEST2022Config(datasets.BuilderConfig):
"""BuilderConfig for NewDataset"""
def __init__(self, pair, **kwargs):
"""
Args:
pair: the language pair to consider
**kwargs: keyword arguments forwarded to super.
"""
self.pair = pair
super().__init__(**kwargs)
# XXX: Artificially removed DE from here, as it also exists within bilingual data
SOURCE_LANGUAGE = "zh"
TARGET_LANGUAGES = ["fr","ru","th","ar","en"]
FORWARD_PAIRS = [f"{SOURCE_LANGUAGE}-{target}" for target in TARGET_LANGUAGES]
BACKWARD_PAIRS = [f"{target}-{SOURCE_LANGUAGE}" for target in TARGET_LANGUAGES]
PAIRS = FORWARD_PAIRS + BACKWARD_PAIRS
class IKCEST2022(datasets.GeneratorBasedBuilder):
"""The IKCEST 2022 Evaluation Campaign includes a multilingual TED Talks MT task."""
VERSION = datasets.Version("1.0.0")
# This is an example of a dataset with multiple configurations.
# If you don't want/need to define several sub-sets in your dataset,
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
BUILDER_CONFIG_CLASS = IKCEST2022Config
BUILDER_CONFIGS = [
IKCEST2022Config(
name="ikcest2022-" + pair,
description="A small dataset",
version=datasets.Version("1.0.0"),
pair=pair,
)
for pair in PAIRS
]
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{"translation": datasets.features.Translation(languages=self.config.pair.split("-"))}
),
homepage=_HOMEPAGE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
source, target = self.config.pair.split("-")
dl_dir = dl_manager.download_and_extract(URL)
TARGET_LANGUAGE = target if target != SOURCE_LANGUAGE else source
data_dir = os.path.join(dl_dir, "ZhFrRuThArEn",f"{SOURCE_LANGUAGE}-{TARGET_LANGUAGE}")
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"source_files": [
os.path.join(
data_dir,
f"train.{self.config.pair}.{source}",
)
],
"target_files": [
os.path.join(
data_dir,
f"train.{self.config.pair}.{target}",
)
],
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"source_files": [
os.path.join(
data_dir,
f"test.{self.config.pair}.{source}",
)
],
"target_files": [
os.path.join(
data_dir,
f"test.{self.config.pair}.{target}",
)
],
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"source_files": [
os.path.join(
data_dir,
f"dev.{self.config.pair}.{source}",
)
],
"target_files": [
os.path.join(
data_dir,
f"dev.{self.config.pair}.{target}",
)
],
},
),
]
def _generate_examples(self, source_files, target_files):
"""Yields examples."""
def _reverse_lang_pair(file_name):
'''
Reverses the language pair in a given file name.
For example, 'data/train.fr-zh.zh' becomes 'data/train.zh-fr.zh'.
'''
base_name = os.path.basename(file_name)
dir_name = os.path.dirname(file_name)
# Extract the language pair part (e.g., 'fr-zh')
lang_pair = base_name.split('.')[-2]
# Split the language pair and reverse it
reversed_lang_pair = '-'.join(reversed(lang_pair.split('-')))
# Replace the original language pair with the reversed one
reversed_file_name = os.path.join(dir_name, base_name.replace(lang_pair, reversed_lang_pair))
return reversed_file_name
id_ = 0
source, target = self.config.pair.split("-")
for idx in range(len(source_files)):
source_file, target_file = source_files[idx], target_files[idx]
if os.path.exists(source_file) and os.path.exists(target_file):
continue
else:
source_files[idx] = _reverse_lang_pair(source_file)
target_files[idx] = _reverse_lang_pair(target_file)
for source_file, target_file in zip(source_files, target_files):
with open(source_file, "r", encoding="utf-8") as sf:
with open(target_file, "r", encoding="utf-8") as tf:
for source_row, target_row in zip(sf, tf):
source_row = source_row.strip()
target_row = target_row.strip()
if source_row=="" or target_row=="":
continue
yield id_, {"translation": {source: source_row, target: target_row}}
id_ += 1