Skip to content
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 #448 | Add/Update Dataloader alorese #541

Merged
merged 22 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c58865d
feat: dataloader for text2text MT
patrickamadeus Mar 18, 2024
1c99845
nitpick: block sp2t to pass tc for t2t task
patrickamadeus Mar 18, 2024
aaba4fc
nitpick join
patrickamadeus Mar 19, 2024
8fcffaf
Merge branch 'master' into alorese
patrickamadeus Mar 19, 2024
7513b50
feat: support sptext, sptext_translated
patrickamadeus Mar 19, 2024
81fde25
feat: final alorese_source code
patrickamadeus Mar 19, 2024
8d7f639
chore: scrape entire URLs
patrickamadeus Mar 19, 2024
069dcb5
nitpick
patrickamadeus Mar 19, 2024
1094c1c
nitpick: config builder naming
patrickamadeus Mar 19, 2024
6d227cd
fix: nitpick naming a bit
patrickamadeus Mar 19, 2024
b4a61c6
Merge remote-tracking branch 'upstream/master' into alorese
patrickamadeus Mar 28, 2024
1bffd96
nitpick PR: formatting, abs import, invalid schema handler
patrickamadeus Mar 28, 2024
874f856
docs: add docstring scraping approach
patrickamadeus Mar 28, 2024
a413ea5
Merge branch 'master' into alorese
patrickamadeus Apr 2, 2024
fa7f8b5
fix: add URL scrape timestamp, revise code formatting, citation
patrickamadeus Apr 2, 2024
f22e93b
nitpick year
patrickamadeus Apr 2, 2024
7bfca50
nitpick review
patrickamadeus Apr 13, 2024
2fa1a1e
Merge branch 'master' into alorese
patrickamadeus Apr 20, 2024
4876bf8
fix: revise schema and remove subset
patrickamadeus Apr 20, 2024
bef5cb2
nitpick formatting
patrickamadeus Apr 23, 2024
0677796
Update seacrowd/sea_datasets/alorese/alorese.py
patrickamadeus Apr 26, 2024
118063a
Update alorese.py
sabilmakbar Apr 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
259 changes: 259 additions & 0 deletions seacrowd/sea_datasets/alorese/alorese.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
# coding=utf-8
# Copyright 2022 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.

"""
Alorese Corpus is a collection of language data in a couple of Alorese variation (Alor and Pantar Alorese). The collection is available in video, audio, and text formats with genres ranging from Experiment or task, Stimuli, Discourse, and Written materials.
"""
import xml.etree.ElementTree as ET
from typing import Dict, List, Tuple

import datasets
import pandas as pd

from seacrowd.sea_datasets.alorese.alorese_url import _URLS_DICT
from seacrowd.utils import schemas
from seacrowd.utils.configs import SEACrowdConfig
from seacrowd.utils.constants import Licenses, Tasks

_CITATION = """\
@article{Moro2018-ms,
title = "The plural word hire in alorese: Contact-induced change from
neighboring Alor-pantar languages",
author = "Moro, Francesca R",
journal = "Ocean. Linguist.",
publisher = "Project MUSE",
patrickamadeus marked this conversation as resolved.
Show resolved Hide resolved
volume = 57,
number = 1,
pages = "177--198",
year = 2018,
language = "en"
}
"""

_DATASETNAME = "alorese"
_DESCRIPTION = """\
Alorese Corpus is a collection of language data in a couple of Alorese variation (Alor and Pantar Alorese). The collection is available in video, audio, and text formats with genres ranging from Experiment or task, Stimuli, Discourse, and Written materials.
"""
_HOMEPAGE = " https://hdl.handle.net/1839/e10d7de5-0a6d-4926-967b-0a8cc6d21fb1"
_LANGUAGES = ["aol", "ind"]
_LICENSE = Licenses.UNKNOWN.value
_LOCAL = False

_URLS = _URLS_DICT

_SUPPORTED_TASKS = [Tasks.SPEECH_RECOGNITION, Tasks.MACHINE_TRANSLATION]

_SOURCE_VERSION = "1.0.0"
_SEACROWD_VERSION = "1.0.0"


class AloreseDataset(datasets.GeneratorBasedBuilder):
"""Alorese Corpus is a collection of language data in a couple of Alorese variation (Alor and Pantar Alorese). The collection is available in video, audio, and text formats with genres ranging from Experiment or task, Stimuli, Discourse, and Written materials."""

SUBSETS = ["t2t", "sptext", "sptext_trans"]

BUILDER_CONFIGS = [
SEACrowdConfig(
name=f"{_DATASETNAME}_{subset}_source",
version=datasets.Version(_SOURCE_VERSION),
description=f"{_DATASETNAME} source schema for {subset} subset",
schema="source",
subset_id=f"{_DATASETNAME}_{subset}",
)
for subset in SUBSETS
] + [
SEACrowdConfig(
name=f"{_DATASETNAME}_t2t_seacrowd_t2t",
version=datasets.Version(_SEACROWD_VERSION),
description=f"{_DATASETNAME} SEACrowd schema for t2t subset",
schema=f"seacrowd_t2t",
subset_id=f"{_DATASETNAME}_t2t",
),
SEACrowdConfig(
name=f"{_DATASETNAME}_sptext_seacrowd_sptext",
version=datasets.Version(_SEACROWD_VERSION),
description=f"{_DATASETNAME} SEACrowd schema for sptext subset",
schema=f"seacrowd_sptext",
subset_id=f"{_DATASETNAME}_sptext",
),
SEACrowdConfig(
name=f"{_DATASETNAME}_sptext_trans_seacrowd_sptext",
version=datasets.Version(_SEACROWD_VERSION),
description=f"{_DATASETNAME} SEACrowd schema for sptext_trans subset",
schema=f"seacrowd_sptext",
subset_id=f"{_DATASETNAME}_sptext_trans",
),
]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may I ask few qns here?

  1. overall, the schema we will use in here are T2T for translation of transcription of same audio from Indonesian to Alorese, and the SPText is the ASR version, right?

  2. Which language does the audio content has? Indonesian or Alorese? bcs when I looked the code, there's no clear way to tell which lang is available in the transcription (and it's crucial to correctly map the Audio to the correct Transcriptions)

  3. For the config name below:
    {_DATASETNAME} _sptext_seacrowd_sptext and {_DATASETNAME}_t2t_seacrowd_t2t, may I know why the naming isn't something like {_DATASETNAME}_seacrowd_sptext and {_DATASETNAME}_seacrowd_t2t? any justifications here?

  4. I saw in the source, the info on speaker_id went missing? I thought source config should include all columns and informations (and stitch them appropriately if the data scattered into different files and configs, like what you did in your code)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @sabilmakbar , thanks for the review and questions!

  1. Yes
  2. The audio is in alorese, the lang available in transcription is denoted by either sptext for alorese or sptext_trans for indonesian. The mapping is done in this part
image
  1. Because of the subset naming, the testing code (and dataset naming format) that was constructed needs to be in <DATASET_NAME>_<SUBSET_NAME>_seacrowd_`. It's just the subset name happens to be same as the schema, so it might be confusing. Happy to change if it might be needed.

  2. Thanks for the input! Please review the latest commit as I have done the nitpick.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the prompt reply, @patrickamadeus!

The audio is in alorese, the lang available in transcription is denoted by either sptext for alorese or sptext_trans for indonesian. The mapping is done in this part

Idk whether we need to create a schema of alorese audio against Indonesian text for the SPText schema. my personal opinion is to remove it since it could be very misleading (ASR schema should provide a text which is an actual transcription as is, not the translated ones)

Because of the subset naming, the testing code (and dataset naming format) that was constructed needs to be in <DATASET_NAME>_<SUBSET_NAME>seacrowd`. It's just the subset name happens to be same as the schema, so it might be confusing. Happy to change if it might be needed.

If I understand this correctly, the subsets for this dataset are only the Alorese version and Indonesian version. The SPText and T2T don't fit properly to the definition of "subset" of a dataset, as only the schema is different.

wdyt? @ljvmiranda921 @holylovenia @SamuelCahyawijaya

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, hmm, does it mean we should have a separate data loaders for the SPText and the T2T versions? I don't have a particularly strong opinion on any approach.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, hmm, does it mean we should have a separate data loaders for the SPText and the T2T versions?

No, we can have it in a single dataloader; I think the subset and configuration naming should be modified slightly.

The audio is in alorese, the lang available in transcription is denoted by either sptext for alorese or sptext_trans for indonesian.

The text information provided in this dataset is sequential; i.e., for every audio file, there is a sequence of annotated texts with their start & end timestamps.

For the Alorese language of text and audio, this can be put in ASR schema (or even a sequential audio split to its annotated text if we want to refine it further).

However, I don't think we should create an ASR schema for the Alorese audio and translated Indonesian annotation since the audio and text are in different languages.

And for T2T of Alorese and Indonesian translation, the existing implementation is correct, just need to reconstruct the configs list.

Therefore, my proposed configs are:

  1. Source -- containing Alorese Audio, Alorese Annotation (and its timestamp), and Indonesian Annotation (and its timestamp too)
  2. SPText -- containing Alorese Audio & its Annotation (the annotation could be combined into single text per audio -- as previously implemented or recreated using new sequenced schema of text)
  3. T2T -- containing Alorese Annotation & translated Indonesian Annotation (we can leave this as full-text translation, not word-to-word or phrase-to-phrase translation)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @sabilmakbar's suggestion.

Btw, just confirming, do the audio recordings and the transcriptions match word-for-word, @patrickamadeus?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Thanks for the suggestions @sabilmakbar !

Btw, just confirming, do the audio recordings and the transcriptions match word-for-word?

Yes! There are multiple timestamps to indicate when each word is spoken. @holylovenia , to be honest I haven't reviewed substantial sample from the data to determine whether it matched word-for-word or no, but as I listened to the first 10 seconds of 1 particular example, it perfectly matched.

If there is no further suggestion or comment, I will implement the change maximum this weekend, got a bunch of stuff to do first.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @sabilmakbar ! Could you please check on the latest commit? I have done the revision 👍.

SPText -- containing Alorese Audio & its Annotation (the annotation could be combined into single text per audio -- as previously implemented or recreated using new sequenced schema of text)

For this one, I went on using the previous implementation first.


DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_source"

def _info(self) -> datasets.DatasetInfo:

if self.config.schema == "source":
features = datasets.Features(
{
"nr": datasets.Value("int64"),
"media_id": datasets.Value("string"),
"audio": datasets.Audio(sampling_rate=16000),
"annotation_aol": datasets.Value("string"),
"annotation_ind": datasets.Value("string"),
"begin_time": datasets.Value("int64"),
"end_time": datasets.Value("int64"),
}
)

elif "seacrowd_sptext" in self.config.schema:
features = schemas.speech_text_features

elif self.config.schema == "seacrowd_t2t":
features = schemas.text2text_features

else:
raise ValueError(f"Invalid config schema: {self.config.schema}")

patrickamadeus marked this conversation as resolved.
Show resolved Hide resolved
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)

def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
if self.config.schema == "source":
paths = dl_manager.download(_URLS)
else:
if "t2t" in self.config.subset_id:
filepath = {k: v["text_path"] for k, v in _URLS.items()}
paths = dl_manager.download(filepath)
else:
paths = dl_manager.download(_URLS)

return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"filepath": paths,
},
),
]

def _generate_examples(self, filepath) -> Tuple[int, Dict]:

if self.config.schema == "source":
source_df = self._get_source_df(filepath)

for k, row in source_df.iterrows():
yield k, {
"nr": k + 1,
"media_id": row["media_id"],
"audio": row["audio_path"],
"annotation_aol": row["annotation_aol"],
"annotation_ind": row["annotation_ind"],
"begin_time": row["begin_time"],
"end_time": row["end_time"],
}

elif "seacrowd" in self.config.schema:
if "t2t" in self.config.subset_id:
caption_df = self._merge_text_dfs(filepath)

for k, row in caption_df.iterrows():
yield k, {
"id": k + 1,
"text_1": row["annotation_aol"],
"text_2": row["annotation_ind"],
"text_1_name": _LANGUAGES[0],
"text_2_name": _LANGUAGES[1],
}
elif "sptext" in self.config.subset_id:
annot_lang = "annotation_aol" if self.config.subset_id.split("_")[-1] == "sptext" else "annotation_ind"
sptext_df = self._get_sptext_df(filepath)

for k, row in sptext_df.iterrows():
yield k, {"id": k + 1, "path": row["audio_path"], "audio": row["audio_path"], "text": row[annot_lang], "speaker_id": row["speaker_id"], "metadata": {"speaker_age": None, "speaker_gender": None}}
patrickamadeus marked this conversation as resolved.
Show resolved Hide resolved

def _get_time_df(self, xml_tree) -> pd.DataFrame:
time_slot_values = [(time_slot.attrib["TIME_SLOT_ID"], int(time_slot.attrib["TIME_VALUE"])) for time_slot in xml_tree.iter(tag="TIME_SLOT")]

return pd.DataFrame({"time_slot_id": [v[0] for v in time_slot_values], "time_value": [v[1] for v in time_slot_values]})

def _get_aol_annotations(self, xml_tree) -> pd.DataFrame:
aol_annotations = [(annotation.attrib["ANNOTATION_ID"], annotation.attrib["TIME_SLOT_REF1"], annotation.attrib["TIME_SLOT_REF2"], annotation.find("ANNOTATION_VALUE").text) for annotation in xml_tree.iter(tag="ALIGNABLE_ANNOTATION")]

return pd.DataFrame({"annotation_id": [v[0] for v in aol_annotations], "time_slot_ref1": [v[1] for v in aol_annotations], "time_slot_ref2": [v[2] for v in aol_annotations], "annotation_value": [v[3] for v in aol_annotations]})

def _get_ind_annotations(self, xml_tree) -> pd.DataFrame:
ind_annotations = [(annotation.attrib["ANNOTATION_ID"], annotation.attrib["ANNOTATION_REF"], annotation.find("ANNOTATION_VALUE").text) for annotation in xml_tree.iter(tag="REF_ANNOTATION")]

return pd.DataFrame({"annotation_id": [v[0] for v in ind_annotations], "annotation_ref_id": [v[1] for v in ind_annotations], "annotation_value": [v[2] for v in ind_annotations]})

def _get_text_df(self, xml_tree) -> pd.DataFrame:
time_df = self._get_time_df(xml_tree)
aol_df = self._get_aol_annotations(xml_tree)
ind_df = self._get_ind_annotations(xml_tree)

df1 = aol_df.merge(time_df, left_on="time_slot_ref1", right_on="time_slot_id", how="left").rename(columns={"time_value": "begin_time", "annotation_value": "annotation_aol"}).drop(columns=["time_slot_ref1", "time_slot_id"])
df2 = df1.merge(time_df, left_on="time_slot_ref2", right_on="time_slot_id", how="left").rename(columns={"time_value": "end_time"}).drop(columns=["time_slot_ref2", "time_slot_id"])
final_df = df2.merge(ind_df, left_on="annotation_id", right_on="annotation_ref_id", how="left").rename(columns={"annotation_value": "annotation_ind"}).drop(columns=["annotation_ref_id", "annotation_id_y", "annotation_id_x"])

return final_df[["annotation_aol", "annotation_ind", "begin_time", "end_time"]]

def _merge_text_dfs(self, xml_dict) -> pd.DataFrame:
final_df = pd.DataFrame()
len_tracker = []
media_ids = []

xml_trees = [ET.parse(xml_path) for xml_path in xml_dict.values()]
for xml_tree in xml_trees:
cur_df = self._get_text_df(xml_tree)
final_df = pd.concat([final_df, cur_df], axis=0)
len_tracker.append(len(cur_df))

media_id_list = list(xml_dict.keys())
for i in range(len(len_tracker)):
media_ids.extend([media_id_list[i]] * len_tracker[i])

final_df["media_id"] = media_ids

return final_df.reset_index()

def _groupby_caption_by_media_ids(self, caption_df: pd.DataFrame) -> pd.DataFrame:
caption_df = (
caption_df.groupby("media_id")
.agg({"annotation_aol": lambda x: " ".join([value if value is not None else "<NONE>" for value in x]), "annotation_ind": lambda x: " ".join([value if value is not None else "<NONE>" for value in x])})
patrickamadeus marked this conversation as resolved.
Show resolved Hide resolved
.reset_index()
)
return caption_df

def _get_sptext_df(self, complete_dict) -> pd.DataFrame:
xml_dict = {k: v["text_path"] for k, v in complete_dict.items()}

audio_df = pd.DataFrame({"media_id": [k for k in complete_dict.keys()], "speaker_id": [k.split("_")[-1] for k in complete_dict.keys()], "audio_path": [v["audio_path"] for v in complete_dict.values()]})
caption_df = self._groupby_caption_by_media_ids(self._merge_text_dfs(xml_dict))

df = caption_df.merge(audio_df, on="media_id", how="inner")

return df[["media_id", "speaker_id", "audio_path", "annotation_aol", "annotation_ind"]]

def _get_source_df(self, complete_dict) -> pd.DataFrame:
xml_dict = {k: v["text_path"] for k, v in complete_dict.items()}

audio_df = pd.DataFrame({"media_id": [k for k in complete_dict.keys()], "audio_path": [v["audio_path"] for v in complete_dict.values()]})
text_df = self._merge_text_dfs(xml_dict)

df = text_df.merge(audio_df, on="media_id", how="inner")

return df[["media_id", "audio_path", "annotation_aol", "annotation_ind", "begin_time", "end_time"]]
Loading