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
151 lines
No EOL
5.2 KiB
JavaScript
151 lines
No EOL
5.2 KiB
JavaScript
/**
|
|
* 🧠 Natural Language Query Processor - STATIC VERSION
|
|
* No runtime initialization, no memory leaks, patterns pre-built at compile time
|
|
*
|
|
* Uses static pattern matching with 220 pre-built patterns
|
|
*/
|
|
import { patternMatchQuery } from './staticPatternMatcher.js';
|
|
export class NaturalLanguageProcessor {
|
|
constructor() {
|
|
this.queryHistory = [];
|
|
// Patterns are static - no initialization needed!
|
|
}
|
|
/**
|
|
* No initialization needed - patterns are pre-built!
|
|
*/
|
|
async init() {
|
|
// Nothing to do - patterns are compiled into the code
|
|
return Promise.resolve();
|
|
}
|
|
/**
|
|
* Process natural language query into structured Triple Intelligence query
|
|
* @param naturalQuery The natural language query string
|
|
* @param queryEmbedding Pre-computed embedding from Brainy (passed in to avoid circular dependency)
|
|
*/
|
|
async processNaturalQuery(naturalQuery, queryEmbedding) {
|
|
// Use static pattern matcher (no async, no memory allocation!)
|
|
const structuredQuery = patternMatchQuery(naturalQuery, queryEmbedding);
|
|
// Step 3: Enhance with intent analysis if needed
|
|
if (!structuredQuery.where && !structuredQuery.connected) {
|
|
const intent = await this.analyzeIntent(naturalQuery);
|
|
// Add metadata based on intent
|
|
if (intent.type === 'field' && intent.extractedTerms.fields) {
|
|
structuredQuery.where = this.buildFieldConstraints(intent.extractedTerms.fields);
|
|
}
|
|
}
|
|
// Track for learning (but don't create new Brainy!)
|
|
this.queryHistory.push({
|
|
query: naturalQuery,
|
|
result: structuredQuery,
|
|
success: false // Will be updated based on user interaction
|
|
});
|
|
// Keep history limited to prevent memory growth
|
|
if (this.queryHistory.length > 100) {
|
|
this.queryHistory.shift();
|
|
}
|
|
return structuredQuery;
|
|
}
|
|
/**
|
|
* Analyze query intent using keywords
|
|
*/
|
|
async analyzeIntent(query) {
|
|
const lowerQuery = query.toLowerCase();
|
|
// Check for field-specific keywords
|
|
const fieldKeywords = ['where', 'filter', 'with', 'has', 'contains', 'equals', 'greater', 'less', 'between'];
|
|
const hasFieldIntent = fieldKeywords.some(kw => lowerQuery.includes(kw));
|
|
// Check for graph keywords
|
|
const graphKeywords = ['related', 'connected', 'linked', 'associated', 'references'];
|
|
const hasGraphIntent = graphKeywords.some(kw => lowerQuery.includes(kw));
|
|
// Determine type
|
|
let type = 'vector';
|
|
if (hasFieldIntent && hasGraphIntent) {
|
|
type = 'combined';
|
|
}
|
|
else if (hasFieldIntent) {
|
|
type = 'field';
|
|
}
|
|
else if (hasGraphIntent) {
|
|
type = 'graph';
|
|
}
|
|
return {
|
|
type,
|
|
confidence: 0.8,
|
|
extractedTerms: {
|
|
fields: hasFieldIntent ? this.extractFieldTerms(query) : undefined,
|
|
relationships: hasGraphIntent ? this.extractRelationshipTerms(query) : undefined
|
|
}
|
|
};
|
|
}
|
|
/**
|
|
* Extract field terms from query
|
|
*/
|
|
extractFieldTerms(query) {
|
|
const terms = [];
|
|
// Simple extraction of potential field names
|
|
const words = query.split(/\s+/);
|
|
const fieldIndicators = ['year', 'date', 'author', 'type', 'category', 'status', 'price'];
|
|
for (const word of words) {
|
|
if (fieldIndicators.includes(word.toLowerCase())) {
|
|
terms.push(word.toLowerCase());
|
|
}
|
|
}
|
|
return terms;
|
|
}
|
|
/**
|
|
* Extract relationship terms
|
|
*/
|
|
extractRelationshipTerms(query) {
|
|
const terms = [];
|
|
const relationshipWords = ['related', 'connected', 'linked', 'references', 'cites'];
|
|
const words = query.toLowerCase().split(/\s+/);
|
|
for (const word of words) {
|
|
if (relationshipWords.includes(word)) {
|
|
terms.push(word);
|
|
}
|
|
}
|
|
return terms;
|
|
}
|
|
/**
|
|
* Build field constraints from extracted terms
|
|
*/
|
|
buildFieldConstraints(fields) {
|
|
const constraints = {};
|
|
// Simple mapping for common fields
|
|
for (const field of fields) {
|
|
// This would be enhanced with actual value extraction
|
|
constraints[field] = { exists: true };
|
|
}
|
|
return constraints;
|
|
}
|
|
/**
|
|
* Find similar queries from history (without using Brainy)
|
|
*/
|
|
findSimilarQueries(embedding) {
|
|
// Simple similarity check against recent history
|
|
// This is just a placeholder - real implementation would use cosine similarity
|
|
return [];
|
|
}
|
|
/**
|
|
* Adapt a previous query for new input
|
|
*/
|
|
adaptQuery(newQuery, previousResult) {
|
|
return previousResult;
|
|
}
|
|
/**
|
|
* Extract entities from query
|
|
*/
|
|
async extractEntities(query) {
|
|
// Could use the Entity Registry here if available
|
|
return [];
|
|
}
|
|
/**
|
|
* Build query from components
|
|
*/
|
|
buildQuery(query, intent, entities) {
|
|
return {
|
|
like: query,
|
|
limit: 10
|
|
};
|
|
}
|
|
}
|
|
//# sourceMappingURL=naturalLanguageProcessorStatic.js.map
|