Skip to content

armgit / clipy

Public Imported from GitHub
0

The AI-native screen recorder. Record your screen and talk; the people you share it with and your AI agents both get it.

Merge upstream/main into fix/transcript-word-timing

Pushkar0c5afa2acdc9
diff --cc desktop/src-tauri/crates/clipy-local-stt/src/chunking.rs
index 7a7e94a,5392ad7..14b37b1
--- a/desktop/src-tauri/crates/clipy-local-stt/src/chunking.rs
+++ b/desktop/src-tauri/crates/clipy-local-stt/src/chunking.rs
@@@ -407,11 -405,73 +405,72 @@@ pub fn group_words_into_segments(words
.filter(|t| !t.is_empty())
.collect::<Vec<_>>()
.join(joiner);
- let text = tidy_punctuation(&joined).trim().to_string();
+ tidy_punctuation(&joined)
+ })
+ }
+
+ /// `group_words_into_segments` for a transcript in `language`: scripts written
+ /// without spaces between words (Japanese, Chinese, Thai, Lao, Khmer, Burmese)
+ /// take the spacing of the provider's display `transcript` when every word
+ /// matches it in order, and otherwise only a space between two Latin words
+ /// ("GitHub Actions"). Every other language is joined with spaces.
+ /// `groupWordsForLanguage` in packages/transcribe/src/chunking.ts.
+ pub fn group_words_for_language(
+ words: &[TimedWord],
+ language: Option<&str>,
+ transcript: Option<&str>,
+ ) -> Vec<RelativeSegment> {
+ if !is_unspaced_language(language) {
+ return group_words_into_segments(words, " ");
+ }
+ let aligned = transcript.and_then(|t| words_with_transcript_spacing(words, t));
+ let placed: Vec<Placed> = match &aligned {
+ Some(a) => a
+ .iter()
+ .map(|(word, before)| Placed {
+ word,
+ before: Some(before.as_str()),
+ })
+ .collect(),
+ None => words
+ .iter()
+ .map(|word| Placed { word, before: None })
+ .collect(),
+ };
+ group_placed(&placed, |cur| {
+ let mut out = String::new();
+ for (i, p) in cur.iter().enumerate() {
+ if i > 0 {
+ out.push_str(unspaced_separator(cur[i - 1], p));
+ }
+ out.push_str(&p.word.text);
+ }
+ out
+ })
+ }
+
+ /// A word and, when the display transcript gave it, the whitespace before it.
+ struct Placed<'w> {
+ word: &'w TimedWord,
+ before: Option<&'w str>,
+ }
+
+ fn group_placed<'w>(
+ words: &[Placed<'w>],
+ join: impl Fn(&[&Placed<'w>]) -> String,
+ ) -> Vec<RelativeSegment> {
+ let mut segs = Vec::new();
+ let mut cur: Vec<&Placed<'w>> = Vec::new();
- let flush = |cur: &mut Vec<&Placed<'w>>, segs: &mut Vec<RelativeSegment>| {
++ let mut segment_end = 0.0_f64;
++ let flush = |cur: &mut Vec<&Placed<'w>>, segment_end: f64, segs: &mut Vec<RelativeSegment>| {
+ if cur.is_empty() {
+ return;
+ }
+ let text = join(cur).trim().to_string();
if !text.is_empty() {
- let first = cur[0].word;
- let last = cur[cur.len() - 1].word;
segs.push(RelativeSegment {
- start: cur[0].start,
- start: first.start,
- end: last.end.max(first.start),
++ start: cur[0].word.start,
+ end: segment_end,
text,
});
}
@@@ -421,24 -482,19 +481,25 @@@
if w.text.trim().is_empty() {
continue;
}
- if let Some(last) = cur.last() {
- if w.start - last.word.end > SEGMENT_GAP_SEC
- || w.end - cur[0].word.start > SEGMENT_MAX_SEC
- {
- flush(&mut cur, &mut segs);
- }
+ if !cur.is_empty()
- && (w.start - segment_end > SEGMENT_GAP_SEC || w.end - cur[0].start > SEGMENT_MAX_SEC)
++ && (w.start - segment_end > SEGMENT_GAP_SEC
++ || w.end - cur[0].word.start > SEGMENT_MAX_SEC)
+ {
+ flush(&mut cur, segment_end, &mut segs);
}
+ // Overlapping word timestamps can end out of order.
+ let word_end = w.start.max(w.end);
+ segment_end = if cur.is_empty() {
+ word_end
+ } else {
+ segment_end.max(word_end)
+ };
- cur.push(w);
- if ends_sentence(w.text.trim()) && w.end - cur[0].start >= 3.0 {
+ cur.push(p);
+ if ends_sentence(w.text.trim()) && w.end - cur[0].word.start >= 3.0 {
- flush(&mut cur, &mut segs);
+ flush(&mut cur, segment_end, &mut segs);
}
}
- flush(&mut cur, &mut segs);
+ flush(&mut cur, segment_end, &mut segs);
segs
}
diff --cc packages/transcribe/src/chunking.ts
index 855d6dc,e62540f..800b032
--- a/packages/transcribe/src/chunking.ts
+++ b/packages/transcribe/src/chunking.ts
@@@ -402,7 -440,7 +440,8 @@@ export function groupWordsIntoSegments(
const segs: RelativeSegment[] = [];
let cur: TimedWord[] = [];
let curSpeaker: string | undefined;
+ let segmentEnd = 0;
+ let sentenceEnded = false;
const flush = () => {
if (cur.length === 0) return;
const words = cur.map((w) => ({ start: w.start, end: Math.max(w.end, w.start), text: w.text.trim() })).filter((w) => w.text);
@@@ -428,23 -466,36 +467,39 @@@
}
cur = [];
curSpeaker = undefined;
+ sentenceEnded = false;
};
for (const w of words) {
- if (!w.text.trim()) continue;
- if (
- cur.length > 0 &&
- (w.start - segmentEnd > SEGMENT_GAP_SEC ||
+ const trimmed = w.text.trim();
+ if (!trimmed) continue;
+ const last = cur[cur.length - 1];
+ const wouldSplit =
+ last !== undefined &&
+ (sentenceEnded ||
- w.start - last.end > SEGMENT_GAP_SEC ||
++ w.start - segmentEnd > SEGMENT_GAP_SEC ||
w.end - cur[0].start > SEGMENT_MAX_SEC ||
- w.speaker !== curSpeaker)
- ) {
+ w.speaker !== curSpeaker);
+ if (wouldSplit) {
+ // Closing/trailing punctuation never starts a new segment: models often
+ // emit a mark after the pause, length limit, or speaker boundary following its word.
+ if (!/[\p{L}\p{N}]/u.test(trimmed) && !OPENING_PUNCT.test(trimmed)) {
+ const mergedText = `${last.text.trim()}${trimmed}`;
+ cur[cur.length - 1] = { ...last, text: mergedText };
+ if (SENTENCE_END.test(mergedText) && last.end - cur[0].start >= 3) {
+ sentenceEnded = true;
+ }
+ continue;
+ }
flush();
}
curSpeaker = w.speaker;
+ // Overlapping word timestamps can end out of order.
+ const wordEnd = Math.max(w.start, w.end);
+ segmentEnd = cur.length === 0 ? wordEnd : Math.max(segmentEnd, wordEnd);
cur.push(w);
- if (SENTENCE_END.test(w.text.trim()) && w.end - cur[0].start >= 3) flush();
+ if (SENTENCE_END.test(trimmed) && w.end - cur[0].start >= 3) {
+ sentenceEnded = true;
+ }
}
flush();
return segs;