hh-shipments 0.3.8 → 0.3.10
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.
- package/package.json +1 -1
- package/src/extract.js +54 -2
package/package.json
CHANGED
package/src/extract.js
CHANGED
|
@@ -41,9 +41,20 @@
|
|
|
41
41
|
function extractContext(text) {
|
|
42
42
|
var out = { seller_name: null, device: null };
|
|
43
43
|
if (!text) return out;
|
|
44
|
+
// A plausible person name: no digits (addresses have them), not ALL CAPS
|
|
45
|
+
// (shipping-label blocks are), proper name shape. Shipping labels like
|
|
46
|
+
// "ORANGE NJ 07050 DWT: 11,9,7" must never land in the seller field.
|
|
47
|
+
function looksLikeName(s) {
|
|
48
|
+
s = String(s).trim();
|
|
49
|
+
if (!s || s.length > 40) return false;
|
|
50
|
+
if (/\d/.test(s)) return false;
|
|
51
|
+
if (/^[A-Z\s.,'-]+$/.test(s)) return false;
|
|
52
|
+
if (!/^[A-Za-z][A-Za-z .,'-]*$/.test(s)) return false;
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
44
55
|
// WhatsApp export style: "[10/12/26, 9:41 AM] John Smith: message"
|
|
45
56
|
var m = text.match(/^\[?[^\]\n]*\]?\s*([^:\[\]\n]{2,40}):\s/m);
|
|
46
|
-
if (m && !/^(you|me|http)/i.test(m[1].trim())) out.seller_name = m[1].trim();
|
|
57
|
+
if (m && !/^(you|me|http)/i.test(m[1].trim()) && looksLikeName(m[1])) out.seller_name = m[1].trim();
|
|
47
58
|
// "from John", "seller: John"
|
|
48
59
|
if (!out.seller_name) {
|
|
49
60
|
var m2 = text.match(/(?:from|seller)\s*:\s*([A-Z][a-z]+(?:\s+[A-Z][a-z]+)?)/);
|
|
@@ -76,7 +87,13 @@
|
|
|
76
87
|
var m, re = /\b(?:1Z|12|IZ)[\s-]?(?:[0-9A-Z][\s-]?){16}\b/gi;
|
|
77
88
|
while ((m = re.exec(text)) !== null) {
|
|
78
89
|
var ups = normalize(m[0]);
|
|
79
|
-
var num =
|
|
90
|
+
var num = null;
|
|
91
|
+
if (/^1Z[0-9A-Z]{6}[0-9]{10}$/.test(ups)) num = ups; // fully strict
|
|
92
|
+
else if (/^1Z[0-9A-Z]{16}$/.test(ups)) {
|
|
93
|
+
// letters sitting in the digit zone are OCR confusions — repair them
|
|
94
|
+
num = repairUpsTail(ups.slice(2)) || ups;
|
|
95
|
+
}
|
|
96
|
+
else num = repairUpsZ(ups) || repairUpsTail(ups.slice(2));
|
|
80
97
|
if (num && !seen[num]) {
|
|
81
98
|
seen[num] = true;
|
|
82
99
|
found.push({ number: num, carrier: 'ups', confidence: conf, ocr_fixed: num !== ups });
|
|
@@ -84,6 +101,39 @@
|
|
|
84
101
|
}
|
|
85
102
|
}
|
|
86
103
|
|
|
104
|
+
// Tesseract also mangles punctuation on labels: ")" / "]" / "}" for J,
|
|
105
|
+
// "(" / "[" / "{" for C, "|" / "!" for 1 (e.g. "1Z 1X) 539 ..." for 1XJ).
|
|
106
|
+
// The UPS structure is strict (1Z + 6-char shipper + 2-digit service +
|
|
107
|
+
// 8-digit serial), so a mangled tail can be repaired and accepted only
|
|
108
|
+
// when the repaired result validates strictly — never invented.
|
|
109
|
+
var OCR_PUNCT_FIX = { ')': 'J', ']': 'J', '}': 'J', '(': 'C', '[': 'C', '{': 'C', '|': '1', '!': '1' };
|
|
110
|
+
var OCR_TAIL_DIGIT_FIX = { 'O': '0', 'Q': '0', 'I': '1', 'l': '1', '|': '1', '!': '1', 'Z': '2', 'S': '5', 'B': '8' };
|
|
111
|
+
function repairUpsTail(tail) {
|
|
112
|
+
// tail: 16 chars after the 1Z anchor (6-char shipper + 2-digit service +
|
|
113
|
+
// 8-digit serial), possibly with OCR confusions.
|
|
114
|
+
var ship = tail.slice(0, 6), digits = tail.slice(6, 16);
|
|
115
|
+
var fixShip = ship.replace(/[)\]}(\[{|!]/g, function (c) { return OCR_PUNCT_FIX[c]; });
|
|
116
|
+
var fixDigits = digits.replace(/[OQIil|!ZSB]/g, function (c) { return OCR_TAIL_DIGIT_FIX[c]; });
|
|
117
|
+
var cand = '1Z' + fixShip + fixDigits;
|
|
118
|
+
if (!UPS_STRICT.test(cand)) return null;
|
|
119
|
+
if (!/[A-Z]/.test(cand.slice(2, 8))) return null; // real shipper numbers contain a letter
|
|
120
|
+
return cand;
|
|
121
|
+
}
|
|
122
|
+
function looseUpsIn(text, found, seen, conf) {
|
|
123
|
+
// Loose: anchor + 16 chars from an OCR-confusion-tolerant alphabet,
|
|
124
|
+
// spaces/dashes allowed. repairUpsTail decides; strictness lives there.
|
|
125
|
+
var m, re = /\b(?:1Z|12|IZ)[\s-]?((?:[0-9A-Z)\]}(\[{|!][\s-]?){16})\b/gi;
|
|
126
|
+
while ((m = re.exec(text)) !== null) {
|
|
127
|
+
var tail = m[1].replace(/[\s-]/g, '').toUpperCase();
|
|
128
|
+
if (tail.length !== 16) continue;
|
|
129
|
+
var num = repairUpsTail(tail);
|
|
130
|
+
if (num && !seen[num]) {
|
|
131
|
+
seen[num] = true;
|
|
132
|
+
found.push({ number: num, carrier: 'ups', confidence: conf, ocr_fixed: true });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
87
137
|
function extractTrackingNumbers(text) {
|
|
88
138
|
var found = [];
|
|
89
139
|
var seen = {};
|
|
@@ -95,6 +145,7 @@
|
|
|
95
145
|
anchorZones(text).forEach(function (z) {
|
|
96
146
|
runPatterns(z.text, found, seen, z.carrier);
|
|
97
147
|
spacedUpsIn(z.text, found, seen, 'high');
|
|
148
|
+
looseUpsIn(z.text, found, seen, 'high');
|
|
98
149
|
});
|
|
99
150
|
// 1) Whole-text strict patterns
|
|
100
151
|
runPatterns(text, found, seen, false);
|
|
@@ -194,6 +245,7 @@
|
|
|
194
245
|
// a) UPS number split by spaces/dashes (whole-text fallback; the anchor
|
|
195
246
|
// pass already tried the hot zones at high confidence)
|
|
196
247
|
spacedUpsIn(text, found, seen, 'medium');
|
|
248
|
+
looseUpsIn(text, found, seen, 'medium');
|
|
197
249
|
// b) long digit-like runs with OCR confusions -> fix -> re-match digit formats
|
|
198
250
|
var runRe = /[0-9OILSBoilszb]{10,24}/g, run;
|
|
199
251
|
while ((run = runRe.exec(text)) !== null) {
|