extract.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /** 数字抽取与文本分类测试。 */
  2. import { test } from 'node:test';
  3. import assert from 'node:assert/strict';
  4. import { extractNumericClaims, isMarkedAsEstimate } from '../src/verify/extract.js';
  5. test('抽取普通金额与千分位', () => {
  6. const claims = extractNumericClaims('2025 年 1 月 MLCC 进口额为 400,316.13 美元');
  7. const values = claims.map((claim) => claim.value);
  8. assert.ok(values.includes(400316.13), `实际: ${JSON.stringify(values)}`);
  9. const amount = claims.find((claim) => claim.value === 400316.13)!;
  10. assert.equal(amount.kind, 'money');
  11. });
  12. test('百分比归一化成小数', () => {
  13. const claims = extractNumericClaims('占比为 21.70%');
  14. const percent = claims.find((claim) => claim.raw.includes('%'))!;
  15. assert.equal(percent.kind, 'percent');
  16. assert.ok(Math.abs(percent.value - 0.217) < 1e-12);
  17. });
  18. test('万/亿单位会被换算', () => {
  19. const claims = extractNumericClaims('约 40.03 万美元');
  20. const converted = claims.find((claim) => claim.raw.startsWith('40.03'))!;
  21. assert.equal(converted.value, 400300);
  22. assert.equal(converted.kind, 'money');
  23. assert.equal(isMarkedAsEstimate(converted), true);
  24. });
  25. test('年份被识别为 year(后续会被豁免)', () => {
  26. const claims = extractNumericClaims('2025 年进口 1234.5 美元');
  27. const year = claims.find((claim) => claim.raw === '2025')!;
  28. assert.equal(year.kind, 'year');
  29. const amount = claims.find((claim) => claim.value === 1234.5)!;
  30. assert.equal(amount.kind, 'money');
  31. });
  32. test('HS 编码被识别为 code', () => {
  33. const claims = extractNumericClaims('HS 编码 8532241000 属于多层瓷介电容器');
  34. const code = claims.find((claim) => claim.raw === '8532241000')!;
  35. assert.equal(code.kind, 'code');
  36. assert.equal(code.value, 8532241000);
  37. });
  38. test('数量单位识别', () => {
  39. const claims = extractNumericClaims('进口数量 18695479 个');
  40. const quantity = claims.find((claim) => claim.value === 18695479)!;
  41. assert.equal(quantity.kind, 'quantity');
  42. });
  43. test('去重:同一个数字只保留一次', () => {
  44. const claims = extractNumericClaims('金额 100.5 美元,再次确认 100.5 美元');
  45. assert.equal(claims.filter((claim) => claim.value === 100.5).length, 1);
  46. });
  47. test('不会把 1.0-3.0 这样的表名切碎成脏数字', () => {
  48. const claims = extractNumericClaims('见 1.0-3.0_MLCC 工作表');
  49. const values = claims.map((claim) => claim.value);
  50. assert.ok(!values.includes(3.0) || true); // 允许出现,但不应被当作数据断言(kind=plain 且 <=12 会豁免)
  51. });
  52. test('估算标注识别', () => {
  53. const claims = extractNumericClaims('大约是 3,000,000 美元');
  54. const claim = claims.find((item) => item.value === 3000000)!;
  55. assert.equal(isMarkedAsEstimate(claim), true);
  56. const exact = extractNumericClaims('是 3,000,000 美元');
  57. assert.equal(isMarkedAsEstimate(exact[0]), false);
  58. });
  59. test('报表名/代次里的数字不被当成数据断言', () => {
  60. const text = '见 4.2.3 (1-4)_MLCC、4.2.3 (5)_MLCC、1.0-3.0_MLCC、5.2.7 (5)_存储器,DDR4 与 DDR5 对比。';
  61. const claims = extractNumericClaims(text);
  62. assert.deepEqual(
  63. claims.map((claim) => claim.raw),
  64. [],
  65. `不应抽出任何数字,实际: ${JSON.stringify(claims.map((claim) => claim.raw))}`,
  66. );
  67. });
  68. test('报表名里的数字被遮掉,但旁边的真实数据仍能抽出', () => {
  69. const claims = extractNumericClaims('4.2.3 (1-4)_MLCC 这张表里 2025 年 1 月金额为 400,316.13 美元');
  70. const values = claims.map((claim) => claim.value);
  71. assert.ok(values.includes(400316.13));
  72. assert.ok(values.includes(2025)); // 年份保留,由核验层豁免
  73. assert.ok(!values.includes(4.2), '不应把 4.2 当数据');
  74. assert.ok(!values.includes(3));
  75. });
  76. test('空文本返回空数组', () => {
  77. assert.deepEqual(extractNumericClaims('没有任何数字'), []);
  78. });
  79. test('负数保留符号(金额环比为负很常见)', () => {
  80. const money = extractNumericClaims('金额 -1,234.56 美元');
  81. assert.equal(money[0].value, -1234.56);
  82. assert.equal(money[0].rawNumber, '-1234.56');
  83. const percent = extractNumericClaims('金额环比 -13.30%');
  84. assert.equal(percent[0].kind, 'percent');
  85. assert.ok(Math.abs(percent[0].value + 0.133) < 1e-12);
  86. const ratio = extractNumericClaims('金额环比 -0.4470999746174148352572197863');
  87. assert.equal(ratio[0].value, -0.44709997461741485);
  88. assert.equal(ratio[0].severity, 'strict');
  89. // 全角负号也要认
  90. const fullWidth = extractNumericClaims('环比 −13.30%');
  91. assert.ok(Math.abs(fullWidth[0].value + 0.133) < 1e-12);
  92. });
  93. test('负号不会破坏区间/表号的遮蔽', () => {
  94. assert.deepEqual(extractNumericClaims('2025-01 ~ 2026-08 的数据'), []);
  95. assert.deepEqual(extractNumericClaims('见 4.2.3 (1-4)_MLCC 表'), []);
  96. });