| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- /** 数字抽取与文本分类测试。 */
- import { test } from 'node:test';
- import assert from 'node:assert/strict';
- import { extractNumericClaims, isMarkedAsEstimate } from '../src/verify/extract.js';
- test('抽取普通金额与千分位', () => {
- const claims = extractNumericClaims('2025 年 1 月 MLCC 进口额为 400,316.13 美元');
- const values = claims.map((claim) => claim.value);
- assert.ok(values.includes(400316.13), `实际: ${JSON.stringify(values)}`);
- const amount = claims.find((claim) => claim.value === 400316.13);
- assert.equal(amount.kind, 'money');
- });
- test('百分比归一化成小数', () => {
- const claims = extractNumericClaims('占比为 21.70%');
- const percent = claims.find((claim) => claim.raw.includes('%'));
- assert.equal(percent.kind, 'percent');
- assert.ok(Math.abs(percent.value - 0.217) < 1e-12);
- });
- test('万/亿单位会被换算', () => {
- const claims = extractNumericClaims('约 40.03 万美元');
- const converted = claims.find((claim) => claim.raw.startsWith('40.03'));
- assert.equal(converted.value, 400300);
- assert.equal(converted.kind, 'money');
- assert.equal(isMarkedAsEstimate(converted), true);
- });
- test('年份被识别为 year(后续会被豁免)', () => {
- const claims = extractNumericClaims('2025 年进口 1234.5 美元');
- const year = claims.find((claim) => claim.raw === '2025');
- assert.equal(year.kind, 'year');
- const amount = claims.find((claim) => claim.value === 1234.5);
- assert.equal(amount.kind, 'money');
- });
- test('HS 编码被识别为 code', () => {
- const claims = extractNumericClaims('HS 编码 8532241000 属于多层瓷介电容器');
- const code = claims.find((claim) => claim.raw === '8532241000');
- assert.equal(code.kind, 'code');
- assert.equal(code.value, 8532241000);
- });
- test('数量单位识别', () => {
- const claims = extractNumericClaims('进口数量 18695479 个');
- const quantity = claims.find((claim) => claim.value === 18695479);
- assert.equal(quantity.kind, 'quantity');
- });
- test('去重:同一个数字只保留一次', () => {
- const claims = extractNumericClaims('金额 100.5 美元,再次确认 100.5 美元');
- assert.equal(claims.filter((claim) => claim.value === 100.5).length, 1);
- });
- test('不会把 1.0-3.0 这样的表名切碎成脏数字', () => {
- const claims = extractNumericClaims('见 1.0-3.0_MLCC 工作表');
- const values = claims.map((claim) => claim.value);
- assert.ok(!values.includes(3.0) || true); // 允许出现,但不应被当作数据断言(kind=plain 且 <=12 会豁免)
- });
- test('估算标注识别', () => {
- const claims = extractNumericClaims('大约是 3,000,000 美元');
- const claim = claims.find((item) => item.value === 3000000);
- assert.equal(isMarkedAsEstimate(claim), true);
- const exact = extractNumericClaims('是 3,000,000 美元');
- assert.equal(isMarkedAsEstimate(exact[0]), false);
- });
- test('报表名/代次里的数字不被当成数据断言', () => {
- const text = '见 4.2.3 (1-4)_MLCC、4.2.3 (5)_MLCC、1.0-3.0_MLCC、5.2.7 (5)_存储器,DDR4 与 DDR5 对比。';
- const claims = extractNumericClaims(text);
- assert.deepEqual(claims.map((claim) => claim.raw), [], `不应抽出任何数字,实际: ${JSON.stringify(claims.map((claim) => claim.raw))}`);
- });
- test('报表名里的数字被遮掉,但旁边的真实数据仍能抽出', () => {
- const claims = extractNumericClaims('4.2.3 (1-4)_MLCC 这张表里 2025 年 1 月金额为 400,316.13 美元');
- const values = claims.map((claim) => claim.value);
- assert.ok(values.includes(400316.13));
- assert.ok(values.includes(2025)); // 年份保留,由核验层豁免
- assert.ok(!values.includes(4.2), '不应把 4.2 当数据');
- assert.ok(!values.includes(3));
- });
- test('空文本返回空数组', () => {
- assert.deepEqual(extractNumericClaims('没有任何数字'), []);
- });
- test('负数保留符号(金额环比为负很常见)', () => {
- const money = extractNumericClaims('金额 -1,234.56 美元');
- assert.equal(money[0].value, -1234.56);
- assert.equal(money[0].rawNumber, '-1234.56');
- const percent = extractNumericClaims('金额环比 -13.30%');
- assert.equal(percent[0].kind, 'percent');
- assert.ok(Math.abs(percent[0].value + 0.133) < 1e-12);
- const ratio = extractNumericClaims('金额环比 -0.4470999746174148352572197863');
- assert.equal(ratio[0].value, -0.44709997461741485);
- assert.equal(ratio[0].severity, 'strict');
- // 全角负号也要认
- const fullWidth = extractNumericClaims('环比 −13.30%');
- assert.ok(Math.abs(fullWidth[0].value + 0.133) < 1e-12);
- });
- test('负号不会破坏区间/表号的遮蔽', () => {
- assert.deepEqual(extractNumericClaims('2025-01 ~ 2026-08 的数据'), []);
- assert.deepEqual(extractNumericClaims('见 4.2.3 (1-4)_MLCC 表'), []);
- });
- //# sourceMappingURL=extract.test.js.map
|