fix(docpage): windowed 절에 조각별 분석(유형/신뢰도/요약) 집계 노출

절-레벨 분석(chunk_section_analysis)은 코퍼스 전역에 이미 있으나(절 보유 344문서 중 336)
window 조각의 chunk_id 에 붙어 있고, D3 는 window 를 split-parent 대표로 collapse 하며 버려서
windowed 절은 요약/유형/신뢰도가 안 떴다(분석은 대표가 아닌 조각에 있음).

- collapseWindows 가 멤버(대표+흡수 window)에서 절-레벨 분석 집계:
  sectionType=다수결(동률 첫등장) · confidence=평균 · summaries=조각 요약 배열(빈 것 제외)
- D3 트리/focus/모바일카드/이전다음이 it.sectionType/it.confidence/it.summaries 사용
- 요약은 단일 절=문단, windowed 절="절 요약 · N개 부분" 번호목록
- headingPath.test.ts: 집계 회귀 테스트 추가 (11/11 pass)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
hyungi
2026-06-14 07:27:41 +09:00
parent b6a4821cac
commit d007ad5492
3 changed files with 98 additions and 19 deletions
@@ -106,6 +106,32 @@ test('collapseWindows: bodyText — 정상 leaf 는 자기 본문, split-parent
assert.equal(legacy[0].bodyText, 'p1\n\np2');
});
test('collapseWindows: 절-레벨 분석 집계 — windowed 절은 window 멤버에서 type 다수결/conf 평균/summaries 합본', () => {
// split-parent(분석 없음) + window 3개(요약·유형·신뢰도 보유) → 대표에 집계
const out = collapseWindows([
sec({ heading_path: 'Sec A', node_type: 'section_split', is_leaf: false, char_start: 10, text: '# Sec A', section_type: null, summary: null, confidence: null }),
sec({ heading_path: 'Sec A', node_type: 'window', text: 'b1', section_type: 'requirement', summary: '요약1', confidence: 0.9 }),
sec({ heading_path: 'Sec A', node_type: 'window', text: 'b2', section_type: 'requirement', summary: '요약2', confidence: 0.8 }),
sec({ heading_path: 'Sec A', node_type: 'window', text: 'b3', section_type: 'overview', summary: '', confidence: 1.0 }),
]);
assert.equal(out.length, 1);
assert.equal(out[0].sectionType, 'requirement', '다수결 = requirement(2) > overview(1)');
assert.ok(Math.abs(out[0].confidence! - 0.9) < 1e-9, '평균 (0.9+0.8+1.0)/3 = 0.9');
assert.deepEqual(out[0].summaries, ['요약1', '요약2'], '빈 요약 제외, 순서 유지');
// 단일 leaf 는 대표 자신의 분석
const single = collapseWindows([sec({ heading_path: 'X', node_type: null, text: 'body', section_type: 'definition', summary: '정의 요약', confidence: 0.7 })]);
assert.equal(single[0].sectionType, 'definition');
assert.equal(single[0].confidence, 0.7);
assert.deepEqual(single[0].summaries, ['정의 요약']);
// 분석 전혀 없는 절 → null/빈
const none = collapseWindows([sec({ heading_path: 'Y', node_type: null, text: 'body' })]);
assert.equal(none[0].sectionType, null);
assert.equal(none[0].confidence, null);
assert.deepEqual(none[0].summaries, []);
});
test('groupOrFlat: 적은 그룹 + 낮은 기타% → group (5140-류)', () => {
// 3 top segment × 4 = 12절, window 없음 → group_count 3, 기타 0%
const sections: DocumentSection[] = [];
+38 -1
View File
@@ -30,6 +30,14 @@ export interface OutlineItem {
/** 대표 + 흡수된 window child 들의 본문을 순서대로 이어붙인 논리 절 전체 본문.
* split-parent 는 heading 줄(text)을 본문에서 제외(제목과 중복) — window 본문만 합친다. */
bodyText: string;
/** 집계된 절-레벨 분석. windowed 절은 분석이 window child(chunk_section_analysis)에 붙고
* 대표=split-parent 엔 없으므로 멤버에서 집계한다. 단일 절은 대표 자신의 값.
* - sectionType: 멤버 section_type 다수결(동률=첫 등장)
* - confidence: 멤버 confidence 평균
* - summaries: 멤버 요약(빈 것 제외, chunk_index 순) — 단일=1개, windowed=N개(부분별 요약) */
sectionType: string | null;
confidence: number | null;
summaries: string[];
}
export interface OutlineGroup {
@@ -112,8 +120,24 @@ function topSegment(s: DocumentSection): string {
* fragmentCount: split-parent 대표는 0 에서 시작(자신은 조각 아님) + 흡수 child 수 = 실제 조각 수;
* legacy window 대표는 1 에서 시작(자신이 첫 조각).
*/
/** 멤버 section_type 다수결(동률은 첫 등장 우선). 비어있으면 null. */
function majorityType(types: (string | null)[]): string | null {
const vals = types.filter((t): t is string => !!t);
if (!vals.length) return null;
const count = new Map<string, number>();
for (const t of vals) count.set(t, (count.get(t) ?? 0) + 1);
let best: string | null = null;
let bestN = -1;
for (const t of vals) {
const n = count.get(t)!;
if (n > bestN) { bestN = n; best = t; } // 첫 등장 우선 tie-break
}
return best;
}
export function collapseWindows(sections: DocumentSection[]): OutlineItem[] {
const out: OutlineItem[] = [];
const members: DocumentSection[][] = []; // out[i] 의 멤버(대표 + 흡수된 window child)
for (const s of sections) {
const prev = out[out.length - 1];
const h = cleanHeading(s.heading_path);
@@ -126,12 +150,25 @@ export function collapseWindows(sections: DocumentSection[]): OutlineItem[] {
prev!.fragmentCount += 1; // window child 흡수 — 대표(split-parent 우선)는 그대로 유지
const t = (s.text ?? '').trim(); // 흡수한 조각 본문을 대표 bodyText 에 이어붙임
if (t) prev!.bodyText = prev!.bodyText ? `${prev!.bodyText}\n\n${t}` : t;
members[members.length - 1].push(s);
} else {
const isSplit = !!s.node_type?.endsWith('_split');
// split-parent 의 text 는 heading 줄뿐 → 본문에서 제외(window 가 본문 보유). 그 외엔 자기 본문으로 시작.
out.push({ section: s, fragmentCount: isSplit ? 0 : 1, bodyText: isSplit ? '' : (s.text ?? '') });
out.push({
section: s, fragmentCount: isSplit ? 0 : 1, bodyText: isSplit ? '' : (s.text ?? ''),
sectionType: null, confidence: null, summaries: [],
});
members.push([s]);
}
}
// 멤버에서 절-레벨 분석 집계 (windowed 절: 대표 split-parent 엔 분석 없고 window 들이 보유).
for (let i = 0; i < out.length; i++) {
const mem = members[i];
out[i].sectionType = majorityType(mem.map((m) => m.section_type));
const confs = mem.map((m) => m.confidence).filter((c): c is number => c != null);
out[i].confidence = confs.length ? confs.reduce((a, b) => a + b, 0) / confs.length : null;
out[i].summaries = mem.map((m) => (m.summary ?? '').trim()).filter((x) => x !== '');
}
return out;
}
+34 -18
View File
@@ -169,10 +169,10 @@
</div>
{#each outline as it (it.section.chunk_id)}
{@const s = it.section}
{@const tm = typeMeta(s.section_type)}
{@const tm = typeMeta(it.sectionType)}
{@const active = !jumpMode && s.chunk_id === selectedSection?.chunk_id}
{@const child = secDepth(s) > 0}
{@const low = isMidLow(s.confidence)}
{@const low = isMidLow(it.confidence)}
<svelte:element this={jumpMode ? 'a' : 'div'} href={jumpMode ? `#m-sec-${s.chunk_id}` : undefined} role="button" tabindex="0"
onclick={() => !jumpMode && (selectedSectionId = s.chunk_id)}
onkeydown={(e) => { if (!jumpMode && (e.key === 'Enter' || e.key === ' ')) { e.preventDefault(); selectedSectionId = s.chunk_id; } }}
@@ -184,7 +184,7 @@
{#if low}
<span class="d3warn" title="저신뢰 절" style="display:inline-flex;width:14px;height:14px;border-radius:50%;background:#b5840a;color:#fff;align-items:center;justify-content:center;font-size:9px;font-weight:700;flex-shrink:0;">!</span>
{:else if !child}
<span title="신뢰도 {s.confidence != null ? s.confidence.toFixed(2) : '—'}" style="width:7px;height:7px;border-radius:50%;background:{confColor(s.confidence)};flex-shrink:0;"></span>
<span title="신뢰도 {it.confidence != null ? it.confidence.toFixed(2) : '—'}" style="width:7px;height:7px;border-radius:50%;background:{confColor(it.confidence)};flex-shrink:0;"></span>
{/if}
</div>
</svelte:element>
@@ -205,7 +205,9 @@
<!-- ════ 절 집중 뷰 (데스크탑 중앙) ════ -->
{#snippet focusView()}
{#if selectedSection}
{@const tm = typeMeta(selectedSection.section_type)}
{@const tm = typeMeta(selectedItem?.sectionType)}
{@const conf = selectedItem?.confidence ?? null}
{@const summaries = selectedItem?.summaries ?? []}
<div style="display:flex;align-items:center;gap:6px;font-size:12px;color:#9aa090;margin-bottom:12px;flex-wrap:wrap;">
<span class="truncate" style="max-width:200px;">{doc.title}</span>
{#each pathSegments(selectedSection.heading_path) as seg}<span style="color:#c8d6c0;">/</span><span style="color:#697061;font-weight:600;">{seg}</span>{/each}
@@ -214,20 +216,26 @@
<h2 style="margin:0;font-size:22px;font-weight:700;color:#23291f;line-height:1.3;flex:1;min-width:180px;">{secTitle(selectedSection)}</h2>
{#if tm.label}<span style="display:inline-flex;align-items:center;gap:5px;padding:4px 11px;border-radius:999px;background:{tm.color}1a;border:1px solid {tm.color}55;font-size:12px;color:{tm.color};font-weight:600;"><span style="width:8px;height:8px;border-radius:2px;background:{tm.color};"></span>{tm.label} {tm.en}</span>{/if}
</div>
{#if selectedSection.confidence != null}
{#if conf != null}
<div style="display:flex;align-items:center;gap:9px;margin-bottom:18px;">
<span style="font-size:11px;color:#697061;font-weight:600;flex-shrink:0;">신뢰도</span>
<div style="flex:1;max-width:300px;height:7px;border-radius:999px;background:#e3ebdf;overflow:hidden;"><div style="width:{confPct(selectedSection.confidence)}%;height:100%;background:{confColor(selectedSection.confidence)};border-radius:999px;"></div></div>
<span style="font-size:13px;font-weight:700;color:{confColor(selectedSection.confidence)};font-variant-numeric:tabular-nums;flex-shrink:0;">{selectedSection.confidence.toFixed(2)}</span>
<div style="flex:1;max-width:300px;height:7px;border-radius:999px;background:#e3ebdf;overflow:hidden;"><div style="width:{confPct(conf)}%;height:100%;background:{confColor(conf)};border-radius:999px;"></div></div>
<span style="font-size:13px;font-weight:700;color:{confColor(conf)};font-variant-numeric:tabular-nums;flex-shrink:0;">{conf.toFixed(2)}</span>
</div>
{/if}
{#if isLowConf(selectedSection.confidence)}
{#if isLowConf(conf)}
<div style="display:flex;align-items:flex-start;gap:8px;background:#faf3e2;border:1px solid #ecdca3;border-radius:10px;padding:10px 12px;margin-bottom:16px;font-size:12.5px;color:#8a6306;"><span style="flex-shrink:0;width:16px;height:16px;border-radius:50%;border:1.5px solid #b5840a;color:#b5840a;font-size:10px;font-weight:800;display:inline-flex;align-items:center;justify-content:center;margin-top:1px;">!</span><span>저신뢰 절 — 표·수식 추출이 불완전할 수 있습니다. 정확한 내용은 원본을 확인하세요.</span></div>
{/if}
{#if selectedSection.summary}
{#if summaries.length}
<div style="background:#ecf0e8;border-left:3px solid #4f8a6b;border-radius:0 10px 10px 0;padding:14px 16px;margin-bottom:20px;">
<div style="font-size:10.5px;font-weight:700;color:#3d7256;letter-spacing:.6px;margin-bottom:6px;">절 요약</div>
<div style="font-size:15.5px;line-height:1.6;color:#23291f;white-space:pre-line;">{selectedSection.summary}</div>
<div style="font-size:10.5px;font-weight:700;color:#3d7256;letter-spacing:.6px;margin-bottom:6px;">절 요약{#if summaries.length > 1} · {summaries.length}개 부분{/if}</div>
{#if summaries.length === 1}
<div style="font-size:15.5px;line-height:1.6;color:#23291f;white-space:pre-line;">{summaries[0]}</div>
{:else}
<ul style="margin:0;padding:0;list-style:none;display:flex;flex-direction:column;gap:8px;">
{#each summaries as sm, i}<li style="font-size:13.5px;line-height:1.55;color:#23291f;display:flex;gap:8px;"><span style="flex-shrink:0;color:#7a8b3f;font-weight:700;font-variant-numeric:tabular-nums;">{i + 1}</span><span style="white-space:pre-line;">{sm}</span></li>{/each}
</ul>
{/if}
</div>
{/if}
{#if selectedBodyHtml}
@@ -241,8 +249,9 @@
<button type="button" onclick={() => (selectedSectionId = pv.chunk_id)} style="font-size:12px;color:#697061;border:1px solid #dde3d6;border-radius:9px;padding:8px 12px;background:#fff;cursor:pointer;">← {secTitle(pv)}</button>
{:else}<span></span>{/if}
{#if selIdx >= 0 && selIdx < outline.length - 1}
{@const nx = outline[selIdx + 1].section}
<button type="button" onclick={() => (selectedSectionId = nx.chunk_id)} style="font-size:12px;color:{isMidLow(nx.confidence) ? '#8a6306' : '#697061'};border:1px solid {isMidLow(nx.confidence) ? '#e7d49a' : '#dde3d6'};border-radius:9px;padding:8px 12px;background:#fff;cursor:pointer;display:inline-flex;align-items:center;gap:6px;">{#if isMidLow(nx.confidence)}<span style="display:inline-flex;width:13px;height:13px;border-radius:50%;background:#b5840a;color:#fff;align-items:center;justify-content:center;font-size:8px;font-weight:700;">!</span>{/if}{secTitle(nx)} →</button>
{@const nxIt = outline[selIdx + 1]}
{@const nx = nxIt.section}
<button type="button" onclick={() => (selectedSectionId = nx.chunk_id)} style="font-size:12px;color:{isMidLow(nxIt.confidence) ? '#8a6306' : '#697061'};border:1px solid {isMidLow(nxIt.confidence) ? '#e7d49a' : '#dde3d6'};border-radius:9px;padding:8px 12px;background:#fff;cursor:pointer;display:inline-flex;align-items:center;gap:6px;">{#if isMidLow(nxIt.confidence)}<span style="display:inline-flex;width:13px;height:13px;border-radius:50%;background:#b5840a;color:#fff;align-items:center;justify-content:center;font-size:8px;font-weight:700;">!</span>{/if}{secTitle(nx)} →</button>
{:else}<span></span>{/if}
</div>
{/if}
@@ -307,17 +316,24 @@
<!-- ════ 절 카드 (모바일 연속 본문) ════ -->
{#snippet sectionCard(it)}
{@const s = it.section}
{@const tm = typeMeta(s.section_type)}
<div id="m-sec-{s.chunk_id}" style="scroll-margin-top:12px;background:#f4f7f1;border:1px solid {isLowConf(s.confidence) ? '#e7d49a' : '#dde3d6'};border-radius:14px;padding:14px 15px;">
{@const tm = typeMeta(it.sectionType)}
<div id="m-sec-{s.chunk_id}" style="scroll-margin-top:12px;background:#f4f7f1;border:1px solid {isLowConf(it.confidence) ? '#e7d49a' : '#dde3d6'};border-radius:14px;padding:14px 15px;">
<div style="display:flex;align-items:center;gap:7px;margin-bottom:7px;">
<h2 style="margin:0;font-size:16px;font-weight:700;color:#23291f;flex:1;min-width:0;line-height:1.3;">{secTitle(s)}</h2>
{#if tm.label}<span style="flex-shrink:0;font-size:10.5px;font-weight:650;padding:2px 8px;border-radius:999px;background:{tm.color}1a;color:{tm.color};white-space:nowrap;">{tm.label}</span>{/if}
</div>
{#if isLowConf(s.confidence)}
{#if isLowConf(it.confidence)}
<div style="display:flex;align-items:flex-start;gap:7px;background:#faf3e2;border:1px solid #ecdca3;border-radius:9px;padding:8px 10px;margin-bottom:10px;font-size:12px;color:#8a6306;"><span style="flex-shrink:0;width:15px;height:15px;border-radius:50%;border:1.5px solid #b5840a;color:#b5840a;font-size:10px;font-weight:800;display:inline-flex;align-items:center;justify-content:center;margin-top:1px;">!</span><span>저신뢰 — 표·수식 추출 불완전, 원본 확인 권장</span></div>
{/if}
{#if s.summary}
<div style="border-left:3px solid #4f8a6b;background:#ecf0e8;border-radius:0 8px 8px 0;padding:9px 12px;margin-bottom:12px;"><div style="font-size:9.5px;font-weight:700;color:#3d7256;letter-spacing:.5px;margin-bottom:3px;">절 요약</div><div style="font-size:13.5px;line-height:1.55;color:#23291f;white-space:pre-line;">{s.summary}</div></div>
{#if it.summaries.length}
<div style="border-left:3px solid #4f8a6b;background:#ecf0e8;border-radius:0 8px 8px 0;padding:9px 12px;margin-bottom:12px;">
<div style="font-size:9.5px;font-weight:700;color:#3d7256;letter-spacing:.5px;margin-bottom:3px;">절 요약{#if it.summaries.length > 1} · {it.summaries.length}개 부분{/if}</div>
{#if it.summaries.length === 1}
<div style="font-size:13.5px;line-height:1.55;color:#23291f;white-space:pre-line;">{it.summaries[0]}</div>
{:else}
<ul style="margin:0;padding:0;list-style:none;display:flex;flex-direction:column;gap:6px;">{#each it.summaries as sm, i}<li style="font-size:12.5px;line-height:1.5;color:#23291f;display:flex;gap:6px;"><span style="flex-shrink:0;color:#7a8b3f;font-weight:700;font-variant-numeric:tabular-nums;">{i + 1}</span><span style="white-space:pre-line;">{sm}</span></li>{/each}</ul>
{/if}
</div>
{/if}
{#if it.bodyText}
<details class="m-secbody" ontoggle={(e) => { if (e.currentTarget.open) mBodyOpen[s.chunk_id] = true; }}>