R73-A2 收敛 VAT OCR 归一化与质量门槛

这个提交包含在:
2026-04-28 21:13:21 +08:00
父节点 82fbd39114
当前提交 b7c018922c
共修改 9 个文件,包含 600 行新增和 14 行删除
+27
查看文件
@@ -1,6 +1,10 @@
const { runTencentVatOcr } = require("./tencent");
const { runMockOcr } = require("./mock");
const REQUIRED_OCR_FIELDS_BY_TYPE = {
vat: ["invoiceCode", "invoiceNumber", "invoiceDate", "totalAmount", "sellerName"],
};
function isOcrEnabledForType(config, ocrType) {
if (config.ocrProvider === "mock") {
return ocrType === "vat";
@@ -9,6 +13,27 @@ function isOcrEnabledForType(config, ocrType) {
return config.ocrProvider === "tencent" && ocrType === "vat";
}
function getMissingRequiredOcrFields(ocrType, normalized) {
const requiredFields = REQUIRED_OCR_FIELDS_BY_TYPE[ocrType] ?? [];
if (!requiredFields.length) {
return [];
}
return requiredFields.filter((field) => {
const value = normalized?.[field];
return value == null || value === "";
});
}
function buildMissingRequiredOcrError(ocrType, normalized) {
const missingFields = getMissingRequiredOcrFields(ocrType, normalized);
if (!missingFields.length) {
return null;
}
return `missing_required_fields:${missingFields.join(",")}`;
}
async function runOcrForInvoice({ config, ocrType, imageBase64 }) {
if (config.ocrProvider === "mock") {
return runMockOcr({ ocrType, imageBase64 });
@@ -26,6 +51,8 @@ async function runOcrForInvoice({ config, ocrType, imageBase64 }) {
}
module.exports = {
buildMissingRequiredOcrError,
getMissingRequiredOcrFields,
isOcrEnabledForType,
runOcrForInvoice,
};
+21 -1
查看文件
@@ -18,6 +18,18 @@ function buildInvoiceKey(markerText) {
return `hash:${hash.slice(0, 20)}`;
}
function parseMissingFields(markerText) {
const match = markerText.match(/MOCK_OCR_MISSING:([A-Za-z0-9_,-]+)/);
if (!match?.[1]) {
return [];
}
return match[1]
.split(",")
.map((field) => field.trim())
.filter(Boolean);
}
function toAmount(seedHex, fallback) {
const value = Number.parseInt(seedHex.slice(0, 8), 16);
if (!Number.isFinite(value)) {
@@ -38,7 +50,7 @@ async function runMockOcr({ ocrType, imageBase64 }) {
const invoiceCode = `MOCK${hash.slice(0, 8).toUpperCase()}`;
const invoiceNumber = hash.slice(8, 16).toUpperCase();
return {
const result = {
invoiceCode,
invoiceNumber,
invoiceDate: "2026-02-08",
@@ -53,6 +65,14 @@ async function runMockOcr({ ocrType, imageBase64 }) {
marker: markerText.slice(0, 128),
},
};
for (const field of parseMissingFields(markerText)) {
if (Object.prototype.hasOwnProperty.call(result, field)) {
result[field] = null;
}
}
return result;
}
module.exports = {
+42 -6
查看文件
@@ -46,12 +46,41 @@ function toNumberString(value) {
return num.toFixed(2);
}
function addNumberStrings(...values) {
const normalized = values.map((value) => toNumberString(value));
if (normalized.some((value) => value == null)) {
return null;
}
const total = normalized.reduce((sum, value) => sum + Number(value), 0);
return total.toFixed(2);
}
function subtractNumberStrings(minuend, subtrahend) {
const normalizedMinuend = toNumberString(minuend);
const normalizedSubtrahend = toNumberString(subtrahend);
if (normalizedMinuend == null || normalizedSubtrahend == null) {
return null;
}
return (Number(normalizedMinuend) - Number(normalizedSubtrahend)).toFixed(2);
}
function normalizeIdentifier(value) {
if (typeof value !== "string") {
return null;
}
const normalized = value.replace(/\s+/g, "").trim();
return normalized || null;
}
function normalizeString(value) {
if (typeof value !== "string") {
return null;
}
const normalized = value.trim();
const normalized = value.replace(/\s+/g, " ").trim();
return normalized || null;
}
@@ -76,13 +105,19 @@ function createTencentOcrClient(config) {
}
function normalizeVatInvoiceResult(result) {
const amount = toNumberString(result?.AmountWithoutTax);
const taxAmount = toNumberString(result?.TaxAmount);
const totalAmountFromSource = toNumberString(result?.TotalAmount ?? result?.AmountInFiguers);
const derivedAmount = amount ?? subtractNumberStrings(totalAmountFromSource, taxAmount);
const derivedTotalAmount = totalAmountFromSource ?? addNumberStrings(derivedAmount, taxAmount);
return {
invoiceCode: normalizeString(result?.InvoiceCode),
invoiceNumber: normalizeString(result?.InvoiceNum),
invoiceCode: normalizeIdentifier(result?.InvoiceCode),
invoiceNumber: normalizeIdentifier(result?.InvoiceNum),
invoiceDate: normalizeDate(result?.InvoiceDate),
amount: toNumberString(result?.AmountWithoutTax),
taxAmount: toNumberString(result?.TaxAmount),
totalAmount: toNumberString(result?.TotalAmount ?? result?.AmountInFiguers),
amount: derivedAmount,
taxAmount,
totalAmount: derivedTotalAmount,
sellerName: normalizeString(result?.SellerName),
buyerName: normalizeString(result?.BuyerName),
raw: result ?? {},
@@ -99,5 +134,6 @@ async function runTencentVatOcr(config, imageBase64) {
}
module.exports = {
normalizeVatInvoiceResult,
runTencentVatOcr,
};