-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtranscript_state.py
More file actions
53 lines (44 loc) · 1.64 KB
/
Copy pathtranscript_state.py
File metadata and controls
53 lines (44 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
"""UI-side state for one dictation, plus transcript text helpers.
TranscriptState tracks what the app has shown and inserted (visible text,
final text and its source, correction target, LLM timing marks). All
transcription-side state and timings live in the Transcriber session
(transcriber.py). merge_transcript and longest_common_prefix are pure
helpers shared by the transcription and insertion modules."""
from dataclasses import dataclass
@dataclass
class TranscriptState:
visible_text: str = ""
final_text: str = ""
final_source: str = ""
first_visible_at: float = None
rate_limited: bool = False
reconcile_started_at: float = None
reconcile_finished_at: float = None
cleanup_started_at: float = None
cleanup_finished_at: float = None
correction_target: str = ""
def merge_transcript(base: str, incoming: str) -> str:
base = base.strip()
incoming = incoming.strip()
if not base:
return incoming
if not incoming:
return base
if incoming.startswith(base):
return incoming
if base.startswith(incoming):
return base
base_words = base.split()
incoming_words = incoming.split()
max_overlap = min(len(base_words), len(incoming_words))
for overlap in range(max_overlap, 0, -1):
if base_words[-overlap:] == incoming_words[:overlap]:
return " ".join(base_words + incoming_words[overlap:])
return f"{base} {incoming}".strip()
def longest_common_prefix(left: str, right: str) -> int:
limit = min(len(left), len(right))
idx = 0
while idx < limit and left[idx] == right[idx]:
idx += 1
return idx