Download process_textgrid.py from ASLP-lab/WSC-Eval: direct link, hf CLI and curl.
- Browser
- Download file 1.78 kB
-
https://huggingface.co/datasets/ASLP-lab/WSC-Eval/resolve/886ca5f1b994b0a54d54ac368b8a90073073ba8f/process_textgrid.py
- Command line
-
hf download hf://datasets/ASLP-lab/WSC-Eval@886ca5f1b994b0a54d54ac368b8a90073073ba8f/process_textgrid.py
-
curl -L -o process_textgrid.py https://huggingface.co/datasets/ASLP-lab/WSC-Eval/resolve/886ca5f1b994b0a54d54ac368b8a90073073ba8f/process_textgrid.py
1.78 kB
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| import re | |
| from pathlib import Path | |
| def extract_first_item_text(textgrid_file): | |
| """提取第一个item的文本(直接拼接,不留空格)""" | |
| with open(textgrid_file, 'r', encoding='utf-8') as f: | |
| content = f.read() | |
| # 提取item [1]的全部内容 | |
| first_item_match = re.search(r'item\s*\[1\]:.*?intervals\s*:\s*size\s*=\s*\d+(.*?)(?=item\s*\[2\]|\Z)', | |
| content, re.DOTALL) | |
| if not first_item_match: | |
| return "" | |
| # 提取所有非空text字段 | |
| texts = re.findall(r'text\s*=\s*"(.*?)"', first_item_match.group(1)) | |
| return "".join([t for t in texts if t.strip()]) # 直接拼接,不留空格 | |
| def process_textgrids(textgrid_dir, wav_dir, output_dir): | |
| """处理目录下所有TextGrid文件""" | |
| textgrid_dir = Path(textgrid_dir) | |
| wav_dir = Path(wav_dir) | |
| output_dir = Path(output_dir) | |
| output_dir.mkdir(parents=True, exist_ok=True) | |
| with open(output_dir/'wav.scp', 'w', encoding='utf-8') as f_wav, \ | |
| open(output_dir/'text', 'w', encoding='utf-8') as f_text: | |
| for textgrid_file in textgrid_dir.glob('*.TextGrid'): | |
| audio_id = textgrid_file.stem | |
| transcript = extract_first_item_text(textgrid_file) | |
| # 写入wav.scp | |
| wav_path = wav_dir/f"{audio_id}.wav" | |
| f_wav.write(f"{audio_id} {wav_path}\n") | |
| # 写入text(直接拼接的文本) | |
| f_text.write(f"{audio_id} {transcript}\n") | |
| if __name__ == "__main__": | |
| textgrid_dir = "" | |
| wav_dir = "" | |
| output_dir = "" | |
| process_textgrids(textgrid_dir, wav_dir, output_dir) | |
| print(f"处理完成!输出目录: {output_dir}") | |