Download check_short_sections.py from baniguru/speech_data_recordings: direct link, hf CLI and curl.
- Browser
- Download file 3.88 kB
-
https://huggingface.co/datasets/baniguru/speech_data_recordings/resolve/main/check_short_sections.py
- Command line
-
hf download hf://datasets/baniguru/speech_data_recordings/check_short_sections.py
-
curl -L -o check_short_sections.py https://huggingface.co/datasets/baniguru/speech_data_recordings/resolve/main/check_short_sections.py
3.88 kB
| #!/usr/bin/env python3 | |
| """Find transcript sections shorter than a duration threshold. | |
| Searches JSON files recursively. Supports transcript files with a top-level | |
| ``sections`` array, including ``aligned_transcript.json`` and | |
| ``transcript_sections.json``. | |
| Examples: | |
| python find_short_sections.py /path/to/root | |
| python find_short_sections.py /path/to/root --threshold 1.5 | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| def get_sections(payload: object) -> list[dict]: | |
| if isinstance(payload, dict): | |
| sections = payload.get("sections", []) | |
| elif isinstance(payload, list): | |
| sections = payload | |
| else: | |
| return [] | |
| if not isinstance(sections, list): | |
| return [] | |
| return [section for section in sections if isinstance(section, dict)] | |
| def main() -> int: | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument( | |
| "root", | |
| nargs="?", | |
| type=Path, | |
| default=Path.cwd(), | |
| help="Folder to search recursively (default: current folder)", | |
| ) | |
| parser.add_argument( | |
| "--threshold", | |
| type=float, | |
| default=0.7, | |
| metavar="SECONDS", | |
| help="Report sections shorter than this many seconds (default: 2)", | |
| ) | |
| args = parser.parse_args() | |
| root = args.root.expanduser().resolve() | |
| if not root.is_dir(): | |
| parser.error(f"root folder does not exist or is not a directory: {root}") | |
| if args.threshold <= 0: | |
| parser.error("--threshold must be greater than 0 seconds") | |
| json_files = sorted(root.rglob("*.json")) | |
| scanned_files = 0 | |
| examined_sections = 0 | |
| matches: list[tuple[Path, dict, float, float, float]] = [] | |
| errors: list[tuple[Path, str]] = [] | |
| for json_path in json_files: | |
| if any(part.startswith(".") for part in json_path.relative_to(root).parts): | |
| continue | |
| try: | |
| payload = json.loads(json_path.read_text(encoding="utf-8-sig")) | |
| except (OSError, json.JSONDecodeError) as exc: | |
| errors.append((json_path, str(exc))) | |
| continue | |
| sections = get_sections(payload) | |
| if not sections: | |
| continue | |
| scanned_files += 1 | |
| for section in sections: | |
| try: | |
| start = float(section["start_time"]) | |
| end = float(section["end_time"]) | |
| except (KeyError, TypeError, ValueError): | |
| continue | |
| examined_sections += 1 | |
| duration = end - start | |
| if duration < args.threshold: | |
| matches.append((json_path, section, start, end, duration)) | |
| matches.sort(key=lambda item: (str(item[0]).casefold(), item[2], item[3])) | |
| if matches: | |
| for json_path, section, start, end, duration in matches: | |
| try: | |
| display_path = json_path.relative_to(root) | |
| except ValueError: | |
| display_path = json_path | |
| transcript = " ".join(str(section.get("transcript", "")).split()) | |
| print( | |
| f"{display_path}\n" | |
| f" section_id: {section.get('section_id', '(no section_id)')}\n" | |
| f" duration: {duration:.3f}s\n" | |
| f" start: {start:.3f}\n" | |
| f" end: {end:.3f}\n" | |
| f" transcript: {transcript}\n" | |
| ) | |
| else: | |
| print(f"No sections shorter than {args.threshold:g} seconds found.") | |
| print(f"Files with sections: {scanned_files}") | |
| print(f"Sections checked: {examined_sections}") | |
| print(f"Sections shorter than {args.threshold:g}s: {len(matches)}") | |
| if errors: | |
| print(f"Unreadable JSON files: {len(errors)}") | |
| for path, error in errors: | |
| print(f" {path.relative_to(root)}: {error}") | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |