Add hometitle service: fuzzy matching engine and change detector FRE-5351

- matcher.service.ts: name/address normalization, Levenshtein distance,
  geocoding proximity, confidence scoring (0.0-1.0)
- change-detector.ts: PropertySnapshot diff engine, severity scoring
  (minor/moderate/major), configurable thresholds, alert triggering
- 57 unit tests with 98%+ coverage across all thresholds
This commit is contained in:
2026-05-14 09:09:23 -04:00
parent 1b917321cf
commit 74949d9bcc
35 changed files with 7716 additions and 0 deletions

View File

@@ -0,0 +1,202 @@
import {
PropertySnapshot,
ChangeDetectionResult,
ChangeType,
Severity,
PropertyChange,
DetectionConfig,
Address,
} from './types';
import { matchRecords } from './matcher.service';
const DEFAULT_DETECTION_CONFIG: DetectionConfig = {
ownershipNameThreshold: 0.7,
deedDateSensitivity: 0.9,
taxAmountChangePercent: 15,
};
function classifyFieldChange(field: string, oldValue: unknown, newValue: unknown, config: DetectionConfig): PropertyChange {
let changeType: ChangeType;
switch (field) {
case 'ownerName':
changeType =
typeof oldValue === 'string' && typeof newValue === 'string'
? isSignificantNameChange(oldValue, newValue, config)
? 'ownership_transfer'
: 'metadata_change'
: 'ownership_transfer';
break;
case 'deedDate':
changeType = 'deed_change';
break;
case 'taxAmount':
changeType = 'tax_change';
break;
case 'lienCount':
changeType = (newValue as number) > (oldValue as number) ? 'lien_filing' : 'metadata_change';
break;
case 'taxId':
changeType = 'deed_change';
break;
default:
changeType = 'metadata_change';
}
return { field, oldValue, newValue, changeType };
}
function isSignificantNameChange(oldName: string, newName: string, config: DetectionConfig): boolean {
const dummyAddress: Address = {
streetNumber: '0',
streetName: 'dummy',
city: 'dummy',
state: 'XX',
zip: '00000',
};
const result = matchRecords(oldName, dummyAddress, newName, dummyAddress);
return result.nameScore < config.ownershipNameThreshold;
}
function determineSeverity(changes: PropertyChange[], config: DetectionConfig): Severity {
const severityOverrides = config.severityOverrides || {};
const typeToSeverity: Record<ChangeType, Severity> = {
ownership_transfer: severityOverrides['ownership_transfer'] || 'major',
deed_change: severityOverrides['deed_change'] || 'moderate',
lien_filing: severityOverrides['lien_filing'] || 'moderate',
tax_change: severityOverrides['tax_change'] || 'minor',
metadata_change: severityOverrides['metadata_change'] || 'minor',
};
const severityOrder: Severity[] = ['major', 'moderate', 'minor'];
for (const change of changes) {
const sev = typeToSeverity[change.changeType];
const idx = severityOrder.indexOf(sev);
if (idx === 0) return 'major';
}
for (const change of changes) {
const sev = typeToSeverity[change.changeType];
if (sev === 'moderate') return 'moderate';
}
return 'minor';
}
function computeChangeConfidence(changes: PropertyChange[], config: DetectionConfig): number {
if (changes.length === 0) return 0;
let totalConfidence = 0;
for (const change of changes) {
switch (change.changeType) {
case 'ownership_transfer':
totalConfidence += 0.95;
break;
case 'deed_change':
totalConfidence += config.deedDateSensitivity;
break;
case 'tax_change': {
const oldVal = change.oldValue as number;
const newVal = change.newValue as number;
const pctChange = oldVal ? Math.abs(newVal - oldVal) / oldVal * 100 : 100;
totalConfidence += pctChange >= config.taxAmountChangePercent ? 0.85 : 0.5;
break;
}
case 'lien_filing':
totalConfidence += 0.9;
break;
default:
totalConfidence += 0.4;
}
}
return Math.round((totalConfidence / changes.length) * 1000) / 1000;
}
export function detectChanges(
previous: PropertySnapshot,
current: PropertySnapshot,
config?: Partial<DetectionConfig>,
): ChangeDetectionResult {
const effectiveConfig = { ...DEFAULT_DETECTION_CONFIG, ...config };
const changes: PropertyChange[] = [];
const fieldsToCompare: (keyof Omit<PropertySnapshot, 'id' | 'capturedAt' | 'propertyId'>)[] = [
'ownerName',
'deedDate',
'taxId',
'taxAmount',
'lienCount',
'propertyType',
];
for (const field of fieldsToCompare) {
const oldValue = previous[field];
const newValue = current[field];
if (oldValue !== newValue) {
changes.push(classifyFieldChange(field, oldValue, newValue, effectiveConfig));
}
}
const addressChanges = detectAddressChanges(previous.address, current.address);
changes.push(...addressChanges);
const severity = determineSeverity(changes, effectiveConfig);
const confidence = computeChangeConfidence(changes, effectiveConfig);
let changeType: ChangeType = 'metadata_change';
if (changes.some(c => c.changeType === 'ownership_transfer')) {
changeType = 'ownership_transfer';
} else if (changes.some(c => c.changeType === 'deed_change')) {
changeType = 'deed_change';
} else if (changes.some(c => c.changeType === 'lien_filing')) {
changeType = 'lien_filing';
} else if (changes.some(c => c.changeType === 'tax_change')) {
changeType = 'tax_change';
}
return {
propertyId: previous.propertyId,
changeType,
severity,
confidence,
changes,
previousSnapshot: previous,
currentSnapshot: current,
detectedAt: new Date().toISOString(),
};
}
function detectAddressChanges(oldAddr: Address, newAddr: Address): PropertyChange[] {
const changes: PropertyChange[] = [];
const addressFields: (keyof Address)[] = ['streetNumber', 'streetName', 'streetType', 'unit', 'city', 'state', 'zip'];
for (const field of addressFields) {
const oldVal = oldAddr[field];
const newVal = newAddr[field];
if (oldVal !== newVal) {
changes.push({
field: `address.${field}`,
oldValue: oldVal,
newValue: newVal,
changeType: 'metadata_change',
});
}
}
return changes;
}
export function shouldTriggerAlert(result: ChangeDetectionResult, minSeverity: Severity = 'moderate'): boolean {
const severityOrder: Severity[] = ['minor', 'moderate', 'major'];
const resultIdx = severityOrder.indexOf(result.severity);
const minIdx = severityOrder.indexOf(minSeverity);
return resultIdx >= minIdx && result.confidence >= 0.7;
}
export { classifyFieldChange, determineSeverity, computeChangeConfidence };

View File

@@ -0,0 +1,34 @@
export {
matchRecords,
getConfigForPropertyType,
parseName,
normalizeString,
normalizeStreetType,
levenshteinDistance,
similarityScore,
} from './matcher.service';
export {
detectChanges,
shouldTriggerAlert,
classifyFieldChange,
determineSeverity,
computeChangeConfidence,
} from './change-detector';
export type {
PropertyRecord,
Address,
PropertyType,
PropertySnapshot,
MatchResult,
MatchDetails,
FieldMatch,
ChangeDetectionResult,
ChangeType,
Severity,
PropertyChange,
MatchingConfig,
DetectionConfig,
NormalizedTokens,
} from './types';

View File

@@ -0,0 +1,309 @@
import {
Address,
MatchResult,
MatchDetails,
FieldMatch,
MatchingConfig,
NormalizedTokens,
PropertyType,
} from './types';
const DEFAULT_CONFIG: MatchingConfig = {
nameThreshold: 0.85,
addressThreshold: 0.9,
overallThreshold: 0.85,
geocodingRadiusMeters: 100,
};
const COMMON_PREFIXES = new Set([
'mr', 'mrs', 'ms', 'miss', 'dr', 'prof', 'jr', 'sr', 'junior', 'senior',
'ii', 'iii', 'iv', 'rev', 'st', 'hon', 'esq',
]);
const COMMON_SUFFIXES = new Set([
'jr', 'sr', 'junior', 'senior', 'ii', 'iii', 'iv', 'v', 'esq',
'phd', 'md', 'llm', 'cpa',
]);
const STREET_TYPE_MAP: Record<string, string> = {
'st': 'street', 'street': 'street',
'ave': 'avenue', 'avenue': 'avenue',
'blvd': 'boulevard', 'boulevard': 'boulevard',
'dr': 'drive', 'drive': 'drive',
'ln': 'lane', 'lane': 'lane',
'ct': 'court', 'court': 'court',
'pl': 'place', 'place': 'place',
'rd': 'road', 'road': 'road',
'way': 'way',
'trl': 'trail', 'trail': 'trail',
'hwy': 'highway', 'highway': 'highway',
'pkwy': 'parkway', 'parkway': 'parkway',
'cir': 'circle', 'circle': 'circle',
'sq': 'square', 'square': 'square',
'ter': 'terrace', 'terrace': 'terrace',
};
const PROPERTY_TYPE_CONFIGS: Record<PropertyType, Partial<MatchingConfig>> = {
'residential': { nameThreshold: 0.85, addressThreshold: 0.9 },
'commercial': { nameThreshold: 0.8, addressThreshold: 0.9 },
'land': { nameThreshold: 0.8, addressThreshold: 0.85 },
'multi-family': { nameThreshold: 0.8, addressThreshold: 0.9 },
};
function levenshteinDistance(a: string, b: string): number {
const matrix: number[][] = Array.from({ length: b.length + 1 }, (_, i) =>
Array.from({ length: a.length + 1 }, (_, j) => (i === 0 ? j : j === 0 ? i : 0))
);
for (let i = 1; i <= b.length; i++) {
for (let j = 1; j <= a.length; j++) {
const cost = a[j - 1] === b[i - 1] ? 0 : 1;
matrix[i][j] = Math.min(
matrix[i - 1][j] + 1,
matrix[i][j - 1] + 1,
matrix[i - 1][j - 1] + cost,
);
}
}
return matrix[b.length][a.length];
}
function similarityScore(distance: number, maxLen: number): number {
if (maxLen === 0) return 1.0;
return 1.0 - distance / maxLen;
}
function normalizeString(str: string): string {
return str
.toLowerCase()
.replace(/[''']/g, '')
.replace(/[^a-z0-9\s]/g, ' ')
.replace(/\s+/g, ' ')
.trim();
}
function parseName(name: string): NormalizedTokens {
const clean = normalizeString(name);
const parts = clean.split(' ').filter(Boolean);
let firstName = '';
let lastName = '';
let middleName = '';
const initials: string[] = [];
if (parts.length === 0) return { firstName, lastName, middleName, initials };
let startIdx = 0;
while (startIdx < parts.length && COMMON_PREFIXES.has(parts[startIdx])) {
startIdx++;
}
let endIdx = parts.length;
while (endIdx > startIdx + 1 && COMMON_SUFFIXES.has(parts[endIdx - 1])) {
endIdx--;
}
const coreParts = parts.slice(startIdx, endIdx);
if (coreParts.length === 1) {
lastName = coreParts[0];
} else if (coreParts.length === 2) {
firstName = coreParts[0];
lastName = coreParts[1];
} else {
firstName = coreParts[0];
lastName = coreParts[coreParts.length - 1];
middleName = coreParts.slice(1, -1).join(' ');
}
if (firstName.length === 1) {
initials.push(firstName);
}
if (middleName) {
const middleParts = middleName.split(' ');
for (const mp of middleParts) {
if (mp.length === 1) initials.push(mp);
}
}
return { firstName, lastName, middleName, initials };
}
function normalizeStreetType(type: string): string {
const clean = normalizeString(type);
return STREET_TYPE_MAP[clean] || clean;
}
function normalizeAddress(addr: Address): string {
const parts = [
addr.streetNumber,
normalizeString(addr.streetName),
addr.streetType ? normalizeStreetType(addr.streetType) : '',
addr.unit ? normalizeString(addr.unit) : '',
normalizeString(addr.city),
addr.state.toLowerCase(),
addr.zip,
].filter(Boolean);
return parts.join(' ');
}
function computeFieldMatch(valueA: string, valueB: string, normalizeFn?: (v: string) => string): FieldMatch {
const normFn = normalizeFn || normalizeString;
const normalizedA = normFn(valueA);
const normalizedB = normFn(valueB);
if (!normalizedA && !normalizedB) return { valueA, valueB, normalizedA, normalizedB, score: 1.0 };
if (!normalizedA || !normalizedB) return { valueA, valueB, normalizedA, normalizedB, score: 0.0 };
if (normalizedA === normalizedB) return { valueA, valueB, normalizedA, normalizedB, score: 1.0 };
const dist = levenshteinDistance(normalizedA, normalizedB);
const maxLen = Math.max(normalizedA.length, normalizedB.length);
const score = similarityScore(dist, maxLen);
return { valueA, valueB, normalizedA, normalizedB, score: Math.round(score * 1000) / 1000 };
}
function haversineDistance(lat1: number, lon1: number, lat2: number, lon2: number): number {
const R = 6371000;
const dLat = ((lat2 - lat1) * Math.PI) / 180;
const dLon = ((lon2 - lon1) * Math.PI) / 180;
const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos((lat1 * Math.PI) / 180) *
Math.cos((lat2 * Math.PI) / 180) *
Math.sin(dLon / 2) *
Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
}
function computeNameScore(tokensA: NormalizedTokens, tokensB: NormalizedTokens): number {
const firstScore = computeFieldMatch(tokensA.firstName, tokensB.firstName).score;
const lastScore = computeFieldMatch(tokensA.lastName, tokensB.lastName).score;
const middleScore = computeFieldMatch(tokensA.middleName, tokensB.middleName).score;
let initialMatchScore = 1.0;
if (tokensA.initials.length > 0 || tokensB.initials.length > 0) {
const allInitialsA = new Set(tokensA.initials.map(i => i.toLowerCase()));
const allInitialsB = new Set(tokensB.initials.map(i => i.toLowerCase()));
let matched = 0;
for (const init of allInitialsA) {
if (allInitialsB.has(init)) matched++;
}
const total = Math.max(allInitialsA.size, allInitialsB.size);
initialMatchScore = total > 0 ? matched / total : 1.0;
}
const weighted = (lastScore * 0.45) + (firstScore * 0.35) + (middleScore * 0.1) + (initialMatchScore * 0.1);
return Math.round(weighted * 1000) / 1000;
}
function computeAddressScore(addrA: Address, addrB: Address, config: MatchingConfig): { score: number; geocodingDistance?: number } {
const numberMatch = computeFieldMatch(addrA.streetNumber, addrB.streetNumber).score;
const streetMatch = computeFieldMatch(addrA.streetName, addrB.streetName, normalizeString).score;
const typeMatch = computeFieldMatch(
addrA.streetType ? normalizeStreetType(addrA.streetType) : '',
addrB.streetType ? normalizeStreetType(addrB.streetType) : '',
).score;
const unitMatch = computeFieldMatch(addrA.unit || '', addrB.unit || '').score;
const cityMatch = computeFieldMatch(addrA.city, addrB.city).score;
const stateMatch = computeFieldMatch(addrA.state, addrB.state).score;
const zipMatch = computeFieldMatch(addrA.zip, addrB.zip).score;
let geocodingDistance: number | undefined;
let geoScore = 0.0;
if (addrA.latitude && addrA.longitude && addrB.latitude && addrB.longitude) {
geocodingDistance = haversineDistance(addrA.latitude, addrA.longitude, addrB.latitude, addrB.longitude);
const maxDist = config.geocodingRadiusMeters;
geoScore = geocodingDistance <= maxDist ? 1.0 : Math.max(0, 1.0 - (geocodingDistance - maxDist) / (maxDist * 5));
}
const weighted =
(numberMatch * 0.2) +
(streetMatch * 0.25) +
(typeMatch * 0.1) +
(unitMatch * 0.1) +
(cityMatch * 0.1) +
(stateMatch * 0.1) +
(zipMatch * 0.1) +
(geoScore * (geocodingDistance !== undefined ? 0.05 : 0));
return { score: Math.round(weighted * 1000) / 1000, geocodingDistance };
}
export function matchRecords(
nameA: string,
addressA: Address,
nameB: string,
addressB: Address,
config?: Partial<MatchingConfig>,
): MatchResult {
const effectiveConfig = { ...DEFAULT_CONFIG, ...config };
const tokensA = parseName(nameA);
const tokensB = parseName(nameB);
const nameScore = computeNameScore(tokensA, tokensB);
const { score: addressScore, geocodingDistance } = computeAddressScore(addressA, addressB, effectiveConfig);
const overallConfidence = Math.round((nameScore * 0.5 + addressScore * 0.5) * 1000) / 1000;
const firstMatch = computeFieldMatch(tokensA.firstName, tokensB.firstName);
const lastMatch = computeFieldMatch(tokensA.lastName, tokensB.lastName);
const middleMatch = computeFieldMatch(tokensA.middleName, tokensB.middleName);
const numberMatch = computeFieldMatch(addressA.streetNumber, addressB.streetNumber);
const streetMatch = computeFieldMatch(addressA.streetName, addressB.streetName, normalizeString);
const typeMatch = computeFieldMatch(
addressA.streetType ? normalizeStreetType(addressA.streetType) : '',
addressB.streetType ? normalizeStreetType(addressB.streetType) : '',
);
const unitMatch = computeFieldMatch(addressA.unit || '', addressB.unit || '');
const cityMatch = computeFieldMatch(addressA.city, addressB.city);
const stateMatch = computeFieldMatch(addressA.state, addressB.state);
const zipMatch = computeFieldMatch(addressA.zip, addressB.zip);
const normalizedA = normalizeAddress(addressA);
const normalizedB = normalizeAddress(addressB);
const dist = levenshteinDistance(
normalizeString(nameA),
normalizeString(nameB),
);
const details: MatchDetails = {
nameNormalized: [normalizeString(nameA), normalizeString(nameB)],
addressNormalized: [normalizedA, normalizedB],
levenshteinDistance: dist,
geocodingDistance,
fields: {
firstName: firstMatch,
lastName: lastMatch,
middleName: middleMatch,
streetNumber: numberMatch,
streetName: streetMatch,
streetType: typeMatch,
unit: unitMatch,
city: cityMatch,
state: stateMatch,
zip: zipMatch,
},
};
return {
nameScore,
addressScore,
overallConfidence,
isMatch: overallConfidence >= effectiveConfig.overallThreshold,
details,
};
}
export function getConfigForPropertyType(type: PropertyType): MatchingConfig {
return { ...DEFAULT_CONFIG, ...PROPERTY_TYPE_CONFIGS[type] };
}
export { parseName, normalizeString, normalizeStreetType, levenshteinDistance, similarityScore };

View File

@@ -0,0 +1,114 @@
export interface PropertyRecord {
id: string;
ownerName: string;
address: Address;
deedDate?: string;
taxId?: string;
propertyType: PropertyType;
metadata?: Record<string, unknown>;
}
export interface Address {
streetNumber: string;
streetName: string;
streetType?: string;
unit?: string;
city: string;
state: string;
zip: string;
latitude?: number;
longitude?: number;
}
export type PropertyType = 'residential' | 'commercial' | 'land' | 'multi-family';
export interface PropertySnapshot {
id: string;
propertyId: string;
capturedAt: string;
ownerName: string;
address: Address;
deedDate?: string;
taxId?: string;
propertyType: PropertyType;
taxAmount?: number;
lienCount?: number;
}
export interface MatchResult {
nameScore: number;
addressScore: number;
overallConfidence: number;
isMatch: boolean;
details: MatchDetails;
}
export interface MatchDetails {
nameNormalized: string[];
addressNormalized: string[];
levenshteinDistance: number;
geocodingDistance?: number;
fields: {
firstName: FieldMatch;
lastName: FieldMatch;
middleName: FieldMatch;
streetNumber: FieldMatch;
streetName: FieldMatch;
streetType: FieldMatch;
unit: FieldMatch;
city: FieldMatch;
state: FieldMatch;
zip: FieldMatch;
};
}
export interface FieldMatch {
valueA: string;
valueB: string;
normalizedA: string;
normalizedB: string;
score: number;
}
export interface ChangeDetectionResult {
propertyId: string;
changeType: ChangeType;
severity: Severity;
confidence: number;
changes: PropertyChange[];
previousSnapshot: PropertySnapshot;
currentSnapshot: PropertySnapshot;
detectedAt: string;
}
export type ChangeType = 'tax_change' | 'deed_change' | 'ownership_transfer' | 'lien_filing' | 'metadata_change';
export type Severity = 'minor' | 'moderate' | 'major';
export interface PropertyChange {
field: string;
oldValue: unknown;
newValue: unknown;
changeType: ChangeType;
}
export interface MatchingConfig {
nameThreshold: number;
addressThreshold: number;
overallThreshold: number;
geocodingRadiusMeters: number;
}
export interface DetectionConfig {
ownershipNameThreshold: number;
deedDateSensitivity: number;
taxAmountChangePercent: number;
severityOverrides?: Record<ChangeType, Severity>;
}
export interface NormalizedTokens {
firstName: string;
lastName: string;
middleName: string;
initials: string[];
}