kordoc 2.2.4 → 2.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -4
- package/dist/chunk-FCQEF2ZM.js +457 -0
- package/dist/chunk-FCQEF2ZM.js.map +1 -0
- package/dist/chunk-HXUCZ2IL.cjs +450 -0
- package/dist/chunk-HXUCZ2IL.cjs.map +1 -0
- package/dist/chunk-MUOQXDZ4.cjs +33 -0
- package/dist/chunk-MUOQXDZ4.cjs.map +1 -0
- package/dist/chunk-NL5XLN5R.js +450 -0
- package/dist/chunk-NL5XLN5R.js.map +1 -0
- package/dist/{chunk-SY2RFVLW.js → chunk-RF6UJXR3.js} +135 -2805
- package/dist/chunk-RF6UJXR3.js.map +1 -0
- package/dist/chunk-SBVRCJFH.js +33 -0
- package/dist/chunk-SBVRCJFH.js.map +1 -0
- package/dist/cli.js +12 -7
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +294 -3084
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +77 -2817
- package/dist/index.js.map +1 -1
- package/dist/mcp.js +15 -9
- package/dist/mcp.js.map +1 -1
- package/dist/page-range-3C7UGGEK.cjs +7 -0
- package/dist/page-range-3C7UGGEK.cjs.map +1 -0
- package/dist/page-range-H35FN3OQ.js +7 -0
- package/dist/page-range-H35FN3OQ.js.map +1 -0
- package/dist/parser-43IAQ5KE.js +2278 -0
- package/dist/parser-43IAQ5KE.js.map +1 -0
- package/dist/parser-AMP7MAOH.js +2279 -0
- package/dist/parser-AMP7MAOH.js.map +1 -0
- package/dist/parser-KOWPTDJU.cjs +2278 -0
- package/dist/parser-KOWPTDJU.cjs.map +1 -0
- package/dist/provider-WPIYEALY.js +37 -0
- package/dist/provider-WPIYEALY.js.map +1 -0
- package/dist/provider-YN2SSK4X.cjs +37 -0
- package/dist/provider-YN2SSK4X.cjs.map +1 -0
- package/dist/{watch-5P7DJ3HG.js → watch-IUQXOXW3.js} +6 -4
- package/dist/{watch-5P7DJ3HG.js.map → watch-IUQXOXW3.js.map} +1 -1
- package/package.json +1 -1
- package/dist/chunk-SY2RFVLW.js.map +0 -1
|
@@ -3,448 +3,32 @@ import {
|
|
|
3
3
|
detectFormat,
|
|
4
4
|
detectZipFormat
|
|
5
5
|
} from "./chunk-5Y2Q3BRW.js";
|
|
6
|
+
import {
|
|
7
|
+
HEADING_RATIO_H1,
|
|
8
|
+
HEADING_RATIO_H2,
|
|
9
|
+
HEADING_RATIO_H3,
|
|
10
|
+
KordocError,
|
|
11
|
+
MAX_COLS,
|
|
12
|
+
MAX_ROWS,
|
|
13
|
+
blocksToMarkdown,
|
|
14
|
+
buildTable,
|
|
15
|
+
classifyError,
|
|
16
|
+
convertTableToText,
|
|
17
|
+
flattenLayoutTables,
|
|
18
|
+
isPathTraversal,
|
|
19
|
+
precheckZipSize,
|
|
20
|
+
sanitizeHref,
|
|
21
|
+
stripDtd,
|
|
22
|
+
toArrayBuffer
|
|
23
|
+
} from "./chunk-FCQEF2ZM.js";
|
|
6
24
|
import {
|
|
7
25
|
parsePageRange
|
|
8
26
|
} from "./chunk-MOL7MDBG.js";
|
|
9
27
|
|
|
10
|
-
// src/utils.ts
|
|
11
|
-
var VERSION = true ? "2.2.4" : "0.0.0-dev";
|
|
12
|
-
function toArrayBuffer(buf) {
|
|
13
|
-
if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) {
|
|
14
|
-
return buf.buffer;
|
|
15
|
-
}
|
|
16
|
-
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
|
|
17
|
-
}
|
|
18
|
-
var KordocError = class extends Error {
|
|
19
|
-
constructor(message) {
|
|
20
|
-
super(message);
|
|
21
|
-
this.name = "KordocError";
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
function sanitizeError(err) {
|
|
25
|
-
if (err instanceof KordocError) return err.message;
|
|
26
|
-
return "\uBB38\uC11C \uCC98\uB9AC \uC911 \uC624\uB958\uAC00 \uBC1C\uC0DD\uD588\uC2B5\uB2C8\uB2E4";
|
|
27
|
-
}
|
|
28
|
-
function isPathTraversal(name) {
|
|
29
|
-
if (name.includes("\0")) return true;
|
|
30
|
-
const normalized = name.replace(/\\/g, "/");
|
|
31
|
-
const segments = normalized.split("/");
|
|
32
|
-
return segments.some((s) => s === "..") || normalized.startsWith("/") || /^[A-Za-z]:/.test(normalized);
|
|
33
|
-
}
|
|
34
|
-
function precheckZipSize(buffer, maxUncompressedSize = 100 * 1024 * 1024, maxEntries = 500) {
|
|
35
|
-
try {
|
|
36
|
-
const data = new DataView(buffer);
|
|
37
|
-
const len = buffer.byteLength;
|
|
38
|
-
let eocdOffset = -1;
|
|
39
|
-
for (let i = len - 22; i >= Math.max(0, len - 65557); i--) {
|
|
40
|
-
if (data.getUint32(i, true) === 101010256) {
|
|
41
|
-
eocdOffset = i;
|
|
42
|
-
break;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
if (eocdOffset < 0) return { totalUncompressed: 0, entryCount: 0 };
|
|
46
|
-
const entryCount = data.getUint16(eocdOffset + 10, true);
|
|
47
|
-
if (entryCount > maxEntries) {
|
|
48
|
-
throw new KordocError(`ZIP \uC5D4\uD2B8\uB9AC \uC218 \uCD08\uACFC: ${entryCount} (\uCD5C\uB300 ${maxEntries})`);
|
|
49
|
-
}
|
|
50
|
-
const cdSize = data.getUint32(eocdOffset + 12, true);
|
|
51
|
-
const cdOffset = data.getUint32(eocdOffset + 16, true);
|
|
52
|
-
if (cdOffset + cdSize > len) return { totalUncompressed: 0, entryCount };
|
|
53
|
-
let totalUncompressed = 0;
|
|
54
|
-
let pos = cdOffset;
|
|
55
|
-
for (let i = 0; i < entryCount && pos + 46 <= cdOffset + cdSize; i++) {
|
|
56
|
-
if (data.getUint32(pos, true) !== 33639248) break;
|
|
57
|
-
totalUncompressed += data.getUint32(pos + 24, true);
|
|
58
|
-
const nameLen = data.getUint16(pos + 28, true);
|
|
59
|
-
const extraLen = data.getUint16(pos + 30, true);
|
|
60
|
-
const commentLen = data.getUint16(pos + 32, true);
|
|
61
|
-
pos += 46 + nameLen + extraLen + commentLen;
|
|
62
|
-
}
|
|
63
|
-
if (totalUncompressed > maxUncompressedSize) {
|
|
64
|
-
throw new KordocError(`ZIP \uBE44\uC555\uCD95 \uD06C\uAE30 \uCD08\uACFC: ${(totalUncompressed / 1024 / 1024).toFixed(1)}MB (\uCD5C\uB300 ${maxUncompressedSize / 1024 / 1024}MB)`);
|
|
65
|
-
}
|
|
66
|
-
return { totalUncompressed, entryCount };
|
|
67
|
-
} catch (err) {
|
|
68
|
-
if (err instanceof KordocError) throw err;
|
|
69
|
-
return { totalUncompressed: 0, entryCount: 0 };
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
function stripDtd(xml) {
|
|
73
|
-
return xml.replace(/<!DOCTYPE\s[^[>]*(\[[\s\S]*?\])?\s*>/gi, "");
|
|
74
|
-
}
|
|
75
|
-
var SAFE_HREF_RE = /^(?:https?:|mailto:|tel:|#)/i;
|
|
76
|
-
function sanitizeHref(href) {
|
|
77
|
-
const trimmed = href.trim();
|
|
78
|
-
if (!trimmed || !SAFE_HREF_RE.test(trimmed)) return null;
|
|
79
|
-
return trimmed;
|
|
80
|
-
}
|
|
81
|
-
function safeMin(arr) {
|
|
82
|
-
let min = Infinity;
|
|
83
|
-
for (let i = 0; i < arr.length; i++) if (arr[i] < min) min = arr[i];
|
|
84
|
-
return min;
|
|
85
|
-
}
|
|
86
|
-
function safeMax(arr) {
|
|
87
|
-
let max = -Infinity;
|
|
88
|
-
for (let i = 0; i < arr.length; i++) if (arr[i] > max) max = arr[i];
|
|
89
|
-
return max;
|
|
90
|
-
}
|
|
91
|
-
function classifyError(err) {
|
|
92
|
-
if (!(err instanceof Error)) return "PARSE_ERROR";
|
|
93
|
-
const msg = err.message;
|
|
94
|
-
if (msg.includes("\uC554\uD638\uD654")) return "ENCRYPTED";
|
|
95
|
-
if (msg.includes("DRM")) return "DRM_PROTECTED";
|
|
96
|
-
if (msg.includes("ZIP bomb") || msg.includes("ZIP \uBE44\uC555\uCD95 \uD06C\uAE30 \uCD08\uACFC") || msg.includes("ZIP \uC5D4\uD2B8\uB9AC \uC218 \uCD08\uACFC")) return "ZIP_BOMB";
|
|
97
|
-
if (msg.includes("bomb") || msg.includes("\uD06C\uAE30 \uCD08\uACFC") || msg.includes("\uC555\uCD95 \uD574\uC81C")) return "DECOMPRESSION_BOMB";
|
|
98
|
-
if (msg.includes("\uC774\uBBF8\uC9C0 \uAE30\uBC18")) return "IMAGE_BASED_PDF";
|
|
99
|
-
if (msg.includes("\uC139\uC158") && (msg.includes("\uCC3E\uC744 \uC218 \uC5C6") || msg.includes("\uC5C6\uC74C"))) return "NO_SECTIONS";
|
|
100
|
-
if (msg.includes("\uC2DC\uADF8\uB2C8\uCC98") || msg.includes("\uBCF5\uAD6C\uD560 \uC218 \uC5C6")) return "CORRUPTED";
|
|
101
|
-
return "PARSE_ERROR";
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// src/table/builder.ts
|
|
105
|
-
var MAX_COLS = 200;
|
|
106
|
-
var MAX_ROWS = 1e4;
|
|
107
|
-
function buildTable(rows) {
|
|
108
|
-
if (rows.length > MAX_ROWS) rows = rows.slice(0, MAX_ROWS);
|
|
109
|
-
const numRows = rows.length;
|
|
110
|
-
const hasAddr = rows.some((row) => row.some((c) => c.colAddr !== void 0 && c.rowAddr !== void 0));
|
|
111
|
-
if (hasAddr) return buildTableDirect(rows, numRows);
|
|
112
|
-
let maxCols = 0;
|
|
113
|
-
const tempOccupied = Array.from({ length: numRows }, () => []);
|
|
114
|
-
for (let rowIdx = 0; rowIdx < numRows; rowIdx++) {
|
|
115
|
-
let colIdx = 0;
|
|
116
|
-
for (const cell of rows[rowIdx]) {
|
|
117
|
-
while (colIdx < MAX_COLS && tempOccupied[rowIdx][colIdx]) colIdx++;
|
|
118
|
-
if (colIdx >= MAX_COLS) break;
|
|
119
|
-
for (let r = rowIdx; r < Math.min(rowIdx + cell.rowSpan, numRows); r++) {
|
|
120
|
-
for (let c = colIdx; c < Math.min(colIdx + cell.colSpan, MAX_COLS); c++) {
|
|
121
|
-
tempOccupied[r][c] = true;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
colIdx += cell.colSpan;
|
|
125
|
-
if (colIdx > maxCols) maxCols = colIdx;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
if (maxCols === 0) return { rows: 0, cols: 0, cells: [], hasHeader: false };
|
|
129
|
-
const grid = Array.from(
|
|
130
|
-
{ length: numRows },
|
|
131
|
-
() => Array.from({ length: maxCols }, () => ({ text: "", colSpan: 1, rowSpan: 1 }))
|
|
132
|
-
);
|
|
133
|
-
const occupied = Array.from({ length: numRows }, () => Array(maxCols).fill(false));
|
|
134
|
-
for (let rowIdx = 0; rowIdx < numRows; rowIdx++) {
|
|
135
|
-
let colIdx = 0;
|
|
136
|
-
let cellIdx = 0;
|
|
137
|
-
while (colIdx < maxCols && cellIdx < rows[rowIdx].length) {
|
|
138
|
-
while (colIdx < maxCols && occupied[rowIdx][colIdx]) colIdx++;
|
|
139
|
-
if (colIdx >= maxCols) break;
|
|
140
|
-
const cell = rows[rowIdx][cellIdx];
|
|
141
|
-
grid[rowIdx][colIdx] = {
|
|
142
|
-
text: cell.text.trim(),
|
|
143
|
-
colSpan: cell.colSpan,
|
|
144
|
-
rowSpan: cell.rowSpan
|
|
145
|
-
};
|
|
146
|
-
for (let r = rowIdx; r < Math.min(rowIdx + cell.rowSpan, numRows); r++) {
|
|
147
|
-
for (let c = colIdx; c < Math.min(colIdx + cell.colSpan, maxCols); c++) {
|
|
148
|
-
occupied[r][c] = true;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
colIdx += cell.colSpan;
|
|
152
|
-
cellIdx++;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
return trimAndReturn(grid, numRows, maxCols);
|
|
156
|
-
}
|
|
157
|
-
function buildTableDirect(rows, numRows) {
|
|
158
|
-
let maxCols = 0;
|
|
159
|
-
for (const row of rows) {
|
|
160
|
-
for (const cell of row) {
|
|
161
|
-
const end = (cell.colAddr ?? 0) + cell.colSpan;
|
|
162
|
-
if (end > maxCols) maxCols = end;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
if (maxCols > MAX_COLS) maxCols = MAX_COLS;
|
|
166
|
-
if (maxCols === 0) return { rows: 0, cols: 0, cells: [], hasHeader: false };
|
|
167
|
-
const grid = Array.from(
|
|
168
|
-
{ length: numRows },
|
|
169
|
-
() => Array.from({ length: maxCols }, () => ({ text: "", colSpan: 1, rowSpan: 1 }))
|
|
170
|
-
);
|
|
171
|
-
for (const row of rows) {
|
|
172
|
-
for (const cell of row) {
|
|
173
|
-
const r = cell.rowAddr ?? 0;
|
|
174
|
-
const c = cell.colAddr ?? 0;
|
|
175
|
-
if (r >= numRows || c >= maxCols || r < 0 || c < 0) continue;
|
|
176
|
-
grid[r][c] = { text: cell.text.trim(), colSpan: cell.colSpan, rowSpan: cell.rowSpan };
|
|
177
|
-
for (let dr = 0; dr < cell.rowSpan; dr++) {
|
|
178
|
-
for (let dc = 0; dc < cell.colSpan; dc++) {
|
|
179
|
-
if (dr === 0 && dc === 0) continue;
|
|
180
|
-
if (r + dr < numRows && c + dc < maxCols) {
|
|
181
|
-
grid[r + dr][c + dc] = { text: "", colSpan: 1, rowSpan: 1 };
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
return trimAndReturn(grid, numRows, maxCols);
|
|
188
|
-
}
|
|
189
|
-
function trimAndReturn(grid, numRows, maxCols) {
|
|
190
|
-
let effectiveCols = maxCols;
|
|
191
|
-
while (effectiveCols > 0) {
|
|
192
|
-
const colEmpty = grid.every((row) => !row[effectiveCols - 1]?.text?.trim());
|
|
193
|
-
if (!colEmpty) break;
|
|
194
|
-
effectiveCols--;
|
|
195
|
-
}
|
|
196
|
-
if (effectiveCols < maxCols && effectiveCols > 0) {
|
|
197
|
-
const trimmed = grid.map((row) => row.slice(0, effectiveCols));
|
|
198
|
-
return { rows: numRows, cols: effectiveCols, cells: trimmed, hasHeader: numRows > 1 };
|
|
199
|
-
}
|
|
200
|
-
return { rows: numRows, cols: maxCols, cells: grid, hasHeader: numRows > 1 };
|
|
201
|
-
}
|
|
202
|
-
function convertTableToText(rows) {
|
|
203
|
-
return rows.map(
|
|
204
|
-
(row) => row.map((c) => c.text.trim().replace(/\n/g, " ").replace(/\|/g, "\\|")).filter(Boolean).join(" / ")
|
|
205
|
-
).filter(Boolean).join("\n");
|
|
206
|
-
}
|
|
207
|
-
function escapeGfm(text) {
|
|
208
|
-
return text.replace(/~/g, "\\~");
|
|
209
|
-
}
|
|
210
|
-
var HWP_SHAPE_ALT_TEXT_RE = /(?:모서리가 둥근 |둥근 )?(?:사각형|직사각형|정사각형|원|타원|삼각형|이등변 삼각형|직각 삼각형|선|직선|곡선|화살표|굵은 화살표|이중 화살표|오각형|육각형|팔각형|별|[4-8]점별|십자|십자형|구름|구름형|마름모|도넛|평행사변형|사다리꼴|부채꼴|호|반원|물결|번개|하트|빗금|블록 화살표|수식|표|그림|개체|그리기\s?개체|묶음\s?개체|글상자|수식\s?개체|OLE\s?개체)\s?입니다\.?/g;
|
|
211
|
-
function sanitizeText(text) {
|
|
212
|
-
let result = text.replace(/[\u{F0000}-\u{FFFFD}]/gu, "").replace(HWP_SHAPE_ALT_TEXT_RE, "").replace(/ +/g, " ").trim();
|
|
213
|
-
if (result.length <= 30 && result.includes(" ")) {
|
|
214
|
-
const tokens = result.split(" ");
|
|
215
|
-
const koreanSingleCharCount = tokens.filter((t) => t.length === 1 && /[\uAC00-\uD7AF\u3131-\u318E]/.test(t)).length;
|
|
216
|
-
if (tokens.length >= 3 && koreanSingleCharCount / tokens.length >= 0.7) {
|
|
217
|
-
result = tokens.join("");
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
return result;
|
|
221
|
-
}
|
|
222
|
-
function flattenLayoutTables(blocks) {
|
|
223
|
-
const result = [];
|
|
224
|
-
for (const block of blocks) {
|
|
225
|
-
if (block.type !== "table" || !block.table) {
|
|
226
|
-
result.push(block);
|
|
227
|
-
continue;
|
|
228
|
-
}
|
|
229
|
-
const { rows: numRows, cols: numCols, cells } = block.table;
|
|
230
|
-
if (numRows === 1 && numCols === 1) {
|
|
231
|
-
result.push(block);
|
|
232
|
-
continue;
|
|
233
|
-
}
|
|
234
|
-
if (numRows <= 3) {
|
|
235
|
-
let totalNewlines = 0;
|
|
236
|
-
let totalTextLen = 0;
|
|
237
|
-
for (let r = 0; r < numRows; r++) {
|
|
238
|
-
for (let c = 0; c < numCols; c++) {
|
|
239
|
-
const t = cells[r]?.[c]?.text || "";
|
|
240
|
-
totalNewlines += (t.match(/\n/g) || []).length;
|
|
241
|
-
totalTextLen += t.length;
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
if (totalNewlines > 5 || numRows <= 2 && totalTextLen > 300) {
|
|
245
|
-
for (let r = 0; r < numRows; r++) {
|
|
246
|
-
for (let c = 0; c < numCols; c++) {
|
|
247
|
-
const cellText = cells[r]?.[c]?.text?.trim();
|
|
248
|
-
if (!cellText) continue;
|
|
249
|
-
for (const line of cellText.split("\n")) {
|
|
250
|
-
const trimmed = line.trim();
|
|
251
|
-
if (!trimmed) continue;
|
|
252
|
-
result.push({ type: "paragraph", text: trimmed, pageNumber: block.pageNumber });
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
continue;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
result.push(block);
|
|
260
|
-
}
|
|
261
|
-
return result;
|
|
262
|
-
}
|
|
263
|
-
function blocksToMarkdown(blocks) {
|
|
264
|
-
const lines = [];
|
|
265
|
-
for (let i = 0; i < blocks.length; i++) {
|
|
266
|
-
const block = blocks[i];
|
|
267
|
-
if (block.type === "heading" && block.text) {
|
|
268
|
-
const prefix = "#".repeat(Math.min(block.level || 2, 6));
|
|
269
|
-
const headingText = sanitizeText(block.text);
|
|
270
|
-
if (headingText) lines.push("", `${prefix} ${headingText}`, "");
|
|
271
|
-
continue;
|
|
272
|
-
}
|
|
273
|
-
if (block.type === "image" && block.text) {
|
|
274
|
-
lines.push("", ``, "");
|
|
275
|
-
continue;
|
|
276
|
-
}
|
|
277
|
-
if (block.type === "separator") {
|
|
278
|
-
lines.push("", "---", "");
|
|
279
|
-
continue;
|
|
280
|
-
}
|
|
281
|
-
if (block.type === "list" && block.text) {
|
|
282
|
-
const listText = sanitizeText(block.text);
|
|
283
|
-
if (!listText) continue;
|
|
284
|
-
const alreadyNumbered = block.listType === "ordered" && /^\d+\.\s/.test(listText);
|
|
285
|
-
const prefix = alreadyNumbered ? "" : block.listType === "ordered" ? "1. " : "- ";
|
|
286
|
-
lines.push(`${prefix}${listText}`);
|
|
287
|
-
if (block.children) {
|
|
288
|
-
for (const child of block.children) {
|
|
289
|
-
const childPrefix = child.listType === "ordered" ? "1." : "-";
|
|
290
|
-
lines.push(` ${childPrefix} ${child.text || ""}`);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
continue;
|
|
294
|
-
}
|
|
295
|
-
if (block.type === "paragraph" && block.text) {
|
|
296
|
-
let text = sanitizeText(block.text);
|
|
297
|
-
if (!text) continue;
|
|
298
|
-
if (/^\[별표\s*\d+/.test(text)) {
|
|
299
|
-
const nextBlock = blocks[i + 1];
|
|
300
|
-
if (nextBlock?.type === "paragraph" && nextBlock.text && /관련\)?$/.test(nextBlock.text)) {
|
|
301
|
-
lines.push("", `## ${text} ${nextBlock.text}`, "");
|
|
302
|
-
i++;
|
|
303
|
-
} else {
|
|
304
|
-
lines.push("", `## ${text}`, "");
|
|
305
|
-
}
|
|
306
|
-
continue;
|
|
307
|
-
}
|
|
308
|
-
if (/^\([^)]*조[^)]*관련\)$/.test(text)) {
|
|
309
|
-
lines.push(`*${text}*`, "");
|
|
310
|
-
continue;
|
|
311
|
-
}
|
|
312
|
-
if (block.href) {
|
|
313
|
-
const href = sanitizeHref(block.href);
|
|
314
|
-
if (href) text = `[${text}](${href})`;
|
|
315
|
-
}
|
|
316
|
-
if (block.footnoteText) {
|
|
317
|
-
text += ` (\uC8FC: ${block.footnoteText})`;
|
|
318
|
-
}
|
|
319
|
-
lines.push(escapeGfm(text), "");
|
|
320
|
-
} else if (block.type === "table" && block.table) {
|
|
321
|
-
if (lines.length > 0 && lines[lines.length - 1] !== "") {
|
|
322
|
-
lines.push("");
|
|
323
|
-
}
|
|
324
|
-
const tableMd = tableToMarkdown(block.table);
|
|
325
|
-
if (tableMd) {
|
|
326
|
-
lines.push(tableMd);
|
|
327
|
-
lines.push("");
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
return lines.join("\n").trim();
|
|
332
|
-
}
|
|
333
|
-
function hasMergedCells(table) {
|
|
334
|
-
for (const row of table.cells) {
|
|
335
|
-
for (const cell of row) {
|
|
336
|
-
if (cell.colSpan > 1 || cell.rowSpan > 1) return true;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
return false;
|
|
340
|
-
}
|
|
341
|
-
function tableToHtml(table) {
|
|
342
|
-
const { cells, rows: numRows, cols: numCols } = table;
|
|
343
|
-
const skip = /* @__PURE__ */ new Set();
|
|
344
|
-
const lines = ["<table>"];
|
|
345
|
-
for (let r = 0; r < numRows; r++) {
|
|
346
|
-
const tag = r === 0 ? "th" : "td";
|
|
347
|
-
const rowHtml = [];
|
|
348
|
-
for (let c = 0; c < numCols; c++) {
|
|
349
|
-
if (skip.has(`${r},${c}`)) continue;
|
|
350
|
-
const cell = cells[r]?.[c];
|
|
351
|
-
if (!cell) continue;
|
|
352
|
-
for (let dr = 0; dr < cell.rowSpan; dr++) {
|
|
353
|
-
for (let dc = 0; dc < cell.colSpan; dc++) {
|
|
354
|
-
if (dr === 0 && dc === 0) continue;
|
|
355
|
-
if (r + dr < numRows && c + dc < numCols) skip.add(`${r + dr},${c + dc}`);
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
const text = sanitizeText(cell.text).replace(/\n/g, "<br>");
|
|
359
|
-
const attrs = [];
|
|
360
|
-
if (cell.colSpan > 1) attrs.push(`colspan="${cell.colSpan}"`);
|
|
361
|
-
if (cell.rowSpan > 1) attrs.push(`rowspan="${cell.rowSpan}"`);
|
|
362
|
-
const attrStr = attrs.length ? " " + attrs.join(" ") : "";
|
|
363
|
-
rowHtml.push(`<${tag}${attrStr}>${text}</${tag}>`);
|
|
364
|
-
}
|
|
365
|
-
if (rowHtml.length) lines.push(`<tr>${rowHtml.join("")}</tr>`);
|
|
366
|
-
}
|
|
367
|
-
lines.push("</table>");
|
|
368
|
-
return lines.join("\n");
|
|
369
|
-
}
|
|
370
|
-
function tableToMarkdown(table) {
|
|
371
|
-
if (table.rows === 0 || table.cols === 0) return "";
|
|
372
|
-
const { cells, rows: numRows, cols: numCols } = table;
|
|
373
|
-
if (hasMergedCells(table)) return tableToHtml(table);
|
|
374
|
-
if (numRows === 1 && numCols === 1) {
|
|
375
|
-
const content = sanitizeText(cells[0][0].text);
|
|
376
|
-
if (!content) return "";
|
|
377
|
-
return content.split(/\n/).map((line) => {
|
|
378
|
-
const trimmed = line.trim();
|
|
379
|
-
if (!trimmed) return "";
|
|
380
|
-
if (/^\d+\.\s/.test(trimmed)) return `**${escapeGfm(trimmed)}**`;
|
|
381
|
-
if (/^[가-힣]\.\s/.test(trimmed)) return ` ${escapeGfm(trimmed)}`;
|
|
382
|
-
return escapeGfm(trimmed);
|
|
383
|
-
}).filter(Boolean).join("\n");
|
|
384
|
-
}
|
|
385
|
-
if (numCols === 1 && numRows >= 2) {
|
|
386
|
-
return cells.map((row) => escapeGfm(sanitizeText(row[0].text)).replace(/\n/g, " ")).filter(Boolean).join("\n");
|
|
387
|
-
}
|
|
388
|
-
const display = Array.from({ length: numRows }, () => Array(numCols).fill(""));
|
|
389
|
-
const skip = /* @__PURE__ */ new Set();
|
|
390
|
-
for (let r = 0; r < numRows; r++) {
|
|
391
|
-
for (let c = 0; c < numCols; c++) {
|
|
392
|
-
if (skip.has(`${r},${c}`)) continue;
|
|
393
|
-
const cell = cells[r]?.[c];
|
|
394
|
-
if (!cell) continue;
|
|
395
|
-
display[r][c] = escapeGfm(sanitizeText(cell.text)).replace(/\|/g, "\\|").replace(/\n/g, "<br>");
|
|
396
|
-
for (let dr = 0; dr < cell.rowSpan; dr++) {
|
|
397
|
-
for (let dc = 0; dc < cell.colSpan; dc++) {
|
|
398
|
-
if (dr === 0 && dc === 0) continue;
|
|
399
|
-
if (r + dr < numRows && c + dc < numCols) {
|
|
400
|
-
skip.add(`${r + dr},${c + dc}`);
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
c += cell.colSpan - 1;
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
const uniqueRows = [];
|
|
408
|
-
let pendingFirstCol = "";
|
|
409
|
-
for (let r = 0; r < display.length; r++) {
|
|
410
|
-
const row = display[r];
|
|
411
|
-
const isEmptyPlaceholder = row.every((cell) => cell === "");
|
|
412
|
-
if (isEmptyPlaceholder) continue;
|
|
413
|
-
const nonEmptyCols = row.filter((cell) => cell !== "");
|
|
414
|
-
const hasSkipInRow = row.some((_, c) => skip.has(`${r},${c}`));
|
|
415
|
-
if (!hasSkipInRow && nonEmptyCols.length === 1 && row[0] !== "" && row.slice(1).every((c) => c === "")) {
|
|
416
|
-
pendingFirstCol = row[0];
|
|
417
|
-
continue;
|
|
418
|
-
}
|
|
419
|
-
if (pendingFirstCol && row[0] === "") {
|
|
420
|
-
row[0] = pendingFirstCol;
|
|
421
|
-
pendingFirstCol = "";
|
|
422
|
-
} else {
|
|
423
|
-
pendingFirstCol = "";
|
|
424
|
-
}
|
|
425
|
-
uniqueRows.push(row);
|
|
426
|
-
}
|
|
427
|
-
if (uniqueRows.length === 0) return "";
|
|
428
|
-
const md = [];
|
|
429
|
-
md.push("| " + uniqueRows[0].join(" | ") + " |");
|
|
430
|
-
md.push("| " + uniqueRows[0].map(() => "---").join(" | ") + " |");
|
|
431
|
-
for (let i = 1; i < uniqueRows.length; i++) {
|
|
432
|
-
md.push("| " + uniqueRows[i].join(" | ") + " |");
|
|
433
|
-
}
|
|
434
|
-
return md.join("\n");
|
|
435
|
-
}
|
|
436
|
-
|
|
437
28
|
// src/hwpx/parser.ts
|
|
438
29
|
import JSZip from "jszip";
|
|
439
30
|
import { inflateRawSync } from "zlib";
|
|
440
31
|
import { DOMParser } from "@xmldom/xmldom";
|
|
441
|
-
|
|
442
|
-
// src/types.ts
|
|
443
|
-
var HEADING_RATIO_H1 = 1.5;
|
|
444
|
-
var HEADING_RATIO_H2 = 1.3;
|
|
445
|
-
var HEADING_RATIO_H3 = 1.15;
|
|
446
|
-
|
|
447
|
-
// src/hwpx/parser.ts
|
|
448
32
|
var MAX_DECOMPRESS_SIZE = 100 * 1024 * 1024;
|
|
449
33
|
var MAX_ZIP_ENTRIES = 500;
|
|
450
34
|
function clampSpan(val, max) {
|
|
@@ -2929,2382 +2513,122 @@ function arrangeCells(rows, cols, cells) {
|
|
|
2929
2513
|
return grid.map((row) => row.map((c) => c || { text: "", colSpan: 1, rowSpan: 1 }));
|
|
2930
2514
|
}
|
|
2931
2515
|
|
|
2932
|
-
// src/
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
|
|
2941
|
-
|
|
2942
|
-
|
|
2943
|
-
|
|
2944
|
-
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
|
|
2953
|
-
|
|
2954
|
-
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
|
|
2961
|
-
|
|
2962
|
-
|
|
2516
|
+
// src/form/recognize.ts
|
|
2517
|
+
var LABEL_KEYWORDS = /* @__PURE__ */ new Set([
|
|
2518
|
+
"\uC131\uBA85",
|
|
2519
|
+
"\uC774\uB984",
|
|
2520
|
+
"\uC8FC\uC18C",
|
|
2521
|
+
"\uC804\uD654",
|
|
2522
|
+
"\uC804\uD654\uBC88\uD638",
|
|
2523
|
+
"\uD734\uB300\uD3F0",
|
|
2524
|
+
"\uD578\uB4DC\uD3F0",
|
|
2525
|
+
"\uC5F0\uB77D\uCC98",
|
|
2526
|
+
"\uC0DD\uB144\uC6D4\uC77C",
|
|
2527
|
+
"\uC8FC\uBBFC\uB4F1\uB85D\uBC88\uD638",
|
|
2528
|
+
"\uC18C\uC18D",
|
|
2529
|
+
"\uC9C1\uC704",
|
|
2530
|
+
"\uC9C1\uAE09",
|
|
2531
|
+
"\uBD80\uC11C",
|
|
2532
|
+
"\uC774\uBA54\uC77C",
|
|
2533
|
+
"\uD329\uC2A4",
|
|
2534
|
+
"\uD559\uAD50",
|
|
2535
|
+
"\uD559\uB144",
|
|
2536
|
+
"\uBC18",
|
|
2537
|
+
"\uBC88\uD638",
|
|
2538
|
+
"\uC2E0\uCCAD\uC778",
|
|
2539
|
+
"\uB300\uD45C\uC790",
|
|
2540
|
+
"\uB2F4\uB2F9\uC790",
|
|
2541
|
+
"\uC791\uC131\uC790",
|
|
2542
|
+
"\uD655\uC778\uC790",
|
|
2543
|
+
"\uC2B9\uC778\uC790",
|
|
2544
|
+
"\uC77C\uC2DC",
|
|
2545
|
+
"\uB0A0\uC9DC",
|
|
2546
|
+
"\uAE30\uAC04",
|
|
2547
|
+
"\uC7A5\uC18C",
|
|
2548
|
+
"\uBAA9\uC801",
|
|
2549
|
+
"\uC0AC\uC720",
|
|
2550
|
+
"\uBE44\uACE0",
|
|
2551
|
+
"\uAE08\uC561",
|
|
2552
|
+
"\uC218\uB7C9",
|
|
2553
|
+
"\uB2E8\uAC00",
|
|
2554
|
+
"\uD569\uACC4",
|
|
2555
|
+
"\uACC4",
|
|
2556
|
+
"\uC18C\uACC4",
|
|
2557
|
+
"\uB4F1\uB85D\uAE30\uC900\uC9C0",
|
|
2558
|
+
"\uBCF8\uC801",
|
|
2559
|
+
"\uC704\uC784\uC778",
|
|
2560
|
+
"\uCCAD\uAD6C\uC0AC\uC720",
|
|
2561
|
+
"\uC18C\uBA85\uC790\uB8CC"
|
|
2562
|
+
]);
|
|
2563
|
+
function isLabelCell(text) {
|
|
2564
|
+
const trimmed = text.trim().replace(/[¹²³⁴⁵⁶⁷⁸⁹⁰*※]+$/g, "").trim();
|
|
2565
|
+
if (!trimmed || trimmed.length > 30) return false;
|
|
2566
|
+
for (const kw of LABEL_KEYWORDS) {
|
|
2567
|
+
if (trimmed.includes(kw)) return true;
|
|
2963
2568
|
}
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
|
|
2569
|
+
if (/^[가-힣\s()()·::]+$/.test(trimmed) && trimmed.replace(/\s/g, "").length >= 2 && trimmed.replace(/\s/g, "").length <= 8 && !/\d/.test(trimmed)) return true;
|
|
2570
|
+
if (/^[가-힣A-Za-z\s]+[::]$/.test(trimmed)) return true;
|
|
2571
|
+
return false;
|
|
2572
|
+
}
|
|
2573
|
+
function extractFormFields(blocks) {
|
|
2574
|
+
const fields = [];
|
|
2575
|
+
let totalTables = 0;
|
|
2576
|
+
let formTables = 0;
|
|
2577
|
+
for (const block of blocks) {
|
|
2578
|
+
if (block.type !== "table" || !block.table) continue;
|
|
2579
|
+
totalTables++;
|
|
2580
|
+
const tableFields = extractFromTable(block.table);
|
|
2581
|
+
if (tableFields.length > 0) {
|
|
2582
|
+
formTables++;
|
|
2583
|
+
fields.push(...tableFields);
|
|
2968
2584
|
}
|
|
2969
|
-
|
|
2970
|
-
|
|
2585
|
+
}
|
|
2586
|
+
for (const block of blocks) {
|
|
2587
|
+
if (block.type === "paragraph" && block.text) {
|
|
2588
|
+
const inlineFields = extractInlineFields(block.text);
|
|
2589
|
+
fields.push(...inlineFields);
|
|
2971
2590
|
}
|
|
2972
|
-
currentPath = [];
|
|
2973
2591
|
}
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
const
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
pathStartX = curX;
|
|
2992
|
-
pathStartY = curY;
|
|
2993
|
-
} else if (subOp === OPS.lineTo) {
|
|
2994
|
-
const x2 = coords[ci++], y2 = coords[ci++];
|
|
2995
|
-
currentPath.push({ x1: curX, y1: curY, x2, y2 });
|
|
2996
|
-
curX = x2;
|
|
2997
|
-
curY = y2;
|
|
2998
|
-
} else if (subOp === OPS.rectangle) {
|
|
2999
|
-
const rx = coords[ci++], ry = coords[ci++];
|
|
3000
|
-
const rw = coords[ci++], rh = coords[ci++];
|
|
3001
|
-
pushRectangle(currentPath, rx, ry, rw, rh);
|
|
3002
|
-
} else if (subOp === OPS.closePath) {
|
|
3003
|
-
if (curX !== pathStartX || curY !== pathStartY) {
|
|
3004
|
-
currentPath.push({ x1: curX, y1: curY, x2: pathStartX, y2: pathStartY });
|
|
3005
|
-
}
|
|
3006
|
-
curX = pathStartX;
|
|
3007
|
-
curY = pathStartY;
|
|
3008
|
-
} else if (subOp === OPS.curveTo) {
|
|
3009
|
-
ci += 6;
|
|
3010
|
-
} else if (subOp === OPS.curveTo2 || subOp === OPS.curveTo3) {
|
|
3011
|
-
ci += 4;
|
|
3012
|
-
}
|
|
3013
|
-
}
|
|
3014
|
-
} else {
|
|
3015
|
-
const afterOp = arg0;
|
|
3016
|
-
const dataArr = args[1];
|
|
3017
|
-
const pathData = dataArr?.[0];
|
|
3018
|
-
if (pathData && typeof pathData === "object") {
|
|
3019
|
-
const len = Object.keys(pathData).length;
|
|
3020
|
-
let di = 0;
|
|
3021
|
-
while (di < len) {
|
|
3022
|
-
const drawOp = pathData[di++];
|
|
3023
|
-
if (drawOp === 0 /* moveTo */) {
|
|
3024
|
-
curX = pathData[di++];
|
|
3025
|
-
curY = pathData[di++];
|
|
3026
|
-
pathStartX = curX;
|
|
3027
|
-
pathStartY = curY;
|
|
3028
|
-
} else if (drawOp === 1 /* lineTo */) {
|
|
3029
|
-
const x2 = pathData[di++], y2 = pathData[di++];
|
|
3030
|
-
currentPath.push({ x1: curX, y1: curY, x2, y2 });
|
|
3031
|
-
curX = x2;
|
|
3032
|
-
curY = y2;
|
|
3033
|
-
} else if (drawOp === 2 /* curveTo */) {
|
|
3034
|
-
di += 6;
|
|
3035
|
-
} else if (drawOp === 3 /* quadraticCurveTo */) {
|
|
3036
|
-
di += 4;
|
|
3037
|
-
} else if (drawOp === 4 /* closePath */) {
|
|
3038
|
-
if (curX !== pathStartX || curY !== pathStartY) {
|
|
3039
|
-
currentPath.push({ x1: curX, y1: curY, x2: pathStartX, y2: pathStartY });
|
|
3040
|
-
}
|
|
3041
|
-
curX = pathStartX;
|
|
3042
|
-
curY = pathStartY;
|
|
3043
|
-
} else {
|
|
3044
|
-
break;
|
|
3045
|
-
}
|
|
3046
|
-
}
|
|
3047
|
-
}
|
|
3048
|
-
if (afterOp === OPS.stroke || afterOp === OPS.closeStroke) {
|
|
3049
|
-
flushPath(true);
|
|
3050
|
-
} else if (afterOp === OPS.fill || afterOp === OPS.eoFill || afterOp === OPS.fillStroke || afterOp === OPS.eoFillStroke || afterOp === OPS.closeFillStroke || afterOp === OPS.closeEOFillStroke) {
|
|
3051
|
-
flushPath(true);
|
|
3052
|
-
} else if (afterOp === OPS.endPath) {
|
|
3053
|
-
flushPath(false);
|
|
3054
|
-
}
|
|
2592
|
+
const confidence = totalTables > 0 ? formTables / totalTables : fields.length > 0 ? 0.3 : 0;
|
|
2593
|
+
return { fields, confidence: Math.min(confidence, 1) };
|
|
2594
|
+
}
|
|
2595
|
+
function extractFromTable(table) {
|
|
2596
|
+
const fields = [];
|
|
2597
|
+
if (table.cols >= 2) {
|
|
2598
|
+
for (let r = 0; r < table.rows; r++) {
|
|
2599
|
+
for (let c = 0; c < table.cols - 1; c++) {
|
|
2600
|
+
const labelCell = table.cells[r][c];
|
|
2601
|
+
const valueCell = table.cells[r][c + 1];
|
|
2602
|
+
if (isLabelCell(labelCell.text)) {
|
|
2603
|
+
fields.push({
|
|
2604
|
+
label: labelCell.text.trim().replace(/[::]\s*$/, ""),
|
|
2605
|
+
value: valueCell.text.trim(),
|
|
2606
|
+
row: r,
|
|
2607
|
+
col: c
|
|
2608
|
+
});
|
|
3055
2609
|
}
|
|
3056
|
-
break;
|
|
3057
2610
|
}
|
|
3058
|
-
case OPS.stroke:
|
|
3059
|
-
case OPS.closeStroke:
|
|
3060
|
-
flushPath(true);
|
|
3061
|
-
break;
|
|
3062
|
-
case OPS.fill:
|
|
3063
|
-
case OPS.eoFill:
|
|
3064
|
-
case OPS.fillStroke:
|
|
3065
|
-
case OPS.eoFillStroke:
|
|
3066
|
-
case OPS.closeFillStroke:
|
|
3067
|
-
case OPS.closeEOFillStroke:
|
|
3068
|
-
flushPath(true);
|
|
3069
|
-
break;
|
|
3070
|
-
case OPS.endPath:
|
|
3071
|
-
flushPath(false);
|
|
3072
|
-
break;
|
|
3073
2611
|
}
|
|
3074
2612
|
}
|
|
3075
|
-
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
3082
|
-
|
|
3083
|
-
|
|
3084
|
-
|
|
3085
|
-
|
|
3086
|
-
|
|
3087
|
-
|
|
3088
|
-
|
|
3089
|
-
const y1 = Math.min(seg.y1, seg.y2);
|
|
3090
|
-
const y2 = Math.max(seg.y1, seg.y2);
|
|
3091
|
-
verticals.push({ x1: x, y1, x2: x, y2, lineWidth });
|
|
3092
|
-
}
|
|
3093
|
-
}
|
|
3094
|
-
function preprocessLines(horizontals, verticals) {
|
|
3095
|
-
let h = horizontals.filter((l) => l.lineWidth <= MAX_LINE_WIDTH);
|
|
3096
|
-
let v = verticals.filter((l) => l.lineWidth <= MAX_LINE_WIDTH);
|
|
3097
|
-
h = mergeParallelLines(h, "h");
|
|
3098
|
-
v = mergeParallelLines(v, "v");
|
|
3099
|
-
return { horizontals: h, verticals: v };
|
|
3100
|
-
}
|
|
3101
|
-
function mergeParallelLines(lines, dir) {
|
|
3102
|
-
if (lines.length <= 1) return lines;
|
|
3103
|
-
const sorted = [...lines].sort((a, b) => {
|
|
3104
|
-
const posA = dir === "h" ? a.y1 : a.x1;
|
|
3105
|
-
const posB = dir === "h" ? b.y1 : b.x1;
|
|
3106
|
-
if (Math.abs(posA - posB) > 0.1) return posA - posB;
|
|
3107
|
-
return dir === "h" ? a.x1 - b.x1 : a.y1 - b.y1;
|
|
3108
|
-
});
|
|
3109
|
-
const MERGE_TOL = 3;
|
|
3110
|
-
const result = [sorted[0]];
|
|
3111
|
-
for (let i = 1; i < sorted.length; i++) {
|
|
3112
|
-
const prev = result[result.length - 1];
|
|
3113
|
-
const curr = sorted[i];
|
|
3114
|
-
const prevPos = dir === "h" ? prev.y1 : prev.x1;
|
|
3115
|
-
const currPos = dir === "h" ? curr.y1 : curr.x1;
|
|
3116
|
-
if (Math.abs(prevPos - currPos) <= MERGE_TOL) {
|
|
3117
|
-
const prevStart = dir === "h" ? prev.x1 : prev.y1;
|
|
3118
|
-
const prevEnd = dir === "h" ? prev.x2 : prev.y2;
|
|
3119
|
-
const currStart = dir === "h" ? curr.x1 : curr.y1;
|
|
3120
|
-
const currEnd = dir === "h" ? curr.x2 : curr.y2;
|
|
3121
|
-
const overlap = Math.min(prevEnd, currEnd) - Math.max(prevStart, currStart);
|
|
3122
|
-
const minLen = Math.min(prevEnd - prevStart, currEnd - currStart);
|
|
3123
|
-
if (overlap > minLen * 0.3) {
|
|
3124
|
-
if (dir === "h") {
|
|
3125
|
-
prev.x1 = Math.min(prev.x1, curr.x1);
|
|
3126
|
-
prev.x2 = Math.max(prev.x2, curr.x2);
|
|
3127
|
-
prev.y1 = (prev.y1 + curr.y1) / 2;
|
|
3128
|
-
prev.y2 = prev.y1;
|
|
3129
|
-
} else {
|
|
3130
|
-
prev.y1 = Math.min(prev.y1, curr.y1);
|
|
3131
|
-
prev.y2 = Math.max(prev.y2, curr.y2);
|
|
3132
|
-
prev.x1 = (prev.x1 + curr.x1) / 2;
|
|
3133
|
-
prev.x2 = prev.x1;
|
|
2613
|
+
if (fields.length === 0 && table.rows >= 2 && table.cols >= 2) {
|
|
2614
|
+
const headerRow = table.cells[0];
|
|
2615
|
+
const allLabels = headerRow.every((cell) => {
|
|
2616
|
+
const t = cell.text.trim();
|
|
2617
|
+
return t.length > 0 && t.length <= 20;
|
|
2618
|
+
});
|
|
2619
|
+
if (allLabels) {
|
|
2620
|
+
for (let r = 1; r < table.rows; r++) {
|
|
2621
|
+
for (let c = 0; c < table.cols; c++) {
|
|
2622
|
+
const label = headerRow[c].text.trim();
|
|
2623
|
+
const value = table.cells[r][c].text.trim();
|
|
2624
|
+
if (label && value) {
|
|
2625
|
+
fields.push({ label, value, row: r, col: c });
|
|
2626
|
+
}
|
|
3134
2627
|
}
|
|
3135
|
-
prev.lineWidth = Math.max(prev.lineWidth, curr.lineWidth);
|
|
3136
|
-
continue;
|
|
3137
2628
|
}
|
|
3138
2629
|
}
|
|
3139
|
-
result.push(curr);
|
|
3140
2630
|
}
|
|
3141
|
-
return
|
|
3142
|
-
}
|
|
3143
|
-
function filterPageBorderLines(horizontals, verticals, pageWidth, pageHeight) {
|
|
3144
|
-
const margin = 5;
|
|
3145
|
-
return {
|
|
3146
|
-
horizontals: horizontals.filter(
|
|
3147
|
-
(l) => !(Math.abs(l.y1) < margin || Math.abs(l.y1 - pageHeight) < margin) || l.x2 - l.x1 < pageWidth * 0.9
|
|
3148
|
-
),
|
|
3149
|
-
verticals: verticals.filter(
|
|
3150
|
-
(l) => !(Math.abs(l.x1) < margin || Math.abs(l.x1 - pageWidth) < margin) || l.y2 - l.y1 < pageHeight * 0.9
|
|
3151
|
-
)
|
|
3152
|
-
};
|
|
3153
|
-
}
|
|
3154
|
-
function buildVertices(horizontals, verticals) {
|
|
3155
|
-
const vertices = [];
|
|
3156
|
-
const tol = CONNECT_TOL;
|
|
3157
|
-
for (const h of horizontals) {
|
|
3158
|
-
for (const v of verticals) {
|
|
3159
|
-
if (v.x1 >= h.x1 - tol && v.x1 <= h.x2 + tol && h.y1 >= v.y1 - tol && h.y1 <= v.y2 + tol) {
|
|
3160
|
-
const radius = Math.max(h.lineWidth, v.lineWidth, 1);
|
|
3161
|
-
vertices.push({ x: v.x1, y: h.y1, radius });
|
|
3162
|
-
}
|
|
3163
|
-
}
|
|
3164
|
-
}
|
|
3165
|
-
return vertices;
|
|
3166
|
-
}
|
|
3167
|
-
function mergeVertices(vertices) {
|
|
3168
|
-
if (vertices.length <= 1) return vertices;
|
|
3169
|
-
const merged = [];
|
|
3170
|
-
const used = new Array(vertices.length).fill(false);
|
|
3171
|
-
for (let i = 0; i < vertices.length; i++) {
|
|
3172
|
-
if (used[i]) continue;
|
|
3173
|
-
let sumX = vertices[i].x, sumY = vertices[i].y;
|
|
3174
|
-
let maxRadius = vertices[i].radius;
|
|
3175
|
-
let count = 1;
|
|
3176
|
-
for (let j = i + 1; j < vertices.length; j++) {
|
|
3177
|
-
if (used[j]) continue;
|
|
3178
|
-
const mergeTol = VERTEX_MERGE_FACTOR * Math.max(maxRadius, vertices[j].radius);
|
|
3179
|
-
if (Math.abs(vertices[i].x - vertices[j].x) <= mergeTol && Math.abs(vertices[i].y - vertices[j].y) <= mergeTol) {
|
|
3180
|
-
sumX += vertices[j].x;
|
|
3181
|
-
sumY += vertices[j].y;
|
|
3182
|
-
maxRadius = Math.max(maxRadius, vertices[j].radius);
|
|
3183
|
-
count++;
|
|
3184
|
-
used[j] = true;
|
|
3185
|
-
}
|
|
3186
|
-
}
|
|
3187
|
-
merged.push({ x: sumX / count, y: sumY / count, radius: maxRadius });
|
|
3188
|
-
}
|
|
3189
|
-
return merged;
|
|
3190
|
-
}
|
|
3191
|
-
function buildTableGrids(horizontals, verticals) {
|
|
3192
|
-
if (horizontals.length < 2 || verticals.length < 2) return [];
|
|
3193
|
-
const allVertices = buildVertices(horizontals, verticals);
|
|
3194
|
-
const vertices = mergeVertices(allVertices);
|
|
3195
|
-
if (vertices.length < 4) return [];
|
|
3196
|
-
const globalRadius = vertices.reduce((max, v) => Math.max(max, v.radius), 1);
|
|
3197
|
-
const allLines = [
|
|
3198
|
-
...horizontals.map((l, i) => ({ ...l, type: "h", id: i })),
|
|
3199
|
-
...verticals.map((l, i) => ({ ...l, type: "v", id: i + horizontals.length }))
|
|
3200
|
-
];
|
|
3201
|
-
const groups = groupConnectedLines(allLines);
|
|
3202
|
-
const grids = [];
|
|
3203
|
-
for (const group of groups) {
|
|
3204
|
-
const hLines = group.filter((l) => l.type === "h");
|
|
3205
|
-
const vLines = group.filter((l) => l.type === "v");
|
|
3206
|
-
if (hLines.length < 2 || vLines.length < 2) continue;
|
|
3207
|
-
let gx1 = Infinity, gy1 = Infinity, gx2 = -Infinity, gy2 = -Infinity;
|
|
3208
|
-
for (const l of vLines) {
|
|
3209
|
-
if (l.x1 < gx1) gx1 = l.x1;
|
|
3210
|
-
if (l.x1 > gx2) gx2 = l.x1;
|
|
3211
|
-
}
|
|
3212
|
-
for (const l of hLines) {
|
|
3213
|
-
if (l.y1 < gy1) gy1 = l.y1;
|
|
3214
|
-
if (l.y1 > gy2) gy2 = l.y1;
|
|
3215
|
-
}
|
|
3216
|
-
const groupBbox = {
|
|
3217
|
-
x1: gx1 - CONNECT_TOL,
|
|
3218
|
-
y1: gy1 - CONNECT_TOL,
|
|
3219
|
-
x2: gx2 + CONNECT_TOL,
|
|
3220
|
-
y2: gy2 + CONNECT_TOL
|
|
3221
|
-
};
|
|
3222
|
-
const groupVertices = vertices.filter(
|
|
3223
|
-
(v) => v.x >= groupBbox.x1 && v.x <= groupBbox.x2 && v.y >= groupBbox.y1 && v.y <= groupBbox.y2
|
|
3224
|
-
);
|
|
3225
|
-
const groupRadius = groupVertices.length > 0 ? groupVertices.reduce((max, v) => Math.max(max, v.radius), 1) : globalRadius;
|
|
3226
|
-
const coordMergeTol = Math.max(VERTEX_MERGE_FACTOR * groupRadius, MIN_COORD_MERGE_TOL);
|
|
3227
|
-
const rawYs = [
|
|
3228
|
-
...hLines.map((l) => l.y1),
|
|
3229
|
-
...groupVertices.map((v) => v.y)
|
|
3230
|
-
];
|
|
3231
|
-
const rowYs = clusterCoordinates(rawYs, coordMergeTol).sort((a, b) => b - a);
|
|
3232
|
-
const rawXs = [
|
|
3233
|
-
...vLines.map((l) => l.x1),
|
|
3234
|
-
...groupVertices.map((v) => v.x)
|
|
3235
|
-
];
|
|
3236
|
-
const colXs = clusterCoordinates(rawXs, coordMergeTol).sort((a, b) => a - b);
|
|
3237
|
-
if (rowYs.length < 2 || colXs.length < 2) continue;
|
|
3238
|
-
const validColXs = enforceMinWidth(colXs, MIN_COL_WIDTH);
|
|
3239
|
-
const validRowYs = enforceMinHeight(rowYs, MIN_ROW_HEIGHT);
|
|
3240
|
-
if (validRowYs.length < 2 || validColXs.length < 2) continue;
|
|
3241
|
-
const bbox = {
|
|
3242
|
-
x1: validColXs[0],
|
|
3243
|
-
y1: validRowYs[validRowYs.length - 1],
|
|
3244
|
-
x2: validColXs[validColXs.length - 1],
|
|
3245
|
-
y2: validRowYs[0]
|
|
3246
|
-
};
|
|
3247
|
-
grids.push({ rowYs: validRowYs, colXs: validColXs, bbox, vertexRadius: groupRadius });
|
|
3248
|
-
}
|
|
3249
|
-
return mergeAdjacentGrids(grids);
|
|
3250
|
-
}
|
|
3251
|
-
function enforceMinWidth(colXs, minWidth) {
|
|
3252
|
-
if (colXs.length <= 2) return colXs;
|
|
3253
|
-
const result = [colXs[0]];
|
|
3254
|
-
for (let i = 1; i < colXs.length; i++) {
|
|
3255
|
-
const prevX = result[result.length - 1];
|
|
3256
|
-
if (colXs[i] - prevX < minWidth && i < colXs.length - 1) {
|
|
3257
|
-
continue;
|
|
3258
|
-
}
|
|
3259
|
-
result.push(colXs[i]);
|
|
3260
|
-
}
|
|
3261
|
-
return result;
|
|
3262
|
-
}
|
|
3263
|
-
function enforceMinHeight(rowYs, minHeight) {
|
|
3264
|
-
if (rowYs.length <= 2) return rowYs;
|
|
3265
|
-
const result = [rowYs[0]];
|
|
3266
|
-
for (let i = 1; i < rowYs.length; i++) {
|
|
3267
|
-
const prevY = result[result.length - 1];
|
|
3268
|
-
if (prevY - rowYs[i] < minHeight && i < rowYs.length - 1) {
|
|
3269
|
-
continue;
|
|
3270
|
-
}
|
|
3271
|
-
result.push(rowYs[i]);
|
|
3272
|
-
}
|
|
3273
|
-
return result;
|
|
3274
|
-
}
|
|
3275
|
-
function mergeAdjacentGrids(grids) {
|
|
3276
|
-
if (grids.length <= 1) return grids;
|
|
3277
|
-
const sorted = [...grids].sort((a, b) => b.bbox.y2 - a.bbox.y2);
|
|
3278
|
-
const merged = [sorted[0]];
|
|
3279
|
-
for (let i = 1; i < sorted.length; i++) {
|
|
3280
|
-
const prev = merged[merged.length - 1];
|
|
3281
|
-
const curr = sorted[i];
|
|
3282
|
-
if (prev.colXs.length === curr.colXs.length) {
|
|
3283
|
-
const mergeTol = Math.max(VERTEX_MERGE_FACTOR * Math.max(prev.vertexRadius, curr.vertexRadius), 6) * 3;
|
|
3284
|
-
const colMatch = prev.colXs.every((x, ci) => Math.abs(x - curr.colXs[ci]) <= mergeTol);
|
|
3285
|
-
const verticalGap = prev.bbox.y1 - curr.bbox.y2;
|
|
3286
|
-
if (colMatch && verticalGap >= -CONNECT_TOL && verticalGap <= 20) {
|
|
3287
|
-
const allRowYs = [.../* @__PURE__ */ new Set([...prev.rowYs, ...curr.rowYs])].sort((a, b) => b - a);
|
|
3288
|
-
merged[merged.length - 1] = {
|
|
3289
|
-
rowYs: allRowYs,
|
|
3290
|
-
colXs: prev.colXs,
|
|
3291
|
-
bbox: {
|
|
3292
|
-
x1: Math.min(prev.bbox.x1, curr.bbox.x1),
|
|
3293
|
-
y1: Math.min(prev.bbox.y1, curr.bbox.y1),
|
|
3294
|
-
x2: Math.max(prev.bbox.x2, curr.bbox.x2),
|
|
3295
|
-
y2: Math.max(prev.bbox.y2, curr.bbox.y2)
|
|
3296
|
-
},
|
|
3297
|
-
vertexRadius: Math.max(prev.vertexRadius, curr.vertexRadius)
|
|
3298
|
-
};
|
|
3299
|
-
continue;
|
|
3300
|
-
}
|
|
3301
|
-
}
|
|
3302
|
-
merged.push(curr);
|
|
3303
|
-
}
|
|
3304
|
-
return merged;
|
|
3305
|
-
}
|
|
3306
|
-
function clusterCoordinates(values, tolerance) {
|
|
3307
|
-
if (values.length === 0) return [];
|
|
3308
|
-
const sorted = [...values].sort((a, b) => a - b);
|
|
3309
|
-
const clusters = [{ sum: sorted[0], count: 1 }];
|
|
3310
|
-
for (let i = 1; i < sorted.length; i++) {
|
|
3311
|
-
const last = clusters[clusters.length - 1];
|
|
3312
|
-
const avg = last.sum / last.count;
|
|
3313
|
-
if (Math.abs(sorted[i] - avg) <= tolerance) {
|
|
3314
|
-
last.sum += sorted[i];
|
|
3315
|
-
last.count++;
|
|
3316
|
-
} else {
|
|
3317
|
-
clusters.push({ sum: sorted[i], count: 1 });
|
|
3318
|
-
}
|
|
3319
|
-
}
|
|
3320
|
-
return clusters.map((c) => c.sum / c.count);
|
|
3321
|
-
}
|
|
3322
|
-
function groupConnectedLines(lines) {
|
|
3323
|
-
const parent = lines.map((_, i) => i);
|
|
3324
|
-
function find(x) {
|
|
3325
|
-
while (parent[x] !== x) {
|
|
3326
|
-
parent[x] = parent[parent[x]];
|
|
3327
|
-
x = parent[x];
|
|
3328
|
-
}
|
|
3329
|
-
return x;
|
|
3330
|
-
}
|
|
3331
|
-
function union(a, b) {
|
|
3332
|
-
const ra = find(a), rb = find(b);
|
|
3333
|
-
if (ra !== rb) parent[ra] = rb;
|
|
3334
|
-
}
|
|
3335
|
-
for (let i = 0; i < lines.length; i++) {
|
|
3336
|
-
for (let j = i + 1; j < lines.length; j++) {
|
|
3337
|
-
if (linesIntersect(lines[i], lines[j])) {
|
|
3338
|
-
union(i, j);
|
|
3339
|
-
}
|
|
3340
|
-
}
|
|
3341
|
-
}
|
|
3342
|
-
const groups = /* @__PURE__ */ new Map();
|
|
3343
|
-
for (let i = 0; i < lines.length; i++) {
|
|
3344
|
-
const root = find(i);
|
|
3345
|
-
if (!groups.has(root)) groups.set(root, []);
|
|
3346
|
-
groups.get(root).push(lines[i]);
|
|
3347
|
-
}
|
|
3348
|
-
return [...groups.values()];
|
|
3349
|
-
}
|
|
3350
|
-
function linesIntersect(a, b) {
|
|
3351
|
-
if (a.type === b.type) {
|
|
3352
|
-
if (a.type === "h") {
|
|
3353
|
-
if (Math.abs(a.y1 - b.y1) > CONNECT_TOL) return false;
|
|
3354
|
-
return Math.min(a.x2, b.x2) >= Math.max(a.x1, b.x1) - CONNECT_TOL;
|
|
3355
|
-
} else {
|
|
3356
|
-
if (Math.abs(a.x1 - b.x1) > CONNECT_TOL) return false;
|
|
3357
|
-
return Math.min(a.y2, b.y2) >= Math.max(a.y1, b.y1) - CONNECT_TOL;
|
|
3358
|
-
}
|
|
3359
|
-
}
|
|
3360
|
-
const h = a.type === "h" ? a : b;
|
|
3361
|
-
const v = a.type === "h" ? b : a;
|
|
3362
|
-
const tol = CONNECT_TOL;
|
|
3363
|
-
return v.x1 >= h.x1 - tol && v.x1 <= h.x2 + tol && h.y1 >= v.y1 - tol && h.y1 <= v.y2 + tol;
|
|
3364
|
-
}
|
|
3365
|
-
function extractCells(grid, horizontals, verticals) {
|
|
3366
|
-
const { rowYs, colXs } = grid;
|
|
3367
|
-
const numRows = rowYs.length - 1;
|
|
3368
|
-
const numCols = colXs.length - 1;
|
|
3369
|
-
if (numRows <= 0 || numCols <= 0) return [];
|
|
3370
|
-
const vBorders = Array.from(
|
|
3371
|
-
{ length: numRows },
|
|
3372
|
-
(_, r) => Array.from(
|
|
3373
|
-
{ length: numCols + 1 },
|
|
3374
|
-
(_2, c) => hasVerticalLine(verticals, colXs[c], rowYs[r], rowYs[r + 1], grid.vertexRadius)
|
|
3375
|
-
)
|
|
3376
|
-
);
|
|
3377
|
-
const hBorders = Array.from(
|
|
3378
|
-
{ length: numRows + 1 },
|
|
3379
|
-
(_, r) => Array.from(
|
|
3380
|
-
{ length: numCols },
|
|
3381
|
-
(_2, c) => hasHorizontalLine(horizontals, rowYs[r], colXs[c], colXs[c + 1], grid.vertexRadius)
|
|
3382
|
-
)
|
|
3383
|
-
);
|
|
3384
|
-
const occupied = Array.from({ length: numRows }, () => Array(numCols).fill(false));
|
|
3385
|
-
const cells = [];
|
|
3386
|
-
for (let r = 0; r < numRows; r++) {
|
|
3387
|
-
for (let c = 0; c < numCols; c++) {
|
|
3388
|
-
if (occupied[r][c]) continue;
|
|
3389
|
-
let colSpan = 1;
|
|
3390
|
-
let rowSpan = 1;
|
|
3391
|
-
while (c + colSpan < numCols && !vBorders[r][c + colSpan]) {
|
|
3392
|
-
let canExpand = true;
|
|
3393
|
-
for (let dr = 0; dr < rowSpan; dr++) {
|
|
3394
|
-
if (vBorders[r + dr][c + colSpan]) {
|
|
3395
|
-
canExpand = false;
|
|
3396
|
-
break;
|
|
3397
|
-
}
|
|
3398
|
-
}
|
|
3399
|
-
if (!canExpand) break;
|
|
3400
|
-
colSpan++;
|
|
3401
|
-
}
|
|
3402
|
-
while (r + rowSpan < numRows) {
|
|
3403
|
-
let hasLine = false;
|
|
3404
|
-
for (let dc = 0; dc < colSpan; dc++) {
|
|
3405
|
-
if (hBorders[r + rowSpan][c + dc]) {
|
|
3406
|
-
hasLine = true;
|
|
3407
|
-
break;
|
|
3408
|
-
}
|
|
3409
|
-
}
|
|
3410
|
-
if (hasLine) break;
|
|
3411
|
-
rowSpan++;
|
|
3412
|
-
}
|
|
3413
|
-
for (let dr = 0; dr < rowSpan; dr++) {
|
|
3414
|
-
for (let dc = 0; dc < colSpan; dc++) {
|
|
3415
|
-
occupied[r + dr][c + dc] = true;
|
|
3416
|
-
}
|
|
3417
|
-
}
|
|
3418
|
-
cells.push({
|
|
3419
|
-
row: r,
|
|
3420
|
-
col: c,
|
|
3421
|
-
rowSpan,
|
|
3422
|
-
colSpan,
|
|
3423
|
-
bbox: {
|
|
3424
|
-
x1: colXs[c],
|
|
3425
|
-
y1: rowYs[r + rowSpan],
|
|
3426
|
-
x2: colXs[c + colSpan],
|
|
3427
|
-
y2: rowYs[r]
|
|
3428
|
-
}
|
|
3429
|
-
});
|
|
3430
|
-
}
|
|
3431
|
-
}
|
|
3432
|
-
return cells;
|
|
3433
|
-
}
|
|
3434
|
-
function hasVerticalLine(verticals, x, topY, botY, vertexRadius) {
|
|
3435
|
-
const tol = Math.max(VERTEX_MERGE_FACTOR * vertexRadius, 4);
|
|
3436
|
-
for (const v of verticals) {
|
|
3437
|
-
if (Math.abs(v.x1 - x) <= tol) {
|
|
3438
|
-
const cellH = Math.abs(topY - botY);
|
|
3439
|
-
if (cellH < 0.1) continue;
|
|
3440
|
-
const overlapTop = Math.min(v.y2, topY);
|
|
3441
|
-
const overlapBot = Math.max(v.y1, botY);
|
|
3442
|
-
const overlap = overlapTop - overlapBot;
|
|
3443
|
-
if (overlap >= cellH * 0.75) return true;
|
|
3444
|
-
}
|
|
3445
|
-
}
|
|
3446
|
-
return false;
|
|
3447
|
-
}
|
|
3448
|
-
function hasHorizontalLine(horizontals, y, leftX, rightX, vertexRadius) {
|
|
3449
|
-
const tol = Math.max(VERTEX_MERGE_FACTOR * vertexRadius, 4);
|
|
3450
|
-
for (const h of horizontals) {
|
|
3451
|
-
if (Math.abs(h.y1 - y) <= tol) {
|
|
3452
|
-
const cellW = Math.abs(rightX - leftX);
|
|
3453
|
-
if (cellW < 0.1) continue;
|
|
3454
|
-
const overlapLeft = Math.max(h.x1, leftX);
|
|
3455
|
-
const overlapRight = Math.min(h.x2, rightX);
|
|
3456
|
-
const overlap = overlapRight - overlapLeft;
|
|
3457
|
-
if (overlap >= cellW * 0.75) return true;
|
|
3458
|
-
}
|
|
3459
|
-
}
|
|
3460
|
-
return false;
|
|
3461
|
-
}
|
|
3462
|
-
function mapTextToCells(items, cells) {
|
|
3463
|
-
const result = /* @__PURE__ */ new Map();
|
|
3464
|
-
for (const cell of cells) {
|
|
3465
|
-
result.set(cell, []);
|
|
3466
|
-
}
|
|
3467
|
-
for (const item of items) {
|
|
3468
|
-
const pad = CELL_PADDING;
|
|
3469
|
-
let bestCell = null;
|
|
3470
|
-
let bestScore = 0;
|
|
3471
|
-
for (const cell of cells) {
|
|
3472
|
-
const ix1 = Math.max(item.x, cell.bbox.x1 - pad);
|
|
3473
|
-
const ix2 = Math.min(item.x + item.w, cell.bbox.x2 + pad);
|
|
3474
|
-
const iy1 = Math.max(item.y, cell.bbox.y1 - pad);
|
|
3475
|
-
const iy2 = Math.min(item.y + (item.h || item.fontSize), cell.bbox.y2 + pad);
|
|
3476
|
-
if (ix1 >= ix2 || iy1 >= iy2) continue;
|
|
3477
|
-
const intersectArea = (ix2 - ix1) * (iy2 - iy1);
|
|
3478
|
-
const itemArea = Math.max(item.w, 1) * Math.max(item.h || item.fontSize, 1);
|
|
3479
|
-
const score = intersectArea / itemArea;
|
|
3480
|
-
if (score > bestScore) {
|
|
3481
|
-
bestScore = score;
|
|
3482
|
-
bestCell = cell;
|
|
3483
|
-
}
|
|
3484
|
-
}
|
|
3485
|
-
if (bestCell && bestScore > 0.3) {
|
|
3486
|
-
result.get(bestCell).push(item);
|
|
3487
|
-
}
|
|
3488
|
-
}
|
|
3489
|
-
return result;
|
|
3490
|
-
}
|
|
3491
|
-
function cellTextToString(items) {
|
|
3492
|
-
if (items.length === 0) return "";
|
|
3493
|
-
if (items.length === 1) return items[0].text;
|
|
3494
|
-
const sorted = [...items].sort((a, b) => b.y - a.y || a.x - b.x);
|
|
3495
|
-
const lines = [];
|
|
3496
|
-
let curLine = [sorted[0]];
|
|
3497
|
-
let curY = sorted[0].y;
|
|
3498
|
-
for (let i = 1; i < sorted.length; i++) {
|
|
3499
|
-
const tol = Math.max(3, Math.min(sorted[i].fontSize, curLine[0].fontSize) * 0.6);
|
|
3500
|
-
if (Math.abs(sorted[i].y - curY) <= tol) {
|
|
3501
|
-
curLine.push(sorted[i]);
|
|
3502
|
-
} else {
|
|
3503
|
-
lines.push(curLine);
|
|
3504
|
-
curLine = [sorted[i]];
|
|
3505
|
-
curY = sorted[i].y;
|
|
3506
|
-
}
|
|
3507
|
-
}
|
|
3508
|
-
lines.push(curLine);
|
|
3509
|
-
const textLines = lines.map((line) => {
|
|
3510
|
-
const s = line.sort((a, b) => a.x - b.x);
|
|
3511
|
-
if (s.length === 1) return s[0].text;
|
|
3512
|
-
const evenSpaced = detectEvenSpacedItems(s);
|
|
3513
|
-
let result = s[0].text;
|
|
3514
|
-
for (let j = 1; j < s.length; j++) {
|
|
3515
|
-
if (evenSpaced[j]) {
|
|
3516
|
-
result += s[j].text;
|
|
3517
|
-
continue;
|
|
3518
|
-
}
|
|
3519
|
-
const gap = s[j].x - (s[j - 1].x + s[j - 1].w);
|
|
3520
|
-
const avgFs = (s[j].fontSize + s[j - 1].fontSize) / 2;
|
|
3521
|
-
const prevIsKorean = /[가-힣]$/.test(result);
|
|
3522
|
-
const currIsKorean = /^[가-힣]/.test(s[j].text);
|
|
3523
|
-
if (gap < avgFs * 0.15) {
|
|
3524
|
-
result += s[j].text;
|
|
3525
|
-
} else if (gap < avgFs * 0.35 && (prevIsKorean || currIsKorean)) {
|
|
3526
|
-
result += s[j].text;
|
|
3527
|
-
} else {
|
|
3528
|
-
result += " " + s[j].text;
|
|
3529
|
-
}
|
|
3530
|
-
}
|
|
3531
|
-
return result;
|
|
3532
|
-
});
|
|
3533
|
-
return mergeCellTextLines(textLines);
|
|
3534
|
-
}
|
|
3535
|
-
function detectEvenSpacedItems(items) {
|
|
3536
|
-
const result = new Array(items.length).fill(false);
|
|
3537
|
-
if (items.length < 3) return result;
|
|
3538
|
-
let runStart = -1;
|
|
3539
|
-
for (let i = 0; i < items.length; i++) {
|
|
3540
|
-
const isShortKorean = /^[가-힣]{1}$/.test(items[i].text) || /^[\d]{1}$/.test(items[i].text);
|
|
3541
|
-
if (isShortKorean && runStart >= 0 && i > 0) {
|
|
3542
|
-
const gap = items[i].x - (items[i - 1].x + items[i - 1].w);
|
|
3543
|
-
const maxRunGap = Math.max(items[i].fontSize * 3, 30);
|
|
3544
|
-
if (gap > maxRunGap) {
|
|
3545
|
-
if (i - runStart >= 3) markEvenRun(items, result, runStart, i);
|
|
3546
|
-
runStart = i;
|
|
3547
|
-
continue;
|
|
3548
|
-
}
|
|
3549
|
-
}
|
|
3550
|
-
if (isShortKorean) {
|
|
3551
|
-
if (runStart < 0) runStart = i;
|
|
3552
|
-
} else {
|
|
3553
|
-
if (runStart >= 0 && i - runStart >= 3) {
|
|
3554
|
-
markEvenRun(items, result, runStart, i);
|
|
3555
|
-
}
|
|
3556
|
-
runStart = -1;
|
|
3557
|
-
}
|
|
3558
|
-
}
|
|
3559
|
-
if (runStart >= 0 && items.length - runStart >= 3) {
|
|
3560
|
-
markEvenRun(items, result, runStart, items.length);
|
|
3561
|
-
}
|
|
3562
|
-
return result;
|
|
3563
|
-
}
|
|
3564
|
-
function markEvenRun(items, result, start, end) {
|
|
3565
|
-
const gaps = [];
|
|
3566
|
-
for (let i = start + 1; i < end; i++) {
|
|
3567
|
-
gaps.push(items[i].x - (items[i - 1].x + items[i - 1].w));
|
|
3568
|
-
}
|
|
3569
|
-
const posGaps = gaps.filter((g2) => g2 > 0);
|
|
3570
|
-
if (posGaps.length < 2) return;
|
|
3571
|
-
let minGap = Infinity, maxGap = -Infinity;
|
|
3572
|
-
for (const g2 of posGaps) {
|
|
3573
|
-
if (g2 < minGap) minGap = g2;
|
|
3574
|
-
if (g2 > maxGap) maxGap = g2;
|
|
3575
|
-
}
|
|
3576
|
-
const avgFs = items[start].fontSize;
|
|
3577
|
-
if (minGap >= avgFs * 0.1 && maxGap <= avgFs * 3 && maxGap / Math.max(minGap, 0.1) <= 3) {
|
|
3578
|
-
for (let i = start + 1; i < end; i++) {
|
|
3579
|
-
result[i] = true;
|
|
3580
|
-
}
|
|
3581
|
-
}
|
|
3582
|
-
}
|
|
3583
|
-
function mergeCellTextLines(textLines) {
|
|
3584
|
-
if (textLines.length <= 1) return textLines[0] || "";
|
|
3585
|
-
const merged = [textLines[0]];
|
|
3586
|
-
for (let i = 1; i < textLines.length; i++) {
|
|
3587
|
-
const prev = merged[merged.length - 1];
|
|
3588
|
-
const curr = textLines[i];
|
|
3589
|
-
if (/[가-힣]$/.test(prev) && /^[가-힣]+$/.test(curr) && curr.length <= 8 && !curr.includes(" ")) {
|
|
3590
|
-
merged[merged.length - 1] = prev + curr;
|
|
3591
|
-
} else if (curr.trim().length <= 3 && /^[)\]%}]/.test(curr.trim())) {
|
|
3592
|
-
merged[merged.length - 1] = prev + curr.trim();
|
|
3593
|
-
} else if (/[,(]$/.test(prev.trim()) && curr.trim().length <= 15) {
|
|
3594
|
-
merged[merged.length - 1] = prev + curr.trim();
|
|
3595
|
-
} else if (/[\d,]$/.test(prev) && /^[\d,]+[)\]]?$/.test(curr.trim()) && curr.trim().length <= 10) {
|
|
3596
|
-
merged[merged.length - 1] = prev + curr.trim();
|
|
3597
|
-
} else {
|
|
3598
|
-
merged.push(curr);
|
|
3599
|
-
}
|
|
3600
|
-
}
|
|
3601
|
-
return merged.join("\n");
|
|
3602
|
-
}
|
|
3603
|
-
|
|
3604
|
-
// src/pdf/cluster-detector.ts
|
|
3605
|
-
var Y_TOL = 3;
|
|
3606
|
-
var COL_CLUSTER_TOL = 15;
|
|
3607
|
-
var MIN_ROWS = 3;
|
|
3608
|
-
var MIN_COLS = 2;
|
|
3609
|
-
var MIN_GAP_FACTOR = 2;
|
|
3610
|
-
var MIN_GAP_ABSOLUTE = 20;
|
|
3611
|
-
var MIN_COL_FILL_RATIO = 0.4;
|
|
3612
|
-
function detectClusterTables(items, pageNum) {
|
|
3613
|
-
if (items.length < MIN_ROWS * MIN_COLS) return [];
|
|
3614
|
-
const { merged, originMap } = mergeEvenSpacedClusters(items);
|
|
3615
|
-
const rows = groupByBaseline(merged);
|
|
3616
|
-
if (rows.length < MIN_ROWS) return [];
|
|
3617
|
-
const results = [];
|
|
3618
|
-
const headerResult = detectHeaderRow(rows);
|
|
3619
|
-
if (headerResult) {
|
|
3620
|
-
const { columns, headerIdx } = headerResult;
|
|
3621
|
-
const headerRow = rows[headerIdx];
|
|
3622
|
-
const headerItems = [...headerRow.items].sort((a, b) => a.x - b.x);
|
|
3623
|
-
const headerAndBelow = rows.slice(headerIdx);
|
|
3624
|
-
const mergedRows = mergeMultiLineRows(headerAndBelow, columns);
|
|
3625
|
-
const tableRegions = findTableRegionsByHeader(mergedRows, columns, headerItems);
|
|
3626
|
-
for (const region of tableRegions) {
|
|
3627
|
-
const table = buildClusterTable(region.rows, columns, pageNum);
|
|
3628
|
-
if (table) {
|
|
3629
|
-
expandUsedItems(table.usedItems, originMap);
|
|
3630
|
-
results.push(table);
|
|
3631
|
-
}
|
|
3632
|
-
}
|
|
3633
|
-
}
|
|
3634
|
-
if (results.length === 0) {
|
|
3635
|
-
const suspiciousRows = rows.filter((row) => hasSuspiciousGaps(row));
|
|
3636
|
-
if (suspiciousRows.length >= MIN_ROWS) {
|
|
3637
|
-
const columns = extractColumnClusters(suspiciousRows);
|
|
3638
|
-
if (columns.length >= MIN_COLS) {
|
|
3639
|
-
const tableRegions = findTableRegions(rows, columns);
|
|
3640
|
-
for (const region of tableRegions) {
|
|
3641
|
-
const mergedRows = mergeMultiLineRows(region.rows, columns);
|
|
3642
|
-
const table = buildClusterTable(mergedRows, columns, pageNum);
|
|
3643
|
-
if (table) {
|
|
3644
|
-
expandUsedItems(table.usedItems, originMap);
|
|
3645
|
-
results.push(table);
|
|
3646
|
-
}
|
|
3647
|
-
}
|
|
3648
|
-
}
|
|
3649
|
-
}
|
|
3650
|
-
}
|
|
3651
|
-
return results;
|
|
3652
|
-
}
|
|
3653
|
-
function mergeEvenSpacedClusters(items) {
|
|
3654
|
-
const originMap = /* @__PURE__ */ new Map();
|
|
3655
|
-
const rows = groupByBaseline(items);
|
|
3656
|
-
const merged = [];
|
|
3657
|
-
for (const row of rows) {
|
|
3658
|
-
const sorted = [...row.items].sort((a, b) => a.x - b.x);
|
|
3659
|
-
let i = 0;
|
|
3660
|
-
while (i < sorted.length) {
|
|
3661
|
-
if (/^[가-힣\d]$/.test(sorted[i].text)) {
|
|
3662
|
-
let runEnd = i + 1;
|
|
3663
|
-
while (runEnd < sorted.length && /^[가-힣\d]$/.test(sorted[runEnd].text)) {
|
|
3664
|
-
const gap = sorted[runEnd].x - (sorted[runEnd - 1].x + sorted[runEnd - 1].w);
|
|
3665
|
-
const fs = sorted[runEnd].fontSize;
|
|
3666
|
-
if (gap < fs * 0.1 || gap > fs * 3) break;
|
|
3667
|
-
runEnd++;
|
|
3668
|
-
}
|
|
3669
|
-
if (runEnd - i >= 3) {
|
|
3670
|
-
const gaps = [];
|
|
3671
|
-
for (let g2 = i + 1; g2 < runEnd; g2++) {
|
|
3672
|
-
gaps.push(sorted[g2].x - (sorted[g2 - 1].x + sorted[g2 - 1].w));
|
|
3673
|
-
}
|
|
3674
|
-
let minG = Infinity, maxG = -Infinity;
|
|
3675
|
-
for (const g2 of gaps) {
|
|
3676
|
-
if (g2 < minG) minG = g2;
|
|
3677
|
-
if (g2 > maxG) maxG = g2;
|
|
3678
|
-
}
|
|
3679
|
-
if (minG > 0 && maxG / minG <= 3) {
|
|
3680
|
-
const run = sorted.slice(i, runEnd);
|
|
3681
|
-
const text = run.map((r) => r.text).join("");
|
|
3682
|
-
const first = run[0], last = run[runEnd - i - 1];
|
|
3683
|
-
const item = {
|
|
3684
|
-
text,
|
|
3685
|
-
x: first.x,
|
|
3686
|
-
y: first.y,
|
|
3687
|
-
w: last.x + last.w - first.x,
|
|
3688
|
-
h: first.h,
|
|
3689
|
-
fontSize: first.fontSize,
|
|
3690
|
-
fontName: first.fontName
|
|
3691
|
-
};
|
|
3692
|
-
originMap.set(item, run);
|
|
3693
|
-
merged.push(item);
|
|
3694
|
-
i = runEnd;
|
|
3695
|
-
continue;
|
|
3696
|
-
}
|
|
3697
|
-
}
|
|
3698
|
-
}
|
|
3699
|
-
merged.push(sorted[i]);
|
|
3700
|
-
i++;
|
|
3701
|
-
}
|
|
3702
|
-
}
|
|
3703
|
-
return { merged, originMap };
|
|
3704
|
-
}
|
|
3705
|
-
function expandUsedItems(usedItems, originMap) {
|
|
3706
|
-
const toAdd = [];
|
|
3707
|
-
for (const item of usedItems) {
|
|
3708
|
-
const origins = originMap.get(item);
|
|
3709
|
-
if (origins) for (const o of origins) toAdd.push(o);
|
|
3710
|
-
}
|
|
3711
|
-
for (const a of toAdd) usedItems.add(a);
|
|
3712
|
-
}
|
|
3713
|
-
function detectHeaderRow(rows) {
|
|
3714
|
-
const allItems = rows.flatMap((r) => r.items);
|
|
3715
|
-
if (allItems.length === 0) return null;
|
|
3716
|
-
let allMinX = Infinity, allMaxX = -Infinity;
|
|
3717
|
-
for (const i of allItems) {
|
|
3718
|
-
if (i.x < allMinX) allMinX = i.x;
|
|
3719
|
-
const r = i.x + i.w;
|
|
3720
|
-
if (r > allMaxX) allMaxX = r;
|
|
3721
|
-
}
|
|
3722
|
-
const pageSpan = allMaxX - allMinX;
|
|
3723
|
-
if (pageSpan <= 0) return null;
|
|
3724
|
-
for (let ri = 0; ri < rows.length; ri++) {
|
|
3725
|
-
const row = rows[ri];
|
|
3726
|
-
if (row.items.length < MIN_COLS || row.items.length > 6) continue;
|
|
3727
|
-
if (row.items.some((i) => i.text.length > 8)) continue;
|
|
3728
|
-
if (!row.items.some((i) => /[가-힣]/.test(i.text))) continue;
|
|
3729
|
-
if (row.items.some((i) => /^[□■○●·※▶▷◆◇\-]/.test(i.text))) continue;
|
|
3730
|
-
const sorted = [...row.items].sort((a, b) => a.x - b.x);
|
|
3731
|
-
const xSpan = sorted[sorted.length - 1].x + sorted[sorted.length - 1].w - sorted[0].x;
|
|
3732
|
-
if (xSpan / pageSpan < 0.4) continue;
|
|
3733
|
-
const avgFs = sorted.reduce((s, i) => s + i.fontSize, 0) / sorted.length;
|
|
3734
|
-
let hasLargeGap = false;
|
|
3735
|
-
for (let i = 1; i < sorted.length; i++) {
|
|
3736
|
-
const gap = sorted[i].x - (sorted[i - 1].x + sorted[i - 1].w);
|
|
3737
|
-
if (gap >= avgFs * 2.5) {
|
|
3738
|
-
hasLargeGap = true;
|
|
3739
|
-
break;
|
|
3740
|
-
}
|
|
3741
|
-
}
|
|
3742
|
-
if (!hasLargeGap) continue;
|
|
3743
|
-
const columns = sorted.map((item) => ({ x: item.x, count: 0 }));
|
|
3744
|
-
let matchCount = 0;
|
|
3745
|
-
for (let j = ri + 1; j < rows.length && matchCount < MIN_ROWS + 2; j++) {
|
|
3746
|
-
const matched = countMatchedColumnsRange(rows[j], columns, sorted);
|
|
3747
|
-
if (matched >= MIN_COLS) matchCount++;
|
|
3748
|
-
}
|
|
3749
|
-
if (matchCount < MIN_ROWS) continue;
|
|
3750
|
-
return { columns, headerIdx: ri };
|
|
3751
|
-
}
|
|
3752
|
-
return null;
|
|
3753
|
-
}
|
|
3754
|
-
function mergeMultiLineRows(rows, columns) {
|
|
3755
|
-
if (rows.length <= 1) return rows;
|
|
3756
|
-
const result = [rows[0]];
|
|
3757
|
-
const allFontSizes = rows.flatMap((r) => r.items).map((i) => i.fontSize);
|
|
3758
|
-
const avgFontSize = allFontSizes.length > 0 ? allFontSizes.reduce((s, v) => s + v, 0) / allFontSizes.length : 12;
|
|
3759
|
-
for (let i = 1; i < rows.length; i++) {
|
|
3760
|
-
const prev = result[result.length - 1];
|
|
3761
|
-
const curr = rows[i];
|
|
3762
|
-
const yGap = Math.abs(prev.y - curr.y);
|
|
3763
|
-
const matchedCols = countMatchedColumns(curr, columns);
|
|
3764
|
-
if (yGap < avgFontSize * 1.8 && curr.items.length <= 2 && (matchedCols < MIN_COLS || curr.items.length === 1)) {
|
|
3765
|
-
result[result.length - 1] = {
|
|
3766
|
-
y: prev.y,
|
|
3767
|
-
items: [...prev.items, ...curr.items]
|
|
3768
|
-
};
|
|
3769
|
-
} else {
|
|
3770
|
-
result.push(curr);
|
|
3771
|
-
}
|
|
3772
|
-
}
|
|
3773
|
-
return result;
|
|
3774
|
-
}
|
|
3775
|
-
function groupByBaseline(items) {
|
|
3776
|
-
if (items.length === 0) return [];
|
|
3777
|
-
const sorted = [...items].sort((a, b) => b.y - a.y || a.x - b.x);
|
|
3778
|
-
const rows = [];
|
|
3779
|
-
let curItems = [sorted[0]];
|
|
3780
|
-
let curY = sorted[0].y;
|
|
3781
|
-
for (let i = 1; i < sorted.length; i++) {
|
|
3782
|
-
if (Math.abs(sorted[i].y - curY) <= Y_TOL) {
|
|
3783
|
-
curItems.push(sorted[i]);
|
|
3784
|
-
} else {
|
|
3785
|
-
rows.push({ y: curY, items: curItems });
|
|
3786
|
-
curItems = [sorted[i]];
|
|
3787
|
-
curY = sorted[i].y;
|
|
3788
|
-
}
|
|
3789
|
-
}
|
|
3790
|
-
if (curItems.length > 0) rows.push({ y: curY, items: curItems });
|
|
3791
|
-
return rows;
|
|
3792
|
-
}
|
|
3793
|
-
function hasSuspiciousGaps(row) {
|
|
3794
|
-
if (row.items.length < 2) return false;
|
|
3795
|
-
const sorted = [...row.items].sort((a, b) => a.x - b.x);
|
|
3796
|
-
if (sorted.length === 2 && sorted[1].text.length > 20) return false;
|
|
3797
|
-
const avgFontSize = sorted.reduce((s, i) => s + i.fontSize, 0) / sorted.length;
|
|
3798
|
-
const minGap = Math.max(avgFontSize * MIN_GAP_FACTOR, MIN_GAP_ABSOLUTE);
|
|
3799
|
-
for (let i = 1; i < sorted.length; i++) {
|
|
3800
|
-
const gap = sorted[i].x - (sorted[i - 1].x + sorted[i - 1].w);
|
|
3801
|
-
if (gap >= minGap) return true;
|
|
3802
|
-
}
|
|
3803
|
-
return false;
|
|
3804
|
-
}
|
|
3805
|
-
function extractColumnClusters(rows) {
|
|
3806
|
-
const allX = [];
|
|
3807
|
-
for (const row of rows) {
|
|
3808
|
-
for (const item of row.items) allX.push(item.x);
|
|
3809
|
-
}
|
|
3810
|
-
if (allX.length === 0) return [];
|
|
3811
|
-
allX.sort((a, b) => a - b);
|
|
3812
|
-
const clusters = [];
|
|
3813
|
-
let clusterStart = 0;
|
|
3814
|
-
for (let i = 1; i <= allX.length; i++) {
|
|
3815
|
-
if (i === allX.length || allX[i] - allX[i - 1] > COL_CLUSTER_TOL) {
|
|
3816
|
-
const slice = allX.slice(clusterStart, i);
|
|
3817
|
-
const avg = Math.round(slice.reduce((s, v) => s + v, 0) / slice.length);
|
|
3818
|
-
clusters.push({ x: avg, count: slice.length });
|
|
3819
|
-
clusterStart = i;
|
|
3820
|
-
}
|
|
3821
|
-
}
|
|
3822
|
-
const minCount = Math.max(2, Math.floor(rows.length * MIN_COL_FILL_RATIO));
|
|
3823
|
-
return clusters.filter((c) => c.count >= minCount).sort((a, b) => a.x - b.x);
|
|
3824
|
-
}
|
|
3825
|
-
function findTableRegionsByHeader(allRows, columns, headerItems) {
|
|
3826
|
-
const regions = [];
|
|
3827
|
-
let currentRegion = [];
|
|
3828
|
-
let missStreak = 0;
|
|
3829
|
-
for (const row of allRows) {
|
|
3830
|
-
const matchedCols = countMatchedColumnsRange(row, columns, headerItems);
|
|
3831
|
-
if (matchedCols >= MIN_COLS) {
|
|
3832
|
-
currentRegion.push(row);
|
|
3833
|
-
missStreak = 0;
|
|
3834
|
-
} else if (currentRegion.length > 0 && (row.items.length <= 2 || missStreak === 0)) {
|
|
3835
|
-
currentRegion.push(row);
|
|
3836
|
-
missStreak++;
|
|
3837
|
-
} else {
|
|
3838
|
-
while (currentRegion.length > 0) {
|
|
3839
|
-
const last = currentRegion[currentRegion.length - 1];
|
|
3840
|
-
if (countMatchedColumnsRange(last, columns, headerItems) >= MIN_COLS) break;
|
|
3841
|
-
currentRegion.pop();
|
|
3842
|
-
}
|
|
3843
|
-
if (currentRegion.length >= MIN_ROWS) {
|
|
3844
|
-
regions.push({ rows: [...currentRegion] });
|
|
3845
|
-
}
|
|
3846
|
-
currentRegion = [];
|
|
3847
|
-
missStreak = 0;
|
|
3848
|
-
}
|
|
3849
|
-
}
|
|
3850
|
-
while (currentRegion.length > 0) {
|
|
3851
|
-
const last = currentRegion[currentRegion.length - 1];
|
|
3852
|
-
if (countMatchedColumnsRange(last, columns, headerItems) >= MIN_COLS) break;
|
|
3853
|
-
currentRegion.pop();
|
|
3854
|
-
}
|
|
3855
|
-
if (currentRegion.length >= MIN_ROWS) {
|
|
3856
|
-
regions.push({ rows: currentRegion });
|
|
3857
|
-
}
|
|
3858
|
-
return regions;
|
|
3859
|
-
}
|
|
3860
|
-
function findTableRegions(allRows, columns) {
|
|
3861
|
-
const regions = [];
|
|
3862
|
-
let currentRegion = [];
|
|
3863
|
-
for (const row of allRows) {
|
|
3864
|
-
const matchedCols = countMatchedColumns(row, columns);
|
|
3865
|
-
if (matchedCols >= MIN_COLS) {
|
|
3866
|
-
currentRegion.push(row);
|
|
3867
|
-
} else if (row.items.length === 1) {
|
|
3868
|
-
if (currentRegion.length > 0) {
|
|
3869
|
-
currentRegion.push(row);
|
|
3870
|
-
}
|
|
3871
|
-
} else {
|
|
3872
|
-
if (currentRegion.length >= MIN_ROWS) {
|
|
3873
|
-
regions.push({ rows: [...currentRegion] });
|
|
3874
|
-
}
|
|
3875
|
-
currentRegion = [];
|
|
3876
|
-
}
|
|
3877
|
-
}
|
|
3878
|
-
if (currentRegion.length >= MIN_ROWS) {
|
|
3879
|
-
regions.push({ rows: currentRegion });
|
|
3880
|
-
}
|
|
3881
|
-
return regions;
|
|
3882
|
-
}
|
|
3883
|
-
function countMatchedColumns(row, columns) {
|
|
3884
|
-
const matched = /* @__PURE__ */ new Set();
|
|
3885
|
-
for (const item of row.items) {
|
|
3886
|
-
for (let ci = 0; ci < columns.length; ci++) {
|
|
3887
|
-
if (Math.abs(item.x - columns[ci].x) <= COL_CLUSTER_TOL * 2) {
|
|
3888
|
-
matched.add(ci);
|
|
3889
|
-
break;
|
|
3890
|
-
}
|
|
3891
|
-
}
|
|
3892
|
-
}
|
|
3893
|
-
return matched.size;
|
|
3894
|
-
}
|
|
3895
|
-
function countMatchedColumnsRange(row, columns, headerItems) {
|
|
3896
|
-
const boundaries = [];
|
|
3897
|
-
for (let ci = 0; ci < headerItems.length; ci++) {
|
|
3898
|
-
const left = ci === 0 ? 0 : (headerItems[ci - 1].x + headerItems[ci - 1].w + headerItems[ci].x) / 2;
|
|
3899
|
-
const right = ci === headerItems.length - 1 ? Infinity : (headerItems[ci].x + headerItems[ci].w + headerItems[ci + 1].x) / 2;
|
|
3900
|
-
boundaries.push({ left, right });
|
|
3901
|
-
}
|
|
3902
|
-
const matched = /* @__PURE__ */ new Set();
|
|
3903
|
-
for (const item of row.items) {
|
|
3904
|
-
for (let ci = 0; ci < boundaries.length; ci++) {
|
|
3905
|
-
if (item.x >= boundaries[ci].left && item.x < boundaries[ci].right) {
|
|
3906
|
-
matched.add(ci);
|
|
3907
|
-
break;
|
|
3908
|
-
}
|
|
3909
|
-
}
|
|
3910
|
-
}
|
|
3911
|
-
return matched.size;
|
|
3912
|
-
}
|
|
3913
|
-
function assignRowItems(items, columns, numCols) {
|
|
3914
|
-
if (items.length === 0) return [];
|
|
3915
|
-
const sorted = [...items].sort((a, b) => a.x - b.x);
|
|
3916
|
-
const colCenters = columns.map((c) => c.x);
|
|
3917
|
-
const gaps = [];
|
|
3918
|
-
for (let i = 1; i < sorted.length; i++) {
|
|
3919
|
-
gaps.push({ idx: i, size: sorted[i].x - (sorted[i - 1].x + sorted[i - 1].w) });
|
|
3920
|
-
}
|
|
3921
|
-
const gapSizes = gaps.map((g2) => g2.size).sort((a, b) => a - b);
|
|
3922
|
-
const medianGap = gapSizes.length > 0 ? gapSizes[Math.floor(gapSizes.length / 2)] : 0;
|
|
3923
|
-
const gapThreshold = sorted.length <= numCols + 1 ? 12 : Math.max(medianGap * 2.5, 12);
|
|
3924
|
-
const significantGaps = gaps.filter((g2) => g2.size >= gapThreshold).sort((a, b) => b.size - a.size).slice(0, numCols - 1).sort((a, b) => a.idx - b.idx);
|
|
3925
|
-
const groups = [];
|
|
3926
|
-
let start = 0;
|
|
3927
|
-
for (const gap of significantGaps) {
|
|
3928
|
-
groups.push(sorted.slice(start, gap.idx));
|
|
3929
|
-
start = gap.idx;
|
|
3930
|
-
}
|
|
3931
|
-
groups.push(sorted.slice(start));
|
|
3932
|
-
const result = [];
|
|
3933
|
-
const usedCols = /* @__PURE__ */ new Set();
|
|
3934
|
-
const groupCenters = groups.map((g2) => {
|
|
3935
|
-
let minX = Infinity, maxX = -Infinity;
|
|
3936
|
-
for (const i of g2) {
|
|
3937
|
-
if (i.x < minX) minX = i.x;
|
|
3938
|
-
const r = i.x + i.w;
|
|
3939
|
-
if (r > maxX) maxX = r;
|
|
3940
|
-
}
|
|
3941
|
-
return (minX + maxX) / 2;
|
|
3942
|
-
});
|
|
3943
|
-
const assignments = [];
|
|
3944
|
-
for (let gi = 0; gi < groups.length; gi++) {
|
|
3945
|
-
for (let ci = 0; ci < numCols; ci++) {
|
|
3946
|
-
assignments.push({ gi, ci, dist: Math.abs(groupCenters[gi] - colCenters[ci]) });
|
|
3947
|
-
}
|
|
3948
|
-
}
|
|
3949
|
-
assignments.sort((a, b) => a.dist - b.dist);
|
|
3950
|
-
const assignedGroups = /* @__PURE__ */ new Set();
|
|
3951
|
-
for (const { gi, ci } of assignments) {
|
|
3952
|
-
if (assignedGroups.has(gi) || usedCols.has(ci)) continue;
|
|
3953
|
-
result.push({ col: ci, items: groups[gi] });
|
|
3954
|
-
assignedGroups.add(gi);
|
|
3955
|
-
usedCols.add(ci);
|
|
3956
|
-
}
|
|
3957
|
-
for (let gi = 0; gi < groups.length; gi++) {
|
|
3958
|
-
if (assignedGroups.has(gi)) continue;
|
|
3959
|
-
let bestCol = 0, bestDist = Infinity;
|
|
3960
|
-
for (let ci = 0; ci < numCols; ci++) {
|
|
3961
|
-
const d = Math.abs(groupCenters[gi] - colCenters[ci]);
|
|
3962
|
-
if (d < bestDist) {
|
|
3963
|
-
bestDist = d;
|
|
3964
|
-
bestCol = ci;
|
|
3965
|
-
}
|
|
3966
|
-
}
|
|
3967
|
-
result.push({ col: bestCol, items: groups[gi] });
|
|
3968
|
-
}
|
|
3969
|
-
return result;
|
|
3970
|
-
}
|
|
3971
|
-
function buildClusterTable(rows, columns, pageNum) {
|
|
3972
|
-
const numCols = columns.length;
|
|
3973
|
-
const numRows = rows.length;
|
|
3974
|
-
if (numRows < MIN_ROWS || numCols < MIN_COLS) return null;
|
|
3975
|
-
const cells = Array.from(
|
|
3976
|
-
{ length: numRows },
|
|
3977
|
-
() => Array.from({ length: numCols }, () => ({ text: "", colSpan: 1, rowSpan: 1 }))
|
|
3978
|
-
);
|
|
3979
|
-
const usedItems = /* @__PURE__ */ new Set();
|
|
3980
|
-
for (let r = 0; r < numRows; r++) {
|
|
3981
|
-
const row = rows[r];
|
|
3982
|
-
if (row.items.length === 1 && numCols > 1) {
|
|
3983
|
-
cells[r][0] = { text: row.items[0].text, colSpan: numCols, rowSpan: 1 };
|
|
3984
|
-
usedItems.add(row.items[0]);
|
|
3985
|
-
continue;
|
|
3986
|
-
}
|
|
3987
|
-
const assignments = assignRowItems(row.items, columns, numCols);
|
|
3988
|
-
for (const { col, items } of assignments) {
|
|
3989
|
-
const text = items.map((i) => i.text).join(" ");
|
|
3990
|
-
const existing = cells[r][col].text;
|
|
3991
|
-
cells[r][col].text = existing ? existing + " " + text : text;
|
|
3992
|
-
for (const item of items) usedItems.add(item);
|
|
3993
|
-
}
|
|
3994
|
-
}
|
|
3995
|
-
let emptyRows = 0;
|
|
3996
|
-
for (const row of cells) {
|
|
3997
|
-
if (row.every((c) => c.text === "")) emptyRows++;
|
|
3998
|
-
}
|
|
3999
|
-
if (emptyRows > numRows * 0.5) return null;
|
|
4000
|
-
for (let c = 0; c < numCols; c++) {
|
|
4001
|
-
const hasValue = cells.some((row) => row[c].text !== "");
|
|
4002
|
-
if (!hasValue) return null;
|
|
4003
|
-
}
|
|
4004
|
-
for (let r = numRows - 1; r >= 1; r--) {
|
|
4005
|
-
const nonEmptyCols = cells[r].filter((c) => c.text.trim()).length;
|
|
4006
|
-
if (nonEmptyCols !== 1) continue;
|
|
4007
|
-
if (cells[r][0].text.trim() !== "") continue;
|
|
4008
|
-
const contentText = cells[r].find((c) => c.text.trim())?.text.trim() || "";
|
|
4009
|
-
if (/^[○●▶\-·]/.test(contentText)) continue;
|
|
4010
|
-
for (let pr = r - 1; pr >= 0; pr--) {
|
|
4011
|
-
if (cells[pr].some((c) => c.text.trim())) {
|
|
4012
|
-
for (let c = 0; c < numCols; c++) {
|
|
4013
|
-
const prev = cells[pr][c].text.trim();
|
|
4014
|
-
const curr = cells[r][c].text.trim();
|
|
4015
|
-
if (curr) cells[pr][c].text = prev ? prev + " " + curr : curr;
|
|
4016
|
-
}
|
|
4017
|
-
for (let c = 0; c < numCols; c++) cells[r][c].text = "";
|
|
4018
|
-
break;
|
|
4019
|
-
}
|
|
4020
|
-
}
|
|
4021
|
-
}
|
|
4022
|
-
for (let r = 0; r < cells.length - 1; r++) {
|
|
4023
|
-
const row = cells[r];
|
|
4024
|
-
const hasCol0 = row[0].text.trim() !== "";
|
|
4025
|
-
const hasColLast = numCols > 1 && row[numCols - 1].text.trim() !== "";
|
|
4026
|
-
const midEmpty = row.slice(1, numCols - 1).every((c) => c.text.trim() === "");
|
|
4027
|
-
if (hasCol0 && hasColLast && midEmpty) {
|
|
4028
|
-
const next = cells[r + 1];
|
|
4029
|
-
if (next[0].text.trim() === "" && next.some((c) => c.text.trim())) {
|
|
4030
|
-
for (let c = 1; c < numCols; c++) {
|
|
4031
|
-
const curr = next[c].text.trim();
|
|
4032
|
-
if (curr) row[c].text = row[c].text.trim() ? row[c].text.trim() + " " + curr : curr;
|
|
4033
|
-
}
|
|
4034
|
-
for (let c = 0; c < numCols; c++) next[c].text = "";
|
|
4035
|
-
}
|
|
4036
|
-
}
|
|
4037
|
-
}
|
|
4038
|
-
const filteredCells = cells.filter((row) => row.some((c) => c.text.trim()));
|
|
4039
|
-
const finalRowCount = filteredCells.length;
|
|
4040
|
-
if (finalRowCount < MIN_ROWS) return null;
|
|
4041
|
-
const irTable = {
|
|
4042
|
-
rows: finalRowCount,
|
|
4043
|
-
cols: numCols,
|
|
4044
|
-
cells: filteredCells,
|
|
4045
|
-
hasHeader: finalRowCount > 1
|
|
4046
|
-
};
|
|
4047
|
-
const allItems = rows.flatMap((r) => r.items);
|
|
4048
|
-
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
4049
|
-
for (const i of allItems) {
|
|
4050
|
-
if (i.x < minX) minX = i.x;
|
|
4051
|
-
if (i.y < minY) minY = i.y;
|
|
4052
|
-
if (i.x + i.w > maxX) maxX = i.x + i.w;
|
|
4053
|
-
const h = i.h > 0 ? i.h : i.fontSize;
|
|
4054
|
-
if (i.y + h > maxY) maxY = i.y + h;
|
|
4055
|
-
}
|
|
4056
|
-
return {
|
|
4057
|
-
table: irTable,
|
|
4058
|
-
bbox: { page: pageNum, x: minX, y: minY, width: maxX - minX, height: maxY - minY },
|
|
4059
|
-
usedItems
|
|
4060
|
-
};
|
|
4061
|
-
}
|
|
4062
|
-
|
|
4063
|
-
// src/pdf/polyfill.ts
|
|
4064
|
-
import * as pdfjsWorker from "pdfjs-dist/legacy/build/pdf.worker.mjs";
|
|
4065
|
-
var g = globalThis;
|
|
4066
|
-
if (typeof g.DOMMatrix === "undefined") {
|
|
4067
|
-
g.DOMMatrix = class DOMMatrix {
|
|
4068
|
-
m = [1, 0, 0, 1, 0, 0];
|
|
4069
|
-
constructor(init) {
|
|
4070
|
-
if (init) this.m = init;
|
|
4071
|
-
}
|
|
4072
|
-
};
|
|
4073
|
-
}
|
|
4074
|
-
if (typeof g.Path2D === "undefined") {
|
|
4075
|
-
g.Path2D = class Path2D {
|
|
4076
|
-
};
|
|
4077
|
-
}
|
|
4078
|
-
g.pdfjsWorker = pdfjsWorker;
|
|
4079
|
-
|
|
4080
|
-
// src/pdf/parser.ts
|
|
4081
|
-
import { getDocument, GlobalWorkerOptions } from "pdfjs-dist/legacy/build/pdf.mjs";
|
|
4082
|
-
GlobalWorkerOptions.workerSrc = "";
|
|
4083
|
-
var MAX_PAGES = 5e3;
|
|
4084
|
-
var MAX_TOTAL_TEXT = 100 * 1024 * 1024;
|
|
4085
|
-
var PDF_LOAD_TIMEOUT_MS = 3e4;
|
|
4086
|
-
async function loadPdfWithTimeout(buffer) {
|
|
4087
|
-
const loadingTask = getDocument({
|
|
4088
|
-
data: new Uint8Array(buffer),
|
|
4089
|
-
useSystemFonts: true,
|
|
4090
|
-
disableFontFace: true,
|
|
4091
|
-
isEvalSupported: false
|
|
4092
|
-
});
|
|
4093
|
-
let timer;
|
|
4094
|
-
try {
|
|
4095
|
-
return await Promise.race([
|
|
4096
|
-
loadingTask.promise,
|
|
4097
|
-
new Promise((_, reject) => {
|
|
4098
|
-
timer = setTimeout(() => {
|
|
4099
|
-
loadingTask.destroy();
|
|
4100
|
-
reject(new KordocError("PDF \uB85C\uB529 \uD0C0\uC784\uC544\uC6C3 (30\uCD08 \uCD08\uACFC)"));
|
|
4101
|
-
}, PDF_LOAD_TIMEOUT_MS);
|
|
4102
|
-
})
|
|
4103
|
-
]);
|
|
4104
|
-
} finally {
|
|
4105
|
-
if (timer !== void 0) clearTimeout(timer);
|
|
4106
|
-
}
|
|
4107
|
-
}
|
|
4108
|
-
async function parsePdfDocument(buffer, options) {
|
|
4109
|
-
const doc = await loadPdfWithTimeout(buffer);
|
|
4110
|
-
try {
|
|
4111
|
-
const pageCount = doc.numPages;
|
|
4112
|
-
if (pageCount === 0) throw new KordocError("PDF\uC5D0 \uD398\uC774\uC9C0\uAC00 \uC5C6\uC2B5\uB2C8\uB2E4.");
|
|
4113
|
-
const metadata = { pageCount };
|
|
4114
|
-
await extractPdfMetadata(doc, metadata);
|
|
4115
|
-
const blocks = [];
|
|
4116
|
-
const warnings = [];
|
|
4117
|
-
let totalChars = 0;
|
|
4118
|
-
let totalTextBytes = 0;
|
|
4119
|
-
const effectivePageCount = Math.min(pageCount, MAX_PAGES);
|
|
4120
|
-
const pageFilter = options?.pages ? parsePageRange(options.pages, effectivePageCount) : null;
|
|
4121
|
-
const totalTarget = pageFilter ? pageFilter.size : effectivePageCount;
|
|
4122
|
-
const fontSizeFreq = /* @__PURE__ */ new Map();
|
|
4123
|
-
const pageHeights = /* @__PURE__ */ new Map();
|
|
4124
|
-
let parsedPages = 0;
|
|
4125
|
-
for (let i = 1; i <= effectivePageCount; i++) {
|
|
4126
|
-
if (pageFilter && !pageFilter.has(i)) continue;
|
|
4127
|
-
try {
|
|
4128
|
-
const page = await doc.getPage(i);
|
|
4129
|
-
const tc = await page.getTextContent();
|
|
4130
|
-
const viewport = page.getViewport({ scale: 1 });
|
|
4131
|
-
pageHeights.set(i, viewport.height);
|
|
4132
|
-
const rawItems = tc.items;
|
|
4133
|
-
const items = normalizeItems(rawItems);
|
|
4134
|
-
const { visible, hiddenCount } = filterHiddenText(items, viewport.width, viewport.height);
|
|
4135
|
-
if (hiddenCount > 0) {
|
|
4136
|
-
warnings.push({ page: i, message: `${hiddenCount}\uAC1C \uC228\uACA8\uC9C4 \uD14D\uC2A4\uD2B8 \uC694\uC18C \uD544\uD130\uB9C1\uB428`, code: "HIDDEN_TEXT_FILTERED" });
|
|
4137
|
-
}
|
|
4138
|
-
for (const item of visible) {
|
|
4139
|
-
if (item.fontSize > 0) fontSizeFreq.set(item.fontSize, (fontSizeFreq.get(item.fontSize) || 0) + 1);
|
|
4140
|
-
}
|
|
4141
|
-
const opList = await page.getOperatorList();
|
|
4142
|
-
const pageBlocks = extractPageBlocksWithLines(visible, i, opList, viewport.width, viewport.height);
|
|
4143
|
-
for (const b of pageBlocks) blocks.push(b);
|
|
4144
|
-
for (const b of pageBlocks) {
|
|
4145
|
-
const t = b.text || "";
|
|
4146
|
-
totalChars += t.replace(/\s/g, "").length;
|
|
4147
|
-
totalTextBytes += t.length * 2;
|
|
4148
|
-
}
|
|
4149
|
-
if (totalTextBytes > MAX_TOTAL_TEXT) throw new KordocError("\uD14D\uC2A4\uD2B8 \uCD94\uCD9C \uD06C\uAE30 \uCD08\uACFC");
|
|
4150
|
-
parsedPages++;
|
|
4151
|
-
options?.onProgress?.(parsedPages, totalTarget);
|
|
4152
|
-
} catch (pageErr) {
|
|
4153
|
-
if (pageErr instanceof KordocError) throw pageErr;
|
|
4154
|
-
warnings.push({ page: i, message: `\uD398\uC774\uC9C0 ${i} \uD30C\uC2F1 \uC2E4\uD328: ${pageErr instanceof Error ? pageErr.message : "\uC54C \uC218 \uC5C6\uB294 \uC624\uB958"}`, code: "PARTIAL_PARSE" });
|
|
4155
|
-
}
|
|
4156
|
-
}
|
|
4157
|
-
const parsedPageCount = parsedPages || (pageFilter ? pageFilter.size : effectivePageCount);
|
|
4158
|
-
if (totalChars / Math.max(parsedPageCount, 1) < 10) {
|
|
4159
|
-
if (options?.ocr) {
|
|
4160
|
-
try {
|
|
4161
|
-
const { ocrPages } = await import("./provider-7H4CPZYS.js");
|
|
4162
|
-
const ocrBlocks = await ocrPages(doc, options.ocr, pageFilter, effectivePageCount);
|
|
4163
|
-
if (ocrBlocks.length > 0) {
|
|
4164
|
-
const ocrMarkdown = ocrBlocks.map((b) => b.text || "").filter(Boolean).join("\n\n");
|
|
4165
|
-
return { markdown: ocrMarkdown, blocks: ocrBlocks, metadata, warnings, isImageBased: true };
|
|
4166
|
-
}
|
|
4167
|
-
} catch {
|
|
4168
|
-
}
|
|
4169
|
-
}
|
|
4170
|
-
throw Object.assign(new KordocError(`\uC774\uBBF8\uC9C0 \uAE30\uBC18 PDF (${pageCount}\uD398\uC774\uC9C0, ${totalChars}\uC790)`), { isImageBased: true });
|
|
4171
|
-
}
|
|
4172
|
-
if (options?.removeHeaderFooter !== false && parsedPageCount >= 3) {
|
|
4173
|
-
const removed = removeHeaderFooterBlocks(blocks, pageHeights, warnings);
|
|
4174
|
-
for (let ri = removed.length - 1; ri >= 0; ri--) {
|
|
4175
|
-
blocks.splice(removed[ri], 1);
|
|
4176
|
-
}
|
|
4177
|
-
}
|
|
4178
|
-
const medianFontSize = computeMedianFontSizeFromFreq(fontSizeFreq);
|
|
4179
|
-
if (medianFontSize > 0) {
|
|
4180
|
-
detectHeadings(blocks, medianFontSize);
|
|
4181
|
-
}
|
|
4182
|
-
detectMarkerHeadings(blocks);
|
|
4183
|
-
const outline = blocks.filter((b) => b.type === "heading" && b.level && b.text).map((b) => ({ level: b.level, text: b.text, pageNumber: b.pageNumber }));
|
|
4184
|
-
let markdown = cleanPdfText(blocksToMarkdown(blocks));
|
|
4185
|
-
return { markdown, blocks, metadata, outline: outline.length > 0 ? outline : void 0, warnings: warnings.length > 0 ? warnings : void 0 };
|
|
4186
|
-
} finally {
|
|
4187
|
-
await doc.destroy().catch(() => {
|
|
4188
|
-
});
|
|
4189
|
-
}
|
|
4190
|
-
}
|
|
4191
|
-
async function extractPdfMetadata(doc, metadata) {
|
|
4192
|
-
try {
|
|
4193
|
-
const result = await doc.getMetadata();
|
|
4194
|
-
if (!result?.info) return;
|
|
4195
|
-
const info = result.info;
|
|
4196
|
-
if (typeof info.Title === "string" && info.Title.trim()) metadata.title = info.Title.trim();
|
|
4197
|
-
if (typeof info.Author === "string" && info.Author.trim()) metadata.author = info.Author.trim();
|
|
4198
|
-
if (typeof info.Creator === "string" && info.Creator.trim()) metadata.creator = info.Creator.trim();
|
|
4199
|
-
if (typeof info.Subject === "string" && info.Subject.trim()) metadata.description = info.Subject.trim();
|
|
4200
|
-
if (typeof info.Keywords === "string" && info.Keywords.trim()) {
|
|
4201
|
-
metadata.keywords = info.Keywords.split(/[,;]/).map((k) => k.trim()).filter(Boolean);
|
|
4202
|
-
}
|
|
4203
|
-
if (typeof info.CreationDate === "string") metadata.createdAt = parsePdfDate(info.CreationDate);
|
|
4204
|
-
if (typeof info.ModDate === "string") metadata.modifiedAt = parsePdfDate(info.ModDate);
|
|
4205
|
-
} catch {
|
|
4206
|
-
}
|
|
4207
|
-
}
|
|
4208
|
-
function parsePdfDate(dateStr) {
|
|
4209
|
-
const m = dateStr.match(/D:(\d{4})(\d{2})?(\d{2})?(\d{2})?(\d{2})?(\d{2})?/);
|
|
4210
|
-
if (!m) return void 0;
|
|
4211
|
-
const [, year, month = "01", day = "01", hour = "00", min = "00", sec = "00"] = m;
|
|
4212
|
-
return `${year}-${month}-${day}T${hour}:${min}:${sec}`;
|
|
4213
|
-
}
|
|
4214
|
-
async function extractPdfMetadataOnly(buffer) {
|
|
4215
|
-
const doc = await loadPdfWithTimeout(buffer);
|
|
4216
|
-
try {
|
|
4217
|
-
const metadata = { pageCount: doc.numPages };
|
|
4218
|
-
await extractPdfMetadata(doc, metadata);
|
|
4219
|
-
return metadata;
|
|
4220
|
-
} finally {
|
|
4221
|
-
await doc.destroy().catch(() => {
|
|
4222
|
-
});
|
|
4223
|
-
}
|
|
4224
|
-
}
|
|
4225
|
-
function filterHiddenText(items, pageWidth, pageHeight) {
|
|
4226
|
-
let hiddenCount = 0;
|
|
4227
|
-
const visible = [];
|
|
4228
|
-
for (const item of items) {
|
|
4229
|
-
if (item.isHidden) {
|
|
4230
|
-
hiddenCount++;
|
|
4231
|
-
continue;
|
|
4232
|
-
}
|
|
4233
|
-
const margin = Math.max(pageWidth, pageHeight) * 0.1;
|
|
4234
|
-
if (item.x < -margin || item.x > pageWidth + margin || item.y < -margin || item.y > pageHeight + margin) {
|
|
4235
|
-
hiddenCount++;
|
|
4236
|
-
continue;
|
|
4237
|
-
}
|
|
4238
|
-
visible.push(item);
|
|
4239
|
-
}
|
|
4240
|
-
return { visible, hiddenCount };
|
|
4241
|
-
}
|
|
4242
|
-
function computeMedianFontSizeFromFreq(freq) {
|
|
4243
|
-
if (freq.size === 0) return 0;
|
|
4244
|
-
let total = 0;
|
|
4245
|
-
for (const count of freq.values()) total += count;
|
|
4246
|
-
const sorted = [...freq.entries()].sort((a, b) => a[0] - b[0]);
|
|
4247
|
-
const mid = Math.floor(total / 2);
|
|
4248
|
-
let cumulative = 0;
|
|
4249
|
-
for (const [size, count] of sorted) {
|
|
4250
|
-
cumulative += count;
|
|
4251
|
-
if (cumulative > mid) return size;
|
|
4252
|
-
}
|
|
4253
|
-
return sorted[sorted.length - 1][0];
|
|
4254
|
-
}
|
|
4255
|
-
function detectHeadings(blocks, medianFontSize) {
|
|
4256
|
-
for (const block of blocks) {
|
|
4257
|
-
if (block.type !== "paragraph" || !block.text || !block.style?.fontSize) continue;
|
|
4258
|
-
const text = block.text.trim();
|
|
4259
|
-
if (text.length === 0 || text.length > 200) continue;
|
|
4260
|
-
if (/^\d+$/.test(text)) continue;
|
|
4261
|
-
const ratio = block.style.fontSize / medianFontSize;
|
|
4262
|
-
let level = 0;
|
|
4263
|
-
if (ratio >= HEADING_RATIO_H1) level = 1;
|
|
4264
|
-
else if (ratio >= HEADING_RATIO_H2) level = 2;
|
|
4265
|
-
else if (ratio >= HEADING_RATIO_H3) level = 3;
|
|
4266
|
-
if (level > 0) {
|
|
4267
|
-
block.type = "heading";
|
|
4268
|
-
block.level = level;
|
|
4269
|
-
block.text = collapseEvenSpacing(text);
|
|
4270
|
-
}
|
|
4271
|
-
}
|
|
4272
|
-
}
|
|
4273
|
-
function collapseEvenSpacing(text) {
|
|
4274
|
-
const tokens = text.split(" ");
|
|
4275
|
-
const singleCharCount = tokens.filter((t) => t.length === 1).length;
|
|
4276
|
-
if (tokens.length >= 3 && singleCharCount / tokens.length >= 0.7) {
|
|
4277
|
-
return tokens.join("");
|
|
4278
|
-
}
|
|
4279
|
-
return text.replace(
|
|
4280
|
-
/(?<![가-힣])[가-힣](?: [가-힣\d]){2,}(?![가-힣])/g,
|
|
4281
|
-
(match) => match.replace(/ /g, "")
|
|
4282
|
-
);
|
|
4283
|
-
}
|
|
4284
|
-
function shouldDemoteTable(table) {
|
|
4285
|
-
const allCells = table.cells.flatMap((row) => row.map((c) => c.text.trim())).filter(Boolean);
|
|
4286
|
-
const allText = allCells.join(" ");
|
|
4287
|
-
if (table.rows <= 3 && table.cols <= 3) {
|
|
4288
|
-
const totalCells2 = table.rows * table.cols;
|
|
4289
|
-
const emptyCells2 = totalCells2 - allCells.length;
|
|
4290
|
-
if (emptyCells2 >= totalCells2 * 0.3) return true;
|
|
4291
|
-
if (/[□■◆○●▶ㅇ]/.test(allText)) return true;
|
|
4292
|
-
if (/<[^>]+>/.test(allText)) return true;
|
|
4293
|
-
}
|
|
4294
|
-
if (allText.length > 200) return false;
|
|
4295
|
-
if (/[□■◆○●▶]/.test(allText) && table.rows <= 3) return true;
|
|
4296
|
-
const totalCells = table.rows * table.cols;
|
|
4297
|
-
const emptyCells = totalCells - allCells.length;
|
|
4298
|
-
if (table.rows <= 2 && emptyCells > totalCells * 0.5) return true;
|
|
4299
|
-
if (table.rows === 1 && !/\d{2,}/.test(allText)) return true;
|
|
4300
|
-
return false;
|
|
4301
|
-
}
|
|
4302
|
-
function demoteTableToText(table) {
|
|
4303
|
-
const lines = [];
|
|
4304
|
-
for (let r = 0; r < table.rows; r++) {
|
|
4305
|
-
const cells = table.cells[r].map((c) => c.text.trim()).filter(Boolean);
|
|
4306
|
-
if (cells.length === 0) continue;
|
|
4307
|
-
if (table.cols === 2 && cells.length === 2) {
|
|
4308
|
-
lines.push(`${cells[0]} : ${cells[1]}`);
|
|
4309
|
-
} else {
|
|
4310
|
-
lines.push(cells.join(" "));
|
|
4311
|
-
}
|
|
4312
|
-
}
|
|
4313
|
-
return lines.join("\n");
|
|
4314
|
-
}
|
|
4315
|
-
function detectMarkerHeadings(blocks) {
|
|
4316
|
-
for (let i = 0; i < blocks.length; i++) {
|
|
4317
|
-
const block = blocks[i];
|
|
4318
|
-
if (block.type !== "paragraph" || !block.text) continue;
|
|
4319
|
-
const text = block.text.trim();
|
|
4320
|
-
if (text.length < 50 && /^[□■◆◇▶]\s*[가-힣]/.test(text)) {
|
|
4321
|
-
block.type = "heading";
|
|
4322
|
-
block.level = 4;
|
|
4323
|
-
continue;
|
|
4324
|
-
}
|
|
4325
|
-
if (/^[가-힣]{2,6}$/.test(text) && block.style?.fontSize) {
|
|
4326
|
-
const prev = blocks[i - 1];
|
|
4327
|
-
const next = blocks[i + 1];
|
|
4328
|
-
const prevIsStructural = !prev || prev.type === "table" || prev.type === "heading" || prev.type === "separator";
|
|
4329
|
-
const nextIsStructural = !next || next.type === "table" || next.type === "heading" || next.type === "paragraph" && next.text && /^[□■◆○●]/.test(next.text.trim());
|
|
4330
|
-
if (prevIsStructural || nextIsStructural) {
|
|
4331
|
-
block.type = "heading";
|
|
4332
|
-
block.level = 3;
|
|
4333
|
-
}
|
|
4334
|
-
}
|
|
4335
|
-
}
|
|
4336
|
-
}
|
|
4337
|
-
var MAX_XYCUT_DEPTH = 50;
|
|
4338
|
-
function xyCutOrder(items, gapThreshold, depth = 0) {
|
|
4339
|
-
if (items.length === 0) return [];
|
|
4340
|
-
if (items.length <= 2 || depth >= MAX_XYCUT_DEPTH) return [items];
|
|
4341
|
-
const region = computeRegion(items);
|
|
4342
|
-
const ySplit = findYSplit(items, region, gapThreshold);
|
|
4343
|
-
if (ySplit !== null) {
|
|
4344
|
-
const upper = items.filter((i) => i.y > ySplit);
|
|
4345
|
-
const lower = items.filter((i) => i.y <= ySplit);
|
|
4346
|
-
if (upper.length > 0 && lower.length > 0 && upper.length < items.length) {
|
|
4347
|
-
return [...xyCutOrder(upper, gapThreshold, depth + 1), ...xyCutOrder(lower, gapThreshold, depth + 1)];
|
|
4348
|
-
}
|
|
4349
|
-
}
|
|
4350
|
-
const xSplit = findXSplit(items, region, gapThreshold);
|
|
4351
|
-
if (xSplit !== null) {
|
|
4352
|
-
const left = items.filter((i) => i.x + i.w / 2 < xSplit);
|
|
4353
|
-
const right = items.filter((i) => i.x + i.w / 2 >= xSplit);
|
|
4354
|
-
if (left.length > 0 && right.length > 0 && left.length < items.length) {
|
|
4355
|
-
return [...xyCutOrder(left, gapThreshold, depth + 1), ...xyCutOrder(right, gapThreshold, depth + 1)];
|
|
4356
|
-
}
|
|
4357
|
-
}
|
|
4358
|
-
return [items];
|
|
4359
|
-
}
|
|
4360
|
-
function computeRegion(items) {
|
|
4361
|
-
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
4362
|
-
for (const i of items) {
|
|
4363
|
-
if (i.x < minX) minX = i.x;
|
|
4364
|
-
if (i.y < minY) minY = i.y;
|
|
4365
|
-
if (i.x + i.w > maxX) maxX = i.x + i.w;
|
|
4366
|
-
if (i.y + i.h > maxY) maxY = i.y + i.h;
|
|
4367
|
-
}
|
|
4368
|
-
return { items, minX, minY, maxX, maxY };
|
|
4369
|
-
}
|
|
4370
|
-
function findYSplit(items, _region, gapThreshold) {
|
|
4371
|
-
const sorted = [...items].sort((a, b) => b.y - a.y);
|
|
4372
|
-
let bestGap = gapThreshold;
|
|
4373
|
-
let bestSplit = null;
|
|
4374
|
-
for (let i = 1; i < sorted.length; i++) {
|
|
4375
|
-
const prevBottom = sorted[i - 1].y - sorted[i - 1].h;
|
|
4376
|
-
const currTop = sorted[i].y;
|
|
4377
|
-
const gap = prevBottom - currTop;
|
|
4378
|
-
if (gap > bestGap) {
|
|
4379
|
-
bestGap = gap;
|
|
4380
|
-
bestSplit = (prevBottom + currTop) / 2;
|
|
4381
|
-
}
|
|
4382
|
-
}
|
|
4383
|
-
return bestSplit;
|
|
4384
|
-
}
|
|
4385
|
-
function findXSplit(items, _region, gapThreshold) {
|
|
4386
|
-
const sorted = [...items].sort((a, b) => a.x - b.x);
|
|
4387
|
-
let bestGap = gapThreshold;
|
|
4388
|
-
let bestSplit = null;
|
|
4389
|
-
for (let i = 1; i < sorted.length; i++) {
|
|
4390
|
-
const prevRight = sorted[i - 1].x + sorted[i - 1].w;
|
|
4391
|
-
const currLeft = sorted[i].x;
|
|
4392
|
-
const gap = currLeft - prevRight;
|
|
4393
|
-
if (gap > bestGap) {
|
|
4394
|
-
bestGap = gap;
|
|
4395
|
-
bestSplit = (prevRight + currLeft) / 2;
|
|
4396
|
-
}
|
|
4397
|
-
}
|
|
4398
|
-
return bestSplit;
|
|
4399
|
-
}
|
|
4400
|
-
function extractPageBlocksWithLines(items, pageNum, opList, pageWidth, pageHeight) {
|
|
4401
|
-
if (items.length === 0) return [];
|
|
4402
|
-
let { horizontals, verticals } = extractLines(opList.fnArray, opList.argsArray);
|
|
4403
|
-
({ horizontals, verticals } = filterPageBorderLines(horizontals, verticals, pageWidth, pageHeight));
|
|
4404
|
-
({ horizontals, verticals } = preprocessLines(horizontals, verticals));
|
|
4405
|
-
const grids = buildTableGrids(horizontals, verticals);
|
|
4406
|
-
if (grids.length > 0) {
|
|
4407
|
-
return extractBlocksWithGrids(items, pageNum, grids, horizontals, verticals);
|
|
4408
|
-
}
|
|
4409
|
-
return extractPageBlocksFallback(items, pageNum);
|
|
4410
|
-
}
|
|
4411
|
-
function extractBlocksWithGrids(items, pageNum, grids, horizontals, verticals) {
|
|
4412
|
-
const blocks = [];
|
|
4413
|
-
const usedItems = /* @__PURE__ */ new Set();
|
|
4414
|
-
const sortedGrids = [...grids].sort((a, b) => b.bbox.y2 - a.bbox.y2);
|
|
4415
|
-
for (const grid of sortedGrids) {
|
|
4416
|
-
const numGridRows = grid.rowYs.length - 1;
|
|
4417
|
-
const numGridCols = grid.colXs.length - 1;
|
|
4418
|
-
if (numGridRows === 1 && numGridCols >= 2) continue;
|
|
4419
|
-
const tableItems = [];
|
|
4420
|
-
const pad = 3;
|
|
4421
|
-
const gridW = grid.bbox.x2 - grid.bbox.x1;
|
|
4422
|
-
for (const item of items) {
|
|
4423
|
-
if (usedItems.has(item)) continue;
|
|
4424
|
-
if (item.y < grid.bbox.y1 - pad || item.y > grid.bbox.y2 + pad) continue;
|
|
4425
|
-
if (item.x < grid.bbox.x1 - pad || item.x + item.w > grid.bbox.x2 + pad) continue;
|
|
4426
|
-
if (gridW < 120 && item.x + item.w > grid.bbox.x2 - 2) continue;
|
|
4427
|
-
tableItems.push(item);
|
|
4428
|
-
usedItems.add(item);
|
|
4429
|
-
}
|
|
4430
|
-
const cells = extractCells(grid, horizontals, verticals);
|
|
4431
|
-
if (cells.length === 0) continue;
|
|
4432
|
-
const textItems = tableItems.map((i) => ({
|
|
4433
|
-
text: i.text,
|
|
4434
|
-
x: i.x,
|
|
4435
|
-
y: i.y,
|
|
4436
|
-
w: i.w,
|
|
4437
|
-
h: i.h,
|
|
4438
|
-
fontSize: i.fontSize,
|
|
4439
|
-
fontName: i.fontName
|
|
4440
|
-
}));
|
|
4441
|
-
const cellTextMap = mapTextToCells(textItems, cells);
|
|
4442
|
-
const numRows = grid.rowYs.length - 1;
|
|
4443
|
-
const numCols = grid.colXs.length - 1;
|
|
4444
|
-
const irGrid = Array.from(
|
|
4445
|
-
{ length: numRows },
|
|
4446
|
-
() => Array.from({ length: numCols }, () => ({ text: "", colSpan: 1, rowSpan: 1 }))
|
|
4447
|
-
);
|
|
4448
|
-
for (const cell of cells) {
|
|
4449
|
-
const cellItems = cellTextMap.get(cell) || [];
|
|
4450
|
-
let text = cellTextToString(cellItems);
|
|
4451
|
-
text = text.replace(/^[\s]*[-–—]\s*\d+\s*[-–—][\s]*$/gm, "").trim();
|
|
4452
|
-
text = text.split("\n").map((line) => collapseEvenSpacing(line)).join("\n");
|
|
4453
|
-
irGrid[cell.row][cell.col] = {
|
|
4454
|
-
text,
|
|
4455
|
-
colSpan: cell.colSpan,
|
|
4456
|
-
rowSpan: cell.rowSpan
|
|
4457
|
-
};
|
|
4458
|
-
}
|
|
4459
|
-
const irTable = {
|
|
4460
|
-
rows: numRows,
|
|
4461
|
-
cols: numCols,
|
|
4462
|
-
cells: irGrid,
|
|
4463
|
-
hasHeader: numRows > 1
|
|
4464
|
-
};
|
|
4465
|
-
const hasContent = irGrid.some((row) => row.some((cell) => cell.text.trim() !== ""));
|
|
4466
|
-
if (!hasContent) continue;
|
|
4467
|
-
const tableBbox = {
|
|
4468
|
-
page: pageNum,
|
|
4469
|
-
x: grid.bbox.x1,
|
|
4470
|
-
y: grid.bbox.y1,
|
|
4471
|
-
width: grid.bbox.x2 - grid.bbox.x1,
|
|
4472
|
-
height: grid.bbox.y2 - grid.bbox.y1
|
|
4473
|
-
};
|
|
4474
|
-
if (shouldDemoteTable(irTable)) {
|
|
4475
|
-
const demoted = demoteTableToText(irTable);
|
|
4476
|
-
if (demoted) {
|
|
4477
|
-
const text = numGridRows === 1 ? "\n" + demoted + "\n" : demoted;
|
|
4478
|
-
blocks.push({ type: "paragraph", text, pageNumber: pageNum, bbox: tableBbox, style: dominantStyle(tableItems) });
|
|
4479
|
-
}
|
|
4480
|
-
continue;
|
|
4481
|
-
}
|
|
4482
|
-
blocks.push({ type: "table", table: irTable, pageNumber: pageNum, bbox: tableBbox });
|
|
4483
|
-
}
|
|
4484
|
-
let remaining = items.filter((i) => !usedItems.has(i));
|
|
4485
|
-
if (remaining.length > 0) {
|
|
4486
|
-
remaining.sort((a, b) => b.y - a.y || a.x - b.x);
|
|
4487
|
-
const clusterItems = remaining.map((i) => ({
|
|
4488
|
-
text: i.text,
|
|
4489
|
-
x: i.x,
|
|
4490
|
-
y: i.y,
|
|
4491
|
-
w: i.w,
|
|
4492
|
-
h: i.h,
|
|
4493
|
-
fontSize: i.fontSize,
|
|
4494
|
-
fontName: i.fontName
|
|
4495
|
-
}));
|
|
4496
|
-
const clusterResults = detectClusterTables(clusterItems, pageNum);
|
|
4497
|
-
if (clusterResults.length > 0) {
|
|
4498
|
-
const ciToIdx = /* @__PURE__ */ new Map();
|
|
4499
|
-
for (let ci = 0; ci < clusterItems.length; ci++) ciToIdx.set(clusterItems[ci], ci);
|
|
4500
|
-
const usedClusterIndices = /* @__PURE__ */ new Set();
|
|
4501
|
-
for (const cr of clusterResults) {
|
|
4502
|
-
for (const ci of cr.usedItems) {
|
|
4503
|
-
const idx = ciToIdx.get(ci);
|
|
4504
|
-
if (idx !== void 0) usedClusterIndices.add(idx);
|
|
4505
|
-
}
|
|
4506
|
-
blocks.push({ type: "table", table: cr.table, pageNumber: pageNum, bbox: cr.bbox });
|
|
4507
|
-
}
|
|
4508
|
-
remaining = remaining.filter((_, idx) => !usedClusterIndices.has(idx));
|
|
4509
|
-
}
|
|
4510
|
-
if (remaining.length > 0) {
|
|
4511
|
-
const allY = remaining.map((i) => i.y);
|
|
4512
|
-
const pageH = safeMax(allY) - safeMin(allY);
|
|
4513
|
-
const groups = xyCutOrder(remaining, Math.max(15, pageH * 0.03));
|
|
4514
|
-
const textBlocks = [];
|
|
4515
|
-
for (const group of groups) {
|
|
4516
|
-
if (group.length === 0) continue;
|
|
4517
|
-
const groupBlocks = extractPageBlocksFallback(group, pageNum);
|
|
4518
|
-
for (const b of groupBlocks) textBlocks.push(b);
|
|
4519
|
-
}
|
|
4520
|
-
const finalTextBlocks = detectListBlocks(textBlocks);
|
|
4521
|
-
for (const b of finalTextBlocks) blocks.push(b);
|
|
4522
|
-
}
|
|
4523
|
-
blocks.sort((a, b) => {
|
|
4524
|
-
const ay = a.bbox ? a.bbox.y + a.bbox.height : 0;
|
|
4525
|
-
const by = b.bbox ? b.bbox.y + b.bbox.height : 0;
|
|
4526
|
-
return by - ay;
|
|
4527
|
-
});
|
|
4528
|
-
return mergeAdjacentTableBlocks(blocks);
|
|
4529
|
-
}
|
|
4530
|
-
return mergeAdjacentTableBlocks(blocks);
|
|
4531
|
-
}
|
|
4532
|
-
function mergeAdjacentTableBlocks(blocks) {
|
|
4533
|
-
if (blocks.length <= 1) return blocks;
|
|
4534
|
-
const result = [blocks[0]];
|
|
4535
|
-
for (let i = 1; i < blocks.length; i++) {
|
|
4536
|
-
const prev = result[result.length - 1];
|
|
4537
|
-
const curr = blocks[i];
|
|
4538
|
-
if (prev.type === "table" && curr.type === "table" && prev.table && curr.table && prev.table.cols === curr.table.cols) {
|
|
4539
|
-
const merged = {
|
|
4540
|
-
rows: prev.table.rows + curr.table.rows,
|
|
4541
|
-
cols: prev.table.cols,
|
|
4542
|
-
cells: [...prev.table.cells, ...curr.table.cells],
|
|
4543
|
-
hasHeader: prev.table.hasHeader
|
|
4544
|
-
};
|
|
4545
|
-
result[result.length - 1] = { ...prev, table: merged };
|
|
4546
|
-
} else {
|
|
4547
|
-
result.push(curr);
|
|
4548
|
-
}
|
|
4549
|
-
}
|
|
4550
|
-
return result;
|
|
4551
|
-
}
|
|
4552
|
-
function extractPageBlocksFallback(items, pageNum) {
|
|
4553
|
-
if (items.length === 0) return [];
|
|
4554
|
-
const blocks = [];
|
|
4555
|
-
const clusterItems = items.map((i) => ({
|
|
4556
|
-
text: i.text,
|
|
4557
|
-
x: i.x,
|
|
4558
|
-
y: i.y,
|
|
4559
|
-
w: i.w,
|
|
4560
|
-
h: i.h,
|
|
4561
|
-
fontSize: i.fontSize,
|
|
4562
|
-
fontName: i.fontName
|
|
4563
|
-
}));
|
|
4564
|
-
const clusterResults = detectClusterTables(clusterItems, pageNum);
|
|
4565
|
-
if (clusterResults.length > 0) {
|
|
4566
|
-
const ciToIdx = /* @__PURE__ */ new Map();
|
|
4567
|
-
for (let ci = 0; ci < clusterItems.length; ci++) ciToIdx.set(clusterItems[ci], ci);
|
|
4568
|
-
const usedIndices = /* @__PURE__ */ new Set();
|
|
4569
|
-
for (const cr of clusterResults) {
|
|
4570
|
-
for (const ci of cr.usedItems) {
|
|
4571
|
-
const idx = ciToIdx.get(ci);
|
|
4572
|
-
if (idx !== void 0) usedIndices.add(idx);
|
|
4573
|
-
}
|
|
4574
|
-
blocks.push({ type: "table", table: cr.table, pageNumber: pageNum, bbox: cr.bbox });
|
|
4575
|
-
}
|
|
4576
|
-
const remaining = items.filter((_, idx) => !usedIndices.has(idx));
|
|
4577
|
-
if (remaining.length > 0) {
|
|
4578
|
-
const yLines = groupByY(remaining);
|
|
4579
|
-
for (const line of yLines) {
|
|
4580
|
-
const text = mergeLineSimple(line);
|
|
4581
|
-
if (!text.trim()) continue;
|
|
4582
|
-
const bbox = computeBBox(line, pageNum);
|
|
4583
|
-
blocks.push({ type: "paragraph", text, pageNumber: pageNum, bbox, style: dominantStyle(line) });
|
|
4584
|
-
}
|
|
4585
|
-
}
|
|
4586
|
-
blocks.sort((a, b) => {
|
|
4587
|
-
const ay = a.bbox ? a.bbox.y + a.bbox.height : 0;
|
|
4588
|
-
const by = b.bbox ? b.bbox.y + b.bbox.height : 0;
|
|
4589
|
-
return by - ay;
|
|
4590
|
-
});
|
|
4591
|
-
} else {
|
|
4592
|
-
const allYLines = groupByY(items);
|
|
4593
|
-
const columns = detectColumns(allYLines);
|
|
4594
|
-
if (columns && columns.length >= 3) {
|
|
4595
|
-
const tableText = extractWithColumns(allYLines, columns);
|
|
4596
|
-
const bbox = computeBBox(items, pageNum);
|
|
4597
|
-
blocks.push({ type: "paragraph", text: tableText, pageNumber: pageNum, bbox, style: dominantStyle(items) });
|
|
4598
|
-
} else {
|
|
4599
|
-
const allY = items.map((i) => i.y);
|
|
4600
|
-
const pageHeight = safeMax(allY) - safeMin(allY);
|
|
4601
|
-
const gapThreshold = Math.max(15, pageHeight * 0.03);
|
|
4602
|
-
const orderedGroups = xyCutOrder(items, gapThreshold);
|
|
4603
|
-
for (const group of orderedGroups) {
|
|
4604
|
-
if (group.length === 0) continue;
|
|
4605
|
-
const yLines = groupByY(group);
|
|
4606
|
-
const groupColumns = detectColumns(yLines);
|
|
4607
|
-
if (groupColumns && groupColumns.length >= 3) {
|
|
4608
|
-
const tableText = extractWithColumns(yLines, groupColumns);
|
|
4609
|
-
const bbox = computeBBox(group, pageNum);
|
|
4610
|
-
blocks.push({ type: "paragraph", text: tableText, pageNumber: pageNum, bbox, style: dominantStyle(group) });
|
|
4611
|
-
} else {
|
|
4612
|
-
for (const line of yLines) {
|
|
4613
|
-
const text = mergeLineSimple(line);
|
|
4614
|
-
if (!text.trim()) continue;
|
|
4615
|
-
const bbox = computeBBox(line, pageNum);
|
|
4616
|
-
blocks.push({ type: "paragraph", text, pageNumber: pageNum, bbox, style: dominantStyle(line) });
|
|
4617
|
-
}
|
|
4618
|
-
}
|
|
4619
|
-
}
|
|
4620
|
-
}
|
|
4621
|
-
}
|
|
4622
|
-
return detectSpecialKoreanTables(blocks);
|
|
4623
|
-
}
|
|
4624
|
-
function computeBBox(items, pageNum) {
|
|
4625
|
-
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
4626
|
-
for (const i of items) {
|
|
4627
|
-
if (i.x < minX) minX = i.x;
|
|
4628
|
-
if (i.y < minY) minY = i.y;
|
|
4629
|
-
if (i.x + i.w > maxX) maxX = i.x + i.w;
|
|
4630
|
-
const effectiveH = i.h > 0 ? i.h : i.fontSize;
|
|
4631
|
-
if (i.y + effectiveH > maxY) maxY = i.y + effectiveH;
|
|
4632
|
-
}
|
|
4633
|
-
return { page: pageNum, x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
4634
|
-
}
|
|
4635
|
-
function dominantStyle(items) {
|
|
4636
|
-
if (items.length === 0) return void 0;
|
|
4637
|
-
const freq = /* @__PURE__ */ new Map();
|
|
4638
|
-
let maxCount = 0, dominantSize = 0;
|
|
4639
|
-
for (const i of items) {
|
|
4640
|
-
if (i.fontSize <= 0) continue;
|
|
4641
|
-
const count = (freq.get(i.fontSize) || 0) + 1;
|
|
4642
|
-
freq.set(i.fontSize, count);
|
|
4643
|
-
if (count > maxCount) {
|
|
4644
|
-
maxCount = count;
|
|
4645
|
-
dominantSize = i.fontSize;
|
|
4646
|
-
}
|
|
4647
|
-
}
|
|
4648
|
-
if (dominantSize === 0) return void 0;
|
|
4649
|
-
const fontName = items.find((i) => i.fontSize === dominantSize)?.fontName || void 0;
|
|
4650
|
-
return { fontSize: dominantSize, fontName };
|
|
4651
|
-
}
|
|
4652
|
-
function normalizeItems(rawItems) {
|
|
4653
|
-
const items = [];
|
|
4654
|
-
const spacePositions = [];
|
|
4655
|
-
for (const i of rawItems) {
|
|
4656
|
-
if (typeof i.str !== "string") continue;
|
|
4657
|
-
const x = Math.round(i.transform[4]);
|
|
4658
|
-
const y = Math.round(i.transform[5]);
|
|
4659
|
-
if (!i.str.trim()) {
|
|
4660
|
-
spacePositions.push({ x, y });
|
|
4661
|
-
continue;
|
|
4662
|
-
}
|
|
4663
|
-
const scaleY = Math.abs(i.transform[3]);
|
|
4664
|
-
const scaleX = Math.abs(i.transform[0]);
|
|
4665
|
-
const fontSize = Math.round(Math.max(scaleY, scaleX));
|
|
4666
|
-
const w = Math.round(i.width);
|
|
4667
|
-
const h = Math.round(i.height);
|
|
4668
|
-
const isHidden = fontSize === 0 || i.width === 0 && i.str.trim().length > 0;
|
|
4669
|
-
let text = i.str.trim();
|
|
4670
|
-
if (/^[\d\s\-().·,☎]+$/.test(text) && /\d/.test(text) && / /.test(text)) {
|
|
4671
|
-
text = text.replace(/ /g, "");
|
|
4672
|
-
}
|
|
4673
|
-
const split = splitEvenSpacedItem(text, x, w, fontSize);
|
|
4674
|
-
if (split) {
|
|
4675
|
-
for (const s of split) {
|
|
4676
|
-
items.push({ text: s.text, x: s.x, y, w: s.w, h, fontSize, fontName: i.fontName || "", isHidden });
|
|
4677
|
-
}
|
|
4678
|
-
} else {
|
|
4679
|
-
items.push({ text, x, y, w, h, fontSize, fontName: i.fontName || "", isHidden });
|
|
4680
|
-
}
|
|
4681
|
-
}
|
|
4682
|
-
const sorted = items.sort((a, b) => b.y - a.y || a.x - b.x);
|
|
4683
|
-
const deduped = [];
|
|
4684
|
-
for (let i = 0; i < sorted.length; i++) {
|
|
4685
|
-
let isDup = false;
|
|
4686
|
-
for (let j = deduped.length - 1; j >= 0; j--) {
|
|
4687
|
-
const prev = deduped[j];
|
|
4688
|
-
if (prev.y - sorted[i].y > 3) break;
|
|
4689
|
-
if (Math.abs(prev.y - sorted[i].y) <= 3 && prev.text === sorted[i].text && Math.abs(prev.x - sorted[i].x) <= 3) {
|
|
4690
|
-
isDup = true;
|
|
4691
|
-
break;
|
|
4692
|
-
}
|
|
4693
|
-
}
|
|
4694
|
-
if (!isDup) deduped.push(sorted[i]);
|
|
4695
|
-
}
|
|
4696
|
-
if (spacePositions.length > 0) {
|
|
4697
|
-
for (const item of deduped) {
|
|
4698
|
-
for (const sp of spacePositions) {
|
|
4699
|
-
if (Math.abs(sp.y - item.y) <= 3) {
|
|
4700
|
-
const dist = item.x - sp.x;
|
|
4701
|
-
if (dist >= 0 && dist <= 20) {
|
|
4702
|
-
item.hasSpaceBefore = true;
|
|
4703
|
-
break;
|
|
4704
|
-
}
|
|
4705
|
-
}
|
|
4706
|
-
}
|
|
4707
|
-
}
|
|
4708
|
-
}
|
|
4709
|
-
return deduped;
|
|
4710
|
-
}
|
|
4711
|
-
function splitEvenSpacedItem(text, itemX, itemW, fontSize) {
|
|
4712
|
-
if (!/^[가-힣\d](?: [가-힣\d]){2,}$/.test(text)) return null;
|
|
4713
|
-
const chars = text.split(" ");
|
|
4714
|
-
if (chars.length < 3) return null;
|
|
4715
|
-
const charW = itemW / chars.length;
|
|
4716
|
-
if (charW > fontSize * 2) return null;
|
|
4717
|
-
return chars.map((ch, idx) => ({
|
|
4718
|
-
text: ch,
|
|
4719
|
-
x: Math.round(itemX + idx * charW),
|
|
4720
|
-
w: Math.round(charW * 0.8)
|
|
4721
|
-
// 실제 글자 폭은 간격보다 좁음
|
|
4722
|
-
}));
|
|
4723
|
-
}
|
|
4724
|
-
function groupByY(items) {
|
|
4725
|
-
if (items.length === 0) return [];
|
|
4726
|
-
const lines = [];
|
|
4727
|
-
let curY = items[0].y;
|
|
4728
|
-
let curLine = [items[0]];
|
|
4729
|
-
for (let i = 1; i < items.length; i++) {
|
|
4730
|
-
if (Math.abs(items[i].y - curY) > 3) {
|
|
4731
|
-
lines.push(curLine);
|
|
4732
|
-
curLine = [];
|
|
4733
|
-
curY = items[i].y;
|
|
4734
|
-
}
|
|
4735
|
-
curLine.push(items[i]);
|
|
4736
|
-
}
|
|
4737
|
-
if (curLine.length > 0) lines.push(curLine);
|
|
4738
|
-
return lines;
|
|
4739
|
-
}
|
|
4740
|
-
function isProseSpread(items) {
|
|
4741
|
-
if (items.length < 4) return false;
|
|
4742
|
-
const sorted = [...items].sort((a, b) => a.x - b.x);
|
|
4743
|
-
const gaps = [];
|
|
4744
|
-
for (let i = 1; i < sorted.length; i++) {
|
|
4745
|
-
gaps.push(sorted[i].x - (sorted[i - 1].x + sorted[i - 1].w));
|
|
4746
|
-
}
|
|
4747
|
-
const maxGap = safeMax(gaps);
|
|
4748
|
-
const avgLen = items.reduce((s, i) => s + i.text.length, 0) / items.length;
|
|
4749
|
-
return maxGap < 40 && avgLen < 5;
|
|
4750
|
-
}
|
|
4751
|
-
function detectColumns(yLines) {
|
|
4752
|
-
const allItems = yLines.flat();
|
|
4753
|
-
if (allItems.length === 0) return null;
|
|
4754
|
-
const pageWidth = safeMax(allItems.map((i) => i.x + i.w)) - safeMin(allItems.map((i) => i.x));
|
|
4755
|
-
if (pageWidth < 100) return null;
|
|
4756
|
-
let bigoLineIdx = -1;
|
|
4757
|
-
for (let i = 0; i < yLines.length; i++) {
|
|
4758
|
-
if (yLines[i].length <= 2 && yLines[i].some((item) => item.text === "\uBE44\uACE0")) {
|
|
4759
|
-
bigoLineIdx = i;
|
|
4760
|
-
break;
|
|
4761
|
-
}
|
|
4762
|
-
}
|
|
4763
|
-
const tableYLines = bigoLineIdx >= 0 ? yLines.slice(0, bigoLineIdx) : yLines;
|
|
4764
|
-
const CLUSTER_TOL = 22;
|
|
4765
|
-
const xClusters = [];
|
|
4766
|
-
for (const line of tableYLines) {
|
|
4767
|
-
if (isProseSpread(line)) continue;
|
|
4768
|
-
for (const item of line) {
|
|
4769
|
-
let found = false;
|
|
4770
|
-
for (const c of xClusters) {
|
|
4771
|
-
if (Math.abs(item.x - c.center) <= CLUSTER_TOL) {
|
|
4772
|
-
c.center = Math.round((c.center * c.count + item.x) / (c.count + 1));
|
|
4773
|
-
c.minX = Math.min(c.minX, item.x);
|
|
4774
|
-
c.count++;
|
|
4775
|
-
found = true;
|
|
4776
|
-
break;
|
|
4777
|
-
}
|
|
4778
|
-
}
|
|
4779
|
-
if (!found) {
|
|
4780
|
-
xClusters.push({ center: item.x, count: 1, minX: item.x });
|
|
4781
|
-
}
|
|
4782
|
-
}
|
|
4783
|
-
}
|
|
4784
|
-
const peaks = xClusters.filter((c) => c.count >= 3).sort((a, b) => a.minX - b.minX);
|
|
4785
|
-
if (peaks.length < 3) return null;
|
|
4786
|
-
const MERGE_TOL = 40;
|
|
4787
|
-
const merged = [peaks[0]];
|
|
4788
|
-
for (let i = 1; i < peaks.length; i++) {
|
|
4789
|
-
const prev = merged[merged.length - 1];
|
|
4790
|
-
if (peaks[i].minX - prev.minX < MERGE_TOL) {
|
|
4791
|
-
if (peaks[i].count > prev.count) {
|
|
4792
|
-
prev.center = peaks[i].center;
|
|
4793
|
-
}
|
|
4794
|
-
prev.count += peaks[i].count;
|
|
4795
|
-
prev.minX = Math.min(prev.minX, peaks[i].minX);
|
|
4796
|
-
} else {
|
|
4797
|
-
merged.push({ ...peaks[i] });
|
|
4798
|
-
}
|
|
4799
|
-
}
|
|
4800
|
-
const rawColumns = merged.filter((c) => c.count >= 3).map((c) => c.minX);
|
|
4801
|
-
if (rawColumns.length < 3) return null;
|
|
4802
|
-
const MIN_DETECT_COL_WIDTH = 30;
|
|
4803
|
-
const columns = [rawColumns[0]];
|
|
4804
|
-
for (let i = 1; i < rawColumns.length; i++) {
|
|
4805
|
-
if (rawColumns[i] - columns[columns.length - 1] < MIN_DETECT_COL_WIDTH) continue;
|
|
4806
|
-
columns.push(rawColumns[i]);
|
|
4807
|
-
}
|
|
4808
|
-
return columns.length >= 3 ? columns : null;
|
|
4809
|
-
}
|
|
4810
|
-
function findColumn(x, columns) {
|
|
4811
|
-
for (let i = columns.length - 1; i >= 0; i--) {
|
|
4812
|
-
if (x >= columns[i] - 10) return i;
|
|
4813
|
-
}
|
|
4814
|
-
return 0;
|
|
4815
|
-
}
|
|
4816
|
-
function extractWithColumns(yLines, columns) {
|
|
4817
|
-
const result = [];
|
|
4818
|
-
const colMin = columns[0];
|
|
4819
|
-
const colMax = columns[columns.length - 1];
|
|
4820
|
-
let bigoIdx = -1;
|
|
4821
|
-
for (let i = 0; i < yLines.length; i++) {
|
|
4822
|
-
if (yLines[i].length <= 2 && yLines[i].some((item) => item.text === "\uBE44\uACE0")) {
|
|
4823
|
-
bigoIdx = i;
|
|
4824
|
-
break;
|
|
4825
|
-
}
|
|
4826
|
-
}
|
|
4827
|
-
let tableStart = -1;
|
|
4828
|
-
for (let i = 0; i < (bigoIdx >= 0 ? bigoIdx : yLines.length); i++) {
|
|
4829
|
-
const usedCols = new Set(yLines[i].map((item) => findColumn(item.x, columns)));
|
|
4830
|
-
if (usedCols.size >= 3) {
|
|
4831
|
-
tableStart = i;
|
|
4832
|
-
break;
|
|
4833
|
-
}
|
|
4834
|
-
}
|
|
4835
|
-
const tableEnd = bigoIdx >= 0 ? bigoIdx : yLines.length;
|
|
4836
|
-
for (let i = 0; i < (tableStart >= 0 ? tableStart : tableEnd); i++) {
|
|
4837
|
-
result.push(mergeLineSimple(yLines[i]));
|
|
4838
|
-
}
|
|
4839
|
-
if (tableStart >= 0) {
|
|
4840
|
-
const tableLines = yLines.slice(tableStart, tableEnd);
|
|
4841
|
-
const gridLines = [];
|
|
4842
|
-
for (const line of tableLines) {
|
|
4843
|
-
const inRange = line.some(
|
|
4844
|
-
(item) => item.x >= colMin - 20 && item.x <= colMax + 200
|
|
4845
|
-
);
|
|
4846
|
-
if (inRange && !isProseSpread(line)) {
|
|
4847
|
-
gridLines.push(line);
|
|
4848
|
-
} else {
|
|
4849
|
-
if (gridLines.length > 0) {
|
|
4850
|
-
result.push(buildGridTable(gridLines.splice(0), columns));
|
|
4851
|
-
}
|
|
4852
|
-
result.push(mergeLineSimple(line));
|
|
4853
|
-
}
|
|
4854
|
-
}
|
|
4855
|
-
if (gridLines.length > 0) {
|
|
4856
|
-
result.push(buildGridTable(gridLines, columns));
|
|
4857
|
-
}
|
|
4858
|
-
}
|
|
4859
|
-
if (bigoIdx >= 0) {
|
|
4860
|
-
result.push("");
|
|
4861
|
-
for (let i = bigoIdx; i < yLines.length; i++) {
|
|
4862
|
-
result.push(mergeLineSimple(yLines[i]));
|
|
4863
|
-
}
|
|
4864
|
-
}
|
|
4865
|
-
return result.join("\n");
|
|
4866
|
-
}
|
|
4867
|
-
function buildGridTable(lines, columns) {
|
|
4868
|
-
const numCols = columns.length;
|
|
4869
|
-
const yRows = lines.map((items) => {
|
|
4870
|
-
const row = Array(numCols).fill("");
|
|
4871
|
-
for (const item of items) {
|
|
4872
|
-
const col = findColumn(item.x, columns);
|
|
4873
|
-
row[col] = row[col] ? row[col] + " " + item.text : item.text;
|
|
4874
|
-
}
|
|
4875
|
-
return row;
|
|
4876
|
-
});
|
|
4877
|
-
const dataColStart = Math.max(2, Math.floor(numCols / 2));
|
|
4878
|
-
const merged = [];
|
|
4879
|
-
for (const row of yRows) {
|
|
4880
|
-
if (row.every((c) => c === "")) continue;
|
|
4881
|
-
if (merged.length === 0) {
|
|
4882
|
-
merged.push([...row]);
|
|
4883
|
-
continue;
|
|
4884
|
-
}
|
|
4885
|
-
const prev = merged[merged.length - 1];
|
|
4886
|
-
const filledCols = row.map((c, i) => c ? i : -1).filter((i) => i >= 0);
|
|
4887
|
-
const filledCount = filledCols.length;
|
|
4888
|
-
let isNewRow = false;
|
|
4889
|
-
if (row[0] && row[0].length >= 3) {
|
|
4890
|
-
isNewRow = true;
|
|
4891
|
-
}
|
|
4892
|
-
if (!isNewRow && numCols > 1 && row[1]) {
|
|
4893
|
-
isNewRow = true;
|
|
4894
|
-
}
|
|
4895
|
-
if (!isNewRow) {
|
|
4896
|
-
const hasData = row.slice(dataColStart).some((c) => c !== "");
|
|
4897
|
-
const prevHasData = prev.slice(dataColStart).some((c) => c !== "");
|
|
4898
|
-
if (hasData && prevHasData) {
|
|
4899
|
-
isNewRow = true;
|
|
4900
|
-
}
|
|
4901
|
-
}
|
|
4902
|
-
if (isNewRow && filledCount === 1 && row[0] && row[0].length <= 2) {
|
|
4903
|
-
isNewRow = false;
|
|
4904
|
-
}
|
|
4905
|
-
if (isNewRow) {
|
|
4906
|
-
merged.push([...row]);
|
|
4907
|
-
} else {
|
|
4908
|
-
for (let c = 0; c < numCols; c++) {
|
|
4909
|
-
if (row[c]) {
|
|
4910
|
-
prev[c] = prev[c] ? prev[c] + " " + row[c] : row[c];
|
|
4911
|
-
}
|
|
4912
|
-
}
|
|
4913
|
-
}
|
|
4914
|
-
}
|
|
4915
|
-
if (merged.length < 2) {
|
|
4916
|
-
return merged.map((r) => r.filter((c) => c).join(" ")).join("\n");
|
|
4917
|
-
}
|
|
4918
|
-
let headerEnd = 0;
|
|
4919
|
-
for (let r = 0; r < merged.length; r++) {
|
|
4920
|
-
const hasDataValues = merged[r].slice(dataColStart).some((c) => c && /\d/.test(c));
|
|
4921
|
-
if (hasDataValues) break;
|
|
4922
|
-
headerEnd = r + 1;
|
|
4923
|
-
}
|
|
4924
|
-
if (headerEnd > 1) {
|
|
4925
|
-
const headerRow = Array(numCols).fill("");
|
|
4926
|
-
for (let r = 0; r < headerEnd; r++) {
|
|
4927
|
-
for (let c = 0; c < numCols; c++) {
|
|
4928
|
-
if (merged[r][c]) {
|
|
4929
|
-
headerRow[c] = headerRow[c] ? headerRow[c] + " " + merged[r][c] : merged[r][c];
|
|
4930
|
-
}
|
|
4931
|
-
}
|
|
4932
|
-
}
|
|
4933
|
-
merged.splice(0, headerEnd, headerRow);
|
|
4934
|
-
}
|
|
4935
|
-
for (const row of merged) {
|
|
4936
|
-
for (let c = 0; c < row.length; c++) {
|
|
4937
|
-
if (row[c]) row[c] = collapseEvenSpacing(row[c]);
|
|
4938
|
-
}
|
|
4939
|
-
}
|
|
4940
|
-
const totalCells = merged.length * numCols;
|
|
4941
|
-
const filledCells = merged.reduce((s, row) => s + row.filter((c) => c).length, 0);
|
|
4942
|
-
if (filledCells < totalCells * 0.35 || merged.length < 2 || merged.length <= 3 && numCols >= 7) {
|
|
4943
|
-
return merged.map((r) => r.filter((c) => c).join(" ")).join("\n");
|
|
4944
|
-
}
|
|
4945
|
-
const md = [];
|
|
4946
|
-
md.push("| " + merged[0].join(" | ") + " |");
|
|
4947
|
-
md.push("| " + merged[0].map(() => "---").join(" | ") + " |");
|
|
4948
|
-
for (let r = 1; r < merged.length; r++) {
|
|
4949
|
-
md.push("| " + merged[r].join(" | ") + " |");
|
|
4950
|
-
}
|
|
4951
|
-
return md.join("\n");
|
|
4952
|
-
}
|
|
4953
|
-
function mergeLineSimple(items) {
|
|
4954
|
-
if (items.length <= 1) return items[0]?.text || "";
|
|
4955
|
-
const sorted = [...items].sort((a, b) => a.x - b.x);
|
|
4956
|
-
const isEvenSpaced = detectEvenSpacedItems(sorted);
|
|
4957
|
-
let result = sorted[0].text;
|
|
4958
|
-
for (let i = 1; i < sorted.length; i++) {
|
|
4959
|
-
const gap = sorted[i].x - (sorted[i - 1].x + sorted[i - 1].w);
|
|
4960
|
-
const avgFs = (sorted[i].fontSize + sorted[i - 1].fontSize) / 2;
|
|
4961
|
-
const tabThreshold = Math.max(avgFs * 2, 30);
|
|
4962
|
-
if (gap > tabThreshold) {
|
|
4963
|
-
result += " ";
|
|
4964
|
-
result += sorted[i].text;
|
|
4965
|
-
continue;
|
|
4966
|
-
}
|
|
4967
|
-
if (isEvenSpaced[i]) {
|
|
4968
|
-
result += sorted[i].text;
|
|
4969
|
-
continue;
|
|
4970
|
-
}
|
|
4971
|
-
if (sorted[i].hasSpaceBefore && gap >= avgFs * 0.05) {
|
|
4972
|
-
result += " ";
|
|
4973
|
-
result += sorted[i].text;
|
|
4974
|
-
continue;
|
|
4975
|
-
}
|
|
4976
|
-
if (/[□■○●▶◆◇ㅇ]$/.test(sorted[i - 1].text) && /^[가-힣]/.test(sorted[i].text) && gap > 1) {
|
|
4977
|
-
result += " ";
|
|
4978
|
-
result += sorted[i].text;
|
|
4979
|
-
continue;
|
|
4980
|
-
}
|
|
4981
|
-
if (gap < avgFs * 0.15) {
|
|
4982
|
-
} else if (gap < avgFs * 0.35 && (/[가-힣]$/.test(result) || /^[가-힣]/.test(sorted[i].text))) {
|
|
4983
|
-
} else if (gap > 3) result += " ";
|
|
4984
|
-
result += sorted[i].text;
|
|
4985
|
-
}
|
|
4986
|
-
return result;
|
|
4987
|
-
}
|
|
4988
|
-
function cleanPdfText(text) {
|
|
4989
|
-
return mergeKoreanLines(
|
|
4990
|
-
text.replace(/^\d{1,4}\n/, "").replace(/^[\s]*[-–—]\s*[-–—]?\d+[-–—]?[\s]*[-–—]?[\s]*$/gm, "").replace(/^\s*\d+\s*\/\s*\d+\s*$/gm, "").replace(/\n\d{1,4}\n/g, "\n").replace(/\n\d{1,4}$/, "").replace(/^#{1,6}\s*\d{1,4}\s*$/gm, "")
|
|
4991
|
-
).replace(/^(?!\| ---).*$/gm, (line) => collapseEvenSpacing(line)).replace(/([□■◆○●▶ㅇ])\s+([가-힣])\s+([가-힣])/g, "$1 $2$3").replace(/\n{3,}/g, "\n\n").trim();
|
|
4992
|
-
}
|
|
4993
|
-
function startsWithMarker(line) {
|
|
4994
|
-
const t = line.trimStart();
|
|
4995
|
-
return /^[가-힣ㄱ-ㅎ][.)]/.test(t) || /^\d+[.)]/.test(t) || /^\([가-힣ㄱ-ㅎ\d]+\)/.test(t) || /^[○●※▶▷◆◇■□★☆\-·]\s/.test(t) || /^제\d+[조항호장절]/.test(t);
|
|
4996
|
-
}
|
|
4997
|
-
function isStandaloneHeader(line) {
|
|
4998
|
-
return /^제\d+[조항호장절](\([^)]*\))?(\s+\S+){0,7}$/.test(line.trim());
|
|
4999
|
-
}
|
|
5000
|
-
function detectListBlocks(blocks) {
|
|
5001
|
-
const result = [];
|
|
5002
|
-
for (let i = 0; i < blocks.length; i++) {
|
|
5003
|
-
const block = blocks[i];
|
|
5004
|
-
if (block.type === "paragraph" && block.text) {
|
|
5005
|
-
const text = block.text.trim();
|
|
5006
|
-
if (/^\d+\.\s/.test(text)) {
|
|
5007
|
-
result.push({ ...block, type: "list", listType: "ordered", text: block.text });
|
|
5008
|
-
continue;
|
|
5009
|
-
}
|
|
5010
|
-
if (/^[○●·※▶▷◆◇\-]\s/.test(text)) {
|
|
5011
|
-
result.push({ ...block, type: "list", listType: "unordered", text: block.text });
|
|
5012
|
-
continue;
|
|
5013
|
-
}
|
|
5014
|
-
}
|
|
5015
|
-
result.push(block);
|
|
5016
|
-
}
|
|
5017
|
-
return result;
|
|
5018
|
-
}
|
|
5019
|
-
var KOREAN_TABLE_HEADER_RE = /^\(?(구분|항목|종류|분류|유형|대상|내용|기간|금액|비율|방법|절차|요건|조건|근거|목적|범위|기준)\)?[:\s]/;
|
|
5020
|
-
var KV_FALSE_POSITIVE_RE = /\d{1,2}:\d{2}|:\/\/|\d+:\d+/;
|
|
5021
|
-
function detectSpecialKoreanTables(blocks) {
|
|
5022
|
-
const result = [];
|
|
5023
|
-
let kvLines = [];
|
|
5024
|
-
const flushKvTable = () => {
|
|
5025
|
-
if (kvLines.length < 2) {
|
|
5026
|
-
for (const kv of kvLines) result.push(kv.block);
|
|
5027
|
-
kvLines = [];
|
|
5028
|
-
return;
|
|
5029
|
-
}
|
|
5030
|
-
const cells = kvLines.map((kv) => {
|
|
5031
|
-
if (kv.value) {
|
|
5032
|
-
return [
|
|
5033
|
-
{ text: kv.key, colSpan: 1, rowSpan: 1 },
|
|
5034
|
-
{ text: kv.value, colSpan: 1, rowSpan: 1 }
|
|
5035
|
-
];
|
|
5036
|
-
}
|
|
5037
|
-
return [
|
|
5038
|
-
{ text: kv.key, colSpan: 2, rowSpan: 1 },
|
|
5039
|
-
{ text: "", colSpan: 1, rowSpan: 1 }
|
|
5040
|
-
];
|
|
5041
|
-
});
|
|
5042
|
-
const irTable = {
|
|
5043
|
-
rows: cells.length,
|
|
5044
|
-
cols: 2,
|
|
5045
|
-
cells,
|
|
5046
|
-
hasHeader: true
|
|
5047
|
-
};
|
|
5048
|
-
const firstBlock = kvLines[0].block;
|
|
5049
|
-
result.push({
|
|
5050
|
-
type: "table",
|
|
5051
|
-
table: irTable,
|
|
5052
|
-
pageNumber: firstBlock.pageNumber,
|
|
5053
|
-
bbox: firstBlock.bbox
|
|
5054
|
-
});
|
|
5055
|
-
kvLines = [];
|
|
5056
|
-
};
|
|
5057
|
-
for (const block of blocks) {
|
|
5058
|
-
if (block.type !== "paragraph" || !block.text) {
|
|
5059
|
-
flushKvTable();
|
|
5060
|
-
result.push(block);
|
|
5061
|
-
continue;
|
|
5062
|
-
}
|
|
5063
|
-
const text = block.text.trim();
|
|
5064
|
-
if (KOREAN_TABLE_HEADER_RE.test(text)) {
|
|
5065
|
-
const colonIdx = text.indexOf(":");
|
|
5066
|
-
if (colonIdx >= 0) {
|
|
5067
|
-
kvLines.push({
|
|
5068
|
-
key: text.slice(0, colonIdx).trim(),
|
|
5069
|
-
value: text.slice(colonIdx + 1).trim(),
|
|
5070
|
-
block
|
|
5071
|
-
});
|
|
5072
|
-
} else {
|
|
5073
|
-
const spaceIdx = text.search(/\s/);
|
|
5074
|
-
if (spaceIdx > 0) {
|
|
5075
|
-
kvLines.push({
|
|
5076
|
-
key: text.slice(0, spaceIdx).trim(),
|
|
5077
|
-
value: text.slice(spaceIdx + 1).trim(),
|
|
5078
|
-
block
|
|
5079
|
-
});
|
|
5080
|
-
} else {
|
|
5081
|
-
kvLines.push({ key: text, value: "", block });
|
|
5082
|
-
}
|
|
5083
|
-
}
|
|
5084
|
-
continue;
|
|
5085
|
-
}
|
|
5086
|
-
if (kvLines.length > 0 && text.includes(":")) {
|
|
5087
|
-
if (!KV_FALSE_POSITIVE_RE.test(text) && !text.includes("(") && !text.includes(")")) {
|
|
5088
|
-
const colonIdx = text.indexOf(":");
|
|
5089
|
-
const key = text.slice(0, colonIdx).trim();
|
|
5090
|
-
if (/^[가-힣]+$/.test(key) && key.length >= 2 && key.length <= 8) {
|
|
5091
|
-
kvLines.push({
|
|
5092
|
-
key,
|
|
5093
|
-
value: text.slice(colonIdx + 1).trim(),
|
|
5094
|
-
block
|
|
5095
|
-
});
|
|
5096
|
-
continue;
|
|
5097
|
-
}
|
|
5098
|
-
}
|
|
5099
|
-
}
|
|
5100
|
-
flushKvTable();
|
|
5101
|
-
result.push(block);
|
|
5102
|
-
}
|
|
5103
|
-
flushKvTable();
|
|
5104
|
-
return result;
|
|
5105
|
-
}
|
|
5106
|
-
function removeHeaderFooterBlocks(blocks, pageHeights, warnings) {
|
|
5107
|
-
const ZONE_RATIO = 0.1;
|
|
5108
|
-
const MIN_REPEAT = 3;
|
|
5109
|
-
const headerTexts = /* @__PURE__ */ new Map();
|
|
5110
|
-
const footerTexts = /* @__PURE__ */ new Map();
|
|
5111
|
-
for (let bi = 0; bi < blocks.length; bi++) {
|
|
5112
|
-
const b = blocks[bi];
|
|
5113
|
-
if (!b.bbox || !b.pageNumber || !b.text?.trim()) continue;
|
|
5114
|
-
const ph = pageHeights.get(b.bbox.page) || pageHeights.get(b.pageNumber);
|
|
5115
|
-
if (!ph) continue;
|
|
5116
|
-
const blockTop = ph - (b.bbox.y + b.bbox.height);
|
|
5117
|
-
const blockBottom = ph - b.bbox.y;
|
|
5118
|
-
if (blockBottom <= ph * ZONE_RATIO) {
|
|
5119
|
-
const arr = footerTexts.get(b.pageNumber) || [];
|
|
5120
|
-
arr.push(b.text.trim());
|
|
5121
|
-
footerTexts.set(b.pageNumber, arr);
|
|
5122
|
-
} else if (blockTop >= ph * (1 - ZONE_RATIO)) {
|
|
5123
|
-
const arr = headerTexts.get(b.pageNumber) || [];
|
|
5124
|
-
arr.push(b.text.trim());
|
|
5125
|
-
headerTexts.set(b.pageNumber, arr);
|
|
5126
|
-
}
|
|
5127
|
-
}
|
|
5128
|
-
const repeatedPatterns = /* @__PURE__ */ new Set();
|
|
5129
|
-
for (const textsMap of [headerTexts, footerTexts]) {
|
|
5130
|
-
const patternCount = /* @__PURE__ */ new Map();
|
|
5131
|
-
for (const [, texts] of textsMap) {
|
|
5132
|
-
for (const t of texts) {
|
|
5133
|
-
const normalized = t.replace(/\d+/g, "#");
|
|
5134
|
-
patternCount.set(normalized, (patternCount.get(normalized) || 0) + 1);
|
|
5135
|
-
}
|
|
5136
|
-
}
|
|
5137
|
-
for (const [pattern, count] of patternCount) {
|
|
5138
|
-
if (count >= MIN_REPEAT) repeatedPatterns.add(pattern);
|
|
5139
|
-
}
|
|
5140
|
-
}
|
|
5141
|
-
if (repeatedPatterns.size === 0) return [];
|
|
5142
|
-
const removeIndices = [];
|
|
5143
|
-
for (let bi = 0; bi < blocks.length; bi++) {
|
|
5144
|
-
const b = blocks[bi];
|
|
5145
|
-
if (!b.bbox || !b.pageNumber || !b.text?.trim()) continue;
|
|
5146
|
-
const ph = pageHeights.get(b.bbox.page) || pageHeights.get(b.pageNumber);
|
|
5147
|
-
if (!ph) continue;
|
|
5148
|
-
const blockTop = ph - (b.bbox.y + b.bbox.height);
|
|
5149
|
-
const blockBottom = ph - b.bbox.y;
|
|
5150
|
-
const inZone = blockBottom <= ph * ZONE_RATIO || blockTop >= ph * (1 - ZONE_RATIO);
|
|
5151
|
-
if (!inZone) continue;
|
|
5152
|
-
const normalized = b.text.trim().replace(/\d+/g, "#");
|
|
5153
|
-
if (repeatedPatterns.has(normalized)) {
|
|
5154
|
-
removeIndices.push(bi);
|
|
5155
|
-
}
|
|
5156
|
-
}
|
|
5157
|
-
if (removeIndices.length > 0) {
|
|
5158
|
-
warnings.push({ message: `${removeIndices.length}\uAC1C \uBA38\uB9AC\uAE00/\uBC14\uB2E5\uAE00 \uC694\uC18C \uC81C\uAC70\uB428`, code: "HIDDEN_TEXT_FILTERED" });
|
|
5159
|
-
}
|
|
5160
|
-
return removeIndices;
|
|
5161
|
-
}
|
|
5162
|
-
function mergeKoreanLines(text) {
|
|
5163
|
-
if (!text) return "";
|
|
5164
|
-
const lines = text.split("\n");
|
|
5165
|
-
if (lines.length <= 1) return text;
|
|
5166
|
-
const result = [lines[0]];
|
|
5167
|
-
for (let i = 1; i < lines.length; i++) {
|
|
5168
|
-
const prev = result[result.length - 1];
|
|
5169
|
-
const curr = lines[i];
|
|
5170
|
-
const currTrimmed = curr.trim();
|
|
5171
|
-
if (/^#{1,6}\s/.test(prev) || /^#{1,6}\s/.test(curr) || /^\|/.test(currTrimmed) || /^---/.test(currTrimmed)) {
|
|
5172
|
-
result.push(curr);
|
|
5173
|
-
continue;
|
|
5174
|
-
}
|
|
5175
|
-
if (/,$/.test(prev.trim()) && currTrimmed.length > 0) {
|
|
5176
|
-
result[result.length - 1] = prev + "\n" + curr;
|
|
5177
|
-
continue;
|
|
5178
|
-
}
|
|
5179
|
-
if (/^\(※/.test(currTrimmed)) {
|
|
5180
|
-
result[result.length - 1] = prev + " " + currTrimmed;
|
|
5181
|
-
continue;
|
|
5182
|
-
}
|
|
5183
|
-
if (/[가-힣·,\-]$/.test(prev) && /^[가-힣(]/.test(curr) && !startsWithMarker(curr) && !isStandaloneHeader(prev) && !startsWithMarker(prev)) {
|
|
5184
|
-
result[result.length - 1] = prev + " " + curr;
|
|
5185
|
-
} else {
|
|
5186
|
-
result.push(curr);
|
|
5187
|
-
}
|
|
5188
|
-
}
|
|
5189
|
-
return result.join("\n");
|
|
5190
|
-
}
|
|
5191
|
-
|
|
5192
|
-
// src/form/recognize.ts
|
|
5193
|
-
var LABEL_KEYWORDS = /* @__PURE__ */ new Set([
|
|
5194
|
-
"\uC131\uBA85",
|
|
5195
|
-
"\uC774\uB984",
|
|
5196
|
-
"\uC8FC\uC18C",
|
|
5197
|
-
"\uC804\uD654",
|
|
5198
|
-
"\uC804\uD654\uBC88\uD638",
|
|
5199
|
-
"\uD734\uB300\uD3F0",
|
|
5200
|
-
"\uD578\uB4DC\uD3F0",
|
|
5201
|
-
"\uC5F0\uB77D\uCC98",
|
|
5202
|
-
"\uC0DD\uB144\uC6D4\uC77C",
|
|
5203
|
-
"\uC8FC\uBBFC\uB4F1\uB85D\uBC88\uD638",
|
|
5204
|
-
"\uC18C\uC18D",
|
|
5205
|
-
"\uC9C1\uC704",
|
|
5206
|
-
"\uC9C1\uAE09",
|
|
5207
|
-
"\uBD80\uC11C",
|
|
5208
|
-
"\uC774\uBA54\uC77C",
|
|
5209
|
-
"\uD329\uC2A4",
|
|
5210
|
-
"\uD559\uAD50",
|
|
5211
|
-
"\uD559\uB144",
|
|
5212
|
-
"\uBC18",
|
|
5213
|
-
"\uBC88\uD638",
|
|
5214
|
-
"\uC2E0\uCCAD\uC778",
|
|
5215
|
-
"\uB300\uD45C\uC790",
|
|
5216
|
-
"\uB2F4\uB2F9\uC790",
|
|
5217
|
-
"\uC791\uC131\uC790",
|
|
5218
|
-
"\uD655\uC778\uC790",
|
|
5219
|
-
"\uC2B9\uC778\uC790",
|
|
5220
|
-
"\uC77C\uC2DC",
|
|
5221
|
-
"\uB0A0\uC9DC",
|
|
5222
|
-
"\uAE30\uAC04",
|
|
5223
|
-
"\uC7A5\uC18C",
|
|
5224
|
-
"\uBAA9\uC801",
|
|
5225
|
-
"\uC0AC\uC720",
|
|
5226
|
-
"\uBE44\uACE0",
|
|
5227
|
-
"\uAE08\uC561",
|
|
5228
|
-
"\uC218\uB7C9",
|
|
5229
|
-
"\uB2E8\uAC00",
|
|
5230
|
-
"\uD569\uACC4",
|
|
5231
|
-
"\uACC4",
|
|
5232
|
-
"\uC18C\uACC4",
|
|
5233
|
-
"\uB4F1\uB85D\uAE30\uC900\uC9C0",
|
|
5234
|
-
"\uBCF8\uC801",
|
|
5235
|
-
"\uC704\uC784\uC778",
|
|
5236
|
-
"\uCCAD\uAD6C\uC0AC\uC720",
|
|
5237
|
-
"\uC18C\uBA85\uC790\uB8CC"
|
|
5238
|
-
]);
|
|
5239
|
-
function isLabelCell(text) {
|
|
5240
|
-
const trimmed = text.trim().replace(/[¹²³⁴⁵⁶⁷⁸⁹⁰*※]+$/g, "").trim();
|
|
5241
|
-
if (!trimmed || trimmed.length > 30) return false;
|
|
5242
|
-
for (const kw of LABEL_KEYWORDS) {
|
|
5243
|
-
if (trimmed.includes(kw)) return true;
|
|
5244
|
-
}
|
|
5245
|
-
if (/^[가-힣\s()()·::]+$/.test(trimmed) && trimmed.replace(/\s/g, "").length >= 2 && trimmed.replace(/\s/g, "").length <= 8 && !/\d/.test(trimmed)) return true;
|
|
5246
|
-
if (/^[가-힣A-Za-z\s]+[::]$/.test(trimmed)) return true;
|
|
5247
|
-
return false;
|
|
5248
|
-
}
|
|
5249
|
-
function extractFormFields(blocks) {
|
|
5250
|
-
const fields = [];
|
|
5251
|
-
let totalTables = 0;
|
|
5252
|
-
let formTables = 0;
|
|
5253
|
-
for (const block of blocks) {
|
|
5254
|
-
if (block.type !== "table" || !block.table) continue;
|
|
5255
|
-
totalTables++;
|
|
5256
|
-
const tableFields = extractFromTable(block.table);
|
|
5257
|
-
if (tableFields.length > 0) {
|
|
5258
|
-
formTables++;
|
|
5259
|
-
fields.push(...tableFields);
|
|
5260
|
-
}
|
|
5261
|
-
}
|
|
5262
|
-
for (const block of blocks) {
|
|
5263
|
-
if (block.type === "paragraph" && block.text) {
|
|
5264
|
-
const inlineFields = extractInlineFields(block.text);
|
|
5265
|
-
fields.push(...inlineFields);
|
|
5266
|
-
}
|
|
5267
|
-
}
|
|
5268
|
-
const confidence = totalTables > 0 ? formTables / totalTables : fields.length > 0 ? 0.3 : 0;
|
|
5269
|
-
return { fields, confidence: Math.min(confidence, 1) };
|
|
5270
|
-
}
|
|
5271
|
-
function extractFromTable(table) {
|
|
5272
|
-
const fields = [];
|
|
5273
|
-
if (table.cols >= 2) {
|
|
5274
|
-
for (let r = 0; r < table.rows; r++) {
|
|
5275
|
-
for (let c = 0; c < table.cols - 1; c++) {
|
|
5276
|
-
const labelCell = table.cells[r][c];
|
|
5277
|
-
const valueCell = table.cells[r][c + 1];
|
|
5278
|
-
if (isLabelCell(labelCell.text)) {
|
|
5279
|
-
fields.push({
|
|
5280
|
-
label: labelCell.text.trim().replace(/[::]\s*$/, ""),
|
|
5281
|
-
value: valueCell.text.trim(),
|
|
5282
|
-
row: r,
|
|
5283
|
-
col: c
|
|
5284
|
-
});
|
|
5285
|
-
}
|
|
5286
|
-
}
|
|
5287
|
-
}
|
|
5288
|
-
}
|
|
5289
|
-
if (fields.length === 0 && table.rows >= 2 && table.cols >= 2) {
|
|
5290
|
-
const headerRow = table.cells[0];
|
|
5291
|
-
const allLabels = headerRow.every((cell) => {
|
|
5292
|
-
const t = cell.text.trim();
|
|
5293
|
-
return t.length > 0 && t.length <= 20;
|
|
5294
|
-
});
|
|
5295
|
-
if (allLabels) {
|
|
5296
|
-
for (let r = 1; r < table.rows; r++) {
|
|
5297
|
-
for (let c = 0; c < table.cols; c++) {
|
|
5298
|
-
const label = headerRow[c].text.trim();
|
|
5299
|
-
const value = table.cells[r][c].text.trim();
|
|
5300
|
-
if (label && value) {
|
|
5301
|
-
fields.push({ label, value, row: r, col: c });
|
|
5302
|
-
}
|
|
5303
|
-
}
|
|
5304
|
-
}
|
|
5305
|
-
}
|
|
5306
|
-
}
|
|
5307
|
-
return fields;
|
|
2631
|
+
return fields;
|
|
5308
2632
|
}
|
|
5309
2633
|
function extractInlineFields(text) {
|
|
5310
2634
|
const fields = [];
|
|
@@ -6982,6 +4306,18 @@ async function parseHwp(buffer, options) {
|
|
|
6982
4306
|
}
|
|
6983
4307
|
}
|
|
6984
4308
|
async function parsePdf(buffer, options) {
|
|
4309
|
+
let parsePdfDocument;
|
|
4310
|
+
try {
|
|
4311
|
+
const mod = await import("./parser-AMP7MAOH.js");
|
|
4312
|
+
parsePdfDocument = mod.parsePdfDocument;
|
|
4313
|
+
} catch {
|
|
4314
|
+
return {
|
|
4315
|
+
success: false,
|
|
4316
|
+
fileType: "pdf",
|
|
4317
|
+
error: "PDF \uD30C\uC2F1\uC5D0 pdfjs-dist\uAC00 \uD544\uC694\uD569\uB2C8\uB2E4. \uC124\uCE58: npm install pdfjs-dist",
|
|
4318
|
+
code: "MISSING_DEPENDENCY"
|
|
4319
|
+
};
|
|
4320
|
+
}
|
|
6985
4321
|
try {
|
|
6986
4322
|
const { markdown, blocks, metadata, outline, warnings, isImageBased } = await parsePdfDocument(buffer, options);
|
|
6987
4323
|
return { success: true, fileType: "pdf", markdown, blocks, metadata, outline, warnings, isImageBased };
|
|
@@ -7185,14 +4521,8 @@ function diffTableCells(a, b) {
|
|
|
7185
4521
|
}
|
|
7186
4522
|
|
|
7187
4523
|
export {
|
|
7188
|
-
VERSION,
|
|
7189
|
-
toArrayBuffer,
|
|
7190
|
-
KordocError,
|
|
7191
|
-
sanitizeError,
|
|
7192
|
-
blocksToMarkdown,
|
|
7193
4524
|
extractHwpxMetadataOnly,
|
|
7194
4525
|
extractHwp5MetadataOnly,
|
|
7195
|
-
extractPdfMetadataOnly,
|
|
7196
4526
|
extractFormFields,
|
|
7197
4527
|
fillFormFields,
|
|
7198
4528
|
fillHwpx,
|
|
@@ -7200,4 +4530,4 @@ export {
|
|
|
7200
4530
|
compare,
|
|
7201
4531
|
parse
|
|
7202
4532
|
};
|
|
7203
|
-
//# sourceMappingURL=chunk-
|
|
4533
|
+
//# sourceMappingURL=chunk-RF6UJXR3.js.map
|