@soulcraft/brainy 1.5.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +188 -0
- package/LICENSE +2 -2
- package/README.md +200 -595
- package/bin/brainy-interactive.js +564 -0
- package/bin/brainy-ts.js +18 -0
- package/bin/brainy.js +672 -81
- package/dist/augmentationPipeline.d.ts +48 -220
- package/dist/augmentationPipeline.js +60 -508
- package/dist/augmentationRegistry.d.ts +22 -31
- package/dist/augmentationRegistry.js +28 -79
- package/dist/augmentations/apiServerAugmentation.d.ts +108 -0
- package/dist/augmentations/apiServerAugmentation.js +502 -0
- package/dist/augmentations/batchProcessingAugmentation.d.ts +95 -0
- package/dist/augmentations/batchProcessingAugmentation.js +567 -0
- package/dist/augmentations/brainyAugmentation.d.ts +153 -0
- package/dist/augmentations/brainyAugmentation.js +145 -0
- package/dist/augmentations/cacheAugmentation.d.ts +105 -0
- package/dist/augmentations/cacheAugmentation.js +238 -0
- package/dist/augmentations/conduitAugmentations.d.ts +54 -156
- package/dist/augmentations/conduitAugmentations.js +156 -1082
- package/dist/augmentations/connectionPoolAugmentation.d.ts +62 -0
- package/dist/augmentations/connectionPoolAugmentation.js +316 -0
- package/dist/augmentations/defaultAugmentations.d.ts +53 -0
- package/dist/augmentations/defaultAugmentations.js +88 -0
- package/dist/augmentations/entityRegistryAugmentation.d.ts +126 -0
- package/dist/augmentations/entityRegistryAugmentation.js +386 -0
- package/dist/augmentations/indexAugmentation.d.ts +117 -0
- package/dist/augmentations/indexAugmentation.js +284 -0
- package/dist/augmentations/intelligentVerbScoringAugmentation.d.ts +152 -0
- package/dist/augmentations/intelligentVerbScoringAugmentation.js +554 -0
- package/dist/augmentations/metricsAugmentation.d.ts +202 -0
- package/dist/augmentations/metricsAugmentation.js +291 -0
- package/dist/augmentations/monitoringAugmentation.d.ts +94 -0
- package/dist/augmentations/monitoringAugmentation.js +227 -0
- package/dist/augmentations/neuralImport.d.ts +50 -117
- package/dist/augmentations/neuralImport.js +255 -629
- package/dist/augmentations/requestDeduplicatorAugmentation.d.ts +52 -0
- package/dist/augmentations/requestDeduplicatorAugmentation.js +162 -0
- package/dist/augmentations/serverSearchAugmentations.d.ts +43 -22
- package/dist/augmentations/serverSearchAugmentations.js +125 -72
- package/dist/augmentations/storageAugmentation.d.ts +54 -0
- package/dist/augmentations/storageAugmentation.js +93 -0
- package/dist/augmentations/storageAugmentations.d.ts +96 -0
- package/dist/augmentations/storageAugmentations.js +182 -0
- package/dist/augmentations/synapseAugmentation.d.ts +156 -0
- package/dist/augmentations/synapseAugmentation.js +312 -0
- package/dist/augmentations/walAugmentation.d.ts +108 -0
- package/dist/augmentations/walAugmentation.js +515 -0
- package/dist/brainyData.d.ts +404 -130
- package/dist/brainyData.js +1331 -853
- package/dist/chat/BrainyChat.d.ts +16 -8
- package/dist/chat/BrainyChat.js +60 -32
- package/dist/chat/ChatCLI.d.ts +1 -1
- package/dist/chat/ChatCLI.js +6 -6
- package/dist/cli/catalog.d.ts +3 -3
- package/dist/cli/catalog.js +116 -70
- package/dist/cli/commands/core.d.ts +61 -0
- package/dist/cli/commands/core.js +348 -0
- package/dist/cli/commands/neural.d.ts +25 -0
- package/dist/cli/commands/neural.js +508 -0
- package/dist/cli/commands/utility.d.ts +37 -0
- package/dist/cli/commands/utility.js +276 -0
- package/dist/cli/index.d.ts +7 -0
- package/dist/cli/index.js +167 -0
- package/dist/cli/interactive.d.ts +164 -0
- package/dist/cli/interactive.js +542 -0
- package/dist/cortex/neuralImport.js +5 -5
- package/dist/critical/model-guardian.js +11 -4
- package/dist/embeddings/lightweight-embedder.d.ts +23 -0
- package/dist/embeddings/lightweight-embedder.js +136 -0
- package/dist/embeddings/universal-memory-manager.d.ts +38 -0
- package/dist/embeddings/universal-memory-manager.js +206 -0
- package/dist/embeddings/worker-embedding.d.ts +7 -0
- package/dist/embeddings/worker-embedding.js +77 -0
- package/dist/embeddings/worker-manager.d.ts +28 -0
- package/dist/embeddings/worker-manager.js +162 -0
- package/dist/examples/basicUsage.js +7 -7
- package/dist/graph/pathfinding.d.ts +78 -0
- package/dist/graph/pathfinding.js +393 -0
- package/dist/hnsw/hnswIndex.d.ts +13 -0
- package/dist/hnsw/hnswIndex.js +35 -0
- package/dist/hnsw/hnswIndexOptimized.d.ts +1 -0
- package/dist/hnsw/hnswIndexOptimized.js +3 -0
- package/dist/index.d.ts +9 -11
- package/dist/index.js +21 -11
- package/dist/indices/fieldIndex.d.ts +76 -0
- package/dist/indices/fieldIndex.js +357 -0
- package/dist/mcp/brainyMCPAdapter.js +3 -2
- package/dist/mcp/mcpAugmentationToolset.js +11 -17
- package/dist/neural/embeddedPatterns.d.ts +41 -0
- package/dist/neural/embeddedPatterns.js +4044 -0
- package/dist/neural/naturalLanguageProcessor.d.ts +94 -0
- package/dist/neural/naturalLanguageProcessor.js +317 -0
- package/dist/neural/naturalLanguageProcessorStatic.d.ts +64 -0
- package/dist/neural/naturalLanguageProcessorStatic.js +151 -0
- package/dist/neural/neuralAPI.d.ts +255 -0
- package/dist/neural/neuralAPI.js +612 -0
- package/dist/neural/patternLibrary.d.ts +101 -0
- package/dist/neural/patternLibrary.js +313 -0
- package/dist/neural/patterns.d.ts +27 -0
- package/dist/neural/patterns.js +68 -0
- package/dist/neural/staticPatternMatcher.d.ts +35 -0
- package/dist/neural/staticPatternMatcher.js +153 -0
- package/dist/scripts/precomputePatternEmbeddings.d.ts +19 -0
- package/dist/scripts/precomputePatternEmbeddings.js +100 -0
- package/dist/storage/adapters/fileSystemStorage.d.ts +5 -0
- package/dist/storage/adapters/fileSystemStorage.js +20 -0
- package/dist/storage/adapters/s3CompatibleStorage.d.ts +5 -0
- package/dist/storage/adapters/s3CompatibleStorage.js +16 -0
- package/dist/storage/enhancedClearOperations.d.ts +83 -0
- package/dist/storage/enhancedClearOperations.js +345 -0
- package/dist/storage/storageFactory.js +31 -27
- package/dist/triple/TripleIntelligence.d.ts +134 -0
- package/dist/triple/TripleIntelligence.js +548 -0
- package/dist/types/augmentations.d.ts +45 -344
- package/dist/types/augmentations.js +5 -2
- package/dist/types/brainyDataInterface.d.ts +20 -10
- package/dist/types/graphTypes.d.ts +46 -0
- package/dist/types/graphTypes.js +16 -2
- package/dist/utils/BoundedRegistry.d.ts +29 -0
- package/dist/utils/BoundedRegistry.js +54 -0
- package/dist/utils/embedding.js +20 -3
- package/dist/utils/hybridModelManager.js +10 -5
- package/dist/utils/metadataFilter.d.ts +33 -19
- package/dist/utils/metadataFilter.js +58 -23
- package/dist/utils/metadataIndex.d.ts +37 -6
- package/dist/utils/metadataIndex.js +427 -64
- package/dist/utils/requestDeduplicator.d.ts +10 -0
- package/dist/utils/requestDeduplicator.js +24 -0
- package/dist/utils/unifiedCache.d.ts +103 -0
- package/dist/utils/unifiedCache.js +311 -0
- package/package.json +40 -125
- package/scripts/ensure-models.js +108 -0
- package/scripts/prepare-models.js +387 -0
- package/OFFLINE_MODELS.md +0 -56
- package/dist/intelligence/neuralEngine.d.ts +0 -207
- package/dist/intelligence/neuralEngine.js +0 -706
- package/dist/utils/modelLoader.d.ts +0 -32
- package/dist/utils/modelLoader.js +0 -219
- package/dist/utils/modelManager.d.ts +0 -77
- package/dist/utils/modelManager.js +0 -219
|
@@ -4,260 +4,110 @@
|
|
|
4
4
|
* 🧠 Built-in AI augmentation for intelligent data processing
|
|
5
5
|
* ⚛️ Always free, always included, always enabled
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
* It provides intelligent data understanding, entity detection, and relationship analysis.
|
|
7
|
+
* Now using the unified BrainyAugmentation interface!
|
|
9
8
|
*/
|
|
10
|
-
import {
|
|
11
|
-
import * as fs from '../universal/fs.js';
|
|
9
|
+
import { BaseAugmentation } from './brainyAugmentation.js';
|
|
12
10
|
import * as path from '../universal/path.js';
|
|
13
11
|
/**
|
|
14
|
-
* Neural Import
|
|
12
|
+
* Neural Import Augmentation - Unified Implementation
|
|
13
|
+
* Processes data with AI before storage operations
|
|
15
14
|
*/
|
|
16
|
-
export class NeuralImportAugmentation {
|
|
17
|
-
constructor(
|
|
15
|
+
export class NeuralImportAugmentation extends BaseAugmentation {
|
|
16
|
+
constructor(config = {}) {
|
|
17
|
+
super();
|
|
18
18
|
this.name = 'neural-import';
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
this.
|
|
19
|
+
this.timing = 'before'; // Process data before storage
|
|
20
|
+
this.operations = ['add', 'addNoun', 'addVerb', 'all']; // Use 'all' to catch batch operations
|
|
21
|
+
this.priority = 80; // High priority for data processing
|
|
22
|
+
this.analysisCache = new Map();
|
|
22
23
|
this.config = {
|
|
23
24
|
confidenceThreshold: 0.7,
|
|
24
25
|
enableWeights: true,
|
|
25
26
|
skipDuplicates: true,
|
|
27
|
+
dataType: 'json',
|
|
26
28
|
...config
|
|
27
29
|
};
|
|
28
30
|
}
|
|
29
|
-
async
|
|
30
|
-
|
|
31
|
-
console.log('🧠 Neural Import augmentation initialized');
|
|
31
|
+
async onInitialize() {
|
|
32
|
+
this.log('🧠 Neural Import augmentation initialized');
|
|
32
33
|
}
|
|
33
|
-
async
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
async getStatus() {
|
|
37
|
-
return this.enabled ? 'active' : 'inactive';
|
|
34
|
+
async onShutdown() {
|
|
35
|
+
this.analysisCache.clear();
|
|
36
|
+
this.log('🧠 Neural Import augmentation shut down');
|
|
38
37
|
}
|
|
39
38
|
/**
|
|
40
|
-
*
|
|
39
|
+
* Execute augmentation - process data with AI before storage
|
|
41
40
|
*/
|
|
42
|
-
async
|
|
41
|
+
async execute(operation, params, next) {
|
|
42
|
+
// Only process on add operations
|
|
43
|
+
if (!this.operations.includes(operation)) {
|
|
44
|
+
return next();
|
|
45
|
+
}
|
|
43
46
|
try {
|
|
44
|
-
//
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
// Extract data from params based on operation
|
|
48
|
+
const rawData = this.extractRawData(operation, params);
|
|
49
|
+
if (!rawData) {
|
|
50
|
+
return next();
|
|
51
|
+
}
|
|
48
52
|
// Perform neural analysis
|
|
49
|
-
const analysis = await this.performNeuralAnalysis(
|
|
50
|
-
//
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
const analysis = await this.performNeuralAnalysis(rawData, this.config);
|
|
54
|
+
// Enhance params with neural insights
|
|
55
|
+
if (params.metadata) {
|
|
56
|
+
params.metadata._neuralProcessed = true;
|
|
57
|
+
params.metadata._neuralConfidence = analysis.confidence;
|
|
58
|
+
params.metadata._detectedEntities = analysis.detectedEntities.length;
|
|
59
|
+
params.metadata._detectedRelationships = analysis.detectedRelationships.length;
|
|
60
|
+
params.metadata._neuralInsights = analysis.insights;
|
|
61
|
+
}
|
|
62
|
+
else if (typeof params === 'object') {
|
|
63
|
+
params.metadata = {
|
|
64
|
+
_neuralProcessed: true,
|
|
65
|
+
_neuralConfidence: analysis.confidence,
|
|
66
|
+
_detectedEntities: analysis.detectedEntities.length,
|
|
67
|
+
_detectedRelationships: analysis.detectedRelationships.length,
|
|
68
|
+
_neuralInsights: analysis.insights
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
// Store neural analysis for later retrieval
|
|
54
72
|
await this.storeNeuralAnalysis(analysis);
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
description: insight.description,
|
|
64
|
-
confidence: insight.confidence
|
|
65
|
-
})),
|
|
66
|
-
metadata: {
|
|
67
|
-
detectedEntities: analysis.detectedEntities.length,
|
|
68
|
-
detectedRelationships: analysis.detectedRelationships.length,
|
|
69
|
-
timestamp: new Date().toISOString(),
|
|
70
|
-
augmentation: 'neural-import-sense'
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
catch (error) {
|
|
76
|
-
return {
|
|
77
|
-
success: false,
|
|
78
|
-
data: { nouns: [], verbs: [] },
|
|
79
|
-
error: error instanceof Error ? error.message : 'Neural analysis failed'
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Listen to real-time data feeds and process them
|
|
85
|
-
*/
|
|
86
|
-
async listenToFeed(feedUrl, callback) {
|
|
87
|
-
// For file-based feeds, watch for changes
|
|
88
|
-
if (feedUrl.startsWith('file://')) {
|
|
89
|
-
const filePath = feedUrl.replace('file://', '');
|
|
90
|
-
// Watch file for changes using Node.js fs.watch
|
|
91
|
-
const fsWatch = require('fs');
|
|
92
|
-
const watcher = fsWatch.watch(filePath, async (eventType) => {
|
|
93
|
-
if (eventType === 'change') {
|
|
94
|
-
try {
|
|
95
|
-
const fileContent = await fs.readFile(filePath);
|
|
96
|
-
const result = await this.processRawData(fileContent, this.getDataTypeFromPath(filePath));
|
|
97
|
-
if (result.success) {
|
|
98
|
-
callback({
|
|
99
|
-
nouns: result.data.nouns,
|
|
100
|
-
verbs: result.data.verbs,
|
|
101
|
-
confidence: result.data.confidence
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
catch (error) {
|
|
106
|
-
console.error('Neural Import feed error:', error);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
// For other feed types, implement appropriate listeners
|
|
113
|
-
console.log(`🧠 Neural Import listening to feed: ${feedUrl}`);
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Analyze data structure without processing (preview mode)
|
|
117
|
-
*/
|
|
118
|
-
async analyzeStructure(rawData, dataType, options) {
|
|
119
|
-
try {
|
|
120
|
-
// Parse the raw data
|
|
121
|
-
const parsedData = await this.parseRawData(rawData, dataType);
|
|
122
|
-
// Perform lightweight analysis for structure detection
|
|
123
|
-
const analysis = await this.performNeuralAnalysis(parsedData, { ...this.config, ...options });
|
|
124
|
-
// Summarize entity types
|
|
125
|
-
const entityTypeCounts = new Map();
|
|
126
|
-
analysis.detectedEntities.forEach(entity => {
|
|
127
|
-
const existing = entityTypeCounts.get(entity.nounType) || { count: 0, totalConfidence: 0 };
|
|
128
|
-
entityTypeCounts.set(entity.nounType, {
|
|
129
|
-
count: existing.count + 1,
|
|
130
|
-
totalConfidence: existing.totalConfidence + entity.confidence
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
const entityTypes = Array.from(entityTypeCounts.entries()).map(([type, stats]) => ({
|
|
134
|
-
type,
|
|
135
|
-
count: stats.count,
|
|
136
|
-
confidence: stats.totalConfidence / stats.count
|
|
137
|
-
}));
|
|
138
|
-
// Summarize relationship types
|
|
139
|
-
const relationshipTypeCounts = new Map();
|
|
140
|
-
analysis.detectedRelationships.forEach(rel => {
|
|
141
|
-
const existing = relationshipTypeCounts.get(rel.verbType) || { count: 0, totalConfidence: 0 };
|
|
142
|
-
relationshipTypeCounts.set(rel.verbType, {
|
|
143
|
-
count: existing.count + 1,
|
|
144
|
-
totalConfidence: existing.totalConfidence + rel.confidence
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
const relationshipTypes = Array.from(relationshipTypeCounts.entries()).map(([type, stats]) => ({
|
|
148
|
-
type,
|
|
149
|
-
count: stats.count,
|
|
150
|
-
confidence: stats.totalConfidence / stats.count
|
|
151
|
-
}));
|
|
152
|
-
// Assess data quality
|
|
153
|
-
const dataQuality = this.assessDataQuality(parsedData, analysis);
|
|
154
|
-
// Generate recommendations
|
|
155
|
-
const recommendations = this.generateRecommendations(parsedData, analysis, entityTypes, relationshipTypes);
|
|
156
|
-
return {
|
|
157
|
-
success: true,
|
|
158
|
-
data: {
|
|
159
|
-
entityTypes,
|
|
160
|
-
relationshipTypes,
|
|
161
|
-
dataQuality,
|
|
162
|
-
recommendations
|
|
163
|
-
}
|
|
164
|
-
};
|
|
73
|
+
// If we detected entities/relationships, potentially add them
|
|
74
|
+
if (this.context?.brain && analysis.detectedEntities.length > 0) {
|
|
75
|
+
// This could automatically create entities/relationships
|
|
76
|
+
// But for now, just enhance the metadata
|
|
77
|
+
this.log(`Detected ${analysis.detectedEntities.length} entities and ${analysis.detectedRelationships.length} relationships`);
|
|
78
|
+
}
|
|
79
|
+
// Continue with enhanced data
|
|
80
|
+
return next();
|
|
165
81
|
}
|
|
166
82
|
catch (error) {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
entityTypes: [],
|
|
171
|
-
relationshipTypes: [],
|
|
172
|
-
dataQuality: { completeness: 0, consistency: 0, accuracy: 0 },
|
|
173
|
-
recommendations: []
|
|
174
|
-
},
|
|
175
|
-
error: error instanceof Error ? error.message : 'Structure analysis failed'
|
|
176
|
-
};
|
|
83
|
+
this.log(`Neural analysis failed: ${error}`, 'warn');
|
|
84
|
+
// Continue without neural processing
|
|
85
|
+
return next();
|
|
177
86
|
}
|
|
178
87
|
}
|
|
179
88
|
/**
|
|
180
|
-
*
|
|
89
|
+
* Extract raw data from operation params
|
|
181
90
|
*/
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
type: 'confidence',
|
|
195
|
-
description: `${lowConfidenceEntities.length} entities have low confidence scores`,
|
|
196
|
-
severity: 'medium'
|
|
197
|
-
});
|
|
198
|
-
suggestions.push('Consider reviewing field names and data structure for better entity detection');
|
|
199
|
-
}
|
|
200
|
-
// Check for missing relationships
|
|
201
|
-
if (analysis.detectedRelationships.length === 0 && analysis.detectedEntities.length > 1) {
|
|
202
|
-
issues.push({
|
|
203
|
-
type: 'relationships',
|
|
204
|
-
description: 'No relationships detected between entities',
|
|
205
|
-
severity: 'low'
|
|
206
|
-
});
|
|
207
|
-
suggestions.push('Consider adding contextual fields that describe entity relationships');
|
|
208
|
-
}
|
|
209
|
-
// Check for data type compatibility
|
|
210
|
-
const supportedTypes = ['json', 'csv', 'yaml', 'text'];
|
|
211
|
-
if (!supportedTypes.includes(dataType.toLowerCase())) {
|
|
212
|
-
issues.push({
|
|
213
|
-
type: 'format',
|
|
214
|
-
description: `Data type '${dataType}' may not be fully supported`,
|
|
215
|
-
severity: 'high'
|
|
216
|
-
});
|
|
217
|
-
suggestions.push(`Convert data to one of: ${supportedTypes.join(', ')}`);
|
|
218
|
-
}
|
|
219
|
-
// Check for data completeness
|
|
220
|
-
const incompleteEntities = analysis.detectedEntities.filter((e) => !e.originalData || Object.keys(e.originalData).length < 2);
|
|
221
|
-
if (incompleteEntities.length > 0) {
|
|
222
|
-
issues.push({
|
|
223
|
-
type: 'completeness',
|
|
224
|
-
description: `${incompleteEntities.length} entities have insufficient data`,
|
|
225
|
-
severity: 'medium'
|
|
226
|
-
});
|
|
227
|
-
suggestions.push('Ensure each entity has multiple descriptive fields');
|
|
228
|
-
}
|
|
229
|
-
const compatible = issues.filter(i => i.severity === 'high').length === 0;
|
|
230
|
-
return {
|
|
231
|
-
success: true,
|
|
232
|
-
data: {
|
|
233
|
-
compatible,
|
|
234
|
-
issues,
|
|
235
|
-
suggestions
|
|
236
|
-
}
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
catch (error) {
|
|
240
|
-
return {
|
|
241
|
-
success: false,
|
|
242
|
-
data: {
|
|
243
|
-
compatible: false,
|
|
244
|
-
issues: [{
|
|
245
|
-
type: 'error',
|
|
246
|
-
description: error instanceof Error ? error.message : 'Validation failed',
|
|
247
|
-
severity: 'high'
|
|
248
|
-
}],
|
|
249
|
-
suggestions: []
|
|
250
|
-
},
|
|
251
|
-
error: error instanceof Error ? error.message : 'Compatibility validation failed'
|
|
252
|
-
};
|
|
91
|
+
extractRawData(operation, params) {
|
|
92
|
+
switch (operation) {
|
|
93
|
+
case 'add':
|
|
94
|
+
return params.content || params.data || params;
|
|
95
|
+
case 'addNoun':
|
|
96
|
+
return params.noun || params.data || params;
|
|
97
|
+
case 'addVerb':
|
|
98
|
+
return params.verb || params;
|
|
99
|
+
case 'addBatch':
|
|
100
|
+
return params.items || params.batch || params;
|
|
101
|
+
default:
|
|
102
|
+
return null;
|
|
253
103
|
}
|
|
254
104
|
}
|
|
255
105
|
/**
|
|
256
|
-
* Get the full neural analysis result (
|
|
106
|
+
* Get the full neural analysis result (for external use)
|
|
257
107
|
*/
|
|
258
108
|
async getNeuralAnalysis(rawData, dataType) {
|
|
259
|
-
const parsedData = await this.parseRawData(rawData, dataType);
|
|
260
|
-
return await this.performNeuralAnalysis(parsedData);
|
|
109
|
+
const parsedData = await this.parseRawData(rawData, dataType || this.config.dataType || 'json');
|
|
110
|
+
return await this.performNeuralAnalysis(parsedData, this.config);
|
|
261
111
|
}
|
|
262
112
|
/**
|
|
263
113
|
* Parse raw data based on type
|
|
@@ -266,33 +116,45 @@ export class NeuralImportAugmentation {
|
|
|
266
116
|
const content = typeof rawData === 'string' ? rawData : rawData.toString('utf8');
|
|
267
117
|
switch (dataType.toLowerCase()) {
|
|
268
118
|
case 'json':
|
|
269
|
-
|
|
270
|
-
|
|
119
|
+
try {
|
|
120
|
+
const jsonData = JSON.parse(content);
|
|
121
|
+
return Array.isArray(jsonData) ? jsonData : [jsonData];
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
// If JSON parse fails, treat as text
|
|
125
|
+
return [{ text: content }];
|
|
126
|
+
}
|
|
271
127
|
case 'csv':
|
|
272
128
|
return this.parseCSV(content);
|
|
273
129
|
case 'yaml':
|
|
274
130
|
case 'yml':
|
|
275
131
|
// For now, basic YAML support - in full implementation would use yaml parser
|
|
276
|
-
|
|
132
|
+
try {
|
|
133
|
+
return JSON.parse(content); // Placeholder
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
return [{ text: content }];
|
|
137
|
+
}
|
|
277
138
|
case 'txt':
|
|
278
139
|
case 'text':
|
|
279
140
|
// Split text into sentences/paragraphs for analysis
|
|
280
141
|
return content.split(/\n+/).filter(line => line.trim()).map(line => ({ text: line }));
|
|
281
142
|
default:
|
|
282
|
-
|
|
143
|
+
// Unknown type, treat as text
|
|
144
|
+
return [{ text: content }];
|
|
283
145
|
}
|
|
284
146
|
}
|
|
285
147
|
/**
|
|
286
|
-
*
|
|
148
|
+
* Parse CSV data
|
|
287
149
|
*/
|
|
288
150
|
parseCSV(content) {
|
|
289
151
|
const lines = content.split('\n').filter(line => line.trim());
|
|
290
|
-
if (lines.length
|
|
152
|
+
if (lines.length === 0)
|
|
291
153
|
return [];
|
|
292
|
-
const headers = lines[0].split(',').map(h => h.trim()
|
|
154
|
+
const headers = lines[0].split(',').map(h => h.trim());
|
|
293
155
|
const data = [];
|
|
294
156
|
for (let i = 1; i < lines.length; i++) {
|
|
295
|
-
const values = lines[i].split(',').map(v => v.trim()
|
|
157
|
+
const values = lines[i].split(',').map(v => v.trim());
|
|
296
158
|
const row = {};
|
|
297
159
|
headers.forEach((header, index) => {
|
|
298
160
|
row[header] = values[index] || '';
|
|
@@ -304,447 +166,211 @@ export class NeuralImportAugmentation {
|
|
|
304
166
|
/**
|
|
305
167
|
* Perform neural analysis on parsed data
|
|
306
168
|
*/
|
|
307
|
-
async performNeuralAnalysis(
|
|
308
|
-
|
|
309
|
-
const
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
169
|
+
async performNeuralAnalysis(data, config) {
|
|
170
|
+
const detectedEntities = [];
|
|
171
|
+
const detectedRelationships = [];
|
|
172
|
+
const insights = [];
|
|
173
|
+
// Simple entity detection (in real implementation, would use ML)
|
|
174
|
+
for (const item of data) {
|
|
175
|
+
if (typeof item === 'object') {
|
|
176
|
+
// Detect entities from object properties
|
|
177
|
+
const entityId = item.id || item.name || item.title || `entity_${Date.now()}_${Math.random()}`;
|
|
178
|
+
detectedEntities.push({
|
|
179
|
+
originalData: item,
|
|
180
|
+
nounType: this.inferNounType(item),
|
|
181
|
+
confidence: 0.85,
|
|
182
|
+
suggestedId: String(entityId),
|
|
183
|
+
reasoning: 'Detected from structured data',
|
|
184
|
+
alternativeTypes: []
|
|
185
|
+
});
|
|
186
|
+
// Detect relationships from references
|
|
187
|
+
this.detectRelationships(item, entityId, detectedRelationships);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
// Generate insights
|
|
191
|
+
if (detectedEntities.length > 10) {
|
|
192
|
+
insights.push({
|
|
193
|
+
type: 'pattern',
|
|
194
|
+
description: `Large dataset with ${detectedEntities.length} entities detected`,
|
|
195
|
+
confidence: 0.9,
|
|
196
|
+
affectedEntities: detectedEntities.slice(0, 5).map(e => e.suggestedId),
|
|
197
|
+
recommendation: 'Consider batch processing for optimal performance'
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
// Look for clusters
|
|
201
|
+
const typeGroups = this.groupByType(detectedEntities);
|
|
202
|
+
if (Object.keys(typeGroups).length > 1) {
|
|
203
|
+
insights.push({
|
|
204
|
+
type: 'cluster',
|
|
205
|
+
description: `Multiple entity types detected: ${Object.keys(typeGroups).join(', ')}`,
|
|
206
|
+
confidence: 0.8,
|
|
207
|
+
affectedEntities: [],
|
|
208
|
+
recommendation: 'Data contains diverse entity types suitable for graph analysis'
|
|
209
|
+
});
|
|
210
|
+
}
|
|
316
211
|
return {
|
|
317
212
|
detectedEntities,
|
|
318
213
|
detectedRelationships,
|
|
319
|
-
confidence:
|
|
214
|
+
confidence: detectedEntities.length > 0 ? 0.85 : 0.5,
|
|
320
215
|
insights
|
|
321
216
|
};
|
|
322
217
|
}
|
|
323
218
|
/**
|
|
324
|
-
*
|
|
325
|
-
*/
|
|
326
|
-
async detectEntitiesWithNeuralAnalysis(rawData, config = this.config) {
|
|
327
|
-
const entities = [];
|
|
328
|
-
const nounTypes = Object.values(NounType);
|
|
329
|
-
for (const [index, dataItem] of rawData.entries()) {
|
|
330
|
-
const mainText = this.extractMainText(dataItem);
|
|
331
|
-
const detections = [];
|
|
332
|
-
// Test against all noun types using semantic similarity
|
|
333
|
-
for (const nounType of nounTypes) {
|
|
334
|
-
const confidence = await this.calculateEntityTypeConfidence(mainText, dataItem, nounType);
|
|
335
|
-
if (confidence >= config.confidenceThreshold - 0.2) { // Allow slightly lower for alternatives
|
|
336
|
-
const reasoning = await this.generateEntityReasoning(mainText, dataItem, nounType);
|
|
337
|
-
detections.push({ type: nounType, confidence, reasoning });
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
if (detections.length > 0) {
|
|
341
|
-
// Sort by confidence
|
|
342
|
-
detections.sort((a, b) => b.confidence - a.confidence);
|
|
343
|
-
const primaryType = detections[0];
|
|
344
|
-
const alternatives = detections.slice(1, 3); // Top 2 alternatives
|
|
345
|
-
entities.push({
|
|
346
|
-
originalData: dataItem,
|
|
347
|
-
nounType: primaryType.type,
|
|
348
|
-
confidence: primaryType.confidence,
|
|
349
|
-
suggestedId: this.generateSmartId(dataItem, primaryType.type, index),
|
|
350
|
-
reasoning: primaryType.reasoning,
|
|
351
|
-
alternativeTypes: alternatives
|
|
352
|
-
});
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
return entities;
|
|
356
|
-
}
|
|
357
|
-
/**
|
|
358
|
-
* Calculate entity type confidence using AI
|
|
359
|
-
*/
|
|
360
|
-
async calculateEntityTypeConfidence(text, data, nounType) {
|
|
361
|
-
// Base semantic similarity using search
|
|
362
|
-
const searchResults = await this.brainy.search(text + ' ' + nounType, 1);
|
|
363
|
-
const textSimilarity = searchResults.length > 0 ? searchResults[0].score : 0.5;
|
|
364
|
-
// Field-based confidence boost
|
|
365
|
-
const fieldBoost = this.calculateFieldBasedConfidence(data, nounType);
|
|
366
|
-
// Pattern-based confidence boost
|
|
367
|
-
const patternBoost = this.calculatePatternBasedConfidence(text, data, nounType);
|
|
368
|
-
// Combine confidences with weights
|
|
369
|
-
const combined = (textSimilarity * 0.5) + (fieldBoost * 0.3) + (patternBoost * 0.2);
|
|
370
|
-
return Math.min(combined, 1.0);
|
|
371
|
-
}
|
|
372
|
-
/**
|
|
373
|
-
* Field-based confidence calculation
|
|
219
|
+
* Infer noun type from object structure
|
|
374
220
|
*/
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
boost += 0.1;
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
return Math.min(boost, 0.5);
|
|
221
|
+
inferNounType(obj) {
|
|
222
|
+
// Simple heuristics for type detection
|
|
223
|
+
if (obj.email || obj.username)
|
|
224
|
+
return 'Person';
|
|
225
|
+
if (obj.title && obj.content)
|
|
226
|
+
return 'Document';
|
|
227
|
+
if (obj.price || obj.product)
|
|
228
|
+
return 'Product';
|
|
229
|
+
if (obj.date || obj.timestamp)
|
|
230
|
+
return 'Event';
|
|
231
|
+
if (obj.url || obj.link)
|
|
232
|
+
return 'Resource';
|
|
233
|
+
if (obj.lat || obj.longitude)
|
|
234
|
+
return 'Location';
|
|
235
|
+
// Default fallback
|
|
236
|
+
return 'Entity';
|
|
396
237
|
}
|
|
397
238
|
/**
|
|
398
|
-
*
|
|
239
|
+
* Detect relationships from object references
|
|
399
240
|
*/
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
[NounType.Location]: [
|
|
414
|
-
/\b\d{5}(-\d{4})?\b/, // ZIP code
|
|
415
|
-
/Street|Ave|Road|Blvd/i
|
|
416
|
-
]
|
|
417
|
-
};
|
|
418
|
-
const relevantPatterns = patterns[nounType] || [];
|
|
419
|
-
for (const pattern of relevantPatterns) {
|
|
420
|
-
if (pattern.test(text)) {
|
|
421
|
-
boost += 0.15;
|
|
241
|
+
detectRelationships(obj, sourceId, relationships) {
|
|
242
|
+
// Look for reference patterns
|
|
243
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
244
|
+
if (key.endsWith('Id') || key.endsWith('_id') || key === 'parentId' || key === 'userId') {
|
|
245
|
+
relationships.push({
|
|
246
|
+
sourceId,
|
|
247
|
+
targetId: String(value),
|
|
248
|
+
verbType: this.inferVerbType(key),
|
|
249
|
+
confidence: 0.75,
|
|
250
|
+
weight: 1,
|
|
251
|
+
reasoning: `Reference detected in field: ${key}`,
|
|
252
|
+
context: key
|
|
253
|
+
});
|
|
422
254
|
}
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
* Generate reasoning for entity type selection
|
|
428
|
-
*/
|
|
429
|
-
async generateEntityReasoning(text, data, nounType) {
|
|
430
|
-
const reasons = [];
|
|
431
|
-
// Semantic similarity reason
|
|
432
|
-
const searchResults = await this.brainy.search(text + ' ' + nounType, 1);
|
|
433
|
-
const similarity = searchResults.length > 0 ? searchResults[0].score : 0.5;
|
|
434
|
-
if (similarity > 0.7) {
|
|
435
|
-
reasons.push(`High semantic similarity (${(similarity * 100).toFixed(1)}%)`);
|
|
436
|
-
}
|
|
437
|
-
// Field-based reasons
|
|
438
|
-
const relevantFields = this.getRelevantFields(data, nounType);
|
|
439
|
-
if (relevantFields.length > 0) {
|
|
440
|
-
reasons.push(`Contains ${nounType}-specific fields: ${relevantFields.join(', ')}`);
|
|
441
|
-
}
|
|
442
|
-
// Pattern-based reasons
|
|
443
|
-
const matchedPatterns = this.getMatchedPatterns(text, data, nounType);
|
|
444
|
-
if (matchedPatterns.length > 0) {
|
|
445
|
-
reasons.push(`Matches ${nounType} patterns: ${matchedPatterns.join(', ')}`);
|
|
446
|
-
}
|
|
447
|
-
return reasons.length > 0 ? reasons.join('; ') : 'General semantic match';
|
|
448
|
-
}
|
|
449
|
-
/**
|
|
450
|
-
* Neural Relationship Detection
|
|
451
|
-
*/
|
|
452
|
-
async detectRelationshipsWithNeuralAnalysis(entities, rawData, config = this.config) {
|
|
453
|
-
const relationships = [];
|
|
454
|
-
const verbTypes = Object.values(VerbType);
|
|
455
|
-
// For each pair of entities, test relationship possibilities
|
|
456
|
-
for (let i = 0; i < entities.length; i++) {
|
|
457
|
-
for (let j = i + 1; j < entities.length; j++) {
|
|
458
|
-
const sourceEntity = entities[i];
|
|
459
|
-
const targetEntity = entities[j];
|
|
460
|
-
// Extract context for relationship detection
|
|
461
|
-
const context = this.extractRelationshipContext(sourceEntity.originalData, targetEntity.originalData, rawData);
|
|
462
|
-
// Test all verb types
|
|
463
|
-
for (const verbType of verbTypes) {
|
|
464
|
-
const confidence = await this.calculateRelationshipConfidence(sourceEntity, targetEntity, verbType, context);
|
|
465
|
-
if (confidence >= config.confidenceThreshold - 0.1) { // Slightly lower threshold for relationships
|
|
466
|
-
const weight = config.enableWeights ?
|
|
467
|
-
this.calculateRelationshipWeight(sourceEntity, targetEntity, verbType, context) :
|
|
468
|
-
0.5;
|
|
469
|
-
const reasoning = await this.generateRelationshipReasoning(sourceEntity, targetEntity, verbType, context);
|
|
255
|
+
// Array of IDs
|
|
256
|
+
if (Array.isArray(value) && value.length > 0 && typeof value[0] === 'string') {
|
|
257
|
+
if (key.endsWith('Ids') || key.endsWith('_ids')) {
|
|
258
|
+
for (const targetId of value) {
|
|
470
259
|
relationships.push({
|
|
471
|
-
sourceId
|
|
472
|
-
targetId:
|
|
473
|
-
verbType,
|
|
474
|
-
confidence,
|
|
475
|
-
weight,
|
|
476
|
-
reasoning
|
|
477
|
-
context
|
|
478
|
-
metadata: this.extractRelationshipMetadata(sourceEntity.originalData, targetEntity.originalData, verbType)
|
|
260
|
+
sourceId,
|
|
261
|
+
targetId: String(targetId),
|
|
262
|
+
verbType: this.inferVerbType(key),
|
|
263
|
+
confidence: 0.7,
|
|
264
|
+
weight: 1,
|
|
265
|
+
reasoning: `Array reference in field: ${key}`,
|
|
266
|
+
context: key
|
|
479
267
|
});
|
|
480
268
|
}
|
|
481
269
|
}
|
|
482
270
|
}
|
|
483
271
|
}
|
|
484
|
-
// Sort by confidence and remove duplicates/conflicts
|
|
485
|
-
return this.pruneRelationships(relationships);
|
|
486
|
-
}
|
|
487
|
-
/**
|
|
488
|
-
* Calculate relationship confidence
|
|
489
|
-
*/
|
|
490
|
-
async calculateRelationshipConfidence(source, target, verbType, context) {
|
|
491
|
-
// Semantic similarity between entities and verb type
|
|
492
|
-
const relationshipText = `${this.extractMainText(source.originalData)} ${verbType} ${this.extractMainText(target.originalData)}`;
|
|
493
|
-
const directResults = await this.brainy.search(relationshipText, 1);
|
|
494
|
-
const directSimilarity = directResults.length > 0 ? directResults[0].score : 0.5;
|
|
495
|
-
// Context-based similarity
|
|
496
|
-
const contextResults = await this.brainy.search(context + ' ' + verbType, 1);
|
|
497
|
-
const contextSimilarity = contextResults.length > 0 ? contextResults[0].score : 0.5;
|
|
498
|
-
// Entity type compatibility
|
|
499
|
-
const typeCompatibility = this.calculateTypeCompatibility(source.nounType, target.nounType, verbType);
|
|
500
|
-
// Combine with weights
|
|
501
|
-
return (directSimilarity * 0.4) + (contextSimilarity * 0.4) + (typeCompatibility * 0.2);
|
|
502
|
-
}
|
|
503
|
-
/**
|
|
504
|
-
* Calculate relationship weight/strength
|
|
505
|
-
*/
|
|
506
|
-
calculateRelationshipWeight(source, target, verbType, context) {
|
|
507
|
-
let weight = 0.5; // Base weight
|
|
508
|
-
// Context richness (more descriptive = stronger)
|
|
509
|
-
const contextWords = context.split(' ').length;
|
|
510
|
-
weight += Math.min(contextWords / 20, 0.2);
|
|
511
|
-
// Entity importance (higher confidence entities = stronger relationships)
|
|
512
|
-
const avgEntityConfidence = (source.confidence + target.confidence) / 2;
|
|
513
|
-
weight += avgEntityConfidence * 0.2;
|
|
514
|
-
// Verb type specificity (more specific verbs = stronger)
|
|
515
|
-
const verbSpecificity = this.getVerbSpecificity(verbType);
|
|
516
|
-
weight += verbSpecificity * 0.1;
|
|
517
|
-
return Math.min(weight, 1.0);
|
|
518
272
|
}
|
|
519
273
|
/**
|
|
520
|
-
*
|
|
274
|
+
* Infer verb type from field name
|
|
521
275
|
*/
|
|
522
|
-
|
|
523
|
-
const
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
confidence: cluster.confidence,
|
|
542
|
-
affectedEntities: cluster.entities,
|
|
543
|
-
recommendation: `These ${cluster.primaryType}s might form a natural grouping`
|
|
544
|
-
});
|
|
545
|
-
});
|
|
546
|
-
// Detect patterns
|
|
547
|
-
const patterns = this.detectPatterns(relationships);
|
|
548
|
-
patterns.forEach(pattern => {
|
|
549
|
-
insights.push({
|
|
550
|
-
type: 'pattern',
|
|
551
|
-
description: `Common relationship pattern: ${pattern.description}`,
|
|
552
|
-
confidence: pattern.confidence,
|
|
553
|
-
affectedEntities: pattern.entities,
|
|
554
|
-
recommendation: pattern.recommendation
|
|
555
|
-
});
|
|
556
|
-
});
|
|
557
|
-
return insights;
|
|
276
|
+
inferVerbType(fieldName) {
|
|
277
|
+
const normalized = fieldName.toLowerCase();
|
|
278
|
+
if (normalized.includes('parent'))
|
|
279
|
+
return 'childOf';
|
|
280
|
+
if (normalized.includes('user'))
|
|
281
|
+
return 'belongsTo';
|
|
282
|
+
if (normalized.includes('author'))
|
|
283
|
+
return 'authoredBy';
|
|
284
|
+
if (normalized.includes('owner'))
|
|
285
|
+
return 'ownedBy';
|
|
286
|
+
if (normalized.includes('creator'))
|
|
287
|
+
return 'createdBy';
|
|
288
|
+
if (normalized.includes('member'))
|
|
289
|
+
return 'memberOf';
|
|
290
|
+
if (normalized.includes('tag'))
|
|
291
|
+
return 'taggedWith';
|
|
292
|
+
if (normalized.includes('category'))
|
|
293
|
+
return 'categorizedAs';
|
|
294
|
+
return 'relatedTo';
|
|
558
295
|
}
|
|
559
296
|
/**
|
|
560
|
-
*
|
|
297
|
+
* Group entities by type
|
|
561
298
|
*/
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
const
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
return data[field];
|
|
299
|
+
groupByType(entities) {
|
|
300
|
+
const groups = {};
|
|
301
|
+
for (const entity of entities) {
|
|
302
|
+
if (!groups[entity.nounType]) {
|
|
303
|
+
groups[entity.nounType] = [];
|
|
568
304
|
}
|
|
305
|
+
groups[entity.nounType].push(entity);
|
|
569
306
|
}
|
|
570
|
-
|
|
571
|
-
return Object.values(data)
|
|
572
|
-
.filter(v => typeof v === 'string')
|
|
573
|
-
.join(' ')
|
|
574
|
-
.substring(0, 200); // Limit length
|
|
575
|
-
}
|
|
576
|
-
generateSmartId(data, nounType, index) {
|
|
577
|
-
const mainText = this.extractMainText(data);
|
|
578
|
-
const cleanText = mainText.toLowerCase().replace(/[^a-z0-9]/g, '_').substring(0, 20);
|
|
579
|
-
return `${nounType}_${cleanText}_${index}`;
|
|
307
|
+
return groups;
|
|
580
308
|
}
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
[NounType.Organization]: [VerbType.MemberOf, VerbType.WorksWith],
|
|
594
|
-
[NounType.Project]: [VerbType.WorksWith, VerbType.Creates],
|
|
595
|
-
[NounType.Person]: [VerbType.WorksWith, VerbType.Mentors, VerbType.ReportsTo]
|
|
309
|
+
/**
|
|
310
|
+
* Store neural analysis results
|
|
311
|
+
*/
|
|
312
|
+
async storeNeuralAnalysis(analysis) {
|
|
313
|
+
// Cache the analysis for potential later use
|
|
314
|
+
const key = `analysis_${Date.now()}`;
|
|
315
|
+
this.analysisCache.set(key, analysis);
|
|
316
|
+
// Limit cache size
|
|
317
|
+
if (this.analysisCache.size > 100) {
|
|
318
|
+
const firstKey = this.analysisCache.keys().next().value;
|
|
319
|
+
if (firstKey) {
|
|
320
|
+
this.analysisCache.delete(firstKey);
|
|
596
321
|
}
|
|
597
|
-
// Add more compatibility rules
|
|
598
|
-
};
|
|
599
|
-
const sourceCompatibility = compatibilityMatrix[sourceType];
|
|
600
|
-
if (sourceCompatibility && sourceCompatibility[targetType]) {
|
|
601
|
-
return sourceCompatibility[targetType].includes(verbType) ? 1.0 : 0.3;
|
|
602
322
|
}
|
|
603
|
-
return 0.5; // Default compatibility
|
|
604
|
-
}
|
|
605
|
-
getVerbSpecificity(verbType) {
|
|
606
|
-
// More specific verbs get higher scores
|
|
607
|
-
const specificityScores = {
|
|
608
|
-
[VerbType.RelatedTo]: 0.1, // Very generic
|
|
609
|
-
[VerbType.WorksWith]: 0.7, // Specific
|
|
610
|
-
[VerbType.Mentors]: 0.9, // Very specific
|
|
611
|
-
[VerbType.ReportsTo]: 0.9, // Very specific
|
|
612
|
-
[VerbType.Supervises]: 0.9 // Very specific
|
|
613
|
-
};
|
|
614
|
-
return specificityScores[verbType] || 0.5;
|
|
615
|
-
}
|
|
616
|
-
getRelevantFields(data, nounType) {
|
|
617
|
-
// Implementation for finding relevant fields
|
|
618
|
-
return [];
|
|
619
|
-
}
|
|
620
|
-
getMatchedPatterns(text, data, nounType) {
|
|
621
|
-
// Implementation for finding matched patterns
|
|
622
|
-
return [];
|
|
623
|
-
}
|
|
624
|
-
pruneRelationships(relationships) {
|
|
625
|
-
// Remove duplicates and low-confidence relationships
|
|
626
|
-
return relationships
|
|
627
|
-
.sort((a, b) => b.confidence - a.confidence)
|
|
628
|
-
.slice(0, 1000); // Limit to top 1000 relationships
|
|
629
|
-
}
|
|
630
|
-
detectHierarchies(relationships) {
|
|
631
|
-
// Detect hierarchical structures
|
|
632
|
-
return [];
|
|
633
|
-
}
|
|
634
|
-
detectClusters(entities, relationships) {
|
|
635
|
-
// Detect entity clusters
|
|
636
|
-
return [];
|
|
637
|
-
}
|
|
638
|
-
detectPatterns(relationships) {
|
|
639
|
-
// Detect relationship patterns
|
|
640
|
-
return [];
|
|
641
|
-
}
|
|
642
|
-
calculateOverallConfidence(entities, relationships) {
|
|
643
|
-
if (entities.length === 0)
|
|
644
|
-
return 0;
|
|
645
|
-
const entityConfidence = entities.reduce((sum, e) => sum + e.confidence, 0) / entities.length;
|
|
646
|
-
if (relationships.length === 0)
|
|
647
|
-
return entityConfidence;
|
|
648
|
-
const relationshipConfidence = relationships.reduce((sum, r) => sum + r.confidence, 0) / relationships.length;
|
|
649
|
-
return (entityConfidence + relationshipConfidence) / 2;
|
|
650
|
-
}
|
|
651
|
-
async storeNeuralAnalysis(analysis) {
|
|
652
|
-
// Store the full analysis result for later retrieval by Neural Import or other systems
|
|
653
|
-
// This could be stored in the brainy instance metadata or a separate analysis store
|
|
654
323
|
}
|
|
324
|
+
/**
|
|
325
|
+
* Helper to get data type from file path
|
|
326
|
+
*/
|
|
655
327
|
getDataTypeFromPath(filePath) {
|
|
656
328
|
const ext = path.extname(filePath).toLowerCase();
|
|
657
329
|
switch (ext) {
|
|
658
330
|
case '.json': return 'json';
|
|
659
331
|
case '.csv': return 'csv';
|
|
332
|
+
case '.txt': return 'text';
|
|
660
333
|
case '.yaml':
|
|
661
334
|
case '.yml': return 'yaml';
|
|
662
|
-
case '.txt': return 'text';
|
|
663
335
|
default: return 'text';
|
|
664
336
|
}
|
|
665
337
|
}
|
|
666
|
-
async generateRelationshipReasoning(source, target, verbType, context) {
|
|
667
|
-
return `Neural analysis detected ${verbType} relationship based on semantic context`;
|
|
668
|
-
}
|
|
669
|
-
extractRelationshipMetadata(sourceData, targetData, verbType) {
|
|
670
|
-
return {
|
|
671
|
-
sourceType: typeof sourceData,
|
|
672
|
-
targetType: typeof targetData,
|
|
673
|
-
detectedBy: 'neural-import-sense',
|
|
674
|
-
timestamp: new Date().toISOString()
|
|
675
|
-
};
|
|
676
|
-
}
|
|
677
|
-
/**
|
|
678
|
-
* Assess data quality metrics
|
|
679
|
-
*/
|
|
680
|
-
assessDataQuality(parsedData, analysis) {
|
|
681
|
-
// Completeness: ratio of fields with data
|
|
682
|
-
let totalFields = 0;
|
|
683
|
-
let filledFields = 0;
|
|
684
|
-
parsedData.forEach(item => {
|
|
685
|
-
const fields = Object.keys(item);
|
|
686
|
-
totalFields += fields.length;
|
|
687
|
-
filledFields += fields.filter(field => item[field] !== null &&
|
|
688
|
-
item[field] !== undefined &&
|
|
689
|
-
item[field] !== '').length;
|
|
690
|
-
});
|
|
691
|
-
const completeness = totalFields > 0 ? filledFields / totalFields : 0;
|
|
692
|
-
// Consistency: variance in field structure
|
|
693
|
-
const fieldSets = parsedData.map(item => new Set(Object.keys(item)));
|
|
694
|
-
const allFields = new Set(fieldSets.flatMap(set => Array.from(set)));
|
|
695
|
-
let consistencyScore = 0;
|
|
696
|
-
if (fieldSets.length > 0) {
|
|
697
|
-
consistencyScore = Array.from(allFields).reduce((score, field) => {
|
|
698
|
-
const hasField = fieldSets.filter(set => set.has(field)).length;
|
|
699
|
-
return score + (hasField / fieldSets.length);
|
|
700
|
-
}, 0) / allFields.size;
|
|
701
|
-
}
|
|
702
|
-
// Accuracy: average confidence of detected entities
|
|
703
|
-
const accuracy = analysis.detectedEntities.length > 0 ?
|
|
704
|
-
analysis.detectedEntities.reduce((sum, e) => sum + e.confidence, 0) / analysis.detectedEntities.length :
|
|
705
|
-
0;
|
|
706
|
-
return {
|
|
707
|
-
completeness,
|
|
708
|
-
consistency: consistencyScore,
|
|
709
|
-
accuracy
|
|
710
|
-
};
|
|
711
|
-
}
|
|
712
338
|
/**
|
|
713
|
-
*
|
|
339
|
+
* PUBLIC API: Process raw data (for external use, like Synapses)
|
|
340
|
+
* This maintains compatibility with code that wants to use Neural Import directly
|
|
714
341
|
*/
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
if (dominantEntityType && dominantEntityType.count > parsedData.length * 0.8) {
|
|
740
|
-
recommendations.push(`Consider diversifying entity types - ${dominantEntityType.type} dominates the dataset`);
|
|
342
|
+
async processRawData(rawData, dataType, options) {
|
|
343
|
+
try {
|
|
344
|
+
const analysis = await this.getNeuralAnalysis(rawData, dataType);
|
|
345
|
+
// Convert to legacy format for compatibility
|
|
346
|
+
const nouns = analysis.detectedEntities.map(e => e.suggestedId);
|
|
347
|
+
const verbs = analysis.detectedRelationships.map(r => `${r.sourceId}->${r.verbType}->${r.targetId}`);
|
|
348
|
+
return {
|
|
349
|
+
success: true,
|
|
350
|
+
data: {
|
|
351
|
+
nouns,
|
|
352
|
+
verbs,
|
|
353
|
+
confidence: analysis.confidence,
|
|
354
|
+
insights: analysis.insights.map(i => ({
|
|
355
|
+
type: i.type,
|
|
356
|
+
description: i.description,
|
|
357
|
+
confidence: i.confidence
|
|
358
|
+
})),
|
|
359
|
+
metadata: {
|
|
360
|
+
detectedEntities: analysis.detectedEntities.length,
|
|
361
|
+
detectedRelationships: analysis.detectedRelationships.length,
|
|
362
|
+
timestamp: new Date().toISOString()
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
};
|
|
741
366
|
}
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
367
|
+
catch (error) {
|
|
368
|
+
return {
|
|
369
|
+
success: false,
|
|
370
|
+
data: { nouns: [], verbs: [] },
|
|
371
|
+
error: error instanceof Error ? error.message : 'Neural analysis failed'
|
|
372
|
+
};
|
|
746
373
|
}
|
|
747
|
-
return recommendations;
|
|
748
374
|
}
|
|
749
375
|
}
|
|
750
376
|
//# sourceMappingURL=neuralImport.js.map
|