CRITICAL CHECKPOINT - DO NOT PUSH TO GITHUB Recovery Status: - Successfully recovered brainy.ts from compiled JavaScript - All core v3.0 API methods functional (add, get, update, delete, relate, find, etc.) - Neural subsystem intact (562KB embedded patterns, NLP working) - Augmentation pipeline operational (20+ augmentations) - HNSW clustering system complete - Triple Intelligence compiled (needs constructor fix) - Test suite validates functionality Changes preserved: - 898 files with changes from last 3 days - 144,475 insertions - All augmentation improvements - All test coverage enhancements - Complete v3.0 feature set This is a LOCAL checkpoint only - contains recovered work after corruption incident. Created backup in .backups/brainy-full-20250910-151314.tar.gz Branch: recovery-checkpoint-20250910-151433 Date: Wed Sep 10 03:18:04 PM PDT 2025
155 lines
3.9 KiB
TypeScript
155 lines
3.9 KiB
TypeScript
/**
|
|
* 🧠 Pattern Library for Natural Language Processing
|
|
* Manages pre-computed pattern embeddings and smart matching
|
|
*
|
|
* Uses Brainy's own features for self-leveraging intelligence:
|
|
* - Embeddings for semantic similarity
|
|
* - Pattern caching for performance
|
|
* - Progressive learning from usage
|
|
*/
|
|
import { Vector } from '../coreTypes.js';
|
|
import { Brainy } from '../brainy.js';
|
|
export interface Pattern {
|
|
id: string;
|
|
category: string;
|
|
examples: string[];
|
|
pattern: string;
|
|
template: any;
|
|
confidence: number;
|
|
embedding?: Vector;
|
|
domain?: string;
|
|
frequency?: number | string;
|
|
slots?: SlotDefinition[];
|
|
}
|
|
export interface SlotDefinition {
|
|
name: string;
|
|
type: 'text' | 'number' | 'date' | 'entity' | 'location' | 'person' | 'any';
|
|
required?: boolean;
|
|
default?: any;
|
|
pattern?: string;
|
|
transform?: (value: string) => any;
|
|
}
|
|
export interface SlotExtraction {
|
|
slots: Record<string, any>;
|
|
confidence: number;
|
|
errors?: string[];
|
|
}
|
|
export declare class PatternLibrary {
|
|
private patterns;
|
|
private patternEmbeddings;
|
|
private brain;
|
|
private embeddingCache;
|
|
private successMetrics;
|
|
constructor(brain: Brainy);
|
|
/**
|
|
* Initialize pattern library with pre-computed embeddings
|
|
*/
|
|
init(): Promise<void>;
|
|
/**
|
|
* Pre-compute embeddings for all patterns for fast matching
|
|
*/
|
|
private precomputeEmbeddings;
|
|
/**
|
|
* Get embedding with caching
|
|
*/
|
|
private getEmbedding;
|
|
/**
|
|
* Find best matching patterns for a query
|
|
*/
|
|
findBestPatterns(queryEmbedding: Vector, k?: number): Promise<Array<{
|
|
pattern: Pattern;
|
|
similarity: number;
|
|
}>>;
|
|
/**
|
|
* Extract slots from query based on pattern with enhanced fuzzy matching
|
|
*/
|
|
extractSlots(query: string, pattern: Pattern): SlotExtraction;
|
|
/**
|
|
* Extract named slots with type validation
|
|
*/
|
|
private extractNamedSlots;
|
|
/**
|
|
* Fuzzy extraction using Levenshtein distance
|
|
*/
|
|
private fuzzyExtractSlots;
|
|
/**
|
|
* Fuzzy extraction for named slots
|
|
*/
|
|
private fuzzyExtractNamedSlots;
|
|
/**
|
|
* Find slot value in tokens based on type
|
|
*/
|
|
private findSlotValueInTokens;
|
|
/**
|
|
* Get default regex pattern for slot type
|
|
*/
|
|
private getDefaultPatternForType;
|
|
/**
|
|
* Transform value based on type
|
|
*/
|
|
private transformByType;
|
|
/**
|
|
* Validate slot value against definition
|
|
*/
|
|
private validateSlotValue;
|
|
/**
|
|
* Calculate Levenshtein distance between two strings
|
|
*/
|
|
private levenshteinDistance;
|
|
/**
|
|
* Align two strings for slot extraction
|
|
*/
|
|
private alignStrings;
|
|
/**
|
|
* Find best token match using fuzzy comparison
|
|
*/
|
|
private findBestTokenMatch;
|
|
/**
|
|
* Extract slots from string alignment
|
|
*/
|
|
private extractSlotsFromAlignment;
|
|
/**
|
|
* Fill template with extracted slots
|
|
*/
|
|
fillTemplate(template: any, slots: Record<string, any>): any;
|
|
/**
|
|
* Update pattern success metrics based on usage
|
|
*/
|
|
updateSuccessMetric(patternId: string, success: boolean): void;
|
|
/**
|
|
* Learn new pattern from successful query
|
|
*/
|
|
learnPattern(query: string, result: any): Promise<void>;
|
|
/**
|
|
* Helper: Average multiple vectors
|
|
*/
|
|
private averageVectors;
|
|
/**
|
|
* Helper: Calculate cosine similarity
|
|
*/
|
|
private cosineSimilarity;
|
|
/**
|
|
* Helper: Simple tokenization
|
|
*/
|
|
private tokenize;
|
|
/**
|
|
* Helper: Post-process extracted slots
|
|
*/
|
|
private postProcessSlots;
|
|
/**
|
|
* Helper: Generate regex pattern from query
|
|
*/
|
|
private generateRegexFromQuery;
|
|
/**
|
|
* Get pattern statistics for monitoring
|
|
*/
|
|
getStatistics(): {
|
|
totalPatterns: number;
|
|
categories: Record<string, number>;
|
|
averageConfidence: number;
|
|
topPatterns: Array<{
|
|
id: string;
|
|
success: number;
|
|
}>;
|
|
};
|
|
}
|