fix(reader): preserve full context sentence with abbreviations (Mr., Mrs., etc.) and log bug fix

This commit is contained in:
Antigravity committed 2026-10-06 17:03:38 +03:00
1 parent 6a6b165c7a
commit 0ff4465d34
3 files changed
+40 -4

No files matched your search

+31 -3
View File
@@ -1070,11 +1070,39 @@ export function renderInteractiveSpans(container, text, contextText, knownWordsM
});
}
const READER_HONORIFICS = [
'Mr.', 'Mrs.', 'Ms.', 'Dr.', 'Prof.', 'St.', 'vs.', 'etc.', 'e.g.', 'i.e.', 'Jr.', 'Sr.',
'Capt.', 'Col.', 'Gen.', 'Lt.', 'Sgt.', 'Rev.', 'Hon.'
];
export function extractContextSentence(fullText, word) {
if (!fullText) return '';
const re = new RegExp(`([^.!?\\n]*?\\b${escapeRegex(word)}\\b[^.!?\\n]*[.!?]?)`, 'i');
const m = fullText.match(re);
if (m && m[1]) return m[1].trim();
let protectedText = fullText;
for (const h of READER_HONORIFICS) {
if (protectedText.toLowerCase().includes(h.toLowerCase())) {
const safe = h.replace(/\./g, '§DOT§');
const reH = new RegExp(escapeRegex(h), 'gi');
protectedText = protectedText.replace(reH, safe);
}
}
// Protect decimal numbers (e.g. 3.14, 1.5)
protectedText = protectedText.replace(/(\d+)\.(\d+)/g, '$1§DOT§$2');
// Split sentences on terminal punctuation (.!?) followed by optional quotes/brackets and whitespace
const marked = protectedText.replace(/([.!?]['"”’»)]*)\s+/g, '$1§SEP§');
const rawSentences = marked.split('§SEP§');
const cleanWord = (word || '').trim();
if (cleanWord) {
const escaped = escapeRegex(cleanWord);
const wordRe = new RegExp(`(?:^|\\W)${escaped}(?:$|\\W)`, 'i');
for (const s of rawSentences) {
if (wordRe.test(s)) {
return s.replace(/§DOT§/g, '.').trim();
}
}
}
return fullText.slice(0, 160).trim();
}