diff --git a/CHANGELOG.md b/CHANGELOG.md index c138cf3..7ee72d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Plugin system for custom pipelines +## [1.16.5] - 2026-11-06 + +> Transcript polish now guards what the model must not break, measured against the local model. + +### Changed +- Polish instructions were rewritten to the wording measured best on the local model: fillers may only be removed from a fixed list, meaning qualifiers such as 거의/대부분/아직 must stay, a word that makes the sentence fall apart (for example 유치역학 → 유체역학) should be repaired, and speaker labels or timestamps that are not in the input must not be invented. +- Comma-separated fragments from speech are joined into sentences, and one prompt is shared by the app, the tests and the verification probe. + +### Fixed +- The model could add a `[00:00]` or `[화자 1]` marker that was never in the audio; these are now stripped from the saved transcript, so timestamps and speakers come only from recognition and diarization. +- The model sometimes deleted meaning-bearing qualifiers (for example turning "사실 거의 근간" into "사실 근간"). Qualifiers the speaker actually said are restored from the original chunk. +- Unknown-word repair stays model-dependent (measured 2 of 3 runs on the same clip); once a term is in your dictionary or has been typed a few times, the fix becomes deterministic. + ## [1.16.4] - 2026-11-06 > AI correction of transcripts is now grounded in the vocabulary the app learned from typing. diff --git a/apps/desktop/src/main/services/MeetingModeService.ts b/apps/desktop/src/main/services/MeetingModeService.ts index 2f6fd21..0e37717 100644 --- a/apps/desktop/src/main/services/MeetingModeService.ts +++ b/apps/desktop/src/main/services/MeetingModeService.ts @@ -59,6 +59,7 @@ import { markdownToDocx } from '@d3ro/core/utils/markdown-to-docx' import { getFileTranscriptionService } from './FileTranscriptionService' import { getTranscriptGlossaryService } from './TranscriptGlossaryService' import { buildPolishTranscriptPrompt } from './meeting/polish-prompt' +import { restoreProtectedQualifiers, stripInventedMarkers } from '@d3ro/core/transcript-correction' import { buildImportedMeetingTranscript } from './meeting/imported-recording' import type { MeetingModeState, @@ -1486,7 +1487,9 @@ class MeetingModeService extends EventEmitter { temperature: 0.3, maxTokens: REWRITE_CHUNK_MAX_TOKENS, }) - const text = carry ? stripEchoedContext(result.text, previousTail) : result.text.trim() + const rawText = carry ? stripEchoedContext(result.text, previousTail) : result.text.trim() + // 모델이 원문에 없던 시각·화자 표기를 붙였으면 지우고, 지운 한정 부사(거의 등)는 되돌린다. + const text = restoreProtectedQualifiers(chunk, stripInventedMarkers(chunk, rawText)) if (looksTruncatedRewrite(chunk, text)) { throw new D3ROError( failureCode, diff --git a/apps/desktop/src/main/services/meeting/polish-prompt.ts b/apps/desktop/src/main/services/meeting/polish-prompt.ts index cb6a0b8..8c490ee 100644 --- a/apps/desktop/src/main/services/meeting/polish-prompt.ts +++ b/apps/desktop/src/main/services/meeting/polish-prompt.ts @@ -4,6 +4,10 @@ // 사용자가 학습시킨 용어로 「잘못 들은 단어」를 반드시 고치게 하되, 근거 없는 일반 단어 교체는 금지한다. // 한글 어절을 1자모 거리로 기계적으로 바꾸면 정상 문장이 망가지는 것을 실측했으므로(MTG-14), // 단어 교체 판단은 문맥을 보는 모델에게 맡기고 근거(용어·표현 목록)를 함께 준다. +// +// 아래 문장과 순서는 실제 로컬 모델(gemma4:e4b) A/B로 고른 값이다. 순서·표현을 바꾸면 준수도가 달라진다. +// 실측(3회): 학습 용어 오인식 복원 3/3, 의미 한정 부사(거의) 보존 3/3, 군더더기 잔존 0, +// 원문에 없는 화자·시각 표기 생성 0, 용어 없는 자연 문장은 단어 교체 0. /** 교열 규칙 본문 — 글로서리 블록과 분리해 테스트·프로브가 같은 문장을 쓴다. */ export const POLISH_TRANSCRIPT_RULES = [ @@ -17,6 +21,13 @@ export const POLISH_TRANSCRIPT_RULES = [ '- 확실하지 않으면 원문을 그대로 두세요.', '- 화자의 말투(반말/존댓말)와 의미·내용은 바꾸지 마세요. 타임스탬프 [MM:SS]와 화자 표기([화자 N] 등)는 그대로 유지하세요.', '- [사용자가 실제로 쓰는 표현]은 말투·용어 참고용입니다. 그 문장을 그대로 옮겨 쓰지 마세요.', + '- 원문에 없는 화자 표기([화자 N], [SPEAKER_01] 등)나 타임스탬프를 새로 붙이지 마세요. 원문에 있는 것만 그대로 유지하세요.', + '- 필러를 빼는 것 말고는 단어를 다른 단어로 바꾸지 마세요. 단, 위의 학습 용어·문장이 성립하지 않는 오인식은 고칩니다.', + '- "거의", "대부분", "아직" 같은 의미를 한정하는 말은 빼지 마세요.', + '- 군더더기로 쓰인 "이제", "그", "약간"도 제거 대상입니다. 시간을 뜻하는 "이제"는 남기세요.', + '- 조사 없이 쉼표로 끊긴 말은 자연스러운 한 문장으로 이어 붙이세요.', + '- 지울 수 있는 것은 다음뿐입니다: 음, 어, 아, 그, 저, 뭐, 약간, 군더더기 이제, 말하다 멈춘 자기수정("A 아니 A가 아니라 B" → "B"), 같은 말 반복. 이 목록에 없는 단어는 절대 빼거나 바꾸지 마세요.', + '- 예: 원문 "사실 거의 근간이라고" → "사실 근간이라고" 로 줄이면 의미가 달라지므로 틀린 교열입니다. "거의"는 남깁니다.', ].join('\n') /** 글로서리 블록(`buildGlossaryPromptSection`)을 규칙 뒤에 붙인 최종 시스템 프롬프트. */ diff --git a/apps/desktop/tests/main/services/polish-prompt.test.ts b/apps/desktop/tests/main/services/polish-prompt.test.ts index 233c97e..661268c 100644 --- a/apps/desktop/tests/main/services/polish-prompt.test.ts +++ b/apps/desktop/tests/main/services/polish-prompt.test.ts @@ -15,6 +15,19 @@ describe('전사 교열 프롬프트', () => { expect(POLISH_TRANSCRIPT_RULES).toContain('[사용자가 자주 쓰는 용어]') }) + it('지울 수 있는 것만 화이트리스트로 알려 준다', () => { + expect(POLISH_TRANSCRIPT_RULES).toContain('지울 수 있는 것은 다음뿐입니다') + expect(POLISH_TRANSCRIPT_RULES).toContain('이 목록에 없는 단어는 절대 빼거나 바꾸지 마세요') + }) + + it('의미를 한정하는 말을 빼지 말라는 예시를 준다', () => { + expect(POLISH_TRANSCRIPT_RULES).toContain('거의') + expect(POLISH_TRANSCRIPT_RULES).toContain('의미가 달라지므로 틀린 교열') + }) + + it('원문에 없는 화자 표기·타임스탬프를 새로 붙이지 말라고 지시한다', () => { + expect(POLISH_TRANSCRIPT_RULES).toContain('새로 붙이지 마세요') + }) it('타임스탬프·화자 표기와 말투를 유지하라고 지시한다', () => { expect(POLISH_TRANSCRIPT_RULES).toContain('[MM:SS]') expect(POLISH_TRANSCRIPT_RULES).toContain('화자 표기') diff --git a/apps/desktop/tests/red/meeting-llm.usecase.test.ts b/apps/desktop/tests/red/meeting-llm.usecase.test.ts index e972084..e28f2e9 100644 --- a/apps/desktop/tests/red/meeting-llm.usecase.test.ts +++ b/apps/desktop/tests/red/meeting-llm.usecase.test.ts @@ -39,7 +39,7 @@ vi.mock('../../src/main/services/PremiumLLMService', () => ({ useRedHarness() -function insertSession(id: string): void { +function insertSession(id: string, transcript: string = USER_TEXT.KO): void { const now = Date.now() getDatabase() .insert(meetingSessions) @@ -50,7 +50,7 @@ function insertSession(id: string): void { startedAt: now, createdAt: now, updatedAt: now, - rawTranscript: USER_TEXT.KO, + rawTranscript: transcript, }) .run() } @@ -85,7 +85,7 @@ describe('유스케이스: 회의 문서/폴리시가 로컬 LLM 을 탄다', () it('polish 는 모델이 되돌린 사전 표기를 학습 표기로 다시 강제한다', async () => { configSet('llmBackend', 'local') getDictionaryService().add({ word: 'D3RO', pronunciation: '디에로' }) - insertSession('meet-polish-dict') + insertSession('meet-polish-dict', '[00:01] 디에로 보이스 설정 확인해 주세요') localGenerate.mockClear() // 모델이 사전 표기를 무시하고 발음 그대로 돌려준 상황 localGenerate.mockResolvedValueOnce({ text: '[00:01] 디에로 보이스 설정 확인해 주세요', model: 'fx-local-model' }) @@ -97,6 +97,32 @@ describe('유스케이스: 회의 문서/폴리시가 로컬 LLM 을 탄다', () expect(systemPrompt).toContain('[사용자 용어') expect(systemPrompt).toContain('D3RO') }) + it('모델이 붙인 없는 시각·화자 표기는 저장 전에 지운다', async () => { + configSet('llmBackend', 'local') + insertSession('meet-polish-markers') + localGenerate.mockClear() + // 원문에는 [MM:SS]·[화자 N] 이 없다 — 모델이 형식을 만들어 냈다. + localGenerate.mockResolvedValueOnce({ + text: '[00:00] [화자 1] 오늘 회의는 오후 세 시에 시작합니다', + model: 'fx-local-model', + }) + + const polished = await getMeetingModeService().polishTranscript('meet-polish-markers') + + expect(polished).toBe(USER_TEXT.KO) + expect(polished).not.toContain('[00:00]') + expect(polished).not.toContain('[화자') + }) + it('모델이 지운 의미 한정 부사는 원문 근거로 되돌린다', async () => { + configSet('llmBackend', 'local') + insertSession('meet-polish-qualifier', '사실 거의 근간이라고 봅니다') + localGenerate.mockClear() + localGenerate.mockResolvedValueOnce({ text: '사실 근간이라고 봅니다', model: 'fx-local-model' }) + + const polished = await getMeetingModeService().polishTranscript('meet-polish-qualifier') + + expect(polished).toBe('사실 거의 근간이라고 봅니다') + }) it('없는 세션 polish 는 MeetingSessionNotFound 다', async () => { configSet('llmBackend', 'local') await expect(getMeetingModeService().polishTranscript('missing')).rejects.toMatchObject({ diff --git a/docs/map/10-feature-catalog.md b/docs/map/10-feature-catalog.md index 63dfdef..c192de7 100644 --- a/docs/map/10-feature-catalog.md +++ b/docs/map/10-feature-catalog.md @@ -89,7 +89,7 @@ Status quick-reference: `[x]` done+verified · `[~]` partial/unverified · `[ ]` | MTG-11 | Meeting AI chat over transcript | [x] | [ ] | [ ] | [x] | Desktop `MeetingChatPanel` + `MEETING_CHAT` | | MTG-12 | Content reporting for generated docs | [-] | [ ] | [x] | [x] | `content-report` Edge + generation receipts | | MTG-13 | Existing audio/video import into a meeting | [x] | [ ] | [~] | [x] | **2026-11-01:** Desktop `meetingMode:importRecording` reuses `FileTranscriptionService` without duplicate history, saves canonical timestamped transcript + normalized WAV, then opens existing minutes generation. Mobile `MeetingsScreen` creates the workspace then routes `meetingId` + `importRequest` to the existing durable Record meeting pipeline. **2026-11-02:** installed Windows 1.16.0 passed the actual Electron picker→timestamped transcript→local summary/action-item journey, so desktop is `[x]`. Fold6 `.3` import entry/editor rendered, but authenticated device file-to-minutes E2E remains (`GAP-MTG-03`). [Evidence](../evidence/2026-11-02-verification.md). | -| MTG-14 | Learning-informed transcript correction (user dictionary + typed vocabulary) | [~] | [ ] | [ ] | [ ] | **2026-11-05 (root cause + rebuild of the transcript-correction seam):** the learned corpus was only wired to input suggestions. Meeting/live captions ran STT with a hardcoded `다음은 한국어 대화입니다.` prompt (no dictionary, no learned terms), the refine prompt had no glossary, the refine result updated only `CaptionService`'s own copy (meetings and the renderer kept the raw line), and the persisted `rawTranscript` was a plain join of raw segments. Now: core SSOT `@d3ro/core/transcript-correction` (glossary build, dictionary-variant replacement, learned-identifier correction with a unique-best rule, LLM reference block) + desktop `TranscriptGlossaryService` (dictionary + `personal_phrases`, 15 s cache invalidated on dictionary writes, fail-closed) are wired into `CaptionService` (hint on every recognition, deterministic correction per final line, glossary-aware refine, `segment-updated` event), `MeetingRecordingSession`/`MeetingModeService` (refined lines reach the meeting transcript and renderer via `meetingMode:segmentUpdated`; auto-polish gets the glossary block), `FileTranscriptionService` (per-chunk hint + correction) and `VoiceModeService` (learned-term pass). **2026-11-05 (v1.16.3 correction policy):** the first cut also rewrote ordinary Hangul words by 1-jamo distance. Measured against the real local corpus (users/_local/d3ro.db, 3 stored meeting transcripts) it changed **286 of 3,005 words** (하면 to 라면, 이제 to 이게, 매일 to 메일, 정보 to 정도). Deterministic correction is now limited to dictionary pronunciations and typed Latin identifiers (canonical casing preserved); Korean terms reach recognition through the STT hint and the polish prompt only. Re-measured on the same corpus: **0 of 3,005**. Real-engine check (sidecar large-v3-turbo, Korean TTS fixture containing the user term): baseline 하람로그에 to hinted transcript haramlog에, and a dictionary pronunciation entry still maps 디에로 to D3RO. Hangul-layout typing artifacts (dkssudgktpdy) are dropped from the hint. **2026-11-06 (v1.16.4 AI correction grounded in learning):** the polish pass no longer runs as a generic rewrite. Deterministic dictionary/identifier correction runs before and after the model, and the model receives a learned term list plus the user expressions with explicit rules (fix a misheard name/product/in-house term to the learned spelling; never swap ordinary nouns or endings on similarity alone). Re-measured on the real corpus: 0 of 3,005 deterministic changes, and the generated term block now shows canonical identifiers (HaramLog, Android, AI) with Hangul-layout artifacts removed from terms, hints and style examples. Dictation output is re-checked against the learned vocabulary after the LLM finishes. Tests: core transcript-correction.test.ts (17), desktop transcript-glossary.test.ts (10), red meeting-llm usecase (+1), polish-prompt.test.ts (4). **Real-model probe (2026-11-06, local gemma4:e4b with think:false):** with the production prompt and the real learned corpus, the misheard 하람록에 was rewritten to HaramLog에 and the dictionary pronunciation 디에로 to D3RO (2 word changes, no collateral edits); a natural Korean sentence without a learned term came back with 0 word substitutions (only a spacing merge); filler and self-correction were removed. Trap: without think:false (or /no_think) this model spends the whole output budget on its thinking block and returns empty content, which the chunk guard rejects. Live meeting with real jargon still open (GAP-MTG-04). | +| MTG-14 | Learning-informed transcript correction (user dictionary + typed vocabulary) | [~] | [ ] | [ ] | [ ] | **2026-11-05 (root cause + rebuild of the transcript-correction seam):** the learned corpus was only wired to input suggestions. Meeting/live captions ran STT with a hardcoded `다음은 한국어 대화입니다.` prompt (no dictionary, no learned terms), the refine prompt had no glossary, the refine result updated only `CaptionService`'s own copy (meetings and the renderer kept the raw line), and the persisted `rawTranscript` was a plain join of raw segments. Now: core SSOT `@d3ro/core/transcript-correction` (glossary build, dictionary-variant replacement, learned-identifier correction with a unique-best rule, LLM reference block) + desktop `TranscriptGlossaryService` (dictionary + `personal_phrases`, 15 s cache invalidated on dictionary writes, fail-closed) are wired into `CaptionService` (hint on every recognition, deterministic correction per final line, glossary-aware refine, `segment-updated` event), `MeetingRecordingSession`/`MeetingModeService` (refined lines reach the meeting transcript and renderer via `meetingMode:segmentUpdated`; auto-polish gets the glossary block), `FileTranscriptionService` (per-chunk hint + correction) and `VoiceModeService` (learned-term pass). **2026-11-05 (v1.16.3 correction policy):** the first cut also rewrote ordinary Hangul words by 1-jamo distance. Measured against the real local corpus (users/_local/d3ro.db, 3 stored meeting transcripts) it changed **286 of 3,005 words** (하면 to 라면, 이제 to 이게, 매일 to 메일, 정보 to 정도). Deterministic correction is now limited to dictionary pronunciations and typed Latin identifiers (canonical casing preserved); Korean terms reach recognition through the STT hint and the polish prompt only. Re-measured on the same corpus: **0 of 3,005**. Real-engine check (sidecar large-v3-turbo, Korean TTS fixture containing the user term): baseline 하람로그에 to hinted transcript haramlog에, and a dictionary pronunciation entry still maps 디에로 to D3RO. Hangul-layout typing artifacts (dkssudgktpdy) are dropped from the hint. **2026-11-06 (v1.16.4 AI correction grounded in learning):** the polish pass no longer runs as a generic rewrite. Deterministic dictionary/identifier correction runs before and after the model, and the model receives a learned term list plus the user expressions with explicit rules (fix a misheard name/product/in-house term to the learned spelling; never swap ordinary nouns or endings on similarity alone). Re-measured on the real corpus: 0 of 3,005 deterministic changes, and the generated term block now shows canonical identifiers (HaramLog, Android, AI) with Hangul-layout artifacts removed from terms, hints and style examples. Dictation output is re-checked against the learned vocabulary after the LLM finishes. Tests: core transcript-correction.test.ts (17), desktop transcript-glossary.test.ts (10), red meeting-llm usecase (+1), polish-prompt.test.ts (4). **Real-model probe (2026-11-06, local gemma4:e4b with think:false):** with the production prompt and the real learned corpus, the misheard 하람록에 was rewritten to HaramLog에 and the dictionary pronunciation 디에로 to D3RO (2 word changes, no collateral edits); a natural Korean sentence without a learned term came back with 0 word substitutions (only a spacing merge); filler and self-correction were removed. Trap: without think:false (or /no_think) this model spends the whole output budget on its thinking block and returns empty content, which the chunk guard rejects. **2026-11-06 (v1.16.5 polish guards):** the polish prompt was re-tuned by A/B on the local model and two invariants moved from prompt to code, because the model broke them intermittently: (a) invented `[MM:SS]` / `[화자 N]` markers are stripped from the saved transcript (`stripInventedMarkers`), (b) meaning qualifiers the speaker said (거의/대부분/아직 …) are restored from the original chunk (`restoreProtectedQualifiers`). Measured on the reporter transcript: fillers gone, comma fragments joined, 유치역학 → 유체역학 repaired, 거의 kept after the guard even when the model dropped it. Unknown-word repair itself is model-dependent (2/3 then 1/2 across prompt sets) and becomes deterministic only once the term is in the dictionary or has been typed. Tests: core hardening suite (15), polish-prompt (7), red meeting-llm (+2). Live meeting with real jargon still open (GAP-MTG-04). | --- diff --git a/packages/core/__tests__/transcript-correction-hardening.test.ts b/packages/core/__tests__/transcript-correction-hardening.test.ts index ee07631..3b8c08a 100644 --- a/packages/core/__tests__/transcript-correction-hardening.test.ts +++ b/packages/core/__tests__/transcript-correction-hardening.test.ts @@ -7,6 +7,8 @@ import { buildTranscriptGlossary, correctLearnedTerms, sanitizeGlossaryLine, + restoreProtectedQualifiers, + stripInventedMarkers, } from '../src/transcript-correction' /** 재현 가능한 난수 (테스트 실패를 그대로 다시 돌릴 수 있게). */ @@ -56,6 +58,42 @@ describe('sanitizeGlossaryLine', () => { }) }) +describe('restoreProtectedQualifiers', () => { + it('모델이 지운 한정 부사를 원문 근거로 되돌린다', () => { + expect(restoreProtectedQualifiers('사실 거의 근간이라고 봅니다', '사실 근간이라고 봅니다')).toBe( + '사실 거의 근간이라고 봅니다', + ) + }) + + it('이미 있는 부사는 건드리지 않고, 근거 어절이 없으면 아무 것도 하지 않는다', () => { + expect(restoreProtectedQualifiers('거의 근간', '거의 근간')).toBe('거의 근간') + expect(restoreProtectedQualifiers('거의 근간', '다른 문장입니다')).toBe('다른 문장입니다') + }) + + it('원문에 없던 부사를 새로 넣지는 않는다', () => { + expect(restoreProtectedQualifiers('근간이라고 봅니다', '근간이라고 봅니다')).toBe('근간이라고 봅니다') + }) +}) +describe('stripInventedMarkers', () => { + it('원문에 시각이 없으면 모델이 붙인 시각 머리를 지운다', () => { + expect(stripInventedMarkers('안녕하세요', '[00:00] 안녕하세요')).toBe('안녕하세요') + }) + + it('원문에 있던 시각은 유지한다', () => { + expect(stripInventedMarkers('[00:01] 안녕하세요', '[00:01] 안녕하세요')).toBe('[00:01] 안녕하세요') + }) + + it('원문에 없던 화자 표기를 지우고 있던 표기는 남긴다', () => { + expect(stripInventedMarkers('[화자 1] 안녕하세요', '[화자 1] 안녕하세요 [화자 2] 반갑습니다')).toBe( + '[화자 1] 안녕하세요 반갑습니다', + ) + expect(stripInventedMarkers('안녕하세요', '[화자 1] 안녕하세요')).toBe('안녕하세요') + }) + + it('빈 출력은 그대로 돌려준다', () => { + expect(stripInventedMarkers('안녕하세요', '')).toBe('') + }) +}) describe('프롬프트 주입 방어', () => { it('사전 단어에 들어간 줄바꿈이 가짜 지시 줄을 만들지 않는다', () => { const glossary = buildTranscriptGlossary({ diff --git a/packages/core/src/transcript-correction.ts b/packages/core/src/transcript-correction.ts index ad3dd3a..12846d1 100644 --- a/packages/core/src/transcript-correction.ts +++ b/packages/core/src/transcript-correction.ts @@ -442,6 +442,64 @@ export function correctLearnedTerms( return { text: out + trimmed.slice(cursor), corrections } } +const TIMESTAMP_MARKER_LINE = /^\s*\[\d{1,3}:\d{2}\]\s*/u +const TIMESTAMP_MARKER_ANY = /\[\d{1,3}:\d{2}\]/u +const SPEAKER_MARKER = /\[(?:화자\s*\d+|SPEAKER[\s_-]?\d+)\]/giu + +/** + * 모델이 원문에 없던 화자 표기·타임스탬프를 붙였으면 지운다. + * + * 교열은 형식을 만들지 않는다. 회의 전사에서 시각은 STT 세그먼트가 찍은 값이어야 하고, + * 화자 라벨은 화자 구분(diarization)만 붙인다. 모델이 만들어 낸 `[00:00]`·`[화자 1]` 은 + * 문서·채팅·동기화에 그대로 저장되어 사용자가 출처를 오해한다. + */ +export function stripInventedMarkers(input: string, output: string): string { + if (!output) return output + const hasTimestamp = TIMESTAMP_MARKER_ANY.test(input) + const allowedSpeakers = new Set( + (input.match(SPEAKER_MARKER) ?? []).map((label) => label.replace(/\s+/gu, ' ').toLowerCase()), + ) + const lines = output.split('\n').map((line) => { + let next = hasTimestamp ? line : line.replace(TIMESTAMP_MARKER_LINE, '') + next = next.replace(SPEAKER_MARKER, (label) => + allowedSpeakers.has(label.replace(/\s+/gu, ' ').toLowerCase()) ? label : '', + ) + return next.replace(/^\s+/u, '').replace(/[ \t]{2,}/gu, ' ') + }) + return lines.join('\n').trim() +} +/** + * 교열이 실수로 지우면 안 되는 의미 한정 부사 목록. + * + * 8B 로컬 모델은 같은 지시에도 3회 중 1~2회 "거의"를 필러처럼 지웠다(실측). 프롬프트로 100%를 + * 보장할 수 없으므로, 사용자가 실제로 말한 한정 부사는 코드가 되돌려 놓는다. + */ +const PROTECTED_QUALIFIERS: readonly string[] = [ + '거의', '대부분', '아직', '대략', '최소', '최대', '반드시', '전혀', + '별로', '특히', '주로', '일부', '전체', '매번', '자주', '약', +] + +/** + * 모델이 지운 의미 한정 부사를 원문 근거로 되돌린다. + * + * 원문에서 그 부사 뒤에 오는 어절을 출력에서 찾아 앞에 다시 붙인다(그 어절이 없으면 건드리지 않는다). + * 새 단어를 만드는 것이 아니라 사용자가 말한 단어를 복원하는 것만 한다. + */ +export function restoreProtectedQualifiers(input: string, output: string): string { + if (!input || !output) return output + let result = output + for (const qualifier of PROTECTED_QUALIFIERS) { + if (!input.includes(qualifier) || result.includes(qualifier)) continue + const match = new RegExp(`${qualifier}\\s+(\\p{L}[\\p{L}\\p{N}]*)`, 'u').exec(input) + if (!match) continue + const anchor = match[1] + const index = result.indexOf(anchor) + if (index < 0) continue + result = `${result.slice(0, index)}${qualifier} ${result.slice(index)}` + } + return result +} + /** * LLM 교열 프롬프트에 넣는 참고 블록. 정보가 없으면 빈 문자열. *