Delete files process_textgrid.py with huggingface_hub
Browse files- process_textgrid.py +0 -50
process_textgrid.py
DELETED
|
@@ -1,50 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
# -*- coding: utf-8 -*-
|
| 3 |
-
|
| 4 |
-
import re
|
| 5 |
-
from pathlib import Path
|
| 6 |
-
|
| 7 |
-
def extract_first_item_text(textgrid_file):
|
| 8 |
-
"""提取第一个item的文本(直接拼接,不留空格)"""
|
| 9 |
-
with open(textgrid_file, 'r', encoding='utf-8') as f:
|
| 10 |
-
content = f.read()
|
| 11 |
-
|
| 12 |
-
# 提取item [1]的全部内容
|
| 13 |
-
first_item_match = re.search(r'item\s*\[1\]:.*?intervals\s*:\s*size\s*=\s*\d+(.*?)(?=item\s*\[2\]|\Z)',
|
| 14 |
-
content, re.DOTALL)
|
| 15 |
-
if not first_item_match:
|
| 16 |
-
return ""
|
| 17 |
-
|
| 18 |
-
# 提取所有非空text字段
|
| 19 |
-
texts = re.findall(r'text\s*=\s*"(.*?)"', first_item_match.group(1))
|
| 20 |
-
return "".join([t for t in texts if t.strip()]) # 直接拼接,不留空格
|
| 21 |
-
|
| 22 |
-
def process_textgrids(textgrid_dir, wav_dir, output_dir):
|
| 23 |
-
"""处理目录下所有TextGrid文件"""
|
| 24 |
-
textgrid_dir = Path(textgrid_dir)
|
| 25 |
-
wav_dir = Path(wav_dir)
|
| 26 |
-
output_dir = Path(output_dir)
|
| 27 |
-
|
| 28 |
-
output_dir.mkdir(parents=True, exist_ok=True)
|
| 29 |
-
|
| 30 |
-
with open(output_dir/'wav.scp', 'w', encoding='utf-8') as f_wav, \
|
| 31 |
-
open(output_dir/'text', 'w', encoding='utf-8') as f_text:
|
| 32 |
-
|
| 33 |
-
for textgrid_file in textgrid_dir.glob('*.TextGrid'):
|
| 34 |
-
audio_id = textgrid_file.stem
|
| 35 |
-
transcript = extract_first_item_text(textgrid_file)
|
| 36 |
-
|
| 37 |
-
# 写入wav.scp
|
| 38 |
-
wav_path = wav_dir/f"{audio_id}.wav"
|
| 39 |
-
f_wav.write(f"{audio_id} {wav_path}\n")
|
| 40 |
-
|
| 41 |
-
# 写入text(直接拼接的文本)
|
| 42 |
-
f_text.write(f"{audio_id} {transcript}\n")
|
| 43 |
-
|
| 44 |
-
if __name__ == "__main__":
|
| 45 |
-
textgrid_dir = ""
|
| 46 |
-
wav_dir = ""
|
| 47 |
-
output_dir = ""
|
| 48 |
-
|
| 49 |
-
process_textgrids(textgrid_dir, wav_dir, output_dir)
|
| 50 |
-
print(f"处理完成!输出目录: {output_dir}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|