Skip to content

Instantly share code, notes, and snippets.

@RyanJarv
Created October 30, 2022 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyanJarv/a67f3e751f07f2cee5af3e4931aab68e to your computer and use it in GitHub Desktop.
Save RyanJarv/a67f3e751f07f2cee5af3e4931aab68e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import json
from dataclasses import dataclass
from pathlib import Path
from typing import List, Dict, Literal, Union, Optional
import typer
# # "start_time": "1749.91",
# # "speaker_label": "spk_0",
# # "end_time": "1760.26",
# # "items": [
# # {
# # "start_time": "1749.91",
# # "speaker_label": "spk_0",
# # "end_time": "1750.55"
# # },
# @dataclass
# class Segments:
# start_time: str
# speaker_label: str
# end_time: str
# items: List[Dict[str, str]]
# "start_time": "1759.77",
# "speaker_label": "spk_0",
# "end_time": "1760.25",
# "alternatives": [
# {
# "confidence": "0.9997",
# "content": "cinco"
# }
# ],
# "type": "pronunciation"
@dataclass
class Item:
alternatives: List[Dict[str, str]]
type: Literal["pronunciation", "punctuation"]
start_time: Optional[str] = None
speaker_label: Optional[str] = None
end_time: Optional[str] = None
def main(path: Path):
j = json.loads(path.read_text())
items = j['results']['items']
current_speaker = None
for i in items:
item = Item(**i)
if item.speaker_label and item.speaker_label != current_speaker:
current_speaker = item.speaker_label
print(f"\n\n{current_speaker}")
if item.type == 'pronunciation':
print(' ', end='')
print(item.alternatives[0]['content'], end='')
if __name__ == "__main__":
typer.run(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment