| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import json |
| from pathlib import Path |
|
|
|
|
| def get_batch_starts_ends(manifest_filepath, batch_size): |
| """ |
| Get the start and end ids of the lines we will use for each 'batch'. |
| """ |
|
|
| with open(manifest_filepath, 'r') as f: |
| num_lines_in_manifest = sum(1 for _ in f) |
|
|
| starts = [x for x in range(0, num_lines_in_manifest, batch_size)] |
| ends = [x - 1 for x in starts] |
| ends.pop(0) |
| ends.append(num_lines_in_manifest) |
|
|
| return starts, ends |
|
|
|
|
| def is_entry_in_any_lines(manifest_filepath, entry): |
| """ |
| Returns True if entry is a key in any of the JSON lines in manifest_filepath |
| """ |
|
|
| entry_in_manifest = False |
|
|
| with open(manifest_filepath, 'r') as f: |
| for line in f: |
| data = json.loads(line) |
|
|
| if entry in data: |
| entry_in_manifest = True |
|
|
| return entry_in_manifest |
|
|
|
|
| def is_entry_in_all_lines(manifest_filepath, entry): |
| """ |
| Returns True is entry is a key in all of the JSON lines in manifest_filepath. |
| """ |
| with open(manifest_filepath, 'r') as f: |
| for line in f: |
| data = json.loads(line) |
|
|
| if entry not in data: |
| return False |
|
|
| return True |
|
|
|
|
| def get_manifest_lines_batch(manifest_filepath, start, end): |
| manifest_lines_batch = [] |
| with open(manifest_filepath, "r", encoding="utf-8-sig") as f: |
| for line_i, line in enumerate(f): |
| if line_i >= start and line_i <= end: |
| data = json.loads(line) |
| data["audio_filepath"] = get_full_path(data["audio_filepath"], manifest_filepath) |
| if "text" in data: |
| |
| |
| data["text"] = data["text"].replace("\ufeff", "") |
| data["text"] = " ".join(data["text"].split()) |
|
|
| |
| |
| |
| |
| data["text"] = data["text"].replace("\u2026", "...") |
|
|
| if not Path(data['audio_filepath']).exists(): |
| extended_path = Path(Path(manifest_filepath).parent, data['audio_filepath']) |
| if extended_path.exists(): |
| data['audio_filepath'] = str(extended_path) |
| else: |
| raise FileNotFoundError( |
| f"Audio file {data['audio_filepath']} not found in {manifest_filepath}" |
| ) |
|
|
| manifest_lines_batch.append(data) |
|
|
| if line_i == end: |
| break |
| return manifest_lines_batch |
|
|