-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapper.js
More file actions
425 lines (366 loc) · 11.6 KB
/
mapper.js
File metadata and controls
425 lines (366 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
* UbiCity Urban Knowledge Mapper
*
* Maps learning experiences across urban space, finding patterns and connections
* that wouldn't be visible within traditional institutional boundaries.
*/
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
class LearningExperience {
constructor(data) {
this.data = data;
if (!this.data.id) {
this.data.id = this.generateId();
}
if (!this.data.timestamp) {
this.data.timestamp = new Date().toISOString();
}
}
generateId() {
const uuid = crypto.randomUUID();
return `ubi-${uuid}`;
}
validate() {
const required = ['id', 'timestamp', 'learner', 'context', 'experience'];
for (const field of required) {
if (!this.data[field]) {
throw new Error(`Missing required field: ${field}`);
}
}
return true;
}
toJSON() {
return this.data;
}
}
class UrbanKnowledgeMapper {
constructor(storageDir = './ubicity-data') {
this.storageDir = storageDir;
this.experiences = new Map();
this.locationIndex = new Map();
this.domainIndex = new Map();
this.learnerIndex = new Map();
this.ensureStorageExists();
}
ensureStorageExists() {
if (!fs.existsSync(this.storageDir)) {
fs.mkdirSync(this.storageDir, { recursive: true });
}
const subdirs = ['experiences', 'maps', 'analyses'];
subdirs.forEach(dir => {
const dirPath = path.join(this.storageDir, dir);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
});
}
/**
* Capture a new learning experience
*/
captureExperience(experienceData) {
const experience = new LearningExperience(experienceData);
experience.validate();
this.experiences.set(experience.data.id, experience);
this.updateIndices(experience);
this.persist(experience);
return experience.data.id;
}
/**
* Update indices for fast lookup
*/
updateIndices(experience) {
const { id } = experience.data;
// Location index
const locationName = experience.data.context.location?.name;
if (locationName) {
if (!this.locationIndex.has(locationName)) {
this.locationIndex.set(locationName, new Set());
}
this.locationIndex.get(locationName).add(id);
}
// Domain index
const domains = experience.data.experience.domains || [];
domains.forEach(domain => {
if (!this.domainIndex.has(domain)) {
this.domainIndex.set(domain, new Set());
}
this.domainIndex.get(domain).add(id);
});
// Learner index
const learnerId = experience.data.learner.id;
if (!this.learnerIndex.has(learnerId)) {
this.learnerIndex.set(learnerId, new Set());
}
this.learnerIndex.get(learnerId).add(id);
}
/**
* Persist experience to disk
*/
persist(experience) {
const filename = `${experience.data.id}.json`;
const filepath = path.join(this.storageDir, 'experiences', filename);
fs.writeFileSync(filepath, JSON.stringify(experience.data, null, 2));
}
/**
* Load all experiences from storage
*/
loadAll() {
const experiencesDir = path.join(this.storageDir, 'experiences');
if (!fs.existsSync(experiencesDir)) return;
const files = fs.readdirSync(experiencesDir);
files.forEach(file => {
if (file.endsWith('.json')) {
const filepath = path.join(experiencesDir, file);
const data = JSON.parse(fs.readFileSync(filepath, 'utf8'));
const experience = new LearningExperience(data);
this.experiences.set(experience.data.id, experience);
this.updateIndices(experience);
}
});
}
/**
* Find interdisciplinary connections
* Returns learning experiences that span multiple domains
*/
findInterdisciplinaryConnections() {
const connections = [];
this.experiences.forEach((experience, id) => {
const domains = experience.data.experience.domains || [];
if (domains.length > 1) {
connections.push({
id,
domains,
description: experience.data.experience.description,
location: experience.data.context.location?.name,
unexpected: experience.data.experience.outcome?.connections_made || []
});
}
});
return connections;
}
/**
* Map experiences by location
* Creates a geographic view of learning density
*/
mapByLocation() {
const locationMap = {};
this.locationIndex.forEach((experienceIds, locationName) => {
const experiences = Array.from(experienceIds).map(id =>
this.experiences.get(id).data
);
const domains = new Set();
const types = new Set();
const learners = new Set();
experiences.forEach(exp => {
(exp.experience.domains || []).forEach(d => domains.add(d));
types.add(exp.experience.type);
learners.add(exp.learner.id);
});
// Get coordinates if available
const firstExp = experiences[0];
const coords = firstExp.context.location?.coordinates;
locationMap[locationName] = {
count: experiences.length,
domains: Array.from(domains),
types: Array.from(types),
learners: learners.size,
coordinates: coords,
diversity: domains.size // How many different disciplines meet here
};
});
return locationMap;
}
/**
* Find learning paths for a specific learner
* Shows progression and evolution of interests
*/
getLearnerJourney(learnerId) {
const experienceIds = this.learnerIndex.get(learnerId);
if (!experienceIds) return null;
const experiences = Array.from(experienceIds)
.map(id => this.experiences.get(id).data)
.sort((a, b) => new Date(a.timestamp) - new Date(b.timestamp));
return {
learnerId,
experienceCount: experiences.length,
timeline: experiences.map(exp => ({
timestamp: exp.timestamp,
location: exp.context.location?.name,
type: exp.experience.type,
domains: exp.experience.domains,
description: exp.experience.description
})),
domainEvolution: this.trackDomainEvolution(experiences),
questionsEmerged: experiences
.flatMap(exp => exp.experience.outcome?.next_questions || [])
};
}
/**
* Track how a learner's domains evolve over time
*/
trackDomainEvolution(experiences) {
const evolution = [];
const seenDomains = new Set();
experiences.forEach(exp => {
const newDomains = (exp.experience.domains || [])
.filter(d => !seenDomains.has(d));
if (newDomains.length > 0) {
evolution.push({
timestamp: exp.timestamp,
newDomains,
context: exp.context.location?.name
});
newDomains.forEach(d => seenDomains.add(d));
}
});
return evolution;
}
/**
* Generate a network of domain connections
* Shows which domains frequently co-occur in learning experiences
*/
generateDomainNetwork() {
const edges = new Map(); // "domain1-domain2" -> weight
this.experiences.forEach(experience => {
const domains = experience.data.experience.domains || [];
// Create edges between all pairs of domains in this experience
for (let i = 0; i < domains.length; i++) {
for (let j = i + 1; j < domains.length; j++) {
const d1 = domains[i];
const d2 = domains[j];
const key = [d1, d2].sort().join('-');
edges.set(key, (edges.get(key) || 0) + 1);
}
}
});
// Convert to network format
const network = {
nodes: Array.from(this.domainIndex.keys()).map(domain => ({
id: domain,
size: this.domainIndex.get(domain).size
})),
edges: Array.from(edges.entries()).map(([key, weight]) => {
const [source, target] = key.split('-');
return { source, target, weight };
})
};
return network;
}
/**
* Identify "learning hotspots" - locations with high diversity
*/
findLearningHotspots(minDiversity = 3) {
const locationMap = this.mapByLocation();
return Object.entries(locationMap)
.filter(([_, data]) => data.diversity >= minDiversity)
.sort((a, b) => b[1].diversity - a[1].diversity)
.map(([name, data]) => ({
location: name,
...data
}));
}
/**
* Export to formats suitable for analysis
*/
exportToVoyant() {
// Create a corpus of learning descriptions
const texts = [];
this.experiences.forEach((experience, id) => {
const exp = experience.data;
const text = {
id,
title: `${exp.context.location?.name || 'Unknown'} - ${exp.experience.type}`,
content: [
exp.experience.description,
...(exp.experience.outcome?.connections_made || []),
...(exp.experience.outcome?.next_questions || [])
].join('\n\n'),
metadata: {
location: exp.context.location?.name,
domains: exp.experience.domains?.join(', '),
type: exp.experience.type,
timestamp: exp.timestamp
}
};
texts.push(text);
});
return texts;
}
/**
* Generate a report of the current state
*/
generateReport() {
const interdisciplinary = this.findInterdisciplinaryConnections();
const hotspots = this.findLearningHotspots();
const domainNetwork = this.generateDomainNetwork();
const report = {
generated: new Date().toISOString(),
summary: {
totalExperiences: this.experiences.size,
uniqueLearners: this.learnerIndex.size,
uniqueLocations: this.locationIndex.size,
uniqueDomains: this.domainIndex.size,
interdisciplinaryExperiences: interdisciplinary.length
},
learningHotspots: hotspots,
interdisciplinaryConnections: interdisciplinary.slice(0, 10),
domainNetwork,
locationMap: this.mapByLocation()
};
const reportPath = path.join(
this.storageDir,
'analyses',
`report-${Date.now()}.json`
);
fs.writeFileSync(reportPath, JSON.stringify(report, null, 2));
return report;
}
}
// CLI interface
if (require.main === module) {
const mapper = new UrbanKnowledgeMapper();
mapper.loadAll();
const command = process.argv[2];
switch (command) {
case 'report':
const report = mapper.generateReport();
console.log(JSON.stringify(report, null, 2));
break;
case 'hotspots':
const hotspots = mapper.findLearningHotspots();
console.log('Learning Hotspots (locations with high disciplinary diversity):');
hotspots.forEach(h => {
console.log(`\n${h.location}:`);
console.log(` Experiences: ${h.count}`);
console.log(` Domains: ${h.domains.join(', ')}`);
console.log(` Diversity: ${h.diversity}`);
});
break;
case 'network':
const network = mapper.generateDomainNetwork();
console.log(JSON.stringify(network, null, 2));
break;
case 'learner':
const learnerId = process.argv[3];
if (!learnerId) {
console.error('Please provide a learner ID');
process.exit(1);
}
const journey = mapper.getLearnerJourney(learnerId);
console.log(JSON.stringify(journey, null, 2));
break;
default:
console.log('UbiCity Urban Knowledge Mapper');
console.log('\nUsage: node mapper.js <command>');
console.log('\nCommands:');
console.log(' report Generate a full analysis report');
console.log(' hotspots Find learning hotspots');
console.log(' network Show domain connection network');
console.log(' learner <id> Show journey for a specific learner');
}
}
module.exports = {
LearningExperience,
UrbanKnowledgeMapper
};