hh-shipments 0.3.8 → 0.3.9

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/extract.js +42 -1
package/package.json CHANGED
@@ -18,7 +18,7 @@
18
18
  "test": "node test/run-tests.js && node test/run-carriers-tests.js"
19
19
  },
20
20
  "type": "commonjs",
21
- "version": "0.3.8",
21
+ "version": "0.3.9",
22
22
  "bin": {
23
23
  "hh-shipments": "server.js"
24
24
  },
package/src/extract.js CHANGED
@@ -76,7 +76,13 @@
76
76
  var m, re = /\b(?:1Z|12|IZ)[\s-]?(?:[0-9A-Z][\s-]?){16}\b/gi;
77
77
  while ((m = re.exec(text)) !== null) {
78
78
  var ups = normalize(m[0]);
79
- var num = /^1Z[0-9A-Z]{16}$/.test(ups) ? ups : repairUpsZ(ups);
79
+ var num = null;
80
+ if (/^1Z[0-9A-Z]{6}[0-9]{10}$/.test(ups)) num = ups; // fully strict
81
+ else if (/^1Z[0-9A-Z]{16}$/.test(ups)) {
82
+ // letters sitting in the digit zone are OCR confusions — repair them
83
+ num = repairUpsTail(ups.slice(2)) || ups;
84
+ }
85
+ else num = repairUpsZ(ups) || repairUpsTail(ups.slice(2));
80
86
  if (num && !seen[num]) {
81
87
  seen[num] = true;
82
88
  found.push({ number: num, carrier: 'ups', confidence: conf, ocr_fixed: num !== ups });
@@ -84,6 +90,39 @@
84
90
  }
85
91
  }
86
92
 
93
+ // Tesseract also mangles punctuation on labels: ")" / "]" / "}" for J,
94
+ // "(" / "[" / "{" for C, "|" / "!" for 1 (e.g. "1Z 1X) 539 ..." for 1XJ).
95
+ // The UPS structure is strict (1Z + 6-char shipper + 2-digit service +
96
+ // 8-digit serial), so a mangled tail can be repaired and accepted only
97
+ // when the repaired result validates strictly — never invented.
98
+ var OCR_PUNCT_FIX = { ')': 'J', ']': 'J', '}': 'J', '(': 'C', '[': 'C', '{': 'C', '|': '1', '!': '1' };
99
+ var OCR_TAIL_DIGIT_FIX = { 'O': '0', 'Q': '0', 'I': '1', 'l': '1', '|': '1', '!': '1', 'Z': '2', 'S': '5', 'B': '8' };
100
+ function repairUpsTail(tail) {
101
+ // tail: 16 chars after the 1Z anchor (6-char shipper + 2-digit service +
102
+ // 8-digit serial), possibly with OCR confusions.
103
+ var ship = tail.slice(0, 6), digits = tail.slice(6, 16);
104
+ var fixShip = ship.replace(/[)\]}(\[{|!]/g, function (c) { return OCR_PUNCT_FIX[c]; });
105
+ var fixDigits = digits.replace(/[OQIil|!ZSB]/g, function (c) { return OCR_TAIL_DIGIT_FIX[c]; });
106
+ var cand = '1Z' + fixShip + fixDigits;
107
+ if (!UPS_STRICT.test(cand)) return null;
108
+ if (!/[A-Z]/.test(cand.slice(2, 8))) return null; // real shipper numbers contain a letter
109
+ return cand;
110
+ }
111
+ function looseUpsIn(text, found, seen, conf) {
112
+ // Loose: anchor + 16 chars from an OCR-confusion-tolerant alphabet,
113
+ // spaces/dashes allowed. repairUpsTail decides; strictness lives there.
114
+ var m, re = /\b(?:1Z|12|IZ)[\s-]?((?:[0-9A-Z)\]}(\[{|!][\s-]?){16})\b/gi;
115
+ while ((m = re.exec(text)) !== null) {
116
+ var tail = m[1].replace(/[\s-]/g, '').toUpperCase();
117
+ if (tail.length !== 16) continue;
118
+ var num = repairUpsTail(tail);
119
+ if (num && !seen[num]) {
120
+ seen[num] = true;
121
+ found.push({ number: num, carrier: 'ups', confidence: conf, ocr_fixed: true });
122
+ }
123
+ }
124
+ }
125
+
87
126
  function extractTrackingNumbers(text) {
88
127
  var found = [];
89
128
  var seen = {};
@@ -95,6 +134,7 @@
95
134
  anchorZones(text).forEach(function (z) {
96
135
  runPatterns(z.text, found, seen, z.carrier);
97
136
  spacedUpsIn(z.text, found, seen, 'high');
137
+ looseUpsIn(z.text, found, seen, 'high');
98
138
  });
99
139
  // 1) Whole-text strict patterns
100
140
  runPatterns(text, found, seen, false);
@@ -194,6 +234,7 @@
194
234
  // a) UPS number split by spaces/dashes (whole-text fallback; the anchor
195
235
  // pass already tried the hot zones at high confidence)
196
236
  spacedUpsIn(text, found, seen, 'medium');
237
+ looseUpsIn(text, found, seen, 'medium');
197
238
  // b) long digit-like runs with OCR confusions -> fix -> re-match digit formats
198
239
  var runRe = /[0-9OILSBoilszb]{10,24}/g, run;
199
240
  while ((run = runRe.exec(text)) !== null) {