-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closes #5 | Add tatoeba dataset loader #22
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
026d00a
Add tatoeba dataset loader
ljvmiranda921 0ee8922
Strip each row
ljvmiranda921 c7f2fd7
Rerun make check_run on the source
ljvmiranda921 aba9c65
Override lang with the actual language code
ljvmiranda921 d5bef23
Fix incorrect spelling in loader class
ljvmiranda921 e95f83e
Change delimiter from period to underscore
ljvmiranda921 a44be2b
Add builder configs that load all instances
ljvmiranda921 414b313
Run formatter on source file
ljvmiranda921 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
from pathlib import Path | ||
from typing import Dict, List, Tuple | ||
|
||
import datasets | ||
from datasets.download.download_manager import DownloadManager | ||
|
||
from seacrowd.utils import schemas | ||
from seacrowd.utils.configs import SEACrowdConfig | ||
from seacrowd.utils.constants import Licenses, Tasks | ||
|
||
_CITATION = """\ | ||
@article{tatoeba, | ||
title = {Massively Multilingual Sentence Embeddings for Zero-Shot Cross-Lingual Transfer and Beyond}, | ||
author = {Mikel, Artetxe and Holger, Schwenk,}, | ||
journal = {arXiv:1812.10464v2}, | ||
year = {2018} | ||
} | ||
""" | ||
|
||
_LOCAL = False | ||
_LANGUAGES = ["ind", "vie", "tgl", "jav", "tha"] | ||
_DATASETNAME = "tatoeba" | ||
_DESCRIPTION = """\ | ||
This dataset is a subset of the Tatoeba corpus containing language pairs for Indonesian, Vietnamese, Tagalog, Javanese, and Thai. | ||
The original dataset description can be found below: | ||
|
||
This data is extracted from the Tatoeba corpus, dated Saturday 2018/11/17. | ||
For each languages, we have selected 1000 English sentences and their translations, if available. Please check | ||
this paper for a description of the languages, their families and scripts as well as baseline results. | ||
Please note that the English sentences are not identical for all language pairs. This means that the results are | ||
not directly comparable across languages. In particular, the sentences tend to have less variety for several | ||
low-resource languages, e.g. "Tom needed water", "Tom needs water", "Tom is getting water", ... | ||
""" | ||
|
||
_HOMEPAGE = "https://github.com/facebookresearch/LASER/blob/main/data/tatoeba/v1/README.md" | ||
_LICENSE = Licenses.APACHE_2_0.value | ||
_URL = "https://github.com/facebookresearch/LASER/raw/main/data/tatoeba/v1/" | ||
|
||
_SUPPORTED_TASKS = [Tasks.MACHINE_TRANSLATION] | ||
_SOURCE_VERSION = "1.0.0" | ||
_SEACROWD_VERSION = "1.0.0" | ||
|
||
|
||
class TatoebaDatset(datasets.GeneratorBasedBuilder): | ||
"""Tatoeba subset for Indonesian, Vietnamese, Tagalog, Javanese, and Thai.""" | ||
|
||
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION) | ||
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION) | ||
|
||
SEACROWD_SCHEMA_NAME = "t2t" | ||
|
||
dataset_names = sorted([f"tatoeba.{lang}" for lang in _LANGUAGES]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! e95f83e |
||
BUILDER_CONFIGS = [] | ||
for name in dataset_names: | ||
source_config = SEACrowdConfig( | ||
name=f"{name}_source", | ||
version=SOURCE_VERSION, | ||
description=f"{_DATASETNAME} source schema", | ||
schema="source", | ||
subset_id=name, | ||
) | ||
BUILDER_CONFIGS.append(source_config) | ||
seacrowd_config = SEACrowdConfig( | ||
name=f"{name}_seacrowd_{SEACROWD_SCHEMA_NAME}", | ||
version=SEACROWD_VERSION, | ||
description=f"{_DATASETNAME} SEACrowd schema", | ||
schema=f"seacrowd_{SEACROWD_SCHEMA_NAME}", | ||
subset_id=name, | ||
) | ||
BUILDER_CONFIGS.append(seacrowd_config) | ||
|
||
# Choose first language as default | ||
DEFAULT_CONFIG_NAME = f"{dataset_names[0]}_source" | ||
|
||
def _info(self) -> datasets.DatasetInfo: | ||
if self.config.schema == "source": | ||
features = datasets.Features( | ||
{ | ||
"source_sentence": datasets.Value("string"), | ||
"target_sentence": datasets.Value("string"), | ||
"source_lang": datasets.Value("string"), | ||
"target_lang": datasets.Value("string"), | ||
} | ||
) | ||
elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}": | ||
features = schemas.text2text_features | ||
return datasets.DatasetInfo( | ||
description=_DESCRIPTION, | ||
features=features, | ||
homepage=_HOMEPAGE, | ||
license=_LICENSE, | ||
citation=_CITATION, | ||
) | ||
|
||
def _split_generators(self, dl_manager: DownloadManager) -> List[datasets.SplitGenerator]: | ||
"""Return SplitGenerators.""" | ||
lang_source = self.config.name.split(".")[1] | ||
lang = lang_source.split("_")[0] | ||
tatoeba_source_data = dl_manager.download_and_extract(_URL + f"tatoeba.{lang}-eng.{lang}") | ||
tatoeba_eng_data = dl_manager.download_and_extract(_URL + f"tatoeba.{lang}-eng.eng") | ||
return [datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": (tatoeba_source_data, tatoeba_eng_data), "split": "dev"})] | ||
|
||
def _generate_examples(self, filepath: Tuple[Path, Path], split: str) -> Tuple[int, Dict]: | ||
"""Yield examples as (key, example) tuples""" | ||
source_file = filepath[0] | ||
target_file = filepath[1] | ||
source_sentences = [] | ||
target_sentences = [] | ||
with open(source_file, encoding="utf-8") as f1: | ||
for row in f1: | ||
source_sentences.append(row) | ||
with open(target_file, encoding="utf-8") as f2: | ||
for row in f2: | ||
target_sentences.append(row) | ||
ljvmiranda921 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for idx in range(len(source_sentences)): | ||
if self.config.schema == "source": | ||
example = { | ||
"source_sentence": source_sentences[idx], | ||
"target_sentence": target_sentences[idx], | ||
"source_lang": source_file.split(".")[-1], | ||
"target_lang": "eng", | ||
} | ||
elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}": | ||
example = { | ||
"id": str(idx), | ||
"text_1": source_sentences[idx], | ||
"text_2": target_sentences[idx], | ||
"text_1_name": source_file.split(".")[-1], | ||
"text_2_name": "eng", | ||
} | ||
yield idx, example |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please change the class name to
TatoebaDataset
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching! Fixed d5bef23