Image-to-Text
English
custom
ocr
document-ai
information-extraction
key-information-extraction
nameplate
nameplate-ocr
equipment-nameplate
data-center
data-centre
datacenter
mission-critical
mep
electrical
mechanical
equipment-schedule
schedule-verification
asset-register
commissioning
quality-assurance
ups
pdu
switchgear
generator
ats
transformer
busway
battery
crah
crac
chiller
cooling
server-rack
construction
aec
edge-ai
on-device
privacy-preserving
tesseract
browser
Eval Results (legacy)
Update: OCR look-alike readings, extraction bench, schedule import
Browse files- README.md +0 -5
- nameplate-model.js +107 -7
- tests/nameplate-model.test.mjs +28 -0
README.md
CHANGED
|
@@ -126,9 +126,6 @@ The result is a pass/mismatch verdict per plate, with the fields that disagree n
|
|
| 126 |
|
| 127 |
## How it works
|
| 128 |
|
| 129 |
-

|
| 130 |
-
|
| 131 |
-
|
| 132 |
```
|
| 133 |
photo ─► photo check ─► OCR (Tesseract.js) ─► field parser ─► equipment typing ─► schedule matcher ─► verdict + CSV
|
| 134 |
blur/glare lines + confidence labelled rules maker line excluded code folding, voltage signal
|
|
@@ -203,8 +200,6 @@ static floor load.
|
|
| 203 |
|
| 204 |
## Results
|
| 205 |
|
| 206 |
-

|
| 207 |
-
|
| 208 |
### Extraction benchmark — labelled plates, field by field
|
| 209 |
|
| 210 |
A generated corpus of plates with **by-construction** ground truth (not the parser's own reading), rendered
|
|
|
|
| 126 |
|
| 127 |
## How it works
|
| 128 |
|
|
|
|
|
|
|
|
|
|
| 129 |
```
|
| 130 |
photo ─► photo check ─► OCR (Tesseract.js) ─► field parser ─► equipment typing ─► schedule matcher ─► verdict + CSV
|
| 131 |
blur/glare lines + confidence labelled rules maker line excluded code folding, voltage signal
|
|
|
|
| 200 |
|
| 201 |
## Results
|
| 202 |
|
|
|
|
|
|
|
| 203 |
### Extraction benchmark — labelled plates, field by field
|
| 204 |
|
| 205 |
A generated corpus of plates with **by-construction** ground truth (not the parser's own reading), rendered
|
nameplate-model.js
CHANGED
|
@@ -185,6 +185,31 @@ export function parseNameplate(input) {
|
|
| 185 |
const CODE = '([A-Z0-9][A-Z0-9\\-\\/\\.]{2,})';
|
| 186 |
scan('model', new RegExp(`(?:\\bMODEL\\b|\\bMOD\\.|\\bM\\/N\\b|\\bCAT(?:ALOG)?\\.?\\s*(?:NO\\b\\.?|#|NUMBER)|\\bP\\/N\\b|\\bPART\\s*(?:NO\\b\\.?|#|NUMBER))\\s*(?:NO\\b\\.?|NUMBER\\b|#)?\\s*[:#.]?\\s*${CODE}`), m => cleanCode(m[1]), 'code');
|
| 187 |
scan('serial', new RegExp(`(?:\\bSERIAL\\b|\\bSER\\.|\\bS\\/N\\b|\\b5\\/N\\b|\\bSN\\b)\\s*(?:NO\\b\\.?|NUMBER\\b|#)?\\s*[:#.]?\\s*([A-Z0-9][A-Z0-9\\-]{3,})`), m => cleanCode(m[1]), 'code');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
scan('refrigerant', /\bR[-\s]?(410A|134A|1234ZE|1234YF|1233ZD|513A|514A|515B|454B|468A|449A|407C|452A|455A|245FA|123|32|22|717|744|290|128|503|508B)\b/, m => `R-${m[1]}`);
|
| 189 |
scan('hz', /\b(50|60)\s*HZ\b|\bHZ\s*[:]?\s*(50|60)\b|\bFREQ(?:UENCY)?\.?\s*[:]?\s*(50|60)\b/, m => +(m[1] || m[2] || m[3]));
|
| 190 |
scan('phase', /\b([13])\s*-?\s*(?:PH|PHASE|Ø|Φ)\b|\bPH(?:ASE)?\s*[:]?\s*([13])\b/, m => +(m[1] || m[2]));
|
|
@@ -209,20 +234,29 @@ export function parseNameplate(input) {
|
|
| 209 |
|
| 210 |
// Voltage: every voltage on the plate (a transformer or UPS carries more than one).
|
| 211 |
const volts = [];
|
|
|
|
| 212 |
let voltLine = null;
|
| 213 |
for (const [i, l] of lines.entries()) {
|
| 214 |
let found = voltagesIn(views[i].raw);
|
| 215 |
for (const t of views[i].nums) if (!found.values.length) found = voltagesIn(t);
|
| 216 |
// A digit run split by a stray space ("4 15Y/240", "2 08 V"): use the rejoined reading only if it finds more.
|
| 217 |
for (const t of [views[i].raw, ...views[i].nums]) {
|
| 218 |
-
const joined = t.replace(/(?<=\b\d{1,2}) (?=\d)/g, '');
|
| 219 |
-
if (joined !== t) {
|
|
|
|
|
|
|
| 220 |
}
|
|
|
|
| 221 |
if (found.values.length) {
|
| 222 |
for (const v of found.values) if (!volts.includes(v)) volts.push(v);
|
| 223 |
if (!voltLine) voltLine = { text: found.display, conf: l.conf, line: i };
|
| 224 |
}
|
| 225 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
if (volts.length) fields.voltage = { value: voltLine.text, conf: voltLine.conf, line: voltLine.line, values: volts };
|
| 227 |
|
| 228 |
// Amps: a labelled rated current first, then any bare "1002 A". Refrigerants (R-410A) are not amps.
|
|
@@ -297,7 +331,12 @@ export function parseNameplate(input) {
|
|
| 297 |
// "…Transformer Corp" or "…Switchgear Ltd" must not type a chiller plate.
|
| 298 |
const keep = views.filter((_, i) => i !== makerLine);
|
| 299 |
const texts = [keep.map(v => v.raw).join('\n'), ...['I', 'L'].map(one => keep.map(v => foldWords(v.raw, one)).join('\n'))];
|
| 300 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
const conf = lines.length ? Math.round(lines.reduce((s, l) => s + l.conf, 0) / lines.length) : 0;
|
| 302 |
return { fields, type, conf, text: lines.map(l => l.text).join('\n'), lines: lines.map(l => ({ text: l.text, conf: l.conf, bbox: l.bbox })) };
|
| 303 |
}
|
|
@@ -365,9 +404,9 @@ export function ocrFix(t) {
|
|
| 365 |
.replace(/(\d)\s*V[vV]\b/g, '$1 V')
|
| 366 |
.replace(/([A-Za-z0-9])-\s+(?=[A-Za-z0-9])/g, '$1-')
|
| 367 |
.replace(/(\d)\.\s+(?=\d)/g, '$1.')
|
| 368 |
-
.replace(/\b(50|60)\s*H[wW2]\b/g, '$1 HZ')
|
| 369 |
// K read as X in KVA / KW / EKW.
|
| 370 |
-
.replace(/(\d\s*)
|
| 371 |
.replace(/(\d\s*)E[KX][WN]\b/g, '$1EKW')
|
| 372 |
.replace(/(\d\s*)XW\b/g, '$1KW');
|
| 373 |
}
|
|
@@ -391,13 +430,20 @@ export function voltagesIn(s) {
|
|
| 391 |
add(+m[1], `${m[1]}${m[2] ? 'Y' : ''}/${m[3]} V`);
|
| 392 |
add(+m[3], null);
|
| 393 |
}
|
| 394 |
-
|
|
|
|
| 395 |
while ((m = single.exec(s))) add(+m[1], `${m[1]} V`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
const labelled = /\bVOLT(?:S|AGE)?\b[^0-9]{0,12}(\d{3,5})\b/g;
|
| 397 |
while ((m = labelled.exec(s))) add(+m[1], `${m[1]} V`);
|
| 398 |
const kv = /\b(\d{1,2}(?:\.\d{1,2})?)\s*KV\b/g;
|
| 399 |
while ((m = kv.exec(s))) add(Math.round(parseFloat(m[1]) * 1000), `${m[1]} kV`);
|
| 400 |
-
return { values, display };
|
| 401 |
}
|
| 402 |
|
| 403 |
// ------------------------------------------------------------------ schedule
|
|
@@ -707,6 +753,60 @@ export const SAMPLE_PLATES = [
|
|
| 707 |
|
| 708 |
function up(s) { return String(s ?? '').toUpperCase(); }
|
| 709 |
function escapeRe(s) { return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 710 |
function cleanCode(s) { return s.replace(/[.\-/]+$/, ''); }
|
| 711 |
function titleCase(s) { return s.toLowerCase().replace(/\b[a-z]/g, c => c.toUpperCase()); }
|
| 712 |
/** Canonical battery chemistry from the many ways plates spell it. */
|
|
|
|
| 185 |
const CODE = '([A-Z0-9][A-Z0-9\\-\\/\\.]{2,})';
|
| 186 |
scan('model', new RegExp(`(?:\\bMODEL\\b|\\bMOD\\.|\\bM\\/N\\b|\\bCAT(?:ALOG)?\\.?\\s*(?:NO\\b\\.?|#|NUMBER)|\\bP\\/N\\b|\\bPART\\s*(?:NO\\b\\.?|#|NUMBER))\\s*(?:NO\\b\\.?|NUMBER\\b|#)?\\s*[:#.]?\\s*${CODE}`), m => cleanCode(m[1]), 'code');
|
| 187 |
scan('serial', new RegExp(`(?:\\bSERIAL\\b|\\bSER\\.|\\bS\\/N\\b|\\b5\\/N\\b|\\bSN\\b)\\s*(?:NO\\b\\.?|NUMBER\\b|#)?\\s*[:#.]?\\s*([A-Z0-9][A-Z0-9\\-]{3,})`), m => cleanCode(m[1]), 'code');
|
| 188 |
+
// OCR splits a label ("MODE L NO", "MO DEL", "MO0ELNO") or a code ("IS-39 50-480", "NPX -7174", "V R-4045UF") with stray
|
| 189 |
+
// spaces. When the strict rule found nothing, match the label with a space allowed between letters and the 0/O
|
| 190 |
+
// look-alike, and in both cases rejoin the code's fragments from the raw line.
|
| 191 |
+
const sp = w => w.split('').map(c => (c === 'O' ? '[O0]' : c === 'D' ? '[D0]' : c === 'I' ? '[I1]' : c === 'L' ? '[L1]' : c === 'S' ? '[S5]' : c)).join('\\s?');
|
| 192 |
+
const CODE_LABELS = {
|
| 193 |
+
model: `(?:${sp('MODEL')}|${sp('MOD')}\\.|\\bM\\s?\\/\\s?N\\b|${sp('CAT')}\\.?\\s*(?:${sp('NO')}\\b\\.?|#|${sp('NUMBER')})|\\bP\\s?\\/\\s?N\\b|${sp('PART')}\\s*(?:${sp('NO')}\\b\\.?|#|${sp('NUMBER')}))\\s*(?:${sp('NO')}\\b\\.?|${sp('NUMBER')}\\b|#)?\\s*[:#.]?\\s*`,
|
| 194 |
+
serial: `(?:${sp('SERIAL')}|${sp('SER')}\\.|\\bS\\s?\\/\\s?N\\b|\\b5\\s?\\/\\s?N\\b|\\bSN\\b)\\s*(?:${sp('NO')}\\b\\.?|${sp('NUMBER')}\\b|#)?\\s*[:#.]?\\s*`,
|
| 195 |
+
};
|
| 196 |
+
for (const key of ['model', 'serial']) {
|
| 197 |
+
const label = new RegExp(CODE_LABELS[key]);
|
| 198 |
+
const cur = fields[key];
|
| 199 |
+
const cands = cur ? [[cur.line, lines[cur.line]]] : [...lines.entries()];
|
| 200 |
+
for (const [i, l] of cands) {
|
| 201 |
+
const raw = up(l.text);
|
| 202 |
+
let rest = null;
|
| 203 |
+
if (cur) { const at = raw.indexOf(String(cur.value)); if (at >= 0) rest = raw.slice(at); }
|
| 204 |
+
else { const m = raw.match(label); if (m) rest = raw.slice(m.index + m[0].length); }
|
| 205 |
+
if (rest == null) continue;
|
| 206 |
+
const joined = joinCodeFragments(rest);
|
| 207 |
+
if (joined.length >= 4 && (!cur || joined.length > String(cur.value).length)) {
|
| 208 |
+
fields[key] = { value: cleanCode(joined), conf: l.conf, line: i };
|
| 209 |
+
}
|
| 210 |
+
break;
|
| 211 |
+
}
|
| 212 |
+
}
|
| 213 |
scan('refrigerant', /\bR[-\s]?(410A|134A|1234ZE|1234YF|1233ZD|513A|514A|515B|454B|468A|449A|407C|452A|455A|245FA|123|32|22|717|744|290|128|503|508B)\b/, m => `R-${m[1]}`);
|
| 214 |
scan('hz', /\b(50|60)\s*HZ\b|\bHZ\s*[:]?\s*(50|60)\b|\bFREQ(?:UENCY)?\.?\s*[:]?\s*(50|60)\b/, m => +(m[1] || m[2] || m[3]));
|
| 215 |
scan('phase', /\b([13])\s*-?\s*(?:PH|PHASE|Ø|Φ)\b|\bPH(?:ASE)?\s*[:]?\s*([13])\b/, m => +(m[1] || m[2]));
|
|
|
|
| 234 |
|
| 235 |
// Voltage: every voltage on the plate (a transformer or UPS carries more than one).
|
| 236 |
const volts = [];
|
| 237 |
+
const lowDc = []; // 12 / 24 / 48 V: used only when no mains voltage is on the plate
|
| 238 |
let voltLine = null;
|
| 239 |
for (const [i, l] of lines.entries()) {
|
| 240 |
let found = voltagesIn(views[i].raw);
|
| 241 |
for (const t of views[i].nums) if (!found.values.length) found = voltagesIn(t);
|
| 242 |
// A digit run split by a stray space ("4 15Y/240", "2 08 V"): use the rejoined reading only if it finds more.
|
| 243 |
for (const t of [views[i].raw, ...views[i].nums]) {
|
| 244 |
+
const joined = t.replace(/(?<=\b\d{1,2}) (?=\d)/g, '').replace(/(?<=\d) (?=[O0](?:\s|V|$))/g, '');
|
| 245 |
+
if (joined !== t) {
|
| 246 |
+
for (const cand of [joined, foldNumbers(joined)]) { const j = voltagesIn(cand); if (j.values.length > found.values.length) found = j; }
|
| 247 |
+
}
|
| 248 |
}
|
| 249 |
+
for (const v of found.lowDc || []) if (!lowDc.includes(v)) lowDc.push(v);
|
| 250 |
if (found.values.length) {
|
| 251 |
for (const v of found.values) if (!volts.includes(v)) volts.push(v);
|
| 252 |
if (!voltLine) voltLine = { text: found.display, conf: l.conf, line: i };
|
| 253 |
}
|
| 254 |
}
|
| 255 |
+
if (!volts.length && lowDc.length) {
|
| 256 |
+
const li = lines.findIndex((_, i) => voltagesIn(views[i].raw).lowDc.length || voltagesIn(views[i].nums[0] || '').lowDc.length);
|
| 257 |
+
volts.push(lowDc[0]);
|
| 258 |
+
voltLine = { text: `${lowDc[0]} V`, conf: lines[Math.max(li, 0)]?.conf ?? 0, line: Math.max(li, 0) };
|
| 259 |
+
}
|
| 260 |
if (volts.length) fields.voltage = { value: voltLine.text, conf: voltLine.conf, line: voltLine.line, values: volts };
|
| 261 |
|
| 262 |
// Amps: a labelled rated current first, then any bare "1002 A". Refrigerants (R-410A) are not amps.
|
|
|
|
| 331 |
// "…Transformer Corp" or "…Switchgear Ltd" must not type a chiller plate.
|
| 332 |
const keep = views.filter((_, i) => i !== makerLine);
|
| 333 |
const texts = [keep.map(v => v.raw).join('\n'), ...['I', 'L'].map(one => keep.map(v => foldWords(v.raw, one)).join('\n'))];
|
| 334 |
+
let typeIdx = EQUIPMENT_TYPES.findIndex(([, , re]) => texts.some(t => re.test(t)));
|
| 335 |
+
// A title OCR has split, dropped letters from or read with look-alikes ("5WITCHBOAR0", "UN1NTERRUPT IBLE P0WER", "P0WERE DGE")
|
| 336 |
+
// matches none of the patterns above. Match the squashed line against the long words in them, a letter or two off.
|
| 337 |
+
// Only when the exact patterns found nothing: ahead of a clean match a fuzzy hit is more often wrong than right.
|
| 338 |
+
if (typeIdx < 0) typeIdx = fuzzyTypeIndex(keep.map(v => v.raw));
|
| 339 |
+
const type = typeIdx >= 0 ? EQUIPMENT_TYPES[typeIdx][0] : null;
|
| 340 |
const conf = lines.length ? Math.round(lines.reduce((s, l) => s + l.conf, 0) / lines.length) : 0;
|
| 341 |
return { fields, type, conf, text: lines.map(l => l.text).join('\n'), lines: lines.map(l => ({ text: l.text, conf: l.conf, bbox: l.bbox })) };
|
| 342 |
}
|
|
|
|
| 404 |
.replace(/(\d)\s*V[vV]\b/g, '$1 V')
|
| 405 |
.replace(/([A-Za-z0-9])-\s+(?=[A-Za-z0-9])/g, '$1-')
|
| 406 |
.replace(/(\d)\.\s+(?=\d)/g, '$1.')
|
| 407 |
+
.replace(/\b(50|60)\s*H\s?[wW2]\b/g, '$1 HZ')
|
| 408 |
// K read as X in KVA / KW / EKW.
|
| 409 |
+
.replace(/(\d\s*)[KX]\s?VA\b/g, '$1KVA')
|
| 410 |
.replace(/(\d\s*)E[KX][WN]\b/g, '$1EKW')
|
| 411 |
.replace(/(\d\s*)XW\b/g, '$1KW');
|
| 412 |
}
|
|
|
|
| 430 |
add(+m[1], `${m[1]}${m[2] ? 'Y' : ''}/${m[3]} V`);
|
| 431 |
add(+m[3], null);
|
| 432 |
}
|
| 433 |
+
// "230 V3 PH": OCR runs the unit into the phase count, so the unit may be followed by a digit but not a letter
|
| 434 |
+
const single = /\b(\d{3,5})\s*(?:V|VAC|VDC|VOLTS?)(?![A-Za-z])/g;
|
| 435 |
while ((m = single.exec(s))) add(+m[1], `${m[1]} V`);
|
| 436 |
+
// Low-voltage DC (a battery cabinet's 48 V) is only read with an explicit unit, from the usual DC bus values.
|
| 437 |
+
const dc = /\b(12|24|48)\s*(?:V|VDC|VOLTS?)(?![A-Za-z])/g;
|
| 438 |
+
const lowDc = [];
|
| 439 |
+
while ((m = dc.exec(s))) lowDc.push(+m[1]);
|
| 440 |
+
// "INPUT 380 3 PH": the V was lost but a voltage-sized number sits right before the phase count
|
| 441 |
+
if (!values.length) { m = /\b(\d{3,4})\s+[13]\s*-?\s*PH\b/.exec(s); if (m) add(+m[1], `${m[1]} V`); }
|
| 442 |
const labelled = /\bVOLT(?:S|AGE)?\b[^0-9]{0,12}(\d{3,5})\b/g;
|
| 443 |
while ((m = labelled.exec(s))) add(+m[1], `${m[1]} V`);
|
| 444 |
const kv = /\b(\d{1,2}(?:\.\d{1,2})?)\s*KV\b/g;
|
| 445 |
while ((m = kv.exec(s))) add(Math.round(parseFloat(m[1]) * 1000), `${m[1]} kV`);
|
| 446 |
+
return { values, display, lowDc };
|
| 447 |
}
|
| 448 |
|
| 449 |
// ------------------------------------------------------------------ schedule
|
|
|
|
| 753 |
|
| 754 |
function up(s) { return String(s ?? '').toUpperCase(); }
|
| 755 |
function escapeRe(s) { return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }
|
| 756 |
+
/** First code on `rest` with the stray-space fragments OCR cuts it into rejoined: across a hyphen ("NPX -7174",
|
| 757 |
+
* "NPX- 9644"), digit-led pieces after a digit ("IS-39 50-480"), and a lone leading letter ("V R-4045UF"). */
|
| 758 |
+
function joinCodeFragments(rest) {
|
| 759 |
+
const toks = rest.trim().split(/\s+/);
|
| 760 |
+
const isCodeTok = t => /^[A-Z0-9][A-Z0-9\-\/.]*$/.test(t);
|
| 761 |
+
if (!toks.length || !isCodeTok(toks[0])) return '';
|
| 762 |
+
let out = toks[0];
|
| 763 |
+
for (let k = 1; k < toks.length && k < 4; k++) {
|
| 764 |
+
const t = toks[k];
|
| 765 |
+
if (!/^[A-Z0-9\-\/.]+$/.test(t)) break;
|
| 766 |
+
const hyphen = out.endsWith('-') || t.startsWith('-');
|
| 767 |
+
const digits = /\d$/.test(out) && /^\d[A-Z0-9\-\/.]*$/.test(t) && t.length >= 2 && /-/.test(out + t);
|
| 768 |
+
const lead = out.length === 1 && /^[A-Z]$/.test(out) && /^[A-Z0-9]+-[A-Z0-9\-]+$/.test(t);
|
| 769 |
+
if (!(hyphen || digits || lead)) break;
|
| 770 |
+
out += t;
|
| 771 |
+
}
|
| 772 |
+
return out;
|
| 773 |
+
}
|
| 774 |
+
|
| 775 |
+
// Squashed type words (spaces and punctuation removed) taken from the EQUIPMENT_TYPES patterns: plain letter runs of 7+.
|
| 776 |
+
const TYPE_WORDS = EQUIPMENT_TYPES.map(([, , re]) => re.source.split('|')
|
| 777 |
+
.map(a => a.replace(/\\b|\\s\??|\[-\\s\]\?|[^A-Z ]/g, '').replace(/\s+/g, ''))
|
| 778 |
+
.filter(w => w.length >= 7));
|
| 779 |
+
// A character OCR reads as a look-alike matches its letters at no cost.
|
| 780 |
+
const LOOKALIKE = { 0: 'OD', 1: 'IL', 5: 'S', 8: 'B', 6: 'G', 2: 'Z' };
|
| 781 |
+
const sameChar = (kw, c) => kw === c || (LOOKALIKE[c] || '').includes(kw);
|
| 782 |
+
|
| 783 |
+
/** Smallest edits (insert / delete / substitute, look-alikes free) for `word` to appear anywhere in `text` (Sellers). */
|
| 784 |
+
function approxIn(word, text, limit) {
|
| 785 |
+
let prev = new Array(word.length + 1).fill(0).map((_, i) => i);
|
| 786 |
+
let best = prev[word.length];
|
| 787 |
+
for (let j = 0; j < text.length; j++) {
|
| 788 |
+
const cur = [0];
|
| 789 |
+
for (let i = 1; i <= word.length; i++) {
|
| 790 |
+
cur[i] = Math.min(prev[i] + 1, cur[i - 1] + 1, prev[i - 1] + (sameChar(word[i - 1], text[j]) ? 0 : 1));
|
| 791 |
+
}
|
| 792 |
+
if (cur[word.length] < best) best = cur[word.length];
|
| 793 |
+
if (best === 0) return 0;
|
| 794 |
+
prev = cur;
|
| 795 |
+
}
|
| 796 |
+
return best <= limit ? best : Infinity;
|
| 797 |
+
}
|
| 798 |
+
|
| 799 |
+
/** Index in EQUIPMENT_TYPES of the first type whose long word turns up (approximately) in a squashed line, or -1. */
|
| 800 |
+
function fuzzyTypeIndex(rawLines) {
|
| 801 |
+
const squashed = rawLines.map(t => t.replace(/[^A-Z0-9]/g, '')).filter(t => t.length >= 7);
|
| 802 |
+
for (let k = 0; k < TYPE_WORDS.length; k++) {
|
| 803 |
+
for (const w of TYPE_WORDS[k]) {
|
| 804 |
+
const limit = w.length < 9 ? 0 : w.length < 14 ? 1 : w.length < 20 ? 2 : 3;
|
| 805 |
+
if (squashed.some(t => approxIn(w, t, limit) <= limit)) return k;
|
| 806 |
+
}
|
| 807 |
+
}
|
| 808 |
+
return -1;
|
| 809 |
+
}
|
| 810 |
function cleanCode(s) { return s.replace(/[.\-/]+$/, ''); }
|
| 811 |
function titleCase(s) { return s.toLowerCase().replace(/\b[a-z]/g, c => c.toUpperCase()); }
|
| 812 |
/** Canonical battery chemistry from the many ways plates spell it. */
|
tests/nameplate-model.test.mjs
CHANGED
|
@@ -364,3 +364,31 @@ test('new rack columns import from the schedule and are checked field by field',
|
|
| 364 |
assert.deepEqual(bad.filter(c => c.status === 'mismatch').map(c => c.key).sort(),
|
| 365 |
['formFactor', 'outletTypes', 'portSpeed', 'rackLoad'].sort());
|
| 366 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
assert.deepEqual(bad.filter(c => c.status === 'mismatch').map(c => c.key).sort(),
|
| 365 |
['formFactor', 'outletTypes', 'portSpeed', 'rackLoad'].sort());
|
| 366 |
});
|
| 367 |
+
|
| 368 |
+
test('OCR-split codes, labels and titles still read (bench cases, seed 7)', () => {
|
| 369 |
+
const ups = plate(['Westgate Power', 'UNINTERRUPTIBLE POWER SUPPLY', 'M/N NPX -7174-415', 'S/N: NL26AARXCJ']);
|
| 370 |
+
assert.equal(ups.fields.model.value, 'NPX-7174-415');
|
| 371 |
+
assert.equal(plate(['Westgate Power', 'SWITCHBOARD', 'M/N IS-39 50-480', 'S/N: NL24ASPTQZ']).fields.model.value, 'IS-3950-480');
|
| 372 |
+
assert.equal(plate(['Westgate Power', 'BATTERY CABINET', 'M/N V R-4045UF', 'S/N: NL25AL2PLM']).fields.model.value, 'VR-4045UF');
|
| 373 |
+
assert.equal(plate(['Meridian Power', 'UNINTERRUPTIBLE POWER SUPPLY', 'MODE L NO: NPX-3113-208']).fields.model.value, 'NPX-3113-208');
|
| 374 |
+
assert.equal(plate(['Coldline Systems', 'SWITCHBOARD', 'MO0ELNO: IS-8971SO']).fields.model.value, 'IS-8971SO');
|
| 375 |
+
// a code is not run into the next word
|
| 376 |
+
assert.equal(plate(['MODEL: HG-3000 DIESEL GENERATOR']).fields.model.value, 'HG-3000');
|
| 377 |
+
// titles with dropped letters, look-alikes and stray spaces
|
| 378 |
+
assert.equal(plate(['Meridian Power', '5WITCHBOAR0', 'MODEL: I5-8000JN']).type, 'switchgear');
|
| 379 |
+
assert.equal(plate(['Harbor Engine Works', 'UN1NTERRUPT IBLE P0WER SUPPLY', 'CAT. NO. NPX-5644-415']).type, 'ups');
|
| 380 |
+
assert.equal(plate(['Polaris Thermal', 'C0MPUTER RO0M A1R HANDLER', 'CAT. NO. CW-851']).type, 'crah');
|
| 381 |
+
// an exact match is never overridden by a fuzzy one
|
| 382 |
+
assert.equal(plate(['CHILLED WATER PUMP', 'MODEL: CP-100']).type, 'pump');
|
| 383 |
+
});
|
| 384 |
+
|
| 385 |
+
test('voltage: a battery cabinet\'s 48 V, a unit run into the phase count, a split digit run, a lost V', () => {
|
| 386 |
+
assert.deepEqual(plate(['BATTERY CABINET', '48 V', 'VRLA 40 CELLS']).fields.voltage.values, [48]);
|
| 387 |
+
assert.deepEqual(plate(['INPUT 230 V3 PH 50 HZ']).fields.voltage.values, [230]);
|
| 388 |
+
assert.deepEqual(plate(['INPUT: 208Y/12 O VAC 3 PH 60 HZ']).fields.voltage.values, [208, 120]);
|
| 389 |
+
assert.deepEqual(plate(['INPUT 380 3 PH 60 HZ']).fields.voltage.values, [380]);
|
| 390 |
+
// "24 V" alone on a mains-voltage plate must not displace a real voltage
|
| 391 |
+
assert.deepEqual(plate(['480 V 3 PH', 'CONTROL 24 V']).fields.voltage.values, [480]);
|
| 392 |
+
assert.equal(plate(['2000 K VA / 1800 KW']).fields.kva.value, 2000);
|
| 393 |
+
assert.equal(plate(['INPUT 480 V 3 PH 60 H 2']).fields.hz.value, 60);
|
| 394 |
+
});
|