UK police force identification via JavaScript pattern matching. ALWAYS use this before AI classification (see Guardrail G-005).
G-005: Fuzzy JS Matching Before AI
Run hardcoded JavaScript pattern matching BEFORE falling back to AI for force identification.
Why: AI = 200ms + $0.002 per call. JS = <1ms + free. Pattern covers ~85% of cases.
// In n8n Code node or JavaScript
const { lookupForce, processJobsWithForceMatching } = require('./patterns/force-matching.js');
// Single lookup
const force = lookupForce('Met Police HR Team');
// Returns: 'Metropolitan Police Service'
// Batch processing
const enrichedJobs = processJobsWithForceMatching(items);
// Adds: matchedForce, forceMatchedBy fields
47 patterns covering:
[Job Data] → [Force Matching JS] → Match? → Use it (skip AI)
→ No match? → [Claude AI]
Add a Code node BEFORE the AI classification node:
// n8n Code node
const FORCE_PATTERNS = {
'metropolitan police': 'Metropolitan Police Service',
'met police': 'Metropolitan Police Service',
// ... (full list in patterns/force-matching.js)
};
function lookupForce(text) {
if (!text) return null;
const textLower = text.toLowerCase();
for (const [pattern, forceName] of Object.entries(FORCE_PATTERNS)) {
if (textLower.includes(pattern)) {
return forceName;
}
}
return null;
}
// Process items
return items.map(item => {
const companyName = item.json.company_name || '';
const matchedForce = lookupForce(companyName);
return {
json: {
...item.json,
matchedForce,
forceMatchedBy: matchedForce ? 'pattern' : null,
needsAIClassification: !matchedForce
}
};
});
Then route:
needsAIClassification === false → Skip AI, proceed with matched forceneedsAIClassification === true → Send to Claude API| Input | Output |
|---|---|
Met Police HR Team | Metropolitan Police Service |
Hampshire Constabulary | Hampshire Constabulary |
PSNI | Police Service of Northern Ireland |
British Transport Police | British Transport Police |
Random Company Ltd | null (needs AI) |
Source file: patterns/force-matching.js
To add a pattern:
patterns/force-matching.jsFORCE_PATTERNS object[patterns] Add force pattern: XRelated data: reference-data/uk-police-forces.json (48 forces)
When building a job classification workflow:
needsAIClassificationforceMatchedBy: 'ai'forceMatchedBy: 'pattern'| Method | Time | Cost |
|---|---|---|
| JS Pattern | <1ms | Free |
| Claude API | ~200ms | ~$0.002 |
At 1000 jobs/day with 85% pattern hit rate:
Pattern not matching expected force:
FORCE_PATTERNSToo many false positives: