Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(stt): stop a sentence end's punctuation scan at an opening bracket

EtienneLescot57684a9d419d
diff --git a/electron/stt/snapWordBoundaries.test.ts b/electron/stt/snapWordBoundaries.test.ts
index 62d48cf12..f3272b38d 100644
--- a/electron/stt/snapWordBoundaries.test.ts
+++ b/electron/stt/snapWordBoundaries.test.ts
@@ -138,6 +138,21 @@ describe("anchorWordsOnSpeech", () => {
]);
});
+ it("does not read an opening bracket after a sentence's last word as part of its end", () => {
+ // "b." is anchored in the pause, then whisper split "(" off the next word:
+ // "b.(" is no sentence end, so "b." would have opened the next stretch.
+ const words = [
+ word("a", 28.2, 30.03, 30.03),
+ word("b.", 30.03, 30.69, 30.69),
+ word("(", 30.69, 31.4, 31.4),
+ word("c)", 31.4, 32, 32),
+ ];
+ expect(ownWords(words, pause)).toEqual([
+ [0, 2],
+ [2, 4],
+ ]);
+ });
+
it("lets a one-word sentence open a stretch when it is anchored in its speech", () => {
const words = [
word("Agreed?", 28.13, 30.5, 30.4),
diff --git a/electron/stt/snapWordBoundaries.ts b/electron/stt/snapWordBoundaries.ts
index 7998a0f8e..0cbcb6991 100644
--- a/electron/stt/snapWordBoundaries.ts
+++ b/electron/stt/snapWordBoundaries.ts
@@ -49,6 +49,9 @@ const TAIL_SEC = 0.1;
/** French puts a space before `!`, `?`, `:` and `;`, so whisper emits them as words. */
const isPunctuation = (word: string) => /^[\p{P}\p{S}]+$/u.test(word);
+/** An opening bracket or quote starts what follows, so it never closes a sentence. */
+const opens = (word: string) => /^[\p{Ps}\p{Pi}]+$/u.test(word);
+
/** `.`, `!`, `?`, `…` or a full-width form, maybe inside a closing quote or bracket. */
const endsSentence = (text: string) => /[.!?…。!?.。][\p{Pe}\p{Pf}"']*$/u.test(text);
@@ -85,7 +88,7 @@ export function ownWords(
let first = k;
while (first < words.length && isPunctuation(words[first].word)) first++;
let end = first + 1;
- while (end < words.length && isPunctuation(words[end].word)) end++;
+ while (end < words.length && isPunctuation(words[end].word) && !opens(words[end].word)) end++;
if (
i + 1 < speech.length &&
first < words.length &&