Update server.js via upload script - 2026-11-09 15:30:34

This commit is contained in:
mike 2026-11-09 15:30:43 +08:00
parent ca8689f50e
commit 36faa567a5
1 changed files with 43 additions and 20 deletions

View File

@ -1223,10 +1223,11 @@ function ocrItems(lines, scale, srcLang, avoid, idBase, pageW) {
const box = { x: l.x0 * scale, y: l.y0 * scale, w: (l.x1 - l.x0) * scale, h: (l.y1 - l.y0) * scale };
if (!hasWords(l.text, srcLang)) continue;
const area = box.w * box.h;
// 和文字层已有的文字重叠就不要(OCR 常把一整行"参数名 + 数值"识别成一行,所以按文字层每个字块被盖住的比例判断)
const covered = avoid.some((a) => {
const ix = Math.max(0, Math.min(a.x + a.w, box.x + box.w) - Math.max(a.x, box.x));
const iy = Math.max(0, Math.min(a.y + a.h, box.y + box.h) - Math.max(a.y, box.y));
return ix * iy > area * 0.3;
return ix * iy > area * 0.3 || ix * iy > a.w * a.h * 0.4;
});
if (covered) continue;
const plain = l.text.replace(/\s+/g, '');
@ -1322,9 +1323,10 @@ async function precisePdfVectorPage(pdfPath, pageNo, pageXml, fontXml, workDir,
for (const r of runs) {
const L = lines[lines.length - 1];
const gap = L ? r.x - (L.x + L.w) : 0;
if (L && L.row === r.row && Math.abs(L.size - r.size) < 0.6 && gap > -1 && gap < r.size * 0.8) {
// 字号不同但颜色相同、挨得很近的也连起来(标题常见"规格参数 (环境25℃)"大小两种字号),整行一起翻译
if (L && L.row === r.row && (Math.abs(L.size - r.size) < 0.6 || L.color === r.color) && gap > -1 && gap < Math.max(L.size, r.size) * (L.color === r.color && Math.max(L.size, r.size) >= 10 ? 1.3 : 0.8)) {
if (gap > 0.8) L.runs.push({ gap: +gap.toFixed(2) });
L.runs.push({ t: r.t, f: r.fam, b: r.b });
L.runs.push({ t: r.t, f: r.fam, b: r.b, ...(Math.abs(L.size - r.size) >= 0.6 ? { s: +r.size.toFixed(2) } : {}) });
L.w = r.x + r.w - L.x; L.top = Math.min(L.top, r.top); L.text += (gap > r.size * 0.25 && !CJK_RE.test(r.t[0]) ? ' ' : '') + r.t;
} else {
lines.push({ row: r.row, x: r.x, w: r.w, top: r.top, base: r.base, size: r.size, color: r.color, runs: [{ t: r.t, f: r.fam, b: r.b }], text: r.t });
@ -1600,33 +1602,54 @@ function htmlSegments(html) {
return parts.map((p, i) => ({ p, text: i % 2 === 0 ? xmlDecode(p).replace(/\s+/g, ' ').trim() : '' }));
}
// 需要翻译的全部文字(去重)
function preciseUnits(doc) {
const set = new Set();
const add = (t) => { const s = String(t || '').trim(); if (s && hasWords(s, doc.srcLang)) set.add(s); };
// 每条文字在原位置能放下多少个西文字符(给 AI 翻译时作长度上限,译文太长版面会挤)
// 图片文字(OCR)里和文字层重叠的条目不用(旧版本生成的彩页里可能还存着,这里显示和翻译时再过滤一次)
function preciseOcrList(pg) {
const boxes = (pg.lines || []).map((l) => ({ x: l.x, y: l.top, w: l.w, h: l.base - l.top + l.size * 0.3 }));
return (pg.ocr || []).filter((o) => !boxes.some((a) => {
const ix = Math.max(0, Math.min(a.x + a.w, o.x + o.w) - Math.max(a.x, o.x));
const iy = Math.max(0, Math.min(a.y + a.h, o.y + o.h) - Math.max(a.y, o.y));
return ix * iy > o.w * o.h * 0.3 || ix * iy > a.w * a.h * 0.4;
}));
}
function preciseUnitBudgets(doc) {
const map = new Map();
const add = (t, max) => {
const s = String(t || '').trim();
if (!s || !hasWords(s, doc.srcLang)) return;
const m = max > 0 ? Math.max(4, Math.floor(max)) : 0;
if (!map.has(s)) map.set(s, m); else if (m && (!map.get(s) || m < map.get(s))) map.set(s, m);
};
const cw = 0.52; // 西文字符平均宽度约为字号的 0.52 倍
if (doc.source === 'html') {
const html = fs.readFileSync(path.join(PRECISE_DIR, doc.id, 'source.html'), 'utf8');
htmlSegments(html).forEach((s) => add(s.text));
htmlSegments(html).forEach((s) => add(s.text, 0));
}
for (const pg of doc.pages) {
(pg.blocks || []).forEach((b) => add(b.text));
(pg.lines || []).filter((l) => !l.block).forEach((l) => add(l.text));
(pg.ocr || []).forEach((o) => add(o.text));
(pg.blocks || []).forEach((b) => add(b.text, ((b.w * b.maxH) / (b.size * cw * b.lh)) * 0.92));
(pg.lines || []).filter((l) => !l.block).forEach((l) => add(l.text, l.maxW / (l.size * cw)));
preciseOcrList(pg).forEach((o) => add(o.text, o.para ? (o.w * o.h) / (o.fs * cw * o.lh) * 0.9 : o.vertical ? o.h / (Math.min(o.w, 12) * 0.74 * cw) : (o.maxW || o.w) / (o.h * 0.74 * cw)));
}
return [...set];
return map;
}
function preciseUnits(doc) { return [...preciseUnitBudgets(doc).keys()]; }
async function translatePreciseDoc(doc, lang, cfg) {
const units = preciseUnits(doc);
const have = doc.translations[lang] || {};
async function translatePreciseDoc(doc, lang, cfg, force) {
const budgets = preciseUnitBudgets(doc);
const units = [...budgets.keys()];
const have = force ? {} : doc.translations[lang] || {};
const todo = units.filter((u) => !have[u]);
const cjkTarget = lang === 'zh';
const sys = `你是专业的电力设备(UPS / 逆变器 / 储能 / 蓄电池)产品资料翻译。把用户给的 JSON 数组里每一条${PRECISE_LANGS[doc.srcLang] || '中文'}文字翻译成${PRECISE_LANGS[lang]},用于在原版彩页的原位置替换原文,所以:
1. 用这个行业规范的书面表达,简洁,长度尽量接近原文(版面空间有限,可以用行业通用缩写,如 Max.、Temp.、Approx.);
1. 输入每一项是 {"t": 原文, "max": 最多字符数}。译文要放回原文的位置,空间有限:译文长度不能超过 max 个字符(max 为 0 表示不限)。
用这个行业规范、简洁的书面表达;放不下时用行业通用缩写、去掉不影响意思的词(如 Temperature→Temp.、Maximum→Max.、Approximately→Approx.、Compensation Coefficient→Temp. Comp.、Characteristics→Char.),宁可短也不要超长;
2. 数字、单位符号(V、A、Ah、W、kg、mm、℃、%、mΩ)、型号、品牌名保持原样;一条里只有一部分是外文的,只翻译需要翻译的部分;
3. 标题就译成标题,表格里的参数名就译成参数名,句子译成完整句子;不要加解释、不要加引号;
4. 严格返回一个 JSON 数组,和输入一一对应、条数相同、顺序相同,不要 markdown 代码块。`;
4. 严格返回一个 JSON 字符串数组(只有译文,不要 t / max 字段),和输入一一对应、条数相同、顺序相同,不要 markdown 代码块。`;
for (let i = 0; i < todo.length; i += 50) {
const chunk = todo.slice(i, i + 50);
const content = await callDeepSeekWithRetry(cfg.apiKey, [{ role: 'system', content: sys }, { role: 'user', content: JSON.stringify(chunk) }], { maxTokens: 6000, temperature: 0.2, timeoutMs: 180000 });
const input = chunk.map((u) => ({ t: u, max: budgets.get(u) ? (cjkTarget ? Math.max(2, Math.floor(budgets.get(u) / 2)) : budgets.get(u)) : 0 }));
const content = await callDeepSeekWithRetry(cfg.apiKey, [{ role: 'system', content: sys }, { role: 'user', content: JSON.stringify(input) }], { maxTokens: 6000, temperature: 0.2, timeoutMs: 180000 });
const { data } = extractJson(content);
if (!Array.isArray(data)) throw new Error('AI 没有按格式返回翻译,请重试');
chunk.forEach((u, k) => { if (typeof data[k] === 'string' && data[k].trim()) have[u] = data[k].trim(); });
@ -1658,7 +1681,7 @@ function renderPreciseHtml(doc, lang) {
const ext = pg.bg.split('.').pop();
const bg = `data:${mime[ext]};base64,${fs.readFileSync(path.join(dir, pg.bg)).toString('base64')}`;
const els = [`<img class="bg" src="${bg}" alt="">`];
const runHtml = (l) => l.runs.map((r) => (r.gap ? `<u class="gap" style="width:${r.gap}pt"></u>` : `<u class="${r.f}${r.b ? ' b' : ''}">${htmlEsc(r.t)}</u>`)).join('');
const runHtml = (l) => l.runs.map((r) => (r.gap ? `<u class="gap" style="width:${r.gap}pt"></u>` : `<u class="${r.f}${r.b ? ' b' : ''}"${r.s ? ` style="font-size:${r.s}pt"` : ''}>${htmlEsc(r.t)}</u>`)).join('');
const doneBlocks = new Set();
for (const l of pg.lines || []) {
if (tr && l.block) {
@ -1686,7 +1709,7 @@ function renderPreciseHtml(doc, lang) {
}
}
if (tr) {
for (const o of pg.ocr || []) {
for (const o of preciseOcrList(pg)) {
const t = T(o.text);
if (!t) continue;
if (o.para) els.push(`<b class="ov op" style="left:${o.x}pt;top:${o.y}pt;width:${o.w}pt;height:${o.h}pt"><em style="font-size:${o.fs.toFixed(2)}pt;line-height:${(o.lh * 0.92).toFixed(2)}pt">${htmlEsc(t)}</em></b>`);
@ -1833,7 +1856,7 @@ app.post('/api/precise/:id/translate', requireAuth, aiLimiter, async (req, res)
const cfg = loadAiConfig();
if (!cfg.apiKey && !cfg.backup.apiKey) return res.status(400).json({ error: 'AI API key not configured(设置里填 DeepSeek 或阿里云的 Key)' });
try {
const r = await translatePreciseDoc(doc, lang, cfg);
const r = await translatePreciseDoc(doc, lang, cfg, !!(req.body || {}).force);
res.json({ ok: true, ...r });
} catch (e) {
res.status(502).json({ error: e.message });