import datasets import os import glob import pandas as pd from pydub import AudioSegment # AudioSegment for reading TextGrid and wav files import textgrid # You might need to install this: pip install praat-textgrids _DESCRIPTION = """ A custom dataset combining L2-ARCTIC and SpeechOcean for L2 English speech analysis. It includes non-native English speech from various L1s, transcripts, and phoneme alignments (from TextGrid). """ _HOMEPAGE = "https://example.com/your_dataset_homepage" # データセットのホームページURL(任意) _LICENSE = "Creative Commons Attribution 4.0 International Public License (CC-BY-4.0)" # 適切なライセンスを記述 _L2_ARCTIC_SPEAKERS = ["ABA", "HJK", "MBMPS", "TXHC", "YBAA"] # L2-ARCTICのスピーカーIDリスト(一部のみ) _L2_ARCTIC_L1_MAP = { # L2-ARCTICのL1情報(例、実際は公式ドキュメントで確認) "ABA": "Hindi", "HJK": "Korean", "MBMPS": "Mandarin", "TXHC": "Spanish", "YBAA": "Arabic" } class MyL2SpeechDataset(datasets.GeneratorBasedBuilder): VERSION = datasets.Version("1.0.0") def _info(self): # ここでデータセットの構造とフィーチャーを定義します features = datasets.Features({ "audio": datasets.Audio(sampling_rate=16_000), # 音声データとサンプリングレート "text": datasets.Value("string"), # 読み上げられたテキスト "speaker_id": datasets.Value("string"), # 話者ID "l1": datasets.Value("string"), # 話者の第一言語 (L1) "dataset_source": datasets.Value("string"), # データソース (l2_arctic or speechocean) "phoneme_alignment": datasets.Sequence( # 音素アライメント情報 (TextGridからパース) { "phoneme": datasets.Value("string"), "start_time": datasets.Value("float"), "end_time": datasets.Value("float"), } ), "pronunciation_score": datasets.Value("float"), # SpeechOceanのスコアなど }) return datasets.DatasetInfo( description=_DESCRIPTION, features=features, homepage=_HOMEPAGE, license=_LICENSE, ) def _split_generators(self, dl_manager): # ここでデータセットのファイルをダウンロードしたり、ローカルパスを指定したりします # この例では、ローカルにデータセットが配置されていることを前提とします。 # dl_manager.download() を使ってURLからダウンロードすることも可能です。 # ローカルパス(datasetsディレクトリの絶対パスを指定するか、スクリプトからの相対パスにする) # 例: data_dir = "/path/to/your/datasets" #data_dir = os.path.abspath("datasets") # 現在のスクリプトからの相対パスで datasets フォルダを探す # L2-ARCTICのパス l2_arctic_path = os.path.join("l2_arctic") # SpeechOceanのパス speechocean_path = os.path.join( "speechocean") return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, # train, validation, testなど、スプリットを定義 gen_kwargs={ "l2_arctic_path": l2_arctic_path, "speechocean_path": speechocean_path, } ) ] def _generate_examples(self, l2_arctic_path, speechocean_path): # ここで実際のデータを読み込み、Yield (生成) します # 各データポイントは、_info() で定義した features に対応するようにします # --- L2-ARCTIC データの処理 --- for speaker_id in _L2_ARCTIC_SPEAKERS: speaker_dir = os.path.join(l2_arctic_path, speaker_id) wav_dir = os.path.join(speaker_dir, "wav") transcript_dir = os.path.join(speaker_dir, "transcript") textgrid_dir = os.path.join(speaker_dir, "textgrid") # L2-ARCTICには'textgrid'フォルダもある # 音声ファイルとテキストファイルのマッチング wav_files = glob.glob(os.path.join(wav_dir, "*.wav")) for wav_file in wav_files: file_id = os.path.basename(wav_file).replace(".wav", "") transcript_file = os.path.join(transcript_dir, f"{file_id}.txt") textgrid_file = os.path.join(textgrid_dir, f"{file_id}.TextGrid") if not os.path.exists(transcript_file): print(f"Warning: Transcript file not found for {file_id}") continue if not os.path.exists(textgrid_file): print(f"Warning: TextGrid file not found for {file_id}") continue with open(transcript_file, "r", encoding="utf-8") as f: text = f.read().strip() phoneme_alignment_data = [] try: tg = textgrid.TextGrid.fromFile(textgrid_file) # TextGridの構造に応じて、音素情報をパースします # L2-ARCTICのTextGridがどのように音素情報を格納しているか確認が必要です # 例えば 'phones' という名前のTierがある場合 for tier_name in tg.tierNames(): if tier_name == 'phones': # 音素のTierを特定 phone_tier = tg.getFirst(tier_name) for interval in phone_tier: phoneme_alignment_data.append({ "phoneme": interval.mark, "start_time": interval.minTime, "end_time": interval.maxTime, }) break # 音素Tierを見つけたらループを抜ける except Exception as e: print(f"Error parsing TextGrid {textgrid_file}: {e}") phoneme_alignment_data = [] # エラー時は空リストに yield file_id, { # file_idをkeyとしてyield "audio": wav_file, # `Audio` Featureはパスを受け取る "text": text, "speaker_id": speaker_id, "l1": _L2_ARCTIC_L1_MAP.get(speaker_id, "unknown"), "dataset_source": "l2_arctic", "phoneme_alignment": phoneme_alignment_data, "pronunciation_score": -1.0, # L2-ARCTICには直接のスコアはないので-1 } # --- SpeechOcean データの処理 --- speechocean_wav_dir = os.path.join(speechocean_path, "wavs") scores_df = pd.read_csv(os.path.join(speechocean_path, "train_scores.csv")) for index, row in scores_df.iterrows(): file_id = row['file_name'].replace(".wav", "") wav_file = os.path.join(speechocean_wav_dir, row['file_name']) # SpeechOceanのTextGridや詳細なアライメント情報が別途存在するか確認 # なければ、空のphoneme_alignmentとするか、MFAなどで後処理する yield f"so_{file_id}", { # 重複を避けるためプレフィックスを付ける "audio": wav_file, "text": row['text'], # SpeechOceanのCSVにtextカラムがあることを想定 "speaker_id": str(row['speaker_id']), # スピーカーIDを文字列に "l1": "Mandarin", # SpeechOcean762は主にMandarin話者 "dataset_source": "speechocean", "phoneme_alignment": [], # SpeechOceanにデフォルトでTextGridがなければ空 "pronunciation_score": row['score'], # スコアをそのまま使う }