@naniteninja/profile-comparison-lib 0.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.
@@ -0,0 +1,3321 @@
1
+ import * as i0 from '@angular/core';
2
+ import { Injectable, EventEmitter, ViewChild, Output, Input, Component, NgModule } from '@angular/core';
3
+ import { switchMap, map, catchError, shareReplay, tap } from 'rxjs/operators';
4
+ import { from, throwError, of, forkJoin, defer, switchMap as switchMap$1, map as map$1, Observable, retryWhen, concatMap, timer, mergeMap, catchError as catchError$1, toArray, firstValueFrom } from 'rxjs';
5
+ import * as i3 from '@angular/forms';
6
+ import { FormGroup, FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
7
+ import * as use from '@tensorflow-models/universal-sentence-encoder';
8
+ import '@tensorflow/tfjs';
9
+ import * as i1 from '@angular/common/http';
10
+ import { HttpHeaders, HttpClientModule } from '@angular/common/http';
11
+ import * as i2 from '@angular/common';
12
+ import { CommonModule } from '@angular/common';
13
+ import * as i1$1 from '@angular/router';
14
+ import { RouterModule } from '@angular/router';
15
+
16
+ class EmbeddingService {
17
+ model$ = null;
18
+ // Get embedding for a single text (returns Observable)
19
+ getEmbedding(text) {
20
+ return this.loadModel().pipe(switchMap((model) => from(model.embed([text]))), switchMap((embeddings) => {
21
+ const t2d = embeddings;
22
+ return from(t2d.array()).pipe(map((arr) => {
23
+ t2d.dispose();
24
+ return Float32Array.from(arr[0]);
25
+ }));
26
+ }), catchError((err) => throwError(() => new Error('Model failed to load or embed: ' + err))));
27
+ }
28
+ /**
29
+ * Group-align two lists based on semantic similarity, allowing one-to-many relationships.
30
+ *
31
+ * Design goals:
32
+ * - Semantic relevance takes priority over original order or spacing.
33
+ * - Each term in listB is assigned to the single most similar anchor in listA (if above threshold).
34
+ * - For an anchor in listA with multiple highly related listB terms, they are emitted as consecutive rows:
35
+ * [A, B1], ['-', B2], ['-', B3], ... so they render adjacently and remain visually connected.
36
+ * - Low-similarity/unassigned terms from either side are appended at the end and never break groups.
37
+ */
38
+ groupAlignLists(listA, listB, threshold = 0.25, preserveLeftOrder = true, exclusivityMargin = 0.06, leftCohesion = 0.23) {
39
+ // Short-circuit simple cases
40
+ if ((!listA || listA.length === 0) && (!listB || listB.length === 0)) {
41
+ return of([]);
42
+ }
43
+ if (!listA || listA.length === 0) {
44
+ return of(listB.map((b) => ({ left: '-', right: b, score: 0 })));
45
+ }
46
+ if (!listB || listB.length === 0) {
47
+ return of(listA.map((a) => ({ left: a, right: '-', score: 0 })));
48
+ }
49
+ return this.loadModel().pipe(switchMap((model) => forkJoin([
50
+ from(model.embed(listA)),
51
+ from(model.embed(listB))
52
+ ]).pipe(switchMap(([tA, tB]) => {
53
+ return forkJoin([from(tA.array()), from(tB.array())]).pipe(map((tensorArrays) => {
54
+ const [arrA, arrB] = tensorArrays;
55
+ // Dispose tensors to avoid memory leaks
56
+ tA.dispose();
57
+ tB.dispose();
58
+ const aCount = listA.length;
59
+ const bCount = listB.length;
60
+ // Pre-convert to Float32Array for faster cosine computations
61
+ const vecA = new Array(aCount);
62
+ const vecB = new Array(bCount);
63
+ for (let i = 0; i < aCount; i++)
64
+ vecA[i] = Float32Array.from(arrA[i]);
65
+ for (let j = 0; j < bCount; j++)
66
+ vecB[j] = Float32Array.from(arrB[j]);
67
+ // Build full cross-list similarity matrix S[i][j] = sim(A_i, B_j)
68
+ const S = new Array(aCount);
69
+ for (let i = 0; i < aCount; i++) {
70
+ const row = new Array(bCount);
71
+ for (let j = 0; j < bCount; j++) {
72
+ const emb = this.cosineSimilarity(vecA[i], vecB[j]);
73
+ const lex = this.computeLexicalBoost(listA[i], listB[j]);
74
+ // Apply domain gating to prevent mismatched category-vs-activity alignments
75
+ const cap = this.domainCompatibilityCap(listA[i], listB[j]);
76
+ row[j] = Math.min(Math.max(emb, lex), cap);
77
+ }
78
+ S[i] = row;
79
+ }
80
+ // Build within-list similarity for A (A↔A) to allow left-side extras to attach to cohesive anchors.
81
+ // We use the same hybrid semantic/lexical metric.
82
+ const SAA = new Array(aCount);
83
+ for (let i = 0; i < aCount; i++) {
84
+ const rowAA = new Array(aCount);
85
+ for (let k = 0; k < aCount; k++) {
86
+ if (i === k) {
87
+ rowAA[k] = 1; // self
88
+ }
89
+ else {
90
+ const embAA = this.cosineSimilarity(vecA[i], vecA[k]);
91
+ const lexAA = this.computeLexicalBoost(listA[i], listA[k]);
92
+ rowAA[k] = Math.max(embAA, lexAA);
93
+ }
94
+ }
95
+ SAA[i] = rowAA;
96
+ }
97
+ // Build thresholded neighborhoods
98
+ const neighborsA = new Map();
99
+ const neighborsB = new Map();
100
+ for (let i = 0; i < aCount; i++) {
101
+ const row = [];
102
+ for (let j = 0; j < bCount; j++) {
103
+ const s = S[i][j];
104
+ if (s >= threshold) {
105
+ row.push({ j, score: s });
106
+ const rev = neighborsB.get(j) || [];
107
+ rev.push({ i, score: s });
108
+ neighborsB.set(j, rev);
109
+ }
110
+ }
111
+ // sort descending for deterministic preference lists
112
+ row.sort((a, b) => b.score - a.score);
113
+ if (row.length)
114
+ neighborsA.set(i, row);
115
+ }
116
+ // Identify exclusive vs ambiguous B nodes
117
+ const exclusiveForA = new Map();
118
+ const ambiguousB = [];
119
+ const unassignedB = [];
120
+ for (let j = 0; j < bCount; j++) {
121
+ const list = (neighborsB.get(j) || []).sort((a, b) => b.score - a.score);
122
+ if (list.length === 0) {
123
+ unassignedB.push(j);
124
+ }
125
+ else if (list.length === 1) {
126
+ const i = list[0].i;
127
+ const arr = exclusiveForA.get(i) || [];
128
+ arr.push({ j, score: list[0].score });
129
+ exclusiveForA.set(i, arr);
130
+ }
131
+ else {
132
+ // If best is significantly better than second-best, treat as exclusive to best
133
+ const best = list[0];
134
+ const second = list[1];
135
+ if (best.score - second.score >= exclusivityMargin) {
136
+ const arr = exclusiveForA.get(best.i) || [];
137
+ arr.push({ j, score: best.score });
138
+ exclusiveForA.set(best.i, arr);
139
+ }
140
+ else {
141
+ ambiguousB.push(j);
142
+ }
143
+ }
144
+ }
145
+ // A-side exclusivity: if an anchor A has a clearly dominant best B, prefer that exclusive pairing.
146
+ const bOwner = new Map();
147
+ for (const [i, arr] of exclusiveForA.entries()) {
148
+ for (const x of arr) {
149
+ const ex = bOwner.get(x.j);
150
+ if (!ex || x.score > ex.score)
151
+ bOwner.set(x.j, { i, score: x.score });
152
+ }
153
+ }
154
+ const ambSetForA = new Set(ambiguousB);
155
+ for (let i = 0; i < aCount; i++) {
156
+ const list = (neighborsA.get(i) || [])
157
+ .slice()
158
+ .sort((a, b) => b.score - a.score);
159
+ if (!list.length)
160
+ continue;
161
+ const top = list[0];
162
+ const second = list[1];
163
+ const hasClearLead = !second || top.score - second.score >= exclusivityMargin;
164
+ if (top.score >= threshold && hasClearLead) {
165
+ const current = bOwner.get(top.j);
166
+ if (!current || top.score > current.score + 1e-6) {
167
+ // Reassign this B exclusively to A=i
168
+ // Remove from any previous exclusiveForA groups
169
+ for (const [ai, arr] of exclusiveForA.entries()) {
170
+ exclusiveForA.set(ai, arr.filter((x) => x.j !== top.j));
171
+ }
172
+ const arr = exclusiveForA.get(i) || [];
173
+ if (!arr.some((x) => x.j === top.j))
174
+ arr.push({ j: top.j, score: top.score });
175
+ arr.sort((u, v) => v.score - u.score);
176
+ exclusiveForA.set(i, arr);
177
+ bOwner.set(top.j, { i, score: top.score });
178
+ ambSetForA.delete(top.j);
179
+ }
180
+ }
181
+ }
182
+ ambiguousB.splice(0, ambiguousB.length, ...Array.from(ambSetForA.values()));
183
+ // Mutual-best pairing stage (within small tolerance) to prioritize exclusive high-similarity pairs
184
+ const mutualTolerance = 0.03;
185
+ const bestBForA = new Array(aCount);
186
+ for (let i = 0; i < aCount; i++) {
187
+ let bBest = -1;
188
+ let bBestScore = -Infinity;
189
+ for (let j = 0; j < bCount; j++) {
190
+ const s = S[i][j];
191
+ if (s > bBestScore) {
192
+ bBestScore = s;
193
+ bBest = j;
194
+ }
195
+ }
196
+ bestBForA[i] = { j: bBest, score: bBestScore };
197
+ }
198
+ const bestAForB = new Array(bCount);
199
+ for (let j = 0; j < bCount; j++) {
200
+ let aBest = -1;
201
+ let aBestScore = -Infinity;
202
+ for (let i = 0; i < aCount; i++) {
203
+ const s = S[i][j];
204
+ if (s > aBestScore) {
205
+ aBestScore = s;
206
+ aBest = i;
207
+ }
208
+ }
209
+ bestAForB[j] = { i: aBest, score: aBestScore };
210
+ }
211
+ // Promote mutual-best pairs to exclusive, removing from ambiguous list if present
212
+ const ambSet = new Set(ambiguousB);
213
+ for (let i = 0; i < aCount; i++) {
214
+ const b = bestBForA[i].j;
215
+ if (b >= 0 && S[i][b] >= threshold) {
216
+ const a = bestAForB[b].i;
217
+ const score = S[i][b];
218
+ // within tolerance of best on both sides
219
+ const isWithinA = score >= bestBForA[i].score - mutualTolerance;
220
+ const isWithinB = score >= bestAForB[b].score - mutualTolerance;
221
+ if (a === i && isWithinA && isWithinB) {
222
+ // Remove from any previous exclusive lists under other anchors
223
+ for (const [ai, arr] of exclusiveForA.entries()) {
224
+ exclusiveForA.set(ai, arr.filter((x) => x.j !== b));
225
+ }
226
+ // Add as exclusive to this anchor
227
+ const arr = exclusiveForA.get(i) || [];
228
+ // Avoid duplicate insert
229
+ if (!arr.some((x) => x.j === b)) {
230
+ arr.push({ j: b, score });
231
+ arr.sort((u, v) => v.score - u.score);
232
+ exclusiveForA.set(i, arr);
233
+ }
234
+ // Remove from ambiguous set
235
+ ambSet.delete(b);
236
+ }
237
+ }
238
+ }
239
+ ambiguousB.splice(0, ambiguousB.length, ...Array.from(ambSet.values()));
240
+ // Build symmetric anchor heads from mutual-best pairs (above threshold)
241
+ const anchorHeads = new Map(); // A_i -> head B_j
242
+ for (let i = 0; i < aCount; i++) {
243
+ const b = bestBForA[i].j;
244
+ if (b >= 0) {
245
+ const score = S[i][b];
246
+ const isWithinA = score >= bestBForA[i].score - mutualTolerance;
247
+ const isWithinB = score >= bestAForB[b].score - mutualTolerance;
248
+ if (bestAForB[b].i === i &&
249
+ score >= threshold &&
250
+ isWithinA &&
251
+ isWithinB) {
252
+ anchorHeads.set(i, b);
253
+ }
254
+ }
255
+ }
256
+ // Resolve ambiguous many-to-many by B-centric greedy assignment:
257
+ // each ambiguous B chooses its single most similar A (enables one-to-many on A side).
258
+ const matchedAmbigForA = new Map();
259
+ for (const j of ambiguousB) {
260
+ let bestI = -1;
261
+ let bestScore = -Infinity;
262
+ for (let i = 0; i < aCount; i++) {
263
+ const s = S[i][j];
264
+ if (s > bestScore) {
265
+ bestScore = s;
266
+ bestI = i;
267
+ }
268
+ }
269
+ if (bestI >= 0 && bestScore >= threshold) {
270
+ const arr = matchedAmbigForA.get(bestI) || [];
271
+ arr.push({ j, score: bestScore });
272
+ matchedAmbigForA.set(bestI, arr);
273
+ }
274
+ }
275
+ // Merge exclusive and matched ambiguous into final assignments per A
276
+ const assignedToA = new Map();
277
+ for (let i = 0; i < aCount; i++) {
278
+ const list = [];
279
+ const ex = exclusiveForA.get(i) || [];
280
+ for (const x of ex)
281
+ list.push(x);
282
+ const amb = matchedAmbigForA.get(i) || [];
283
+ for (const x of amb)
284
+ list.push(x);
285
+ if (list.length) {
286
+ list.sort((a, b) => b.score - a.score);
287
+ assignedToA.set(i, list);
288
+ }
289
+ }
290
+ // Promote anchors for left items that have strong assigned matches even if
291
+ // they were not mutual-best. This guarantees group creation around
292
+ // semantically coherent anchors such as Fish, so items like Clams attach there.
293
+ for (const [i, lst] of assignedToA.entries()) {
294
+ if (!anchorHeads.has(i) && lst.length) {
295
+ anchorHeads.set(i, lst[0].j); // head = top assigned right item
296
+ }
297
+ }
298
+ // Track which B have been assigned (exclusive or ambiguous)
299
+ const assignedBSet = new Set();
300
+ for (const arr of assignedToA.values()) {
301
+ for (const x of arr)
302
+ assignedBSet.add(x.j);
303
+ }
304
+ // Also mark mutual-best anchor head B's as assigned so they are not appended as leftovers
305
+ // even if they weren't already in assignedToA for their group.
306
+ for (const [ai, headJ] of anchorHeads.entries()) {
307
+ assignedBSet.add(headJ);
308
+ }
309
+ // Any B not assigned (and not a head) goes to the bottom unassigned list
310
+ const leftoverB = [];
311
+ for (let j = 0; j < bCount; j++) {
312
+ if (!assignedBSet.has(j))
313
+ leftoverB.push(j);
314
+ }
315
+ // Determine ranking for each A_i.
316
+ // Priority 1: anchors with at least one assigned B (highly related) go to the TOP.
317
+ // Priority 2: among them, sort by descending top assigned similarity.
318
+ // Anchors with NO assignment are pushed to the BOTTOM and sorted by their best potential similarity.
319
+ const rankA = [];
320
+ for (let i = 0; i < aCount; i++) {
321
+ const assigned = assignedToA.get(i) || [];
322
+ let top = 0;
323
+ if (assigned.length > 0) {
324
+ top = assigned.reduce((m, x) => x.score > m ? x.score : m, 0);
325
+ }
326
+ else {
327
+ // If nothing assigned, consider the best potential similarity (even if below threshold)
328
+ let best = 0;
329
+ for (let j = 0; j < bCount; j++)
330
+ best = Math.max(best, S[i][j]);
331
+ top = best;
332
+ }
333
+ rankA.push({
334
+ i,
335
+ score: top,
336
+ hasAssignment: assigned.length > 0,
337
+ });
338
+ }
339
+ rankA.sort((x, y) => Number(y.hasAssignment) - Number(x.hasAssignment) ||
340
+ y.score - x.score);
341
+ // Within each group, sort assigned B by descending similarity to the anchor
342
+ for (const [i, list] of assignedToA.entries()) {
343
+ list.sort((a, b) => b.score - a.score);
344
+ assignedToA.set(i, list);
345
+ }
346
+ // Determine anchor groups strictly from symmetric mutual-best pairs
347
+ const anchorSet = new Set(anchorHeads.keys());
348
+ // Early-out: if no anchors (no cross-list matches above threshold),
349
+ // fall back to simple left-only rows followed by right-only rows
350
+ if (anchorSet.size === 0) {
351
+ const rowsNoAnchors = [];
352
+ for (let i = 0; i < aCount; i++)
353
+ rowsNoAnchors.push({
354
+ left: listA[i],
355
+ right: '-',
356
+ score: 0,
357
+ });
358
+ for (const j of leftoverB)
359
+ rowsNoAnchors.push({
360
+ left: '-',
361
+ right: listB[j],
362
+ score: 0,
363
+ });
364
+ return rowsNoAnchors;
365
+ }
366
+ // Map each B to its owning anchor A for quick lookup (based on assignedToA)
367
+ const ownerOfB = new Array(bCount).fill(-1);
368
+ for (const [ai, lst] of assignedToA.entries()) {
369
+ for (const x of lst)
370
+ ownerOfB[x.j] = ai;
371
+ }
372
+ const leftUsed = new Set();
373
+ // Mark anchors as used so they won't be re-assigned as extras
374
+ for (const ai of anchorSet.values())
375
+ leftUsed.add(ai);
376
+ // Pre-bucket extras: group -> (right j within that group) -> extras[]
377
+ const extrasByGroup = new Map();
378
+ for (let k = 0; k < aCount; k++) {
379
+ if (leftUsed.has(k))
380
+ continue; // skip anchors
381
+ let bestGroup = -1;
382
+ let bestRightJ = -1;
383
+ let bestScore = -Infinity;
384
+ // Evaluate affinity against each anchor group
385
+ for (const ai of anchorSet.values()) {
386
+ const groupList = assignedToA.get(ai) || [];
387
+ // Ensure the head B is present in the consideration list
388
+ const headJ = anchorHeads.get(ai);
389
+ const listWithHead = groupList.some((x) => x.j === headJ)
390
+ ? groupList
391
+ : [{ j: headJ, score: S[ai][headJ] }, ...groupList];
392
+ // Best cross-list similarity to any right item under this anchor
393
+ let localBestJ = -1;
394
+ let localBestCross = -Infinity;
395
+ for (const x of listWithHead) {
396
+ const s = S[k][x.j];
397
+ if (s > localBestCross) {
398
+ localBestCross = s;
399
+ localBestJ = x.j;
400
+ }
401
+ }
402
+ // Within-list cohesion to the anchor left word
403
+ const withinAA = SAA[k][ai];
404
+ const combined = Math.max(localBestCross, withinAA);
405
+ if (combined > bestScore) {
406
+ bestScore = combined;
407
+ bestGroup = ai;
408
+ // Prefer the concrete right target when cross-list is stronger;
409
+ // otherwise fall back to the group's top right assignment.
410
+ bestRightJ =
411
+ localBestCross >= withinAA ? localBestJ : headJ;
412
+ }
413
+ }
414
+ // Attach as extra if cohesive enough to some group
415
+ if (bestGroup >= 0 &&
416
+ bestRightJ >= 0 &&
417
+ bestScore >= leftCohesion) {
418
+ let byRight = extrasByGroup.get(bestGroup);
419
+ if (!byRight) {
420
+ byRight = new Map();
421
+ extrasByGroup.set(bestGroup, byRight);
422
+ }
423
+ const arr = byRight.get(bestRightJ) || [];
424
+ arr.push({ k, jPref: bestRightJ, score: bestScore });
425
+ byRight.set(bestRightJ, arr);
426
+ leftUsed.add(k);
427
+ }
428
+ }
429
+ // Build grouped rows with left extras rendered directly under the specific right item
430
+ // they most strongly match. This ensures highly related words appear exactly below their
431
+ // most similar counterpart on the other side.
432
+ const rows = [];
433
+ const anchorIndices = Array.from(anchorSet.values());
434
+ const orderIndices = preserveLeftOrder
435
+ ? anchorIndices.sort((x, y) => x - y)
436
+ : anchorIndices.sort((i1, i2) => {
437
+ const j1 = anchorHeads.get(i1);
438
+ const j2 = anchorHeads.get(i2);
439
+ const diff = S[i2][j2] - S[i1][j1];
440
+ if (Math.abs(diff) > 1e-9)
441
+ return diff;
442
+ // Side-invariant tie-breaker using symmetric pair key
443
+ const key1 = [
444
+ listA[i1].toLowerCase(),
445
+ listB[j1].toLowerCase(),
446
+ ]
447
+ .sort()
448
+ .join('|');
449
+ const key2 = [
450
+ listA[i2].toLowerCase(),
451
+ listB[j2].toLowerCase(),
452
+ ]
453
+ .sort()
454
+ .join('|');
455
+ return key1.localeCompare(key2);
456
+ });
457
+ for (const i of orderIndices) {
458
+ const groupOrig = assignedToA.get(i) || [];
459
+ const headJ = anchorHeads.get(i);
460
+ // Build group with head first, then others by score
461
+ const others = groupOrig
462
+ .filter((x) => x.j !== headJ)
463
+ .sort((a, b) => {
464
+ const d = b.score - a.score;
465
+ if (Math.abs(d) > 1e-9)
466
+ return d;
467
+ return listB[a.j]
468
+ .toLowerCase()
469
+ .localeCompare(listB[b.j].toLowerCase());
470
+ });
471
+ const group = [{ j: headJ, score: S[i][headJ] }, ...others];
472
+ // First row uses the anchor head pair
473
+ const first = group[0];
474
+ rows.push({
475
+ left: listA[i],
476
+ right: listB[first.j],
477
+ score: first.score,
478
+ });
479
+ // Render left extras that prefer this first right item
480
+ const byRight = extrasByGroup.get(i);
481
+ const extrasForFirst = (byRight && byRight.get(first.j)) || [];
482
+ if (extrasForFirst.length) {
483
+ extrasForFirst.sort((e1, e2) => {
484
+ const s1 = Math.max(S[e1.k][first.j], SAA[e1.k][i]);
485
+ const s2 = Math.max(S[e2.k][first.j], SAA[e2.k][i]);
486
+ const d = s2 - s1;
487
+ if (Math.abs(d) > 1e-9)
488
+ return d;
489
+ return listA[e1.k]
490
+ .toLowerCase()
491
+ .localeCompare(listA[e2.k].toLowerCase());
492
+ });
493
+ for (const ex of extrasForFirst) {
494
+ rows.push({
495
+ left: listA[ex.k],
496
+ right: '-',
497
+ score: Math.max(S[ex.k][first.j], SAA[ex.k][i]),
498
+ });
499
+ }
500
+ }
501
+ // Render remaining right items with their own left extras directly beneath
502
+ for (let idx = 1; idx < group.length; idx++) {
503
+ const g = group[idx];
504
+ rows.push({ left: '-', right: listB[g.j], score: g.score });
505
+ const extrasForJ = (byRight && byRight.get(g.j)) || [];
506
+ if (extrasForJ.length) {
507
+ extrasForJ.sort((e1, e2) => {
508
+ const s1 = Math.max(S[e1.k][g.j], SAA[e1.k][i]);
509
+ const s2 = Math.max(S[e2.k][g.j], SAA[e2.k][i]);
510
+ const d = s2 - s1;
511
+ if (Math.abs(d) > 1e-9)
512
+ return d;
513
+ return listA[e1.k]
514
+ .toLowerCase()
515
+ .localeCompare(listA[e2.k].toLowerCase());
516
+ });
517
+ for (const ex of extrasForJ) {
518
+ rows.push({
519
+ left: listA[ex.k],
520
+ right: '-',
521
+ score: Math.max(S[ex.k][g.j], SAA[ex.k][i]),
522
+ });
523
+ }
524
+ }
525
+ }
526
+ }
527
+ // Append leftover left items (not anchors and not attached as extras)
528
+ for (let i = 0; i < aCount; i++) {
529
+ if (!anchorSet.has(i) && !leftUsed.has(i)) {
530
+ rows.push({ left: listA[i], right: '-', score: 0 });
531
+ }
532
+ }
533
+ // Append unassigned B at the end so they do not disrupt highly related groups
534
+ for (const j of leftoverB) {
535
+ rows.push({ left: '-', right: listB[j], score: 0 });
536
+ }
537
+ return rows;
538
+ }));
539
+ }))), catchError((err) => throwError(() => new Error('Failed to create grouped alignment: ' + err))));
540
+ }
541
+ // Cosine similarity between two vectors
542
+ cosineSimilarity(a, b) {
543
+ let dot = 0, na = 0, nb = 0;
544
+ for (let i = 0; i < a.length; i++) {
545
+ dot += a[i] * b[i];
546
+ na += a[i] * a[i];
547
+ nb += b[i] * b[i];
548
+ }
549
+ return dot / (Math.sqrt(na) * Math.sqrt(nb) + 1e-12);
550
+ }
551
+ /**
552
+ * Align words in listA with their most semantically similar words in listB
553
+ *
554
+ * Algorithm:
555
+ * 1. Generate embeddings for all words in both lists using Universal Sentence Encoder
556
+ * 2. For each word in listA, compare its embedding with all words in listB
557
+ * 3. Find the word in listB with highest cosine similarity score
558
+ * 4. Return alignment results with similarity scores
559
+ *
560
+ * @param listA - Source list of words to align
561
+ * @param listB - Target list of words to find matches in
562
+ * @returns Observable of alignment results with word, alignedWith, score, and index
563
+ */
564
+ alignLists(listA, listB) {
565
+ return this.loadModel().pipe(
566
+ // Step 1: Generate embeddings for both word lists simultaneously
567
+ switchMap((model) => forkJoin([
568
+ from(model.embed(listA)),
569
+ from(model.embed(listB))
570
+ ]).pipe(
571
+ // Step 2: Convert tensor embeddings to JavaScript arrays
572
+ switchMap(([tA, tB]) => {
573
+ return forkJoin([from(tA.array()), from(tB.array())]).pipe(map((tensorArrays) => {
574
+ const [arrA, arrB] = tensorArrays;
575
+ // Clean up TensorFlow tensors to prevent memory leaks
576
+ tA.dispose();
577
+ tB.dispose();
578
+ // For each word in listA, find its best match in listB
579
+ const results = listA.map((wordA, i) => {
580
+ // Get the embedding vector for current word from listA
581
+ const vecA = Float32Array.from(arrA[i]);
582
+ // Initialize variables to track the best match
583
+ let bestScore = -Infinity;
584
+ let bestMatch = null;
585
+ let bestIndex = -1;
586
+ // Compare current word from listA with all words in listB
587
+ for (let j = 0; j < listB.length; j++) {
588
+ // Get the embedding vector for current word from listB
589
+ const vecB = Float32Array.from(arrB[j]);
590
+ // Calculate semantic similarity using cosine similarity
591
+ const score = this.cosineSimilarity(vecA, vecB);
592
+ // Update best match if this similarity is higher
593
+ if (score > bestScore) {
594
+ bestScore = score;
595
+ bestMatch = listB[j];
596
+ bestIndex = j;
597
+ }
598
+ }
599
+ // Return alignment result for this word
600
+ return {
601
+ word: wordA, // Original word from listA
602
+ alignedWith: bestMatch, // Best matching word from listB
603
+ score: bestScore, // Similarity score (0-1, higher is better)
604
+ index: bestIndex, // Index of best match in listB
605
+ };
606
+ });
607
+ // Return all alignment results
608
+ return results;
609
+ }));
610
+ }))), catchError((err) => throwError(() => new Error('Model not loaded or embedding failed: ' + err))));
611
+ }
612
+ /**
613
+ * Find best matching pairs between two lists above a similarity threshold
614
+ *
615
+ * @param listA - Source list of words
616
+ * @param listB - Target list of words to match against
617
+ * @param threshold - Minimum similarity score (0-1, default: 0.15)
618
+ * @returns Observable of word pairs with similarity scores
619
+ */
620
+ findBestMatchingPairs(listA, listB, threshold = 0.15) {
621
+ return this.alignLists(listA, listB).pipe(map((alignments) => {
622
+ const usedB = new Set();
623
+ const pairs = [];
624
+ // Sort by similarity score (highest first)
625
+ alignments.sort((a, b) => b.score - a.score);
626
+ // Debug logging for travel/journey matching
627
+ console.log('Embedding Service - All alignments:', alignments.map((alignment) => ({
628
+ word: alignment.word,
629
+ alignedWith: alignment.alignedWith,
630
+ score: alignment.score.toFixed(3),
631
+ })));
632
+ for (const alignment of alignments) {
633
+ if (alignment.score >= threshold && !usedB.has(alignment.index)) {
634
+ pairs.push({
635
+ wordA: alignment.word,
636
+ wordB: alignment.alignedWith,
637
+ score: alignment.score,
638
+ });
639
+ usedB.add(alignment.index);
640
+ // Special logging for travel-related matches
641
+ if (alignment.word.toLowerCase().includes('travel') ||
642
+ alignment.word.toLowerCase().includes('journey') ||
643
+ alignment.alignedWith.toLowerCase().includes('travel') ||
644
+ alignment.alignedWith.toLowerCase().includes('journey')) {
645
+ console.log('🎯 Travel/Journey Match Found:', {
646
+ wordA: alignment.word,
647
+ wordB: alignment.alignedWith,
648
+ score: alignment.score.toFixed(3),
649
+ threshold: threshold,
650
+ });
651
+ }
652
+ }
653
+ }
654
+ return pairs;
655
+ }), catchError((err) => throwError(() => new Error('Failed to find best matching pairs: ' + err))));
656
+ }
657
+ // Calculate similarity between two texts (returns Observable)
658
+ calculateSimilarity(primaryText, comparisonText) {
659
+ return this.getEmbedding(primaryText).pipe(switchMap((emb1) => this.getEmbedding(comparisonText).pipe(map((emb2) => {
660
+ const similarity = this.cosineSimilarity(emb1, emb2);
661
+ // Debug logging for travel/journey similarity
662
+ if (primaryText.toLowerCase().includes('travel') ||
663
+ primaryText.toLowerCase().includes('journey') ||
664
+ comparisonText.toLowerCase().includes('travel') ||
665
+ comparisonText.toLowerCase().includes('journey')) {
666
+ console.log('🔍 Travel/Journey Similarity:', {
667
+ primaryText,
668
+ comparisonText,
669
+ similarity: similarity.toFixed(3),
670
+ });
671
+ }
672
+ return similarity;
673
+ }))), catchError((err) => throwError(() => new Error('Failed to calculate similarity: ' + err))));
674
+ }
675
+ // Test method to check specific word similarities (Observable only)
676
+ testWordSimilarity(word1, word2) {
677
+ return this.calculateSimilarity(word1, word2).pipe(map((similarity) => {
678
+ console.log(`📊 Similarity between "${word1}" and "${word2}": ${(similarity * 100).toFixed(1)}%`);
679
+ }), catchError((error) => {
680
+ console.error('Error testing word similarity:', error);
681
+ return of();
682
+ }));
683
+ }
684
+ // Private methods
685
+ // Domain compatibility cap to avoid mismatched category-versus-activity pairings.
686
+ // Example: "Fish" (food/animal noun) vs "Bird watching" (activity) should not align.
687
+ // Returns a cap in [0,1] representing the maximum allowed similarity for the pair.
688
+ // Values below the groupAlignLists() threshold (default 0.25) effectively block alignment.
689
+ domainCompatibilityCap(a, b) {
690
+ const norm = (s) => s
691
+ .toLowerCase()
692
+ .replace(/[_-]+/g, ' ')
693
+ .replace(/[^a-z\s]/g, ' ')
694
+ .replace(/\s+/g, ' ')
695
+ .trim();
696
+ const A = norm(a);
697
+ const B = norm(b);
698
+ const watching = /\b[a-z]+\s*watching\b|\bbirdwatching\b/;
699
+ const genericActivities = /\b(hiking|reading|painting|photography|gardening|blogging|writing|gaming|chess|puzzles|travel(?:ing|ling)?|programming|coding|language learning|pottery|clay\s*model(?:ing|ling))\b/;
700
+ const isActivity = (s) => watching.test(s) || genericActivities.test(s);
701
+ const foodAnimal = /\b(fish|sea\s*food|seafood|shell\s*fish|shellfish|clam|clams|mussel|mussels|oyster|oysters|scallop|scallops|shrimp|prawn|prawns|lobster|crab|meat|beef|pork|chicken|egg|eggs)\b/;
702
+ const isFoodAnimal = (s) => foodAnimal.test(s);
703
+ const aAct = isActivity(A);
704
+ const bAct = isActivity(B);
705
+ const aFood = isFoodAnimal(A);
706
+ const bFood = isFoodAnimal(B);
707
+ if ((aAct && bFood) || (bAct && aFood)) {
708
+ // Cap below default threshold so these pairs never form groups
709
+ return 0.2;
710
+ }
711
+ return 1.0;
712
+ }
713
+ // Lightweight lexical similarity booster to catch cases like "AI/ML" ↔ "AI machine learning"
714
+ // Uses token normalization + targeted synonym/expansion rules and token Jaccard.
715
+ computeLexicalBoost(a, b) {
716
+ const expand = (s) => {
717
+ let x = s.toLowerCase();
718
+ // Normalize separators
719
+ x = x.replace(/[_-]+/g, ' ');
720
+ // Targeted expansions
721
+ x = x.replace(/ai\s*\/\s*ml|ai\s*[- ]?ml/g, 'ai machine learning');
722
+ x = x.replace(/\bml\b/g, 'machine learning');
723
+ x = x.replace(/web\s*dev(elopment)?/g, 'web development');
724
+ // Domain expansions – culinary/food categories (to help cases like Fish ↔ Clams)
725
+ x = x.replace(/\bsea\s*food\b/g, 'seafood fish shellfish');
726
+ x = x.replace(/\bshell\s*fish\b/g, 'shellfish clams mussels oysters scallops seafood');
727
+ x = x.replace(/\bfish(es)?\b/g, 'fish seafood');
728
+ x = x.replace(/\bclam(s)?\b/g, 'clams shellfish seafood');
729
+ x = x.replace(/\bmussel(s)?\b/g, 'mussels shellfish seafood');
730
+ x = x.replace(/\boyster(s)?\b/g, 'oysters shellfish seafood');
731
+ x = x.replace(/\bscallop(s)?\b/g, 'scallops shellfish seafood');
732
+ x = x.replace(/\bshrimp(s)?\b/g, 'shrimp seafood');
733
+ x = x.replace(/\bprawn(s)?\b/g, 'prawns shrimp seafood');
734
+ x = x.replace(/\blobster(s)?\b/g, 'lobster seafood');
735
+ x = x.replace(/\bcrab(s)?\b/g, 'crab seafood');
736
+ x = x.replace(/ui\s*\/\s*ux|ui\s*[- ]?ux/g, 'ui ux design');
737
+ return x;
738
+ };
739
+ const normalize = (s) => {
740
+ const x = expand(s)
741
+ .replace(/[^a-z0-9\s]/g, ' ')
742
+ .replace(/\s+/g, ' ')
743
+ .trim();
744
+ return x;
745
+ };
746
+ const A = normalize(a);
747
+ const B = normalize(b);
748
+ if (!A || !B)
749
+ return 0;
750
+ // Strong containment boost (e.g., "ai machine learning" contains "machine learning")
751
+ if (A.includes(B) || B.includes(A)) {
752
+ return Math.max(0.75, Math.min(0.95, Math.max(A.length, B.length) > 12 ? 0.85 : 0.78));
753
+ }
754
+ const toTokens = (s) => s.split(' ').filter(Boolean);
755
+ const setFrom = (arr) => new Set(arr);
756
+ const tA = toTokens(A);
757
+ const tB = toTokens(B);
758
+ if (!tA.length || !tB.length)
759
+ return 0;
760
+ const sA = setFrom(tA);
761
+ const sB = setFrom(tB);
762
+ let inter = 0;
763
+ for (const t of sA)
764
+ if (sB.has(t))
765
+ inter++;
766
+ const union = sA.size + sB.size - inter;
767
+ const jaccard = union > 0 ? inter / union : 0;
768
+ // Small extra for sharing big tokens like "learning"/"development" with qualifiers
769
+ const longTokenBonus = Math.min(0.1, [...sA].filter((t) => t.length >= 6 && sB.has(t)).length * 0.03);
770
+ const score = Math.min(0.95, jaccard + longTokenBonus);
771
+ return score;
772
+ }
773
+ // Lazy load the model only once, as observable
774
+ loadModel() {
775
+ if (!this.model$) {
776
+ this.model$ = defer(() => from(use.load())).pipe(shareReplay(1));
777
+ }
778
+ return this.model$;
779
+ }
780
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: EmbeddingService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
781
+ static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: EmbeddingService, providedIn: 'root' });
782
+ }
783
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: EmbeddingService, decorators: [{
784
+ type: Injectable,
785
+ args: [{
786
+ providedIn: 'root',
787
+ }]
788
+ }] });
789
+
790
+ class FileConversionService {
791
+ static MIME_TYPE_JPEG = 'image/jpeg';
792
+ static MIME_TYPE_PNG = 'image/png';
793
+ static MIME_TYPE_WEBP = 'image/webp';
794
+ static FILE_EXTENSION_JPG = '.jpg';
795
+ static FILE_EXTENSION_JPEG = '.jpeg';
796
+ static FILE_EXTENSION_PNG = '.png';
797
+ static FILE_EXTENSION_WEBP = '.webp';
798
+ urlToFile(url, fileName, mimeType) {
799
+ return from(fetch(url)).pipe(switchMap$1(response => from(response.blob())), map$1(blob => new File([blob], fileName, { type: mimeType })));
800
+ }
801
+ isDataUrl(str) {
802
+ return typeof str === 'string' && str.startsWith('data:');
803
+ }
804
+ dataUrlToFile(dataUrl, fileName) {
805
+ const [header, data] = dataUrl.split(',');
806
+ const mimeMatch = header.match(/data:(.*?);/);
807
+ const mime = mimeMatch ? mimeMatch[1] : FileConversionService.MIME_TYPE_PNG;
808
+ const binary = atob(data || '');
809
+ const len = binary.length;
810
+ const u8 = new Uint8Array(len);
811
+ for (let i = 0; i < len; i++)
812
+ u8[i] = binary.charCodeAt(i);
813
+ return new File([u8], fileName, { type: mime });
814
+ }
815
+ getMimeFromFilename(fileName) {
816
+ const lower = (fileName || '').toLowerCase();
817
+ if (lower.endsWith(FileConversionService.FILE_EXTENSION_JPG) || lower.endsWith(FileConversionService.FILE_EXTENSION_JPEG))
818
+ return FileConversionService.MIME_TYPE_JPEG;
819
+ if (lower.endsWith(FileConversionService.FILE_EXTENSION_PNG))
820
+ return FileConversionService.MIME_TYPE_PNG;
821
+ if (lower.endsWith(FileConversionService.FILE_EXTENSION_WEBP))
822
+ return FileConversionService.MIME_TYPE_WEBP;
823
+ return FileConversionService.MIME_TYPE_PNG;
824
+ }
825
+ getFileForImageString(imageStr, fileBaseName) {
826
+ if (this.isDataUrl(imageStr))
827
+ return of(this.dataUrlToFile(imageStr, fileBaseName));
828
+ const mime = this.getMimeFromFilename(imageStr);
829
+ return this.urlToFile(imageStr, fileBaseName, mime);
830
+ }
831
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: FileConversionService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
832
+ static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: FileConversionService, providedIn: 'root' });
833
+ }
834
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: FileConversionService, decorators: [{
835
+ type: Injectable,
836
+ args: [{
837
+ providedIn: 'root'
838
+ }]
839
+ }] });
840
+
841
+ class ImageCompressionService {
842
+ compressImageFile(file, config, overrides = {}) {
843
+ const cfg = { ...config, ...overrides };
844
+ return this.loadImageFromFile(file).pipe(switchMap$1((img) => {
845
+ let width = img.naturalWidth || img.width;
846
+ let height = img.naturalHeight || img.height;
847
+ const scale = Math.min(cfg.maxWidth / width, cfg.maxHeight / height, 1);
848
+ width = Math.max(1, Math.round(width * scale));
849
+ height = Math.max(1, Math.round(height * scale));
850
+ const canvas = document.createElement('canvas');
851
+ const ctx = canvas.getContext('2d');
852
+ const render = (w, h) => {
853
+ canvas.width = w;
854
+ canvas.height = h;
855
+ ctx.clearRect(0, 0, w, h);
856
+ ctx.drawImage(img, 0, 0, w, h);
857
+ };
858
+ const process = (w, h, q, attempts) => {
859
+ render(w, h);
860
+ return this.canvasToBlob(canvas, cfg.format, q).pipe(switchMap$1((blob) => {
861
+ if (blob.size > cfg.maxBytes && attempts < 25) {
862
+ if (q > cfg.qualityMin + 0.005) {
863
+ return process(w, h, Math.max(cfg.qualityMin, q - cfg.qualityStep), attempts + 1);
864
+ }
865
+ else {
866
+ const nextW = Math.max(Math.round(w * cfg.downscaleStep), cfg.minWidth);
867
+ const nextH = Math.max(Math.round(h * cfg.downscaleStep), cfg.minHeight);
868
+ if (nextW === w && nextH === h)
869
+ return of(blob);
870
+ return process(nextW, nextH, cfg.qualityStart, attempts + 1);
871
+ }
872
+ }
873
+ return of(blob);
874
+ }));
875
+ };
876
+ return process(width, height, cfg.qualityStart, 0).pipe(switchMap$1((finalBlob) => {
877
+ const newName = this.renameFileForFormat(file.name, cfg.format);
878
+ const compressedFile = new File([finalBlob], newName, { type: cfg.format, lastModified: Date.now() });
879
+ return this.blobToDataURL(finalBlob).pipe(map$1((dataUrl) => ({ file: compressedFile, dataUrl })));
880
+ }));
881
+ }));
882
+ }
883
+ loadImageFromFile(file) {
884
+ return new Observable(observer => {
885
+ const url = URL.createObjectURL(file);
886
+ const img = new Image();
887
+ img.onload = () => {
888
+ URL.revokeObjectURL(url);
889
+ observer.next(img);
890
+ observer.complete();
891
+ };
892
+ img.onerror = () => {
893
+ URL.revokeObjectURL(url);
894
+ observer.error(new Error('Failed to load image'));
895
+ };
896
+ img.src = url;
897
+ });
898
+ }
899
+ canvasToBlob(canvas, type, quality) {
900
+ return new Observable(observer => {
901
+ if (canvas.toBlob) {
902
+ canvas.toBlob((blob) => {
903
+ if (blob) {
904
+ observer.next(blob);
905
+ observer.complete();
906
+ }
907
+ else {
908
+ observer.error(new Error('toBlob null'));
909
+ }
910
+ }, type, quality);
911
+ }
912
+ else {
913
+ try {
914
+ const dataUrl = canvas.toDataURL(type, quality);
915
+ const base64 = dataUrl.split(',')[1] || '';
916
+ const binary = atob(base64);
917
+ const u8 = new Uint8Array(binary.length);
918
+ for (let i = 0; i < binary.length; i++)
919
+ u8[i] = binary.charCodeAt(i);
920
+ observer.next(new Blob([u8], { type }));
921
+ observer.complete();
922
+ }
923
+ catch (e) {
924
+ observer.error(e);
925
+ }
926
+ }
927
+ });
928
+ }
929
+ blobToDataURL(blob) {
930
+ return new Observable(observer => {
931
+ const reader = new FileReader();
932
+ reader.onload = () => {
933
+ observer.next(String(reader.result));
934
+ observer.complete();
935
+ };
936
+ reader.onerror = (err) => observer.error(err);
937
+ reader.readAsDataURL(blob);
938
+ });
939
+ }
940
+ renameFileForFormat(name, mime) {
941
+ const base = name.substring(0, name.lastIndexOf('.')) || name;
942
+ if (mime === 'image/jpeg')
943
+ return `${base}.jpg`;
944
+ if (mime === 'image/png')
945
+ return `${base}.png`;
946
+ if (mime === 'image/webp')
947
+ return `${base}.webp`;
948
+ return `${base}.jpg`;
949
+ }
950
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ImageCompressionService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
951
+ static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ImageCompressionService, providedIn: 'root' });
952
+ }
953
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ImageCompressionService, decorators: [{
954
+ type: Injectable,
955
+ args: [{
956
+ providedIn: 'root'
957
+ }]
958
+ }] });
959
+
960
+ /**
961
+ * OpenAI Alignment Service
962
+ * Uses OpenAI Chat Completions API to align two lists of text in a single request.
963
+ * This replaces the previous embedding-based approach to avoid rate limits and reduce complexity.
964
+ */
965
+ class OpenAIEmbeddingService {
966
+ http;
967
+ static API_URL = 'https://api.openai.com/v1/chat/completions';
968
+ static MODEL = 'gpt-4o';
969
+ static HEADER_CONTENT_TYPE = 'Content-Type';
970
+ static HEADER_AUTHORIZATION = 'Authorization';
971
+ static CONTENT_TYPE_JSON = 'application/json';
972
+ static BEARER_PREFIX = 'Bearer ';
973
+ static SPACER_SMALL = '-';
974
+ static SPACER_LARGE = '----';
975
+ API_URL = OpenAIEmbeddingService.API_URL;
976
+ // Using gpt-4o for maximum intelligence and adherence to complex instructions
977
+ MODEL = OpenAIEmbeddingService.MODEL;
978
+ // Cache for alignment results
979
+ // Key: Canonical hash of two lists
980
+ // Value: { originalA: string[], originalB: string[], rows: IAlignmentRow[] }
981
+ alignmentCache = new Map();
982
+ // Cache for new spacer-based alignment
983
+ spacerAlignmentCache = new Map();
984
+ constructor(http) {
985
+ this.http = http;
986
+ }
987
+ /**
988
+ * Get aligned lists with spacers.
989
+ * Now uses a direct LLM call with a specific prompt to generate the aligned lists.
990
+ */
991
+ getAlignedLists(listA, listB, apiKey) {
992
+ const validA = listA.filter(i => i && i.trim() && i.trim() !== OpenAIEmbeddingService.SPACER_SMALL);
993
+ const validB = listB.filter(i => i && i.trim() && i.trim() !== OpenAIEmbeddingService.SPACER_SMALL);
994
+ if (validA.length === 0 && validB.length === 0)
995
+ return of({ listA: [], listB: [] });
996
+ // Cache key
997
+ const key = JSON.stringify({ a: validA, b: validB, v: 'spacer-v35' });
998
+ if (this.spacerAlignmentCache.has(key)) {
999
+ console.log('Alignment cache hit');
1000
+ return of(this.spacerAlignmentCache.get(key));
1001
+ }
1002
+ const headers = new HttpHeaders({
1003
+ [OpenAIEmbeddingService.HEADER_CONTENT_TYPE]: OpenAIEmbeddingService.CONTENT_TYPE_JSON,
1004
+ [OpenAIEmbeddingService.HEADER_AUTHORIZATION]: `${OpenAIEmbeddingService.BEARER_PREFIX}${apiKey}`
1005
+ });
1006
+ const prompt = `
1007
+ You are an expert semantic alignment engine.
1008
+ Task: Align the items from List A to List B based on semantic similarity.
1009
+ Context: This is a heuristic, human-semantic alignment (use embeddings later if you want it algorithmically strict).
1010
+
1011
+ List A: ${JSON.stringify(validA)}
1012
+ List B: ${JSON.stringify(validB)}
1013
+
1014
+ SEMANTIC STRATEGY (3-PASS PROCESS):
1015
+ 1. **Pass 1: Exact & Strong Matches**: First, lock in all exact synonyms (e.g., "Dog" vs "Puppy") and strong category matches (e.g., "Tennis" vs "Badminton"). These are your anchors.
1016
+ 2. **Pass 2: Thematic Clustering**: Next, group remaining items by sub-theme (e.g., "Water Sports", "Asian Cuisine"). Align items within these themes even if they aren't perfect pairs.
1017
+ 3. **Pass 3: Contextual & Loose Matches**: Look for looser associations (e.g., "Reading" vs "Library").
1018
+ 4. **Isolation**: If an item truly has NO relation to anything remaining in the other list, ISOLATE it using "----". Do NOT force a bad match.
1019
+
1020
+ Rules:
1021
+ 1. Align items into clusters with variable dash-gaps to show semantic distance.
1022
+ 2. Use "-" for a small hop (related but distinct).
1023
+ 3. Use "----" for a hard theme switch (different category).
1024
+ 4. You MUST reorder items to create the best semantic clusters.
1025
+ 5. Ensure the output arrays "listA" and "listB" have the SAME length.
1026
+ 6. If an item has no match, align it with "-" or "----" as appropriate, or just a gap.
1027
+ 7. Output MUST be a JSON object with "listA" and "listB".
1028
+ 8. **COMPLETENESS**: You MUST include EVERY item from the input List A and List B in the output. Do not omit any items.
1029
+ 9. **Handling Leftovers**: If you have items left over at the end that don't match well, still include them in the list, aligned with "-" if necessary, or grouped together if they are somewhat related.
1030
+ 10. **Spacer Alignment**: You MUST align "----" with "----" in both lists. Do NOT align a hard switch "----" with a word. If you need a switch on one side, put it on the other side too.
1031
+ 11. **Strict Side Adherence**: Items from Input List A MUST ONLY appear in Output List A. Items from Input List B MUST ONLY appear in Output List B. Do not mix them up.
1032
+ 12. **Mandatory Spacers**: You MUST insert a spacer ("-" or "----") between clusters of related items. Avoid putting "----" between every single item; use it only when the topic truly shifts.
1033
+ 13. **NO TRAILING SPACER**: The last item in both lists MUST be a word. Do NOT put a spacer at the very end.
1034
+ 14. **Length Mismatch Handling**: If one list is shorter than the other, you MUST fill the empty slots in the shorter list with spacers ("-") ONLY. DO NOT invent words or reuse words to fill the space.
1035
+ 15. **HIGH SIMILARITY CONSOLIDATION (>= 0.8)**: This is CRITICAL for center display:
1036
+ - When you find a pair with VERY HIGH semantic similarity (>= 0.8), you MUST output the SAME TEXT in both listA and listB at that position.
1037
+ - Format the combined text as: "word1 × word2" OR "word1 / word2" OR just use one of the words.
1038
+ - **Examples of HIGH similarity (>= 0.8) - MUST consolidate**:
1039
+ * "AI/ML" and "AI machine learning" -> Output "AI/ML × AI machine learning" in BOTH lists
1040
+ * "Programming" and "software" -> Output "Programming × software" in BOTH lists
1041
+ * "Web Development" and "Development" -> Output "Web Development × Development" in BOTH lists
1042
+ * "Gaming" and "Mobile Games" -> Output "Gaming × Mobile Games" in BOTH lists
1043
+ * "graphics design" and "Design" -> Output "graphics design × Design" in BOTH lists
1044
+ * "Startups" and "Entrepreneurship" -> Output "Startups × Entrepreneurship" in BOTH lists
1045
+ * "Jogging" and "Running" -> Output "Jogging × Running" in BOTH lists (GOLD STANDARD)
1046
+ * "Hiking" and "Trail hikes" -> Output "Hiking × Trail hikes" in BOTH lists
1047
+ * "Coffee" and "Cafes" -> Output "Coffee × Cafes" in BOTH lists
1048
+ * "Gym / lifting" and "Working out" -> Output "Gym / lifting × Working out" in BOTH lists
1049
+ * "Board games" and "Gaming" -> Output "Board games × Gaming" in BOTH lists
1050
+ * "Live music" and "Live music" -> Already exact match, keep as is
1051
+ * "Innovation" and "Innovation" -> Already exact match, keep as is
1052
+ - **Examples of LOWER similarity (< 0.8) - Do NOT consolidate**:
1053
+ * "Data Science" and "Blockchain" -> Keep separate (different fields)
1054
+ * "graphics card" and "Hardware" -> Keep separate (component vs category)
1055
+ * "figma" and "Design Thinking" -> Keep separate (tool vs methodology)
1056
+ * "Cybersecurity" and "Privacy" -> Keep separate (related but distinct)
1057
+ * "Cooking" and "Photography" -> Keep separate (unrelated)
1058
+ * "Traveling" and "Reading" -> Keep separate (different activities)
1059
+ - **Key principle**: If the items are synonyms, very similar activities, or one is a specific type of the other, consolidate them!
1060
+ - **STRICT CONSTRAINT**: You can ONLY consolidate if BOTH items actually exist in the input lists and are semantically related.
1061
+ - **NEVER** invent an item for List B just to make a pair with List A. If List A has "figma" and List B has NOTHING related, output "figma" in List A and "-" in List B. DO NOT put "figma" in List B.
1062
+ - **STRICT SYMMETRY**: If you use the "×" symbol to consolidate, you MUST output the EXACT SAME string in BOTH "listA" and "listB" at that index.
1063
+ - This allows the system to automatically move these items to the center display!
1064
+ - **NEVER** use "×" on one side and a spacer on the other. That is a critical failure.
1065
+ - **NEVER** include similarity scores or any other numbers in the output strings unless they are part of the original item text.
1066
+ - **ONLY** use "×" as the separator for consolidated items. Do not use "/", "&", or other symbols for this purpose.
1067
+ - Example: "Jogging" (List A) + "Running" (List B) -> "Jogging × Running" in BOTH listA and listB.
1068
+ - **NEVER DROP CONSOLIDATED ITEMS**: If you consolidate "Jogging" and "Running" into "Jogging × Running", you MUST put this string in **BOTH** List A and List B. Do NOT put it in List A and a spacer in List B. That would delete "Running" from the output.
1069
+ - **CRITICAL ERROR PREVENTION**: Do NOT drop items from List B. If "Running" is in Input List B, it MUST appear in Output List B (either as "Running" or "Jogging × Running"). If you output "Jogging × Running" in List A but "-" in List B, you have FAILED.
1070
+ - **COMPLETENESS CHECK**: You MUST verify that EVERY item from the input lists is present in the output. If "Running" is in Input List B, it MUST be in Output List B (or consolidated). DO NOT DROP ITEMS.
1071
+ - **DETERMINISM**: Process the list from top to bottom. Do not change your output between runs.
1072
+ - This allows the system to automatically move these items to the center display!
1073
+
1074
+ NEGATIVE CONSTRAINTS:
1075
+ - NEVER put an item from List A into List B.
1076
+ - NEVER put an item from List B into List A.
1077
+ - NEVER invent new items. Only use items from the input lists.
1078
+ - **NEVER DUPLICATE ITEMS**. Each item from the input list must appear EXACTLY ONCE in the output.
1079
+
1080
+ VERIFICATION STEP:
1081
+ Before outputting, verify every single item:
1082
+ - "Is this item in Input List A?" -> If yes, put it in Output List A.
1083
+ - "Is this item in Input List B?" -> If yes, put it in Output List B.
1084
+ - "Is this item in neither?" -> It is a hallucination. DELETE IT.
1085
+
1086
+ CRITICAL: OUTPUT MUST CONTAIN EXACTLY THE SAME ITEMS AS INPUT. NO DELETIONS ALLOWED.
1087
+
1088
+ FINAL SANITY CHECK:
1089
+ - **HALLUCINATION CHECK**: Go through every word in your Output List A. Is it EXACTLY present in Input List A? If not, DELETE IT. Do the same for List B.
1090
+ - **DUPLICATION CHECK**: Scan List A and List B. Does ANY word appear more than once? If so, DELETE the extra occurrences immediately. Each item must appear EXACTLY ONCE.
1091
+ - **COUNT CHECK**: Count the number of non-spacer items in your Output List A. Does it match the number of items in Input List A? If not, you are missing items. FIND THEM AND ADD THEM.
1092
+ - **COUNT CHECK**: Do the same for List B.
1093
+ - Look at your generated "listA". Are there any items that belong to Input List B? REMOVE THEM immediately.
1094
+ - Look at your generated "listB". Are there any items that belong to Input List A? REMOVE THEM immediately.
1095
+ - **Did you miss any items from Input List A? ADD THEM NOW.**
1096
+ - **Did you miss any items from Input List B? ADD THEM NOW.**
1097
+ - If you are unsure where an item belongs, check the Input Lists provided above. Do NOT guess.
1098
+
1099
+ Example of Desired Output Logic:
1100
+ - "Volunteering at animal shelter" vs "Dog training" -> Related? Yes. Similarity >= 0.8? No (different activities)
1101
+ - NEXT PAIR: "Trail biking" vs "Rock climbing" -> Is this related to Animals? No. -> INSERT "----"
1102
+ - "Trail biking" vs "Rock climbing" -> Related? Yes (Outdoors).
1103
+ - NEXT PAIR: "Jogging" vs "Running" -> Similarity >= 0.8? YES! -> Output "Jogging × Running" in BOTH lists
1104
+ - "Jogging" vs "Running" -> Related? Yes (Exercise).
1105
+ - NEXT PAIR: "Beach volleyball" vs "Salsa dancing" -> Is this related to Exercise? Yes. -> INSERT "-" (Small hop)
1106
+ - "Beach volleyball" vs "Salsa dancing" -> Related? Yes (Hobbies). Similarity >= 0.8? No (different hobbies)
1107
+ - NEXT PAIR: "Fermenting kimchi" vs "Korean cooking" -> Is this related to Hobbies? No. -> INSERT "----"
1108
+
1109
+ JSON Output Structure:
1110
+ {
1111
+ "listA": ["Consolidated Item × Match", "----", "Unique Item A", "-" , "Unique Item A1" , "-" , "Unique Item A2" ,"----" , "Unique Item A3", "-" , "Unique Item A4 × Unique Item B4" , "----" , Unique Item A5, "----" , Uniue Item A6 ],
1112
+ "listB": ["Consolidated Item × Match", "----", "Unique Item B", "-" , "Unique Item B1" , "-" , "Unique Item B2" ,"----" , "Unique Item B3", "-" , "Unique Item A4 × Unique Item B4" , "----" , Unique Item B5, "----" , - ]
1113
+ }
1114
+ Note: In this case, List A has more items, so the last remaining item is displayed as a single card.In other cases, List B may have more words, and in that situation, the extra word from List B is shown as a single card.
1115
+ Each unique item in List A has a corresponding unique item in List B that is highly similar in meaning or related in terms of concept.
1116
+ VARIATION & SIMILARITY CONTROL: Maintain strict consistency. If the same semantic relationship appears multiple times, align it identically every time.
1117
+ `;
1118
+ const body = {
1119
+ model: this.MODEL,
1120
+ messages: [
1121
+ { role: 'system', content: 'You are a helpful assistant that outputs strictly JSON.' },
1122
+ { role: 'user', content: prompt }
1123
+ ],
1124
+ response_format: { type: 'json_object' },
1125
+ temperature: 0.0, // Zero temperature for maximum determinism
1126
+ seed: 42 // Fixed seed for reproducibility
1127
+ };
1128
+ return this.http.post(this.API_URL, body, { headers }).pipe(map(response => {
1129
+ try {
1130
+ const content = response.choices[0].message.content;
1131
+ const result = JSON.parse(content);
1132
+ // Handle both "listA/listB" format and "alignment" array format
1133
+ if (result.alignment && Array.isArray(result.alignment)) {
1134
+ result.listA = result.alignment.map((row) => row.left || row.itemA || '');
1135
+ result.listB = result.alignment.map((row) => row.right || row.itemB || '');
1136
+ }
1137
+ if (!result.listA || !result.listB || !Array.isArray(result.listA) || !Array.isArray(result.listB)) {
1138
+ console.error('Invalid JSON structure from OpenAI:', result);
1139
+ throw new Error('Invalid JSON structure from OpenAI');
1140
+ }
1141
+ // Just ensure equal length for safety in UI rendering
1142
+ const maxLen = Math.max(result.listA.length, result.listB.length);
1143
+ while (result.listA.length < maxLen)
1144
+ result.listA.push(OpenAIEmbeddingService.SPACER_SMALL);
1145
+ while (result.listB.length < maxLen)
1146
+ result.listB.push(OpenAIEmbeddingService.SPACER_SMALL);
1147
+ // Post-process to ensure consolidated items are in both lists
1148
+ // If one side has "A × B" and the other is a spacer, sync them to ensure center display
1149
+ for (let i = 0; i < result.listA.length; i++) {
1150
+ const a = result.listA[i];
1151
+ const b = result.listB[i];
1152
+ const isConsolidatedA = a && a.includes(' × ');
1153
+ const isConsolidatedB = b && b.includes(' × ');
1154
+ const isSpacerA = !a || a === OpenAIEmbeddingService.SPACER_SMALL || a === OpenAIEmbeddingService.SPACER_LARGE;
1155
+ const isSpacerB = !b || b === OpenAIEmbeddingService.SPACER_SMALL || b === OpenAIEmbeddingService.SPACER_LARGE;
1156
+ if (isConsolidatedA && isSpacerB) {
1157
+ result.listB[i] = a;
1158
+ }
1159
+ else if (isConsolidatedB && isSpacerA) {
1160
+ result.listA[i] = b;
1161
+ }
1162
+ }
1163
+ // SANITIZATION: Strictly enforce that items belong to their respective lists
1164
+ const sanitized = this.sanitizeResponse(result.listA, result.listB, validA, validB);
1165
+ const finalResult = { listA: sanitized.listA, listB: sanitized.listB };
1166
+ this.spacerAlignmentCache.set(key, finalResult);
1167
+ return finalResult;
1168
+ }
1169
+ catch (e) {
1170
+ console.error('Failed to parse OpenAI response', e);
1171
+ throw new Error('Failed to parse alignment result');
1172
+ }
1173
+ }), catchError((error) => {
1174
+ console.error('OpenAI API Error:', error);
1175
+ return of({ listA: [], listB: [] });
1176
+ }));
1177
+ }
1178
+ sanitizeResponse(outA, outB, inA, inB) {
1179
+ const cleanA = [];
1180
+ const cleanB = [];
1181
+ // Track used items to identify missing ones later
1182
+ const usedA = new Set();
1183
+ const usedB = new Set();
1184
+ // Helper to check if a string contains an item (exact or consolidated)
1185
+ const containsItem = (str, item) => {
1186
+ const s = str.toLowerCase();
1187
+ const i = item.toLowerCase();
1188
+ return s === i || s.includes(`× ${i}`) || s.includes(`${i} ×`) || s.includes(`/ ${i}`) || s.includes(`${i} /`);
1189
+ };
1190
+ for (let i = 0; i < outA.length; i++) {
1191
+ let itemA = outA[i];
1192
+ let itemB = outB[i];
1193
+ const isSpacerA = !itemA || itemA === OpenAIEmbeddingService.SPACER_SMALL || itemA === OpenAIEmbeddingService.SPACER_LARGE;
1194
+ const isSpacerB = !itemB || itemB === OpenAIEmbeddingService.SPACER_SMALL || itemB === OpenAIEmbeddingService.SPACER_LARGE;
1195
+ // Validate Item A
1196
+ let validItemA = itemA;
1197
+ if (!isSpacerA) {
1198
+ // Check if it belongs to Input A
1199
+ const originalA = inA.find(a => containsItem(itemA, a));
1200
+ if (originalA) {
1201
+ usedA.add(originalA);
1202
+ }
1203
+ else {
1204
+ // It's not in Input A. Is it in Input B?
1205
+ if (inB.some(b => containsItem(itemA, b))) {
1206
+ // Hallucination! It belongs to B but is in A.
1207
+ validItemA = OpenAIEmbeddingService.SPACER_SMALL; // Remove it
1208
+ }
1209
+ else {
1210
+ // Unknown item (maybe consolidated hallucination). Keep if it looks like a consolidated string, else remove
1211
+ if (!itemA.includes('×') && !itemA.includes('/')) {
1212
+ validItemA = OpenAIEmbeddingService.SPACER_SMALL;
1213
+ }
1214
+ }
1215
+ }
1216
+ }
1217
+ // Validate Item B
1218
+ let validItemB = itemB;
1219
+ if (!isSpacerB) {
1220
+ // Check if it belongs to Input B
1221
+ const originalB = inB.find(b => containsItem(itemB, b));
1222
+ if (originalB) {
1223
+ usedB.add(originalB);
1224
+ }
1225
+ else {
1226
+ // It's not in Input B. Is it in Input A?
1227
+ if (inA.some(a => containsItem(itemB, a))) {
1228
+ // Hallucination! It belongs to A but is in B.
1229
+ validItemB = OpenAIEmbeddingService.SPACER_SMALL; // Remove it
1230
+ }
1231
+ else {
1232
+ // Unknown item.
1233
+ if (!itemB.includes('×') && !itemB.includes('/')) {
1234
+ validItemB = OpenAIEmbeddingService.SPACER_SMALL;
1235
+ }
1236
+ }
1237
+ }
1238
+ }
1239
+ cleanA.push(validItemA);
1240
+ cleanB.push(validItemB);
1241
+ }
1242
+ // Restore Missing Items
1243
+ const missingA = inA.filter(a => !usedA.has(a));
1244
+ const missingB = inB.filter(b => !usedB.has(b));
1245
+ if (missingA.length > 0 || missingB.length > 0) {
1246
+ console.warn('Restoring missing items:', { missingA, missingB });
1247
+ // Add spacer to separate restored items
1248
+ if (cleanA.length > 0) {
1249
+ cleanA.push(OpenAIEmbeddingService.SPACER_LARGE);
1250
+ cleanB.push(OpenAIEmbeddingService.SPACER_LARGE);
1251
+ }
1252
+ // Append missing A items
1253
+ for (const a of missingA) {
1254
+ cleanA.push(a);
1255
+ cleanB.push(OpenAIEmbeddingService.SPACER_SMALL);
1256
+ cleanA.push(OpenAIEmbeddingService.SPACER_SMALL); // Spacer
1257
+ cleanB.push(OpenAIEmbeddingService.SPACER_SMALL);
1258
+ }
1259
+ // Append missing B items
1260
+ for (const b of missingB) {
1261
+ cleanA.push(OpenAIEmbeddingService.SPACER_SMALL);
1262
+ cleanB.push(b);
1263
+ cleanA.push(OpenAIEmbeddingService.SPACER_SMALL); // Spacer
1264
+ cleanB.push(OpenAIEmbeddingService.SPACER_SMALL);
1265
+ }
1266
+ }
1267
+ return { listA: cleanA, listB: cleanB };
1268
+ }
1269
+ /**
1270
+ * Get alignment between two lists using a single OpenAI Chat Completion request.
1271
+ * Uses canonical caching to ensure A->B and B->A use the same API response.
1272
+ */
1273
+ groupAlignLists(listA, listB, threshold = 0.25, preserveLeftOrder = true, exclusivityMargin = 0.06, leftCohesion = 0.23, apiKey) {
1274
+ // Filter valid items
1275
+ const validA = listA.filter(i => i && i.trim() && i.trim() !== OpenAIEmbeddingService.SPACER_SMALL);
1276
+ const validB = listB.filter(i => i && i.trim() && i.trim() !== OpenAIEmbeddingService.SPACER_SMALL);
1277
+ if (validA.length === 0 && validB.length === 0)
1278
+ return of([]);
1279
+ // Key for (A, B)
1280
+ const keyDirect = JSON.stringify({ a: validA, b: validB });
1281
+ // Key for (B, A) - assuming validB passed as first arg
1282
+ const keyReverse = JSON.stringify({ a: validB, b: validA });
1283
+ // Check direct match
1284
+ if (this.alignmentCache.has(keyDirect)) {
1285
+ console.log('Alignment cache hit (Direct)');
1286
+ return of(this.alignmentCache.get(keyDirect).rows);
1287
+ }
1288
+ // Check reverse match
1289
+ if (this.alignmentCache.has(keyReverse)) {
1290
+ console.log('Alignment cache hit (Reverse) - Swapping results');
1291
+ const cached = this.alignmentCache.get(keyReverse);
1292
+ // Swap left/right to match current request (A, B)
1293
+ const swappedRows = cached.rows.map(row => ({
1294
+ left: row.right,
1295
+ right: row.left,
1296
+ score: row.score
1297
+ }));
1298
+ return of(swappedRows);
1299
+ }
1300
+ // No cache hit - Make API Call
1301
+ const headers = new HttpHeaders({
1302
+ [OpenAIEmbeddingService.HEADER_CONTENT_TYPE]: OpenAIEmbeddingService.CONTENT_TYPE_JSON,
1303
+ [OpenAIEmbeddingService.HEADER_AUTHORIZATION]: `${OpenAIEmbeddingService.BEARER_PREFIX}${apiKey}`
1304
+ });
1305
+ // Construct the prompt
1306
+ const prompt = `
1307
+ You are an expert semantic alignment engine.
1308
+ Task: Align the items from List A to List B based on semantic similarity.
1309
+
1310
+ List A: ${JSON.stringify(validA)}
1311
+ List B: ${JSON.stringify(validB)}
1312
+
1313
+ Rules:
1314
+ 1. Match items that are semantically similar.
1315
+ 2. Support One-to-One, One-to-Many, and Many-to-Many matches.
1316
+ 3. **CRITICAL**: Avoid duplicate alignments. If multiple items in List B are highly similar to a single item in List A, group them ALL under that single List A item.
1317
+ 4. **CONSISTENCY**: The alignment must be symmetric. If "Mutton" matches "Chicken" when "Mutton" is on the left, it MUST also match "Chicken" when "Mutton" is on the right.
1318
+ 5. **ORDER**: You MUST reorder the items from BOTH lists to create the best semantic clusters. Place related groups next to each other (e.g., put all food items together, all tech items together).
1319
+ 6. If an item in List A has no match in List B, map it to "-".
1320
+ 7. If an item in List B is not matched to anything in List A, add a row with "-" on the left.
1321
+ 8. Output MUST be a JSON object with a single key "alignment" containing an array of objects.
1322
+ 9. Each object in the array must have:
1323
+ - "left": string (item from List A or "-")
1324
+ - "right": string (item from List B or "-")
1325
+ - "score": number (similarity score between 0 and 1)
1326
+
1327
+ Example Output Format:
1328
+ {
1329
+ "alignment": [
1330
+ { "left": "Seafood", "right": "Fish", "score": 0.9 },
1331
+ { "left": "Seafood", "right": "Clams", "score": 0.85 },
1332
+ { "left": "Seafood", "right": "Crab", "score": 0.88 },
1333
+ { "left": "Chicken", "right": "Meat", "score": 0.8 },
1334
+ { "left": "Chicken", "right": "Poultry", "score": 0.9 },
1335
+ { "left": "Chicken", "right": "Mutton", "score": 0.85 },
1336
+ { "left": "Coffee", "right": "Cafés", "score": 0.9 },
1337
+ { "left": "Coding", "right": "-", "score": 0 }
1338
+ ]
1339
+ }
1340
+ `;
1341
+ const body = {
1342
+ model: this.MODEL,
1343
+ messages: [
1344
+ { role: 'system', content: 'You are a helpful assistant that outputs strictly JSON.' },
1345
+ { role: 'user', content: prompt }
1346
+ ],
1347
+ response_format: { type: 'json_object' },
1348
+ temperature: 0.2
1349
+ };
1350
+ console.log('Sending single alignment request to OpenAI...');
1351
+ return this.http.post(this.API_URL, body, { headers }).pipe(map(response => {
1352
+ try {
1353
+ const content = response.choices[0].message.content;
1354
+ const result = JSON.parse(content);
1355
+ if (!result.alignment || !Array.isArray(result.alignment)) {
1356
+ throw new Error('Invalid JSON structure from OpenAI');
1357
+ }
1358
+ const rows = result.alignment;
1359
+ // --- Post-Processing for Uniqueness, Grouping, Completeness, Reordering, and Validity ---
1360
+ // 0. Pre-process Input Lists: Deduplicate and Trim
1361
+ const validA = Array.from(new Set(listA.filter(i => i && i.trim() && i.trim() !== OpenAIEmbeddingService.SPACER_SMALL).map(i => i.trim())));
1362
+ const validB = Array.from(new Set(listB.filter(i => i && i.trim() && i.trim() !== OpenAIEmbeddingService.SPACER_SMALL).map(i => i.trim())));
1363
+ const validASet = new Set(validA);
1364
+ const validBSet = new Set(validB);
1365
+ // 0.1 Normalize Rows: Trim whitespace from AI response
1366
+ rows.forEach(row => {
1367
+ if (row.left)
1368
+ row.left = row.left.trim();
1369
+ if (row.right)
1370
+ row.right = row.right.trim();
1371
+ });
1372
+ // 0.2 Deduplicate Rows: Remove exact duplicate objects from AI response
1373
+ const uniqueRows = new Map();
1374
+ rows.forEach(row => {
1375
+ const key = `${row.left}|${row.right}`;
1376
+ if (!uniqueRows.has(key)) {
1377
+ uniqueRows.set(key, row);
1378
+ }
1379
+ else {
1380
+ // If duplicate exists, keep the one with higher score
1381
+ if (row.score > uniqueRows.get(key).score) {
1382
+ uniqueRows.set(key, row);
1383
+ }
1384
+ }
1385
+ });
1386
+ const dedupedRows = Array.from(uniqueRows.values());
1387
+ // 0.3 Hallucination Guard: Ensure all returned items actually exist in the input lists
1388
+ dedupedRows.forEach(row => {
1389
+ if (row.left !== '-' && !validASet.has(row.left)) {
1390
+ console.warn(`Hallucination detected: Left item '${row.left}' not in input list. Removing.`);
1391
+ row.left = '-';
1392
+ }
1393
+ if (row.right !== '-' && !validBSet.has(row.right)) {
1394
+ console.warn(`Hallucination detected: Right item '${row.right}' not in input list. Removing.`);
1395
+ row.right = '-';
1396
+ }
1397
+ });
1398
+ // 0.5 Exact Match Enforcer: If item exists in BOTH lists, force them to align
1399
+ const commonItems = validA.filter(a => validBSet.has(a));
1400
+ commonItems.forEach(item => {
1401
+ const exactRow = dedupedRows.find(r => r.left === item && r.right === item);
1402
+ if (exactRow) {
1403
+ exactRow.score = 1.0;
1404
+ }
1405
+ else {
1406
+ dedupedRows.forEach(r => {
1407
+ if (r.left === item)
1408
+ r.left = '-';
1409
+ if (r.right === item)
1410
+ r.right = '-';
1411
+ });
1412
+ dedupedRows.push({ left: item, right: item, score: 1.0 });
1413
+ }
1414
+ });
1415
+ // 1. Enforce Right-Side Uniqueness
1416
+ const rightItemBestMatch = new Map();
1417
+ dedupedRows.forEach((row, index) => {
1418
+ if (row.right && row.right !== '-') {
1419
+ if (!rightItemBestMatch.has(row.right)) {
1420
+ rightItemBestMatch.set(row.right, { row, index });
1421
+ }
1422
+ else {
1423
+ const currentBest = rightItemBestMatch.get(row.right);
1424
+ if (row.score > currentBest.row.score) {
1425
+ currentBest.row.right = '-';
1426
+ currentBest.row.score = 0;
1427
+ rightItemBestMatch.set(row.right, { row, index });
1428
+ }
1429
+ else {
1430
+ row.right = '-';
1431
+ row.score = 0;
1432
+ }
1433
+ }
1434
+ }
1435
+ });
1436
+ // 2. Group rows by their 'left' value
1437
+ const groups = new Map();
1438
+ const unmatchedRightRows = [];
1439
+ dedupedRows.forEach(row => {
1440
+ if (row.left === '-') {
1441
+ unmatchedRightRows.push(row);
1442
+ }
1443
+ else {
1444
+ if (!groups.has(row.left)) {
1445
+ groups.set(row.left, []);
1446
+ }
1447
+ groups.get(row.left).push(row);
1448
+ }
1449
+ });
1450
+ // 3. Reconstruct the list respecting AI ORDER
1451
+ const processedRows = [];
1452
+ const processedLeftItems = new Set();
1453
+ dedupedRows.forEach(row => {
1454
+ if (row.left !== '-' && !processedLeftItems.has(row.left)) {
1455
+ const itemRows = groups.get(row.left);
1456
+ itemRows.sort((a, b) => b.score - a.score);
1457
+ itemRows.forEach((r, idx) => {
1458
+ if (idx > 0)
1459
+ r.left = '-';
1460
+ processedRows.push(r);
1461
+ });
1462
+ processedLeftItems.add(row.left);
1463
+ }
1464
+ });
1465
+ // 4. Add missing Left items
1466
+ validA.forEach(item => {
1467
+ if (!processedLeftItems.has(item)) {
1468
+ processedRows.push({ left: item, right: '-', score: 0 });
1469
+ }
1470
+ });
1471
+ // 5. Add unmatched right rows and missing Right items
1472
+ const validUnmatched = unmatchedRightRows.filter(r => r.right !== '-');
1473
+ processedRows.push(...validUnmatched);
1474
+ const usedB = new Set(processedRows.map(r => r.right).filter(x => x !== '-'));
1475
+ validB.forEach(bItem => {
1476
+ if (!usedB.has(bItem)) {
1477
+ processedRows.push({ left: '-', right: bItem, score: 0 });
1478
+ }
1479
+ });
1480
+ const finalRows = processedRows.filter(r => r.left !== '-' || r.right !== '-');
1481
+ const cacheKey = `${keyDirect}_v2`;
1482
+ const uniqueFinalRows = new Map();
1483
+ const finalRowsClean = [];
1484
+ const seenLeftGroups = new Set();
1485
+ finalRows.forEach(row => {
1486
+ const key = `${row.left}|${row.right}`;
1487
+ if (uniqueFinalRows.has(key))
1488
+ return;
1489
+ uniqueFinalRows.set(key, row);
1490
+ if (row.left !== '-') {
1491
+ if (seenLeftGroups.has(row.left)) {
1492
+ row.left = '-';
1493
+ }
1494
+ else {
1495
+ seenLeftGroups.add(row.left);
1496
+ }
1497
+ }
1498
+ finalRowsClean.push(row);
1499
+ });
1500
+ this.alignmentCache.set(cacheKey, {
1501
+ originalA: validA,
1502
+ originalB: validB,
1503
+ rows: finalRowsClean
1504
+ });
1505
+ return finalRowsClean;
1506
+ }
1507
+ catch (e) {
1508
+ console.error('Failed to parse OpenAI response', e);
1509
+ throw new Error('Failed to parse alignment result');
1510
+ }
1511
+ }), catchError((error) => {
1512
+ console.error('OpenAI API Error:', error);
1513
+ return throwError(() => error);
1514
+ }));
1515
+ }
1516
+ /**
1517
+ * Calculate semantic similarity between two texts using OpenAI Embeddings API.
1518
+ * Returns a cosine similarity score in [0, 1] (0 for empty/invalid inputs).
1519
+ */
1520
+ calculateSimilarity(textA, textB, apiKey) {
1521
+ const a = (textA || '').trim();
1522
+ const b = (textB || '').trim();
1523
+ if (!a.length || !b.length) {
1524
+ return of(0);
1525
+ }
1526
+ const headers = new HttpHeaders({
1527
+ [OpenAIEmbeddingService.HEADER_CONTENT_TYPE]: OpenAIEmbeddingService.CONTENT_TYPE_JSON,
1528
+ [OpenAIEmbeddingService.HEADER_AUTHORIZATION]: `${OpenAIEmbeddingService.BEARER_PREFIX}${apiKey}`
1529
+ });
1530
+ const EMBEDDINGS_URL = 'https://api.openai.com/v1/embeddings';
1531
+ const body = {
1532
+ model: 'text-embedding-3-small',
1533
+ input: [a, b]
1534
+ };
1535
+ return this.http.post(EMBEDDINGS_URL, body, { headers }).pipe(map(res => {
1536
+ const data = res?.data;
1537
+ if (!Array.isArray(data) || data.length < 2) {
1538
+ throw new Error('Invalid embeddings response from OpenAI');
1539
+ }
1540
+ const v1 = data[0]?.embedding;
1541
+ const v2 = data[1]?.embedding;
1542
+ if (!v1 || !v2) {
1543
+ throw new Error('Embeddings not present in OpenAI response');
1544
+ }
1545
+ return this.cosineSimilarityVectors(v1, v2);
1546
+ }), catchError((error) => {
1547
+ const msg = (error?.error && (error.error.error?.message || error.error.message)) || error?.message || 'OpenAI embeddings request failed';
1548
+ return throwError(() => new Error(msg));
1549
+ }));
1550
+ }
1551
+ /**
1552
+ * Fetch embeddings for multiple texts in a single request.
1553
+ */
1554
+ getEmbeddings(texts, apiKey) {
1555
+ if (!texts || texts.length === 0)
1556
+ return of([]);
1557
+ const headers = new HttpHeaders({
1558
+ [OpenAIEmbeddingService.HEADER_CONTENT_TYPE]: OpenAIEmbeddingService.CONTENT_TYPE_JSON,
1559
+ [OpenAIEmbeddingService.HEADER_AUTHORIZATION]: `${OpenAIEmbeddingService.BEARER_PREFIX}${apiKey}`
1560
+ });
1561
+ const EMBEDDINGS_URL = 'https://api.openai.com/v1/embeddings';
1562
+ const body = {
1563
+ model: 'text-embedding-3-small',
1564
+ input: texts
1565
+ };
1566
+ return this.http.post(EMBEDDINGS_URL, body, { headers }).pipe(map(res => {
1567
+ const data = res?.data;
1568
+ if (!Array.isArray(data)) {
1569
+ throw new Error('Invalid embeddings response from OpenAI');
1570
+ }
1571
+ return data.map((item) => item.embedding);
1572
+ }), catchError((error) => {
1573
+ const msg = (error?.error && (error.error.error?.message || error.error.message)) || error?.message || 'OpenAI embeddings request failed';
1574
+ return throwError(() => new Error(msg));
1575
+ }));
1576
+ }
1577
+ // Private helpers
1578
+ cosineSimilarityVectors(a, b) {
1579
+ let dot = 0;
1580
+ let na = 0;
1581
+ let nb = 0;
1582
+ const n = Math.min(a.length, b.length);
1583
+ for (let i = 0; i < n; i++) {
1584
+ const ai = a[i];
1585
+ const bi = b[i];
1586
+ dot += ai * bi;
1587
+ na += ai * ai;
1588
+ nb += bi * bi;
1589
+ }
1590
+ if (na === 0 || nb === 0)
1591
+ return 0;
1592
+ return dot / (Math.sqrt(na) * Math.sqrt(nb));
1593
+ }
1594
+ clearCache() {
1595
+ this.alignmentCache.clear();
1596
+ this.spacerAlignmentCache.clear();
1597
+ }
1598
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: OpenAIEmbeddingService, deps: [{ token: i1.HttpClient }], target: i0.ɵɵFactoryTarget.Injectable });
1599
+ static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: OpenAIEmbeddingService, providedIn: 'root' });
1600
+ }
1601
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: OpenAIEmbeddingService, decorators: [{
1602
+ type: Injectable,
1603
+ args: [{
1604
+ providedIn: 'root'
1605
+ }]
1606
+ }], ctorParameters: () => [{ type: i1.HttpClient }] });
1607
+
1608
+ class ProfileComparisonLibService {
1609
+ constructor() { }
1610
+ /**
1611
+ * Main service for profile comparison functionality
1612
+ */
1613
+ compareProfiles(profile1, profile2) {
1614
+ // Placeholder implementation
1615
+ console.log('Comparing profiles:', profile1, profile2);
1616
+ return {
1617
+ similarity: 0.5,
1618
+ differences: [],
1619
+ timestamp: new Date()
1620
+ };
1621
+ }
1622
+ /**
1623
+ * Get library version
1624
+ */
1625
+ getVersion() {
1626
+ return '0.0.0';
1627
+ }
1628
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ProfileComparisonLibService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
1629
+ static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ProfileComparisonLibService, providedIn: 'root' });
1630
+ }
1631
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ProfileComparisonLibService, decorators: [{
1632
+ type: Injectable,
1633
+ args: [{
1634
+ providedIn: 'root'
1635
+ }]
1636
+ }], ctorParameters: () => [] });
1637
+
1638
+ // Face++ credentials must be provided by the caller (do not import consumer app environments from the library).
1639
+ var FacePlusPlusFormFields;
1640
+ (function (FacePlusPlusFormFields) {
1641
+ FacePlusPlusFormFields["API_KEY"] = "api_key";
1642
+ FacePlusPlusFormFields["API_SECRET"] = "api_secret";
1643
+ FacePlusPlusFormFields["IMAGE_BASE64"] = "image_base64";
1644
+ FacePlusPlusFormFields["IMAGE_FILE"] = "image_file";
1645
+ FacePlusPlusFormFields["RETURN_LANDMARK"] = "return_landmark";
1646
+ })(FacePlusPlusFormFields || (FacePlusPlusFormFields = {}));
1647
+ var FacePlusPlusErrorTypes;
1648
+ (function (FacePlusPlusErrorTypes) {
1649
+ FacePlusPlusErrorTypes["AUTHENTICATION_ERROR"] = "authentication_error";
1650
+ FacePlusPlusErrorTypes["AUTHORIZATION_ERROR"] = "authorization_error";
1651
+ FacePlusPlusErrorTypes["CONCURRENCY_LIMIT_EXCEEDED"] = "concurrency_limit_exceeded";
1652
+ FacePlusPlusErrorTypes["RATE"] = "rate";
1653
+ })(FacePlusPlusErrorTypes || (FacePlusPlusErrorTypes = {}));
1654
+ class ProfileService {
1655
+ http;
1656
+ static BASE_URL = 'https://api.api-ninjas.com/v1/';
1657
+ static FACE_PLUS_PLUS_URL = 'https://api-us.faceplusplus.com/facepp/v3/detect';
1658
+ baseUrl = ProfileService.BASE_URL;
1659
+ baseUrlf = ProfileService.FACE_PLUS_PLUS_URL;
1660
+ constructor(http) {
1661
+ this.http = http;
1662
+ }
1663
+ compareInterests(text_1, text_2, apiKey) {
1664
+ const headers = new HttpHeaders({
1665
+ 'X-Api-Key': apiKey,
1666
+ 'Content-Type': 'application/json',
1667
+ });
1668
+ return this.http.post(`${this.baseUrl}textsimilarity`, { text_1, text_2 }, { headers });
1669
+ }
1670
+ detectFace(image, creds) {
1671
+ const formData = new FormData();
1672
+ // Use Face++ credentials from caller; the library must not depend on consumer application environments
1673
+ const apiKey = creds?.apiKey ?? '';
1674
+ const apiSecret = creds?.apiSecret ?? '';
1675
+ formData.append(FacePlusPlusFormFields.API_KEY, apiKey);
1676
+ formData.append(FacePlusPlusFormFields.API_SECRET, apiSecret);
1677
+ // Provide either image_file (binary) or image_base64
1678
+ if (typeof image === 'string') {
1679
+ const isDataUrl = image.startsWith('data:');
1680
+ const base64 = isDataUrl ? (image.split(',')[1] || '') : image;
1681
+ formData.append(FacePlusPlusFormFields.IMAGE_BASE64, base64);
1682
+ }
1683
+ else {
1684
+ formData.append(FacePlusPlusFormFields.IMAGE_FILE, image, image.name);
1685
+ }
1686
+ // Ask Face++ to return facial landmarks so eyes and nose can be positioned accurately
1687
+ formData.append(FacePlusPlusFormFields.RETURN_LANDMARK, '1');
1688
+ // Map Face++ response to IFaceDetectionResult shape
1689
+ return this.http.post(`${this.baseUrlf}`, formData).pipe(
1690
+ // Retry once after a short delay on Face++ rate-limit/security responses
1691
+ retryWhen((errors) => errors.pipe(concatMap((error, i) => {
1692
+ const status = error?.status;
1693
+ const shouldRetry = (status === 403 || status === 429 || status === 503 || status === 401) && i < 1;
1694
+ if (shouldRetry) {
1695
+ // Wait 3 seconds before a single retry
1696
+ return timer(3000);
1697
+ }
1698
+ // Propagate other errors (or second failure) to the caller
1699
+ return throwError(() => error);
1700
+ }))), map$1((raw) => {
1701
+ // Face++ sometimes returns HTTP 200 with an error payload containing `error_message`
1702
+ // e.g. "AUTHENTICATION_ERROR: api_key and api_secret does not match." or
1703
+ // "AUTHORIZATION_ERROR:<reason>" or "CONCURRENCY_LIMIT_EXCEEDED".
1704
+ const errorMessage = raw?.error_message;
1705
+ if (errorMessage && typeof errorMessage === 'string' && errorMessage.trim().length > 0) {
1706
+ const lower = errorMessage.toLowerCase();
1707
+ let status = 400;
1708
+ if (lower.includes(FacePlusPlusErrorTypes.AUTHENTICATION_ERROR))
1709
+ status = 401;
1710
+ else if (lower.includes(FacePlusPlusErrorTypes.AUTHORIZATION_ERROR))
1711
+ status = 403;
1712
+ else if (lower.includes(FacePlusPlusErrorTypes.CONCURRENCY_LIMIT_EXCEEDED) || lower.includes(FacePlusPlusErrorTypes.RATE))
1713
+ status = 429;
1714
+ // Throw an error-like object so subscribers hit the error path and can show a modal
1715
+ throw { status, error: { message: errorMessage } };
1716
+ }
1717
+ const faces = Array.isArray(raw?.faces)
1718
+ ? raw.faces
1719
+ .map((f) => {
1720
+ const rect = f?.face_rectangle || f?.faceRect || f?.rect;
1721
+ if (!rect || typeof rect.left !== 'number' || typeof rect.top !== 'number') {
1722
+ return null;
1723
+ }
1724
+ let landmarks = undefined;
1725
+ const lm = f?.landmark;
1726
+ if (lm && typeof lm === 'object') {
1727
+ landmarks = {};
1728
+ for (const key of Object.keys(lm)) {
1729
+ const pt = lm[key];
1730
+ if (pt && typeof pt.x === 'number' && typeof pt.y === 'number') {
1731
+ landmarks[key] = { x: pt.x, y: pt.y };
1732
+ }
1733
+ }
1734
+ }
1735
+ return {
1736
+ x: rect.left,
1737
+ y: rect.top,
1738
+ width: rect.width,
1739
+ height: rect.height,
1740
+ landmarks,
1741
+ };
1742
+ })
1743
+ .filter((v) => !!v)
1744
+ : [];
1745
+ const imageUrl = typeof image === 'string' && image.startsWith('http') ? image : '';
1746
+ const result = { imageUrl, faces };
1747
+ return result;
1748
+ }));
1749
+ }
1750
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ProfileService, deps: [{ token: i1.HttpClient }], target: i0.ɵɵFactoryTarget.Injectable });
1751
+ static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ProfileService, providedIn: 'root' });
1752
+ }
1753
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ProfileService, decorators: [{
1754
+ type: Injectable,
1755
+ args: [{ providedIn: 'root' }]
1756
+ }], ctorParameters: () => [{ type: i1.HttpClient }] });
1757
+
1758
+ class ProfileComparisonLibComponent {
1759
+ profileService;
1760
+ openaiEmbeddingService;
1761
+ renderer;
1762
+ fileConversionService;
1763
+ imageCompressionService;
1764
+ static DEFAULT_OBJECT_POSITION = '50% 50%';
1765
+ static DEFAULT_EYE_ALIGNMENT_BIAS_PX = 50;
1766
+ static DEFAULT_IMAGE_FORMAT = 'image/jpeg';
1767
+ static DEFAULT_IMAGE_QUALITY_START = 0.85;
1768
+ static DEFAULT_IMAGE_QUALITY_MIN = 0.5;
1769
+ static DEFAULT_IMAGE_QUALITY_STEP = 0.05;
1770
+ static DEFAULT_MAX_BYTES = 2 * 1024 * 1024;
1771
+ static DEFAULT_DOWNSCALE_STEP = 0.9;
1772
+ static DEFAULT_MIN_WIDTH = 400;
1773
+ static DEFAULT_MIN_HEIGHT = 400;
1774
+ static DEFAULT_MAX_WIDTH = 1600;
1775
+ static DEFAULT_MAX_HEIGHT = 1600;
1776
+ static SPACER_SMALL = '-';
1777
+ static SPACER_LARGE = '----';
1778
+ static CONSOLIDATION_SEPARATOR_X = '×';
1779
+ static CONSOLIDATION_SEPARATOR_SLASH = '/';
1780
+ static FACEPP_RATE_LIMIT_DELAY_MS = 5000;
1781
+ static DEFAULT_PROFILE_IMAGE_NAME = 'ProfilePg1.png';
1782
+ config = {
1783
+ person1Interests: [],
1784
+ person2Interests: [],
1785
+ person3Interests: [],
1786
+ user1Image: '',
1787
+ user2Image: '',
1788
+ };
1789
+ apiNinjasKey = '';
1790
+ faceplusKey = '';
1791
+ faceplusSecret = '';
1792
+ openaiApiKey = '';
1793
+ fadeAllEdges = false;
1794
+ matrixDataChange = new EventEmitter();
1795
+ selectedFile = null;
1796
+ result;
1797
+ firstImageData = null;
1798
+ secondImageData = null;
1799
+ user1Transform = '';
1800
+ user2Transform = '';
1801
+ alignmentCalculated = false;
1802
+ isAligning = false;
1803
+ user1ObjectPosition = ProfileComparisonLibComponent.DEFAULT_OBJECT_POSITION;
1804
+ user2ObjectPosition = ProfileComparisonLibComponent.DEFAULT_OBJECT_POSITION;
1805
+ user1NaturalWidth = 0;
1806
+ user1NaturalHeight = 0;
1807
+ user2NaturalWidth = 0;
1808
+ user2NaturalHeight = 0;
1809
+ user1FaceRaw = null;
1810
+ user2FaceRaw = null;
1811
+ eyeAlignmentBiasPx = ProfileComparisonLibComponent.DEFAULT_EYE_ALIGNMENT_BIAS_PX;
1812
+ firstPersonInterests = [];
1813
+ secondPersonInterests = [];
1814
+ thirdPersonInterests = [];
1815
+ centerItem = [];
1816
+ person1Interests = [];
1817
+ person2Interests = [];
1818
+ person3Interests = [];
1819
+ user1Image = '';
1820
+ user2Image = '';
1821
+ baseConfig = {
1822
+ person1Interests: [],
1823
+ person2Interests: [],
1824
+ person3Interests: [],
1825
+ user1Image: '',
1826
+ user2Image: '',
1827
+ };
1828
+ displayPerson1Interests = [];
1829
+ displayPerson2Interests = [];
1830
+ alignedPerson1Interests = [];
1831
+ alignedPerson2Interests = [];
1832
+ leftProfileClicked = false;
1833
+ rightProfileClicked = false;
1834
+ sortedA = [];
1835
+ sortedB = [];
1836
+ sortedC = [];
1837
+ similarity = 0;
1838
+ similarityMatrixCsv = '';
1839
+ matrixData = null;
1840
+ customApiKey = '';
1841
+ showApiKeyModal = false;
1842
+ apiKeyInputValue = '';
1843
+ quotaExhausted = false;
1844
+ customFaceppKey = '';
1845
+ customFaceppSecret = '';
1846
+ showFaceppKeyModal = false;
1847
+ faceppKeyInputValue = '';
1848
+ faceppSecretInputValue = '';
1849
+ faceppQuotaExhausted = false;
1850
+ customOpenAIKey = '';
1851
+ showOpenAIKeyModal = false;
1852
+ openaiKeyInputValue = '';
1853
+ openaiQuotaExhausted = false;
1854
+ compressionConfig = {
1855
+ maxWidth: ProfileComparisonLibComponent.DEFAULT_MAX_WIDTH,
1856
+ maxHeight: ProfileComparisonLibComponent.DEFAULT_MAX_HEIGHT,
1857
+ format: ProfileComparisonLibComponent.DEFAULT_IMAGE_FORMAT,
1858
+ qualityStart: ProfileComparisonLibComponent.DEFAULT_IMAGE_QUALITY_START,
1859
+ qualityMin: ProfileComparisonLibComponent.DEFAULT_IMAGE_QUALITY_MIN,
1860
+ qualityStep: ProfileComparisonLibComponent.DEFAULT_IMAGE_QUALITY_STEP,
1861
+ maxBytes: ProfileComparisonLibComponent.DEFAULT_MAX_BYTES,
1862
+ downscaleStep: ProfileComparisonLibComponent.DEFAULT_DOWNSCALE_STEP,
1863
+ minWidth: ProfileComparisonLibComponent.DEFAULT_MIN_WIDTH,
1864
+ minHeight: ProfileComparisonLibComponent.DEFAULT_MIN_HEIGHT,
1865
+ };
1866
+ computeSub;
1867
+ detectFaceSub1;
1868
+ detectFaceSub2;
1869
+ activeRequestEpoch = 0;
1870
+ form = new FormGroup({
1871
+ interest1: new FormControl(''),
1872
+ interest2: new FormControl(''),
1873
+ });
1874
+ leftContainer;
1875
+ rightContainer;
1876
+ constructor(profileService, openaiEmbeddingService, renderer, fileConversionService, imageCompressionService) {
1877
+ this.profileService = profileService;
1878
+ this.openaiEmbeddingService = openaiEmbeddingService;
1879
+ this.renderer = renderer;
1880
+ this.fileConversionService = fileConversionService;
1881
+ this.imageCompressionService = imageCompressionService;
1882
+ }
1883
+ anchorSide = 'left';
1884
+ ngOnInit() {
1885
+ this.baseConfig = JSON.parse(JSON.stringify(this.config));
1886
+ this.loadCustomConfig();
1887
+ this.updateConfigProperties();
1888
+ this.loadApiKeyFromStorage();
1889
+ this.loadFaceppCredsFromStorage();
1890
+ this.loadOpenAIKeyFromStorage();
1891
+ this.compute();
1892
+ this.compressConfigImagesIfNeeded().subscribe({
1893
+ next: () => this.initializeFaceDetection(),
1894
+ error: (e) => {
1895
+ console.warn('Config image compression failed, continuing:', e);
1896
+ this.initializeFaceDetection();
1897
+ }
1898
+ });
1899
+ this.user1Transform = 'translateY(0px)';
1900
+ this.user2Transform = 'translateY(0px)';
1901
+ this.alignmentCalculated = false;
1902
+ }
1903
+ ngAfterViewInit() {
1904
+ this.waitForImagesAndInitDrag();
1905
+ }
1906
+ ngOnChanges(changes) {
1907
+ if (changes['config']) {
1908
+ this.updateConfigProperties();
1909
+ }
1910
+ }
1911
+ updateConfigProperties() {
1912
+ const normalized = this.buildConfigWithDefaults(this.config, this.baseConfig);
1913
+ this.config = normalized;
1914
+ this.person1Interests = [...normalized.person1Interests];
1915
+ this.person2Interests = [...normalized.person2Interests];
1916
+ this.user1Image = normalized.user1Image;
1917
+ this.user2Image = normalized.user2Image;
1918
+ }
1919
+ buildConfigWithDefaults(cfg, base) {
1920
+ const list = (val, fb) => {
1921
+ if (Array.isArray(val)) {
1922
+ const cleaned = val
1923
+ .filter((s) => typeof s === 'string')
1924
+ .map((s) => s.trim())
1925
+ .filter((s) => s.length > 0 && s !== ProfileComparisonLibComponent.SPACER_SMALL);
1926
+ if (cleaned.length > 0)
1927
+ return cleaned;
1928
+ }
1929
+ return [...fb];
1930
+ };
1931
+ const str = (val, fb) => {
1932
+ return typeof val === 'string' && val.trim().length > 0 ? val : fb;
1933
+ };
1934
+ const safe = cfg || {};
1935
+ return {
1936
+ person1Interests: list(safe.person1Interests, base.person1Interests),
1937
+ person2Interests: list(safe.person2Interests, base.person2Interests),
1938
+ person3Interests: list(safe.person3Interests, base.person3Interests),
1939
+ user1Image: str(safe.user1Image, base.user1Image),
1940
+ user2Image: str(safe.user2Image, base.user2Image),
1941
+ };
1942
+ }
1943
+ onUserImageLoad(which, event) {
1944
+ const img = event?.target;
1945
+ if (!img)
1946
+ return;
1947
+ if (which === 1) {
1948
+ this.user1NaturalWidth = img.naturalWidth || 0;
1949
+ this.user1NaturalHeight = img.naturalHeight || 0;
1950
+ this.updateObjectPositionForFace(1);
1951
+ if (this.firstImageData && this.secondImageData) {
1952
+ this.calculateFaceAlignment(this.firstImageData, this.secondImageData);
1953
+ }
1954
+ }
1955
+ else {
1956
+ this.user2NaturalWidth = img.naturalWidth || 0;
1957
+ this.user2NaturalHeight = img.naturalHeight || 0;
1958
+ this.updateObjectPositionForFace(2);
1959
+ if (this.firstImageData && this.secondImageData) {
1960
+ this.calculateFaceAlignment(this.firstImageData, this.secondImageData);
1961
+ }
1962
+ }
1963
+ }
1964
+ updateObjectPositionForFace(which) {
1965
+ const face = which === 1 ? this.user1FaceRaw : this.user2FaceRaw;
1966
+ const natW = which === 1 ? this.user1NaturalWidth : this.user2NaturalWidth;
1967
+ const natH = which === 1 ? this.user1NaturalHeight : this.user2NaturalHeight;
1968
+ if (!face || !natW || !natH) {
1969
+ if (which === 1)
1970
+ this.user1ObjectPosition = ProfileComparisonLibComponent.DEFAULT_OBJECT_POSITION;
1971
+ else
1972
+ this.user2ObjectPosition = ProfileComparisonLibComponent.DEFAULT_OBJECT_POSITION;
1973
+ return;
1974
+ }
1975
+ const eyes = this.getEyeCoordinatesFromFace(face);
1976
+ const nose = this.getNoseCoordinateFromFace(face);
1977
+ const minX = Math.min(eyes.leftEye.x, eyes.rightEye.x, nose.x);
1978
+ const maxX = Math.max(eyes.leftEye.x, eyes.rightEye.x, nose.x);
1979
+ const roiCenterX = (minX + maxX) / 2;
1980
+ const { width: cw, height: ch } = this.getProfileContainerSize(which);
1981
+ if (!cw || !ch) {
1982
+ const clamped = Math.max(0, Math.min(natW, roiCenterX));
1983
+ let xPercentFallback = Math.round((clamped / natW) * 10000) / 100;
1984
+ const faceCenterX_fb = face.x + face.width / 2;
1985
+ const ratio_fb = faceCenterX_fb / natW;
1986
+ if (ratio_fb > 0.6) {
1987
+ const over = Math.min(0.4, ratio_fb - 0.6);
1988
+ const shiftPct = 7 + (over / 0.4) * 8;
1989
+ xPercentFallback = Math.max(0, Math.min(100, xPercentFallback + shiftPct));
1990
+ }
1991
+ const posFallback = `${xPercentFallback}% 50%`;
1992
+ if (which === 1)
1993
+ this.user1ObjectPosition = posFallback;
1994
+ else
1995
+ this.user2ObjectPosition = posFallback;
1996
+ return;
1997
+ }
1998
+ const scale = Math.max(cw / natW, ch / natH);
1999
+ const viewW = cw / scale;
2000
+ const paddingCssPx = 12;
2001
+ const overlapCssPx = this.getOverlapSizeCssPx();
2002
+ const fadeSafetyCssPx = 8;
2003
+ const fadeCssPx = overlapCssPx + paddingCssPx + fadeSafetyCssPx;
2004
+ const fadeInImgPx = fadeCssPx / scale;
2005
+ const edgeInImgPx = paddingCssPx / scale;
2006
+ let anchorX = roiCenterX;
2007
+ if (which === 1) {
2008
+ const minAnchorForRightEye = maxX - viewW / 2 + fadeInImgPx;
2009
+ const maxAnchorForLeftEye = minX + viewW / 2 - edgeInImgPx;
2010
+ anchorX = Math.max(anchorX, minAnchorForRightEye);
2011
+ anchorX = Math.min(anchorX, maxAnchorForLeftEye);
2012
+ const faceCenterX = face.x + face.width / 2;
2013
+ const ratio = faceCenterX / natW;
2014
+ if (ratio > 0.6) {
2015
+ const over = Math.min(0.4, ratio - 0.6);
2016
+ const shiftPct = 7 + (over / 0.4) * 8;
2017
+ const shiftPx = (shiftPct / 100) * natW;
2018
+ anchorX += shiftPx;
2019
+ anchorX = Math.min(anchorX, maxAnchorForLeftEye);
2020
+ anchorX = Math.max(anchorX, minAnchorForRightEye);
2021
+ }
2022
+ }
2023
+ else {
2024
+ const maxAnchorForLeftEye = minX + viewW / 2 - fadeInImgPx;
2025
+ const minAnchorForRightEye = maxX - viewW / 2 + edgeInImgPx;
2026
+ anchorX = Math.min(anchorX, maxAnchorForLeftEye);
2027
+ anchorX = Math.max(anchorX, minAnchorForRightEye);
2028
+ const faceCenterX = face.x + face.width / 2;
2029
+ const ratio = faceCenterX / natW;
2030
+ if (ratio > 0.6) {
2031
+ const over = Math.min(0.4, ratio - 0.6);
2032
+ const shiftPct = 7 + (over / 0.4) * 8;
2033
+ const shiftPx = (shiftPct / 100) * natW;
2034
+ anchorX += shiftPx;
2035
+ }
2036
+ }
2037
+ const minAnchor = viewW / 2;
2038
+ const maxAnchor = natW - viewW / 2;
2039
+ anchorX = Math.max(minAnchor, Math.min(maxAnchor, anchorX));
2040
+ const viewLeft = anchorX - viewW / 2;
2041
+ const viewRight = anchorX + viewW / 2;
2042
+ const currentOverlap = this.getOverlapSizeCssPx();
2043
+ let desiredOverlap = currentOverlap;
2044
+ if (which === 1) {
2045
+ const distanceRightImg = Math.max(0, viewRight - maxX);
2046
+ const allowedCss = Math.max(10, distanceRightImg * scale - paddingCssPx);
2047
+ if (allowedCss < desiredOverlap)
2048
+ desiredOverlap = allowedCss;
2049
+ }
2050
+ else {
2051
+ const distanceLeftImg = Math.max(0, minX - viewLeft);
2052
+ const allowedCss = Math.max(10, distanceLeftImg * scale - paddingCssPx);
2053
+ if (allowedCss < desiredOverlap)
2054
+ desiredOverlap = allowedCss;
2055
+ }
2056
+ if (desiredOverlap < currentOverlap - 0.5) {
2057
+ this.setOverlapSizeCssPx(desiredOverlap);
2058
+ }
2059
+ const xPercent = Math.round((anchorX / natW) * 10000) / 100;
2060
+ const pos = `${xPercent}% 50%`;
2061
+ if (which === 1)
2062
+ this.user1ObjectPosition = pos;
2063
+ else
2064
+ this.user2ObjectPosition = pos;
2065
+ }
2066
+ getProfileContainerSize(which) {
2067
+ try {
2068
+ const selector = which === 1 ? '.profile-img.left' : '.profile-img.right';
2069
+ const el = document.querySelector(selector);
2070
+ if (!el)
2071
+ return { width: 0, height: 0 };
2072
+ const rect = el.getBoundingClientRect();
2073
+ return { width: Math.round(rect.width), height: Math.round(rect.height) };
2074
+ }
2075
+ catch {
2076
+ return { width: 0, height: 0 };
2077
+ }
2078
+ }
2079
+ getOverlapSizeCssPx() {
2080
+ try {
2081
+ const root = document.querySelector('.profile-flex');
2082
+ const fallback = 40;
2083
+ if (!root)
2084
+ return fallback;
2085
+ const val = getComputedStyle(root).getPropertyValue('--overlap-size').trim();
2086
+ const parsed = parseFloat(val);
2087
+ return isNaN(parsed) ? fallback : parsed;
2088
+ }
2089
+ catch {
2090
+ return 40;
2091
+ }
2092
+ }
2093
+ setOverlapSizeCssPx(px) {
2094
+ try {
2095
+ const root = document.querySelector('.profile-flex');
2096
+ if (!root)
2097
+ return;
2098
+ const clamped = Math.max(8, Math.min(80, Math.round(px)));
2099
+ root.style.setProperty('--overlap-size', `${clamped}px`);
2100
+ }
2101
+ catch { }
2102
+ }
2103
+ waitForImagesAndInitDrag() {
2104
+ const shapeBg1 = document.querySelector('img.shape-bg1');
2105
+ const shapeBg2 = document.querySelector('img.shape-bg2');
2106
+ if (!shapeBg1 || !shapeBg2) {
2107
+ setTimeout(() => this.waitForImagesAndInitDrag(), 100);
2108
+ return;
2109
+ }
2110
+ const checkImagesLoaded = () => {
2111
+ const img1Loaded = shapeBg1.complete && shapeBg1.naturalWidth > 0;
2112
+ const img2Loaded = shapeBg2.complete && shapeBg2.naturalWidth > 0;
2113
+ if (img1Loaded && img2Loaded) {
2114
+ setTimeout(() => this.initDrag(), 50);
2115
+ }
2116
+ else {
2117
+ setTimeout(checkImagesLoaded, 100);
2118
+ }
2119
+ };
2120
+ checkImagesLoaded();
2121
+ }
2122
+ initDrag() {
2123
+ const shapeBg1 = document.querySelector('img.shape-bg1');
2124
+ const shapeBg2 = document.querySelector('img.shape-bg2');
2125
+ const shapeTextLeft = document.querySelector('.shape-text-left');
2126
+ const shapeTextRight = document.querySelector('.shape-text-right');
2127
+ if (!shapeBg1 || !shapeBg2 || !shapeTextLeft || !shapeTextRight)
2128
+ return;
2129
+ shapeBg1.style.height = '350px';
2130
+ shapeBg1.style.objectFit = 'cover';
2131
+ shapeBg1.style.objectPosition = 'right';
2132
+ shapeBg2.style.height = '350px';
2133
+ shapeBg2.style.objectFit = 'cover';
2134
+ shapeBg2.style.objectPosition = 'left';
2135
+ const originalWidth1 = shapeBg1.offsetWidth;
2136
+ const originalWidth2 = shapeBg2.offsetWidth;
2137
+ const minWidth = 200;
2138
+ const maxWidth = 400;
2139
+ let isDragging = false;
2140
+ let dragStartX = 0;
2141
+ let initialWidth1 = 0;
2142
+ let initialWidth2 = 0;
2143
+ let activeImg = null;
2144
+ const shapeTextCenter = document.querySelector('.shape-text-center');
2145
+ const onMouseMove = (e) => {
2146
+ if (!isDragging || !activeImg)
2147
+ return;
2148
+ const deltaX = e.clientX - dragStartX;
2149
+ const containerWidth = document.querySelector('.shape')?.clientWidth || 0;
2150
+ const centerPosition = containerWidth / 2;
2151
+ let newWidth1 = initialWidth1;
2152
+ let newWidth2 = initialWidth2;
2153
+ if (activeImg === shapeBg1) {
2154
+ newWidth1 = initialWidth1 + deltaX;
2155
+ newWidth2 = initialWidth2 - deltaX;
2156
+ }
2157
+ else if (activeImg === shapeBg2) {
2158
+ newWidth1 = initialWidth1 - deltaX;
2159
+ newWidth2 = initialWidth2 + deltaX;
2160
+ }
2161
+ newWidth1 = Math.max(minWidth, Math.min(maxWidth, newWidth1));
2162
+ newWidth2 = Math.max(minWidth, Math.min(maxWidth, newWidth2));
2163
+ shapeBg1.style.width = newWidth1 + 'px';
2164
+ shapeBg1.style.height = '350px';
2165
+ shapeBg2.style.width = newWidth2 + 'px';
2166
+ shapeBg2.style.height = '350px';
2167
+ const widthChange1 = newWidth1 - originalWidth1;
2168
+ const widthChange2 = newWidth2 - originalWidth2;
2169
+ const dragDirection = deltaX > 0 ? 1 : -1;
2170
+ const dragDistance = Math.abs(deltaX);
2171
+ const maxDragDistance = 100;
2172
+ const centerMoveDistance = Math.min(dragDistance, maxDragDistance) * dragDirection * 0.5;
2173
+ shapeTextLeft.style.transform = `translateX(${widthChange1 * 0.3}px)`;
2174
+ shapeTextRight.style.transform = `translateX(${-widthChange2 * 0.3}px)`;
2175
+ shapeTextCenter.style.transform = `translateX(${centerMoveDistance * 0.3}px)`;
2176
+ };
2177
+ const resetWidths = () => {
2178
+ isDragging = false;
2179
+ activeImg = null;
2180
+ shapeBg1.style.transition = 'width 0.3s ease';
2181
+ shapeBg2.style.transition = 'width 0.3s ease';
2182
+ shapeTextLeft.style.transition = 'transform 0.3s ease';
2183
+ shapeTextRight.style.transition = 'transform 0.3s ease';
2184
+ shapeTextCenter.style.transition = 'transform 0.3s ease';
2185
+ shapeBg1.style.width = originalWidth1 + 'px';
2186
+ shapeBg2.style.width = originalWidth2 + 'px';
2187
+ shapeTextLeft.style.transform = 'translateX(0px)';
2188
+ shapeTextRight.style.transform = 'translateX(0px)';
2189
+ shapeTextCenter.style.transform = 'translateX(0px)';
2190
+ document.removeEventListener('mousemove', onMouseMove);
2191
+ document.removeEventListener('mouseup', resetWidths);
2192
+ document.removeEventListener('mouseleave', resetWidths);
2193
+ setTimeout(() => {
2194
+ shapeBg1.style.transition = '';
2195
+ shapeBg2.style.transition = '';
2196
+ shapeTextLeft.style.transition = '';
2197
+ shapeTextRight.style.transition = '';
2198
+ shapeTextCenter.style.transition = '';
2199
+ }, 300);
2200
+ };
2201
+ [shapeBg1, shapeBg2].forEach((img) => {
2202
+ img.addEventListener('mousedown', (e) => {
2203
+ e.preventDefault();
2204
+ isDragging = true;
2205
+ activeImg = img;
2206
+ dragStartX = e.clientX;
2207
+ initialWidth1 = shapeBg1.offsetWidth;
2208
+ initialWidth2 = shapeBg2.offsetWidth;
2209
+ document.addEventListener('mousemove', onMouseMove);
2210
+ document.addEventListener('mouseup', resetWidths);
2211
+ document.addEventListener('mouseleave', resetWidths);
2212
+ });
2213
+ });
2214
+ }
2215
+ compute() {
2216
+ if (this.computeSub) {
2217
+ this.computeSub.unsubscribe();
2218
+ this.computeSub = undefined;
2219
+ }
2220
+ const requestEpoch = this.activeRequestEpoch;
2221
+ this.centerItem = [];
2222
+ this.computeSub = forkJoin({
2223
+ a: this.sortByMaxSimilarity(this.person1Interests, [this.person2Interests, this.person3Interests]),
2224
+ b: this.sortByMaxSimilarity(this.person2Interests, [this.person1Interests, this.person3Interests]),
2225
+ c: this.sortByMaxSimilarity(this.person3Interests, [this.person1Interests, this.person2Interests]),
2226
+ }).subscribe((result) => {
2227
+ this.sortedA = result.a;
2228
+ this.sortedB = result.b;
2229
+ this.sortedC = result.c;
2230
+ this.displayPerson1Interests = result.a.map((item) => item.item);
2231
+ this.displayPerson2Interests = result.b.map((item) => item.item);
2232
+ this.isAligning = true;
2233
+ this.createEmbeddingBasedAlignment(result.a, result.b)
2234
+ .then(() => {
2235
+ this.isAligning = false;
2236
+ this.generateSimilarityMatrix();
2237
+ })
2238
+ .catch((error) => {
2239
+ console.warn('Embedding alignment failed, falling back to semantic:', error);
2240
+ this.createSemanticAlignment(result.a, result.b).then(() => {
2241
+ this.isAligning = false;
2242
+ this.generateSimilarityMatrix();
2243
+ });
2244
+ });
2245
+ });
2246
+ }
2247
+ async createSemanticAlignment(person1Results, person2Results) {
2248
+ this.alignedPerson1Interests = person1Results.map((item) => item.item);
2249
+ this.alignedPerson2Interests = person2Results.map((item) => item.item);
2250
+ const maxLength = Math.max(this.alignedPerson1Interests.length, this.alignedPerson2Interests.length);
2251
+ while (this.alignedPerson1Interests.length < maxLength)
2252
+ this.alignedPerson1Interests.push(ProfileComparisonLibComponent.SPACER_SMALL);
2253
+ while (this.alignedPerson2Interests.length < maxLength)
2254
+ this.alignedPerson2Interests.push(ProfileComparisonLibComponent.SPACER_SMALL);
2255
+ }
2256
+ async createEmbeddingBasedAlignment(person1Results, person2Results) {
2257
+ try {
2258
+ const baseLeft = this.person1Interests.filter(i => !!i && i.trim() !== ProfileComparisonLibComponent.SPACER_SMALL && i.trim().length > 0 && !this.centerItem.includes(i));
2259
+ const baseRight = this.person2Interests.filter(i => !!i && i.trim() !== ProfileComparisonLibComponent.SPACER_SMALL && i.trim().length > 0 && !this.centerItem.includes(i));
2260
+ this.openaiEmbeddingService
2261
+ .getAlignedLists(baseLeft, baseRight, this.getActiveOpenAIKey())
2262
+ .subscribe({
2263
+ next: (result) => {
2264
+ if (result) {
2265
+ let listA = result.listA;
2266
+ let listB = result.listB;
2267
+ const minLength = Math.min(listA.length, listB.length);
2268
+ for (let i = 0; i < minLength; i++) {
2269
+ const itemA = listA[i];
2270
+ const itemB = listB[i];
2271
+ if (!itemA || !itemB || itemA === ProfileComparisonLibComponent.SPACER_SMALL || itemA === ProfileComparisonLibComponent.SPACER_LARGE || itemB === ProfileComparisonLibComponent.SPACER_SMALL || itemB === ProfileComparisonLibComponent.SPACER_LARGE)
2272
+ continue;
2273
+ const normA = itemA.trim().toLowerCase();
2274
+ const normB = itemB.trim().toLowerCase();
2275
+ let isMatch = normA === normB;
2276
+ if (!isMatch && (normA.includes(ProfileComparisonLibComponent.CONSOLIDATION_SEPARATOR_X) || normA.includes(ProfileComparisonLibComponent.CONSOLIDATION_SEPARATOR_SLASH)) && (normB.includes(ProfileComparisonLibComponent.CONSOLIDATION_SEPARATOR_X) || normB.includes(ProfileComparisonLibComponent.CONSOLIDATION_SEPARATOR_SLASH))) {
2277
+ const separatorRegex = new RegExp(`[${ProfileComparisonLibComponent.CONSOLIDATION_SEPARATOR_X}${ProfileComparisonLibComponent.CONSOLIDATION_SEPARATOR_SLASH}]`);
2278
+ const partsA = normA.split(separatorRegex).map(p => p.trim()).sort();
2279
+ const partsB = normB.split(separatorRegex).map(p => p.trim()).sort();
2280
+ if (partsA.length === partsB.length && partsA.every((val, index) => val === partsB[index]))
2281
+ isMatch = true;
2282
+ }
2283
+ if (isMatch) {
2284
+ const existing = this.centerItem.find(item => item.trim().toLowerCase() === normA);
2285
+ if (!existing)
2286
+ this.centerItem.push(itemA);
2287
+ listA[i] = ProfileComparisonLibComponent.SPACER_SMALL;
2288
+ listB[i] = ProfileComparisonLibComponent.SPACER_SMALL;
2289
+ }
2290
+ }
2291
+ const cleanListA = [];
2292
+ const cleanListB = [];
2293
+ for (let i = 0; i < listA.length; i++) {
2294
+ const itemA = listA[i];
2295
+ const itemB = listB[i];
2296
+ const isSpacerRow = (!itemA || itemA === ProfileComparisonLibComponent.SPACER_SMALL || itemA === ProfileComparisonLibComponent.SPACER_LARGE) && (!itemB || itemB === ProfileComparisonLibComponent.SPACER_SMALL || itemB === ProfileComparisonLibComponent.SPACER_LARGE);
2297
+ if (isSpacerRow) {
2298
+ const lastIndex = cleanListA.length - 1;
2299
+ if (lastIndex >= 0) {
2300
+ const lastA = cleanListA[lastIndex];
2301
+ const lastB = cleanListB[lastIndex];
2302
+ const lastWasSpacer = (!lastA || lastA === ProfileComparisonLibComponent.SPACER_SMALL || lastA === ProfileComparisonLibComponent.SPACER_LARGE) && (!lastB || lastB === ProfileComparisonLibComponent.SPACER_SMALL || lastB === ProfileComparisonLibComponent.SPACER_LARGE);
2303
+ if (lastWasSpacer) {
2304
+ if (itemA === ProfileComparisonLibComponent.SPACER_LARGE || itemB === ProfileComparisonLibComponent.SPACER_LARGE || lastA === ProfileComparisonLibComponent.SPACER_LARGE || lastB === ProfileComparisonLibComponent.SPACER_LARGE) {
2305
+ cleanListA[lastIndex] = ProfileComparisonLibComponent.SPACER_LARGE;
2306
+ cleanListB[lastIndex] = ProfileComparisonLibComponent.SPACER_LARGE;
2307
+ }
2308
+ continue;
2309
+ }
2310
+ }
2311
+ }
2312
+ cleanListA.push(itemA);
2313
+ cleanListB.push(itemB);
2314
+ }
2315
+ if (cleanListA.length > 0) {
2316
+ const firstA = cleanListA[0];
2317
+ const firstB = cleanListB[0];
2318
+ if ((!firstA || firstA === ProfileComparisonLibComponent.SPACER_SMALL || firstA === ProfileComparisonLibComponent.SPACER_LARGE) && (!firstB || firstB === ProfileComparisonLibComponent.SPACER_SMALL || firstB === ProfileComparisonLibComponent.SPACER_LARGE)) {
2319
+ cleanListA.shift();
2320
+ cleanListB.shift();
2321
+ }
2322
+ }
2323
+ if (cleanListA.length > 0) {
2324
+ const lastIndex = cleanListA.length - 1;
2325
+ const lastA = cleanListA[lastIndex];
2326
+ const lastB = cleanListB[lastIndex];
2327
+ if ((!lastA || lastA === ProfileComparisonLibComponent.SPACER_SMALL || lastA === ProfileComparisonLibComponent.SPACER_LARGE) && (!lastB || lastB === ProfileComparisonLibComponent.SPACER_SMALL || lastB === ProfileComparisonLibComponent.SPACER_LARGE)) {
2328
+ cleanListA.pop();
2329
+ cleanListB.pop();
2330
+ }
2331
+ }
2332
+ this.alignedPerson1Interests = cleanListA;
2333
+ this.alignedPerson2Interests = cleanListB;
2334
+ }
2335
+ else {
2336
+ this.createSemanticAlignment(person1Results, person2Results);
2337
+ }
2338
+ },
2339
+ error: (error) => {
2340
+ this.createSemanticAlignment(person1Results, person2Results);
2341
+ }
2342
+ });
2343
+ }
2344
+ catch (error) {
2345
+ // Catch block for baseLeft/baseRight filtering errors
2346
+ this.createSemanticAlignment(person1Results, person2Results);
2347
+ }
2348
+ }
2349
+ sortByMaxSimilarity(personList, otherLists) {
2350
+ const requestEpoch = this.activeRequestEpoch;
2351
+ const others = otherLists.flat().filter(o => !!o && o.trim() !== ProfileComparisonLibComponent.SPACER_SMALL && o.trim().length > 0);
2352
+ const validPersonList = personList.filter(i => !!i && i.trim() !== ProfileComparisonLibComponent.SPACER_SMALL && i.trim().length > 0);
2353
+ const CONCURRENCY = 4;
2354
+ return from(validPersonList).pipe(concatMap((item) => from(others).pipe(mergeMap((o) => this.profileService.compareInterests(item, o, this.getActiveApiKey()).pipe(map$1((res) => res.similarity), catchError$1((error) => {
2355
+ if (requestEpoch === this.activeRequestEpoch)
2356
+ this.handleApiQuotaError(error);
2357
+ return of(0);
2358
+ })), CONCURRENCY), toArray(), map$1((scores) => {
2359
+ const maxScore = scores.length ? Math.max(...scores) : 0;
2360
+ const itemNorm = item.trim().toLowerCase();
2361
+ const hasExact = others.some(o => o && o.trim().toLowerCase() === itemNorm);
2362
+ if (hasExact) {
2363
+ const existing = this.centerItem.find(i => i.trim().toLowerCase() === itemNorm);
2364
+ if (!existing)
2365
+ this.centerItem.push(item);
2366
+ }
2367
+ return { item, score: maxScore };
2368
+ }))), toArray(), map$1((results) => results.sort((a, b) => b.score - a.score)));
2369
+ }
2370
+ onViewProfile() {
2371
+ alert('You have been routed to a profile page ');
2372
+ }
2373
+ getActiveApiKey() {
2374
+ return this.customApiKey || this.apiNinjasKey;
2375
+ }
2376
+ updateApiKey() {
2377
+ if (this.apiKeyInputValue.trim()) {
2378
+ this.customApiKey = this.apiKeyInputValue.trim();
2379
+ this.saveApiKeyToStorage(this.customApiKey);
2380
+ this.showApiKeyModal = false;
2381
+ this.quotaExhausted = false;
2382
+ this.activeRequestEpoch++;
2383
+ this.compute();
2384
+ this.initializeFaceDetection();
2385
+ }
2386
+ }
2387
+ closeApiKeyModal() {
2388
+ this.showApiKeyModal = false;
2389
+ }
2390
+ handleApiQuotaError(error) {
2391
+ if (error?.status === 400 || error?.status === 429 || error?.status === 403 || error?.error?.message?.toLowerCase().includes('quota')) {
2392
+ this.quotaExhausted = true;
2393
+ this.showApiKeyModal = true;
2394
+ }
2395
+ }
2396
+ saveApiKeyToStorage(apiKey) {
2397
+ try {
2398
+ localStorage.setItem('apiNinjasCustomKey', apiKey);
2399
+ }
2400
+ catch { }
2401
+ }
2402
+ loadApiKeyFromStorage() {
2403
+ try {
2404
+ const savedKey = localStorage.getItem('apiNinjasCustomKey');
2405
+ if (savedKey) {
2406
+ this.customApiKey = savedKey;
2407
+ this.apiKeyInputValue = savedKey;
2408
+ }
2409
+ }
2410
+ catch { }
2411
+ }
2412
+ onFileInputChange(event) {
2413
+ const files = event.target.files;
2414
+ this.selectedFile = files && files.length > 0 ? files[0] : null;
2415
+ }
2416
+ compressConfigImagesIfNeeded() {
2417
+ const tasks = [];
2418
+ if (this.user1Image) {
2419
+ tasks.push(this.compressImageStringToDataUrl(this.user1Image, 'ConfigUser1').pipe(tap((dataUrl) => { if (dataUrl)
2420
+ this.user1Image = dataUrl; }), map$1(() => void 0)));
2421
+ }
2422
+ if (this.user2Image) {
2423
+ tasks.push(this.compressImageStringToDataUrl(this.user2Image, 'ConfigUser2').pipe(tap((dataUrl) => { if (dataUrl)
2424
+ this.user2Image = dataUrl; }), map$1(() => void 0)));
2425
+ }
2426
+ return tasks.length > 0 ? forkJoin(tasks).pipe(map$1(() => void 0)) : of(void 0);
2427
+ }
2428
+ compressImageStringToDataUrl(imageStr, baseName) {
2429
+ return this.fileConversionService.getFileForImageString(imageStr, `${baseName}.jpg`).pipe(switchMap$1(file => this.imageCompressionService.compressImageFile(file, this.compressionConfig)), map$1(res => res.dataUrl), catchError$1(() => of(null)));
2430
+ }
2431
+ loadCustomConfig() {
2432
+ try {
2433
+ const raw = localStorage.getItem('customProfileConfig');
2434
+ if (!raw) {
2435
+ this.config = this.baseConfig || this.config;
2436
+ return;
2437
+ }
2438
+ const parsed = JSON.parse(raw);
2439
+ this.config = this.buildConfigWithDefaults(parsed, this.baseConfig);
2440
+ }
2441
+ catch {
2442
+ this.config = this.baseConfig || this.config;
2443
+ }
2444
+ }
2445
+ getEyeCoordinatesFromBBox(bbox) {
2446
+ const eyeY = bbox.y + bbox.height * 0.38;
2447
+ const leftEyeX = bbox.x + bbox.width * 0.32;
2448
+ const rightEyeX = bbox.x + bbox.width * 0.68;
2449
+ return { leftEye: { x: leftEyeX, y: eyeY }, rightEye: { x: rightEyeX, y: eyeY } };
2450
+ }
2451
+ getEyeCoordinatesFromFace(face) {
2452
+ const lm = (face && face.landmarks) || undefined;
2453
+ const pick = (candidates) => {
2454
+ if (!lm)
2455
+ return undefined;
2456
+ for (const key of candidates) {
2457
+ const pt = lm[key];
2458
+ if (pt && typeof pt.x === 'number' && typeof pt.y === 'number')
2459
+ return this.clampPointToFaceRect({ x: pt.x, y: pt.y }, face);
2460
+ }
2461
+ return undefined;
2462
+ };
2463
+ const left = pick(['left_eye_pupil', 'left_eye_center', 'eye_left_pupil', 'left_eye']) || this.getEyeCoordinatesFromBBox(face).leftEye;
2464
+ const right = pick(['right_eye_pupil', 'right_eye_center', 'eye_right_pupil', 'right_eye']) || this.getEyeCoordinatesFromBBox(face).rightEye;
2465
+ return { leftEye: left, rightEye: right };
2466
+ }
2467
+ getNoseCoordinateFromFace(face) {
2468
+ const lm = (face && face.landmarks) || undefined;
2469
+ const pick = (candidates) => {
2470
+ if (!lm)
2471
+ return undefined;
2472
+ for (const key of candidates) {
2473
+ const pt = lm[key];
2474
+ if (pt && typeof pt.x === 'number' && typeof pt.y === 'number')
2475
+ return this.clampPointToFaceRect({ x: pt.x, y: pt.y }, face);
2476
+ }
2477
+ return undefined;
2478
+ };
2479
+ const nose = pick(['nose_tip', 'nose', 'nose_middle', 'nose_bridge1']);
2480
+ if (nose)
2481
+ return nose;
2482
+ return this.getNoseCoordinateFromBBox(face);
2483
+ }
2484
+ clampPointToFaceRect(p, box) {
2485
+ const x = Math.max(box.x, Math.min(box.x + box.width, p.x));
2486
+ const y = Math.max(box.y, Math.min(box.y + box.height, p.y));
2487
+ return { x, y };
2488
+ }
2489
+ getNoseCoordinateFromBBox(bbox) {
2490
+ return { x: bbox.x + bbox.width / 2, y: bbox.y + bbox.height * 0.58 };
2491
+ }
2492
+ getFaceCoordinatesFromBBox(bbox) {
2493
+ return {
2494
+ faceCenter: { x: bbox.x + bbox.width / 2, y: bbox.y + bbox.height / 2 },
2495
+ faceTop: { x: bbox.x + bbox.width / 2, y: bbox.y + bbox.height / 4 },
2496
+ faceBottom: { x: bbox.x + bbox.width / 2, y: bbox.y + (3 * bbox.height) / 4 },
2497
+ faceLeft: { x: bbox.x + bbox.width / 4, y: bbox.y + bbox.height / 2 },
2498
+ faceRight: { x: bbox.x + (3 * bbox.width) / 4, y: bbox.y + bbox.height / 2 },
2499
+ };
2500
+ }
2501
+ initializeFaceDetection() {
2502
+ if (this.detectFaceSub1)
2503
+ this.detectFaceSub1.unsubscribe();
2504
+ if (this.detectFaceSub2)
2505
+ this.detectFaceSub2.unsubscribe();
2506
+ let user1FaceData = null;
2507
+ let user2FaceData = null;
2508
+ const shouldDelayUser2 = !!this.user1Image && !!this.user2Image;
2509
+ const FACEPP_RATE_LIMIT_DELAY_MS = ProfileComparisonLibComponent.FACEPP_RATE_LIMIT_DELAY_MS;
2510
+ if (this.user1Image) {
2511
+ this.fileConversionService.getFileForImageString(this.user1Image, ProfileComparisonLibComponent.DEFAULT_PROFILE_IMAGE_NAME).subscribe((file) => {
2512
+ this.detectFaceSub1 = this.profileService.detectFace(file, { apiKey: this.getActiveFaceppKey(), apiSecret: this.getActiveFaceppSecret() }).subscribe({
2513
+ next: (res) => {
2514
+ this.firstImageData = res;
2515
+ user1FaceData = res;
2516
+ try {
2517
+ const faces = res?.faces || [];
2518
+ this.user1FaceRaw = Array.isArray(faces) && faces.length > 0 ? faces[0] : null;
2519
+ this.updateObjectPositionForFace(1);
2520
+ }
2521
+ catch { }
2522
+ if (user1FaceData && user2FaceData)
2523
+ this.calculateFaceAlignment(user1FaceData, user2FaceData);
2524
+ },
2525
+ error: (err) => this.handleFaceppError(err),
2526
+ });
2527
+ });
2528
+ }
2529
+ if (this.user2Image) {
2530
+ this.fileConversionService.getFileForImageString(this.user2Image, 'ProfilePg2.png').subscribe((file) => {
2531
+ const startUser2Detect = () => {
2532
+ this.detectFaceSub2 = this.profileService.detectFace(file, { apiKey: this.getActiveFaceppKey(), apiSecret: this.getActiveFaceppSecret() }).subscribe({
2533
+ next: (res) => {
2534
+ this.secondImageData = res;
2535
+ user2FaceData = res;
2536
+ try {
2537
+ const faces = res?.faces || [];
2538
+ this.user2FaceRaw = Array.isArray(faces) && faces.length > 0 ? faces[0] : null;
2539
+ this.updateObjectPositionForFace(2);
2540
+ }
2541
+ catch { }
2542
+ if (user1FaceData && user2FaceData)
2543
+ this.calculateFaceAlignment(user1FaceData, user2FaceData);
2544
+ },
2545
+ error: (err) => this.handleFaceppError(err),
2546
+ });
2547
+ };
2548
+ if (shouldDelayUser2)
2549
+ setTimeout(startUser2Detect, FACEPP_RATE_LIMIT_DELAY_MS);
2550
+ else
2551
+ startUser2Detect();
2552
+ });
2553
+ }
2554
+ }
2555
+ calculateFaceAlignment(user1Data, user2Data) {
2556
+ let faces1 = [];
2557
+ let faces2 = [];
2558
+ const getFaces = (data) => {
2559
+ if (Array.isArray(data))
2560
+ return data;
2561
+ if (data && typeof data === 'object' && 'faces' in data && Array.isArray(data.faces))
2562
+ return data.faces;
2563
+ return [];
2564
+ };
2565
+ faces1 = getFaces(user1Data);
2566
+ faces2 = getFaces(user2Data);
2567
+ if (!faces1?.length || !faces2?.length) {
2568
+ this.applyDefaultAlignment();
2569
+ return;
2570
+ }
2571
+ const face1 = faces1[0];
2572
+ const face2 = faces2[0];
2573
+ if (!this.isValidFaceData(face1) || !this.isValidFaceData(face2)) {
2574
+ this.applyDefaultAlignment();
2575
+ return;
2576
+ }
2577
+ const user1FaceCoords = this.getFaceCoordinatesFromBBox(face1);
2578
+ const user2FaceCoords = this.getFaceCoordinatesFromBBox(face2);
2579
+ const user1Eyes = this.getEyeCoordinatesFromFace(face1);
2580
+ const user2Eyes = this.getEyeCoordinatesFromFace(face2);
2581
+ const leftContainerEl = document.querySelector('.profile-img.left');
2582
+ const rightContainerEl = document.querySelector('.profile-img.right');
2583
+ const containerWidth1 = leftContainerEl?.clientWidth || 160;
2584
+ const containerHeight1 = leftContainerEl?.clientHeight || 550;
2585
+ const containerWidth2 = rightContainerEl?.clientWidth || 160;
2586
+ const containerHeight2 = rightContainerEl?.clientHeight || 550;
2587
+ const scale1 = this.user1NaturalWidth ? Math.max(containerWidth1 / this.user1NaturalWidth, containerHeight1 / this.user1NaturalHeight) : 1;
2588
+ const scale2 = this.user2NaturalWidth ? Math.max(containerWidth2 / this.user2NaturalWidth, containerHeight2 / this.user2NaturalHeight) : 1;
2589
+ const displayedFaceHeight1 = face1.height * scale1;
2590
+ const displayedFaceHeight2 = face2.height * scale2;
2591
+ const ratio1 = containerHeight1 ? displayedFaceHeight1 / containerHeight1 : 0;
2592
+ const ratio2 = containerHeight2 ? displayedFaceHeight2 / containerHeight2 : 0;
2593
+ let finalScale1 = ratio1 > 0 && ratio1 < 0.22 ? Math.min(3, 0.32 / ratio1) : 1;
2594
+ let finalScale2 = ratio2 > 0 && ratio2 < 0.22 ? Math.min(3, 0.32 / ratio2) : 1;
2595
+ const effRatio1 = ratio1 * finalScale1;
2596
+ const effRatio2 = ratio2 * finalScale2;
2597
+ if (effRatio1 > effRatio2 * 1.35)
2598
+ finalScale1 = Math.max(0.95, finalScale1 * (effRatio2 / effRatio1));
2599
+ else if (effRatio2 > effRatio1 * 1.35)
2600
+ finalScale2 = Math.max(0.95, finalScale2 * (effRatio1 / effRatio2));
2601
+ const nose1 = this.getNoseCoordinateFromFace(face1);
2602
+ const nose2 = this.getNoseCoordinateFromFace(face2);
2603
+ const anchor1Y = (Math.min(user1Eyes.leftEye.y, user1Eyes.rightEye.y) + nose1.y) / 2;
2604
+ const anchor2Y = (Math.min(user2Eyes.leftEye.y, user2Eyes.rightEye.y) + nose2.y) / 2;
2605
+ const tCenter1 = this.user1NaturalHeight ? -((anchor1Y - this.user1NaturalHeight / 2) * scale1 * finalScale1) : 0;
2606
+ const tCenter2 = this.user2NaturalHeight ? -((anchor2Y - this.user2NaturalHeight / 2) * scale2 * finalScale2) : 0;
2607
+ this.user1Transform = `translateY(${tCenter1 - this.eyeAlignmentBiasPx}px) scale(${finalScale1})`;
2608
+ this.user2Transform = `translateY(${tCenter2 - this.eyeAlignmentBiasPx}px) scale(${finalScale2})`;
2609
+ this.alignmentCalculated = true;
2610
+ }
2611
+ applyDefaultAlignment() {
2612
+ this.user1Transform = 'translateY(0px)';
2613
+ this.user2Transform = 'translateY(0px)';
2614
+ this.alignmentCalculated = true;
2615
+ }
2616
+ isValidFaceData(face) {
2617
+ return !!face && typeof face.x === 'number' && face.width > 0 && face.height > 0;
2618
+ }
2619
+ getActiveFaceppKey() { return this.customFaceppKey || this.faceplusKey; }
2620
+ getActiveFaceppSecret() { return this.customFaceppSecret || this.faceplusSecret; }
2621
+ updateFaceppCredentials() {
2622
+ const newKey = (this.faceppKeyInputValue || '').trim();
2623
+ const newSecret = (this.faceppSecretInputValue || '').trim();
2624
+ if (!newKey || !newSecret)
2625
+ return;
2626
+ if (this.detectFaceSub1)
2627
+ this.detectFaceSub1.unsubscribe();
2628
+ if (this.detectFaceSub2)
2629
+ this.detectFaceSub2.unsubscribe();
2630
+ this.customFaceppKey = newKey;
2631
+ this.customFaceppSecret = newSecret;
2632
+ this.saveFaceppCredsToStorage(newKey, newSecret);
2633
+ this.showFaceppKeyModal = false;
2634
+ this.faceppQuotaExhausted = false;
2635
+ this.activeRequestEpoch++;
2636
+ this.initializeFaceDetection();
2637
+ }
2638
+ closeFaceppKeyModal() { this.showFaceppKeyModal = false; }
2639
+ handleFaceppError(error) {
2640
+ this.faceppQuotaExhausted = true;
2641
+ this.showFaceppKeyModal = true;
2642
+ }
2643
+ saveFaceppCredsToStorage(apiKey, apiSecret) {
2644
+ try {
2645
+ localStorage.setItem('faceppApiKey', apiKey);
2646
+ localStorage.setItem('faceppApiSecret', apiSecret);
2647
+ }
2648
+ catch { }
2649
+ }
2650
+ loadFaceppCredsFromStorage() {
2651
+ try {
2652
+ const key = localStorage.getItem('faceppApiKey');
2653
+ const secret = localStorage.getItem('faceppApiSecret');
2654
+ if (key) {
2655
+ this.customFaceppKey = key;
2656
+ this.faceppKeyInputValue = key;
2657
+ }
2658
+ if (secret) {
2659
+ this.customFaceppSecret = secret;
2660
+ this.faceppSecretInputValue = secret;
2661
+ }
2662
+ }
2663
+ catch { }
2664
+ }
2665
+ getActiveOpenAIKey() { return this.customOpenAIKey || this.openaiApiKey; }
2666
+ updateOpenAIKey() {
2667
+ if (this.openaiKeyInputValue.trim()) {
2668
+ this.customOpenAIKey = this.openaiKeyInputValue.trim();
2669
+ this.saveOpenAIKeyToStorage(this.customOpenAIKey);
2670
+ this.showOpenAIKeyModal = false;
2671
+ this.openaiQuotaExhausted = false;
2672
+ this.openaiEmbeddingService.clearCache();
2673
+ this.compute();
2674
+ }
2675
+ }
2676
+ closeOpenAIKeyModal() { this.showOpenAIKeyModal = false; }
2677
+ saveOpenAIKeyToStorage(apiKey) {
2678
+ try {
2679
+ localStorage.setItem('openaiCustomKey', apiKey);
2680
+ }
2681
+ catch { }
2682
+ }
2683
+ loadOpenAIKeyFromStorage() {
2684
+ try {
2685
+ const savedKey = localStorage.getItem('openaiCustomKey');
2686
+ if (savedKey) {
2687
+ this.customOpenAIKey = savedKey;
2688
+ this.openaiKeyInputValue = savedKey;
2689
+ }
2690
+ }
2691
+ catch { }
2692
+ }
2693
+ async generateSimilarityMatrix() {
2694
+ const allInterests = Array.from(new Set([...this.person1Interests, ...this.person2Interests])).filter(i => i && i.trim() && i.trim() !== '-');
2695
+ if (!allInterests.length) {
2696
+ this.matrixData = null;
2697
+ return;
2698
+ }
2699
+ try {
2700
+ const apiKey = this.getActiveOpenAIKey();
2701
+ if (!apiKey)
2702
+ return;
2703
+ this.openaiEmbeddingService.getEmbeddings(allInterests, apiKey).subscribe({
2704
+ next: (embeddings) => {
2705
+ if (!embeddings?.length)
2706
+ return;
2707
+ const abbreviations = {};
2708
+ allInterests.forEach(interest => {
2709
+ const words = interest.split(/[\s/-]+/);
2710
+ let abbr = words.length === 1 ? words[0].substring(0, 3) : words.map(w => w[0]).join('').substring(0, 3);
2711
+ let finalAbbr = abbr;
2712
+ let counter = 1;
2713
+ while (Object.values(abbreviations).includes(finalAbbr))
2714
+ finalAbbr = abbr + counter++;
2715
+ abbreviations[interest] = finalAbbr;
2716
+ });
2717
+ const legend = Object.entries(abbreviations).map(([full, abbr]) => ({ abbr, full }));
2718
+ const headers = allInterests.map(i => abbreviations[i]);
2719
+ const rows = allInterests.map((interest, i) => ({
2720
+ label: abbreviations[interest],
2721
+ values: allInterests.map((_, j) => this.cosineSimilarity(embeddings[i], embeddings[j]).toFixed(2))
2722
+ }));
2723
+ this.matrixData = { legend, headers, rows };
2724
+ this.matrixDataChange.emit(this.matrixData);
2725
+ }
2726
+ });
2727
+ }
2728
+ catch { }
2729
+ }
2730
+ cosineSimilarity(a, b) {
2731
+ let dot = 0, na = 0, nb = 0;
2732
+ for (let i = 0; i < a.length; i++) {
2733
+ dot += a[i] * b[i];
2734
+ na += a[i] * a[i];
2735
+ nb += b[i] * b[i];
2736
+ }
2737
+ return (na === 0 || nb === 0) ? 0 : dot / (Math.sqrt(na) * Math.sqrt(nb));
2738
+ }
2739
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ProfileComparisonLibComponent, deps: [{ token: ProfileService }, { token: OpenAIEmbeddingService }, { token: i0.Renderer2 }, { token: FileConversionService }, { token: ImageCompressionService }], target: i0.ɵɵFactoryTarget.Component });
2740
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "20.3.16", type: ProfileComparisonLibComponent, isStandalone: false, selector: "lib-profile-comparison", inputs: { config: "config", apiNinjasKey: "apiNinjasKey", faceplusKey: "faceplusKey", faceplusSecret: "faceplusSecret", openaiApiKey: "openaiApiKey", fadeAllEdges: "fadeAllEdges" }, outputs: { matrixDataChange: "matrixDataChange" }, viewQueries: [{ propertyName: "leftContainer", first: true, predicate: ["leftContainer"], descendants: true }, { propertyName: "rightContainer", first: true, predicate: ["rightContainer"], descendants: true }], usesOnChanges: true, ngImport: i0, template: "<!-- API Quota Exhausted Modal (Independent) -->\r\n<div class=\"api-key-modal-overlay\" *ngIf=\"showApiKeyModal\">\r\n <div class=\"api-key-modal\" (click)=\"$event.stopPropagation()\">\r\n <div class=\"api-key-modal-header\">\r\n <div class=\"warning-icon\">\r\n <svg\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"48\"\r\n height=\"48\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"currentColor\"\r\n stroke-width=\"2\"\r\n stroke-linecap=\"round\"\r\n stroke-linejoin=\"round\"\r\n >\r\n <path\r\n d=\"M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z\"\r\n ></path>\r\n <line x1=\"12\" y1=\"9\" x2=\"12\" y2=\"13\"></line>\r\n <line x1=\"12\" y1=\"17\" x2=\"12.01\" y2=\"17\"></line>\r\n </svg>\r\n </div>\r\n <h2>API Quota Limit Exceeded</h2>\r\n <button class=\"close-btn\" (click)=\"closeApiKeyModal()\" aria-label=\"Close\">\r\n <svg\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"24\"\r\n height=\"24\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"currentColor\"\r\n stroke-width=\"2\"\r\n stroke-linecap=\"round\"\r\n stroke-linejoin=\"round\"\r\n >\r\n <line x1=\"18\" y1=\"6\" x2=\"6\" y2=\"18\"></line>\r\n <line x1=\"6\" y1=\"6\" x2=\"18\" y2=\"18\"></line>\r\n </svg>\r\n </button>\r\n </div>\r\n <!-- Face++ modal moved outside to top-level sibling overlay -->\r\n\r\n <div class=\"api-key-modal-body\">\r\n <p class=\"modal-message\">\r\n The default API Ninjas key has reached its free monthly quota limit of\r\n <strong>3,000 API calls</strong>.\r\n </p>\r\n <p class=\"modal-instruction\">\r\n Please enter your own API Ninjas key below to continue using the profile\r\n comparison features:\r\n </p>\r\n\r\n <div class=\"api-key-input-group\">\r\n <input\r\n type=\"text\"\r\n [(ngModel)]=\"apiKeyInputValue\"\r\n placeholder=\"Paste your API Ninjas key here\"\r\n class=\"api-key-input\"\r\n autofocus\r\n />\r\n <button class=\"api-key-load-btn\" (click)=\"updateApiKey()\">\r\n Apply Key\r\n </button>\r\n </div>\r\n\r\n <div class=\"modal-hint\">\r\n <p>\r\n <svg\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"16\"\r\n height=\"16\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"currentColor\"\r\n stroke-width=\"2\"\r\n stroke-linecap=\"round\"\r\n stroke-linejoin=\"round\"\r\n >\r\n <circle cx=\"12\" cy=\"12\" r=\"10\"></circle>\r\n <line x1=\"12\" y1=\"16\" x2=\"12\" y2=\"12\"></line>\r\n <line x1=\"12\" y1=\"8\" x2=\"12.01\" y2=\"8\"></line>\r\n </svg>\r\n Don't have an API key? Get your free key at\r\n <a href=\"https://api-ninjas.com/\" target=\"_blank\" rel=\"noopener\"\r\n >api-ninjas.com</a\r\n >\r\n </p>\r\n </div>\r\n </div>\r\n </div>\r\n</div>\r\n\r\n<!-- Face++ API Credentials Modal (Independent) -->\r\n<div class=\"api-key-modal-overlay facepp-overlay\" *ngIf=\"showFaceppKeyModal\">\r\n <div class=\"api-key-modal\" (click)=\"$event.stopPropagation()\">\r\n <div class=\"api-key-modal-header\">\r\n <div class=\"warning-icon\">\r\n <svg\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"48\"\r\n height=\"48\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"currentColor\"\r\n stroke-width=\"2\"\r\n stroke-linecap=\"round\"\r\n stroke-linejoin=\"round\"\r\n >\r\n <path\r\n d=\"M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z\"\r\n ></path>\r\n <line x1=\"12\" y1=\"9\" x2=\"12\" y2=\"13\"></line>\r\n <line x1=\"12\" y1=\"17\" x2=\"12.01\" y2=\"17\"></line>\r\n </svg>\r\n </div>\r\n <h2>Face++ Credentials Required</h2>\r\n <button\r\n class=\"close-btn\"\r\n (click)=\"closeFaceppKeyModal()\"\r\n aria-label=\"Close\"\r\n >\r\n <svg\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"24\"\r\n height=\"24\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"currentColor\"\r\n stroke-width=\"2\"\r\n stroke-linecap=\"round\"\r\n stroke-linejoin=\"round\"\r\n >\r\n <line x1=\"18\" y1=\"6\" x2=\"6\" y2=\"18\"></line>\r\n <line x1=\"6\" y1=\"6\" x2=\"18\" y2=\"18\"></line>\r\n </svg>\r\n </button>\r\n </div>\r\n\r\n <div class=\"api-key-modal-body\">\r\n <p class=\"modal-message\">\r\n The Face++ API returned an authorization or rate-limit error. Please\r\n enter your Face++ API Key and API Secret.\r\n </p>\r\n\r\n <div\r\n class=\"api-key-input-group\"\r\n style=\"flex-direction: column; align-items: stretch; gap: 10px\"\r\n >\r\n <input\r\n type=\"text\"\r\n [(ngModel)]=\"faceppKeyInputValue\"\r\n placeholder=\"Face++ API Key\"\r\n class=\"api-key-input\"\r\n />\r\n <input\r\n type=\"text\"\r\n [(ngModel)]=\"faceppSecretInputValue\"\r\n placeholder=\"Face++ API Secret\"\r\n class=\"api-key-input\"\r\n />\r\n <button class=\"api-key-load-btn\" (click)=\"updateFaceppCredentials()\">\r\n Apply Face++ Credentials\r\n </button>\r\n </div>\r\n </div>\r\n </div>\r\n</div>\r\n\r\n<!-- OpenAI API Key Modal (Independent) -->\r\n<div class=\"api-key-modal-overlay openai-overlay\" *ngIf=\"showOpenAIKeyModal\">\r\n <div class=\"api-key-modal\" (click)=\"$event.stopPropagation()\">\r\n <div class=\"api-key-modal-header\">\r\n <div class=\"warning-icon\">\r\n <svg\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"48\"\r\n height=\"48\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"currentColor\"\r\n stroke-width=\"2\"\r\n stroke-linecap=\"round\"\r\n stroke-linejoin=\"round\"\r\n >\r\n <path\r\n d=\"M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z\"\r\n ></path>\r\n <line x1=\"12\" y1=\"9\" x2=\"12\" y2=\"13\"></line>\r\n <line x1=\"12\" y1=\"17\" x2=\"12.01\" y2=\"17\"></line>\r\n </svg>\r\n </div>\r\n <h2>OpenAI API Key Required</h2>\r\n <button\r\n class=\"close-btn\"\r\n (click)=\"closeOpenAIKeyModal()\"\r\n aria-label=\"Close\"\r\n >\r\n <svg\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"24\"\r\n height=\"24\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"currentColor\"\r\n stroke-width=\"2\"\r\n stroke-linecap=\"round\"\r\n stroke-linejoin=\"round\"\r\n >\r\n <line x1=\"18\" y1=\"6\" x2=\"6\" y2=\"18\"></line>\r\n <line x1=\"6\" y1=\"6\" x2=\"18\" y2=\"18\"></line>\r\n </svg>\r\n </button>\r\n </div>\r\n\r\n <div class=\"api-key-modal-body\">\r\n <p class=\"modal-message\">\r\n The OpenAI API returned an authorization or rate-limit error. Please\r\n enter your OpenAI API key to continue using the text similarity features.\r\n </p>\r\n <p class=\"modal-instruction\">\r\n Text matching now uses OpenAI embeddings (text-embedding-3-small) for improved semantic similarity.\r\n </p>\r\n\r\n <div class=\"api-key-input-group\">\r\n <input\r\n type=\"text\"\r\n [(ngModel)]=\"openaiKeyInputValue\"\r\n placeholder=\"Paste your OpenAI API key here\"\r\n class=\"api-key-input\"\r\n autofocus\r\n />\r\n <button class=\"api-key-load-btn\" (click)=\"updateOpenAIKey()\">\r\n Apply OpenAI Key\r\n </button>\r\n </div>\r\n\r\n <div class=\"modal-hint\">\r\n <p>\r\n <svg\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"16\"\r\n height=\"16\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"currentColor\"\r\n stroke-width=\"2\"\r\n stroke-linecap=\"round\"\r\n stroke-linejoin=\"round\"\r\n >\r\n <circle cx=\"12\" cy=\"12\" r=\"10\"></circle>\r\n <line x1=\"12\" y1=\"16\" x2=\"12\" y2=\"12\"></line>\r\n <line x1=\"12\" y1=\"8\" x2=\"12.01\" y2=\"8\"></line>\r\n </svg>\r\n Don't have an API key? Get your free key at\r\n <a href=\"https://platform.openai.com/api-keys\" target=\"_blank\" rel=\"noopener\"\r\n >platform.openai.com/api-keys</a\r\n >\r\n </p>\r\n </div>\r\n </div>\r\n </div>\r\n</div>\r\n\r\n<div class=\"blank-box\"></div>\r\n\r\n<div class=\"profile-screen\">\r\n <div class=\"profile-flex\">\r\n <div class=\"profile-img left\" [class.fade-all]=\"fadeAllEdges\">\r\n <img\r\n [src]=\"user1Image\"\r\n alt=\"User 1\"\r\n [style.transform]=\"user1Transform\"\r\n [style.object-position]=\"user1ObjectPosition\"\r\n (load)=\"onUserImageLoad(1, $event)\"\r\n />\r\n </div>\r\n <div class=\"profile-img right\" [class.fade-all]=\"fadeAllEdges\">\r\n <img\r\n [src]=\"user2Image\"\r\n alt=\"User 2\"\r\n [style.transform]=\"user2Transform\"\r\n [style.object-position]=\"user2ObjectPosition\"\r\n (load)=\"onUserImageLoad(2, $event)\"\r\n />\r\n <!-- [style.object-fit]=\"'cover'\" -->\r\n </div>\r\n </div>\r\n\r\n <div class=\"shape\">\r\n <div class=\"shape-bg\">\r\n <img src=\"./assets/s1.svg\" alt=\"Shape\" height=\"350\" class=\"shape-bg1\" />\r\n <img src=\"./assets/s2.svg\" alt=\"Shape\" height=\"350\" class=\"shape-bg2\" />\r\n </div>\r\n\r\n <div class=\"shape-text\">\r\n <div class=\"shape-text-left\">\r\n <div class=\"scroll-container\" #leftContainer>\r\n <ng-container\r\n *ngFor=\"\r\n let interest of alignedPerson1Interests.length > 0\r\n ? alignedPerson1Interests\r\n : displayPerson1Interests.length > 0\r\n ? displayPerson1Interests\r\n : person1Interests;\r\n let i = index\r\n \"\r\n >\r\n <p\r\n class=\"shape-p\"\r\n *ngIf=\"!centerItem.includes(interest)\"\r\n [ngClass]=\"{'dash-item': interest === '-', 'spacer-item': interest === '----', 'aligned-item': interest !== '-' && interest !== '----'}\"\r\n >\r\n {{ interest === \"-\" ? \"\u2014\" : interest }}\r\n </p>\r\n </ng-container>\r\n </div>\r\n <h2 class=\"shape-h2\" (click)=\"onViewProfile()\">View Profile</h2>\r\n </div>\r\n\r\n <div class=\"shape-text-center\">\r\n <ng-container *ngFor=\"let item of centerItem\">\r\n <p class=\"shape-p-center\">{{ item }}</p>\r\n </ng-container>\r\n </div>\r\n <div class=\"shape-text-right\">\r\n <div class=\"scroll-container\" #rightContainer>\r\n <ng-container\r\n *ngFor=\"\r\n let interest of alignedPerson2Interests.length > 0\r\n ? alignedPerson2Interests\r\n : displayPerson2Interests.length > 0\r\n ? displayPerson2Interests\r\n : person2Interests;\r\n let i = index\r\n \"\r\n >\r\n <p\r\n class=\"shape-p-right\"\r\n *ngIf=\"!centerItem.includes(interest)\"\r\n [ngClass]=\"{'dash-item': interest === '-', 'spacer-item': interest === '----', 'aligned-item': interest !== '-' && interest !== '----'}\"\r\n >\r\n {{ interest === \"-\" ? \"\u2014\" : interest }}\r\n </p>\r\n </ng-container>\r\n </div>\r\n <h2 class=\"shape-h2-right\" (click)=\"onViewProfile()\">View Profile</h2>\r\n </div>\r\n </div>\r\n </div>\r\n\r\n <!-- Loading indicator for alignment process -->\r\n <div *ngIf=\"isAligning\" class=\"loading-indicator\">\r\n <div class=\"loading-spinner\"></div>\r\n </div>\r\n</div>\r\n\r\n\r\n", styles: [".profile-img.left{-webkit-mask-image:linear-gradient(to right,rgb(0,0,0) 0%,rgb(0,0,0) calc(100% - var(--overlap-size, 40px)),rgba(0,0,0,0) 100%);-webkit-mask-size:100% 100%;-webkit-mask-repeat:no-repeat;mask-image:linear-gradient(to right,#fff 0% calc(100% - var(--overlap-size, 40px)),#fff0);mask-size:100% 100%;mask-repeat:no-repeat;margin-right:calc(-.5 * var(--overlap-size, 40px))}.profile-img.right{-webkit-mask-image:linear-gradient(to left,rgb(0,0,0) 0%,rgb(0,0,0) calc(100% - var(--overlap-size, 40px)),rgba(0,0,0,0) 100%);-webkit-mask-size:100% 100%;-webkit-mask-repeat:no-repeat;mask-image:linear-gradient(to left,#fff 0% calc(100% - var(--overlap-size, 40px)),#fff0);mask-size:100% 100%;mask-repeat:no-repeat;margin-left:calc(-.5 * var(--overlap-size, 40px))}.profile-img.fade-all{-webkit-mask-image:linear-gradient(to bottom,transparent 75px,black 125px,black 425px,transparent 475px),linear-gradient(to right,transparent,black 15%,black 85%,transparent);-webkit-mask-composite:source-in;mask-image:linear-gradient(to bottom,transparent 75px,black 125px,black 425px,transparent 475px),linear-gradient(to right,transparent,black 15%,black 85%,transparent);mask-composite:intersect}.profile-img.fade-all.left{-webkit-mask-image:linear-gradient(to bottom,transparent 75px,black 125px,black 425px,transparent 475px),linear-gradient(to right,transparent 0%,black 15%,black calc(100% - var(--overlap-size, 40px)),transparent 100%);mask-image:linear-gradient(to bottom,transparent 75px,black 125px,black 425px,transparent 475px),linear-gradient(to right,transparent 0%,black 15%,black calc(100% - var(--overlap-size, 40px)),transparent 100%)}.profile-img.fade-all.right{-webkit-mask-image:linear-gradient(to bottom,transparent 75px,black 125px,black 425px,transparent 475px),linear-gradient(to left,transparent 0%,black 15%,black calc(100% - var(--overlap-size, 40px)),transparent 100%);mask-image:linear-gradient(to bottom,transparent 75px,black 125px,black 425px,transparent 475px),linear-gradient(to left,transparent 0%,black 15%,black calc(100% - var(--overlap-size, 40px)),transparent 100%)}.profile-screen{width:320px;max-width:100%;margin:0 auto;overflow:hidden;position:relative}.blank-box{background-color:#000;height:20px;width:100%;position:absolute;left:0;top:26%;transform:translateY(-50%);z-index:-5}.profile-flex{display:flex;width:100%;height:400px;overflow:hidden;background:linear-gradient(135deg,#3a3a3a,#2a2a2a);gap:0;margin:0;padding:0;position:relative;box-shadow:none;border:none;--overlap-size: 40px}.shape-bg1,.shape-bg2{position:absolute;max-width:250px;opacity:.5;transition:transform .2s ease-out}.shape-bg1{z-index:1;left:0}.shape-bg2{right:0}.shape-bg img{cursor:grab}.shape-bg img:active{cursor:grabbing}.profile-img{width:180px;height:550px;position:relative;flex-shrink:0;background:linear-gradient(135deg,#3a3a3a,#2a2a2a);margin:0;padding:0;left:0;top:-75px;border:none;box-sizing:border-box;overflow:hidden;box-shadow:none;outline:none}.profile-img img{transition:transform .3s ease;width:100%;height:100%;object-fit:cover;display:block;object-position:center center;opacity:.6;transform-origin:center center;border:none;outline:none;box-shadow:none;position:relative;top:0;left:0;filter:contrast(1.2) brightness(1.1) saturate(1.1);image-rendering:auto}.shape{position:absolute;top:40px;width:100%}.shape-bg{position:relative}.shape-text{position:absolute;top:0;left:0;right:0;width:100%;height:100%;z-index:1;padding:0 1.25rem;box-sizing:border-box;pointer-events:none}.shape-text-left{margin-top:40px;text-align:left;position:absolute;top:0;bottom:0;right:calc(50% + 75px);z-index:2;pointer-events:auto;display:flex;flex-direction:column;align-items:flex-end}.shape-text-left .scroll-container{width:100%}.shape-text-center{position:absolute;left:0;right:0;margin:0 auto;width:fit-content;font-family:Gilroy,sans-serif;font-weight:700;font-size:.75rem;line-height:100%;letter-spacing:0%;color:#fff;text-align:center;display:flex;justify-content:center;align-items:center;flex-direction:column;text-shadow:2px 2px 4px rgba(0,0,0,.3);z-index:10;transition:transform .3s ease;pointer-events:none;top:50%;transform:translateY(-50%);padding:0 .625rem;margin-top:3.8125rem}.shape-text-right{margin-top:40px;text-align:right;position:absolute;top:0;bottom:0;left:calc(50% + 70px);z-index:2;pointer-events:auto;display:flex;flex-direction:column;align-items:flex-start}.shape-text-right .scroll-container{width:100%}.shape-p{font-family:Gilroy,sans-serif;font-weight:400;font-size:.75rem;line-height:100%;letter-spacing:0%;color:#fff;padding-bottom:.125rem;text-align:right;text-shadow:2px 2px 4px rgba(0,0,0,.3);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;direction:rtl;-webkit-user-select:none;user-select:none;margin:0}.shape-p-center{font-family:Gilroy,sans-serif;font-weight:700;font-size:.75rem;line-height:100%;letter-spacing:0%;color:#fff;margin:0;text-align:center;display:flex;justify-content:center;align-items:center;text-shadow:2px 2px 4px rgba(0,0,0,.3)}.shape-p-right{font-family:Gilroy,sans-serif;font-weight:400;font-size:.75rem;line-height:100%;letter-spacing:0%;color:#fff;padding-bottom:.125rem;text-align:left;text-shadow:2px 2px 4px rgba(0,0,0,.3);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;margin:0;-webkit-user-select:none;user-select:none}.dash-item{color:#ffffff4d!important;font-size:.625rem!important;height:.625rem}.spacer-item{height:auto!important;color:#fffc!important;font-weight:700;font-size:.75rem!important;letter-spacing:2px}.shape-h2{font-family:Calistoga,serif;font-weight:400;font-size:.625rem;letter-spacing:0%;color:#ffffff80;text-shadow:2px 2px 4px rgba(0,0,0,.3);cursor:pointer;transition:all .3s ease;border:none;outline:none;white-space:nowrap;pointer-events:auto;z-index:100;position:relative;margin-left:.5rem}.shape-h2:hover{opacity:.8;transform:translateY(-2px)}.shape-h2-right{font-family:Calistoga,serif;font-weight:400;font-size:.625rem;letter-spacing:0%;color:#ffffff80;text-shadow:2px 2px 4px rgba(0,0,0,.3);cursor:pointer;transition:all .3s ease;border:none;outline:none;white-space:nowrap;pointer-events:auto;z-index:100;position:relative;margin-right:3.75rem}.shape-h2-right:hover{opacity:.8;transform:translateY(-2px)}.scroll-when-long{display:inline-block;max-width:4.5625rem;white-space:nowrap;overflow-x:auto;overflow-y:hidden;-webkit-overflow-scrolling:touch}.scroll-when-long::-webkit-scrollbar{height:2px}.scroll-when-long::-webkit-scrollbar-track{background:#eee;border-radius:.625rem}.scroll-when-long::-webkit-scrollbar-thumb{background:#888;border-radius:.625rem}.scroll-when-long::-webkit-scrollbar-thumb:hover{background:#555}.shape-text p{cursor:default;-webkit-user-select:none;user-select:none}.shape-text p:active{cursor:default}#drag-preview{position:absolute;pointer-events:none;display:none;background:#000000b3;color:#fff;padding:5px 10px;border-radius:5px;z-index:9999;max-width:200px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.shape-text-container{width:70px;position:relative;overflow:hidden}.draggable{width:70px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;cursor:grab;-webkit-user-select:none;user-select:none;position:relative;display:inline-block}.dragging{width:auto;text-overflow:clip;cursor:grabbing;position:absolute;z-index:1000}.p-wrapper{width:70px;overflow-x:hidden}.shape-text-center{display:flex;justify-content:space-between;flex-direction:column;gap:10px;height:100%;margin-top:55px}.loading-indicator{position:fixed;top:25%;left:50%;transform:translate(-50%,-50%);background:transparent;color:#fff;padding:20px 30px;border-radius:10px;text-align:center;z-index:1000;display:flex;flex-direction:column;align-items:center;gap:8px}.loading-spinner{width:40px;height:40px;border:4px solid rgba(255,255,255,.3);border-top:4px solid #667eea;border-radius:50%;animation:spin 1s linear infinite}@keyframes spin{0%{transform:rotate(0)}to{transform:rotate(360deg)}}.loading-indicator p{margin:0;font-size:16px;font-weight:500}.api-key-modal-overlay{position:fixed;inset:0;background:#0f172a99;-webkit-backdrop-filter:blur(2px);backdrop-filter:blur(2px);display:flex;align-items:center;justify-content:center;z-index:2000}.api-key-modal{width:min(640px,92vw);background:#0f172a;color:#e2e8f0;border:1px solid #334155;border-radius:12px;box-shadow:0 20px 50px #00000073;padding:16px 18px}.api-key-modal-header{display:flex;align-items:center;justify-content:space-between;gap:12px}.api-key-modal-header h2{margin:0;font-size:20px;font-weight:700;color:#e2e8f0}.warning-icon{color:#fbbf24}.api-key-modal .close-btn{background:#0b1220;border:1px solid #334155;color:#94a3b8;border-radius:8px;padding:6px;cursor:pointer;line-height:0}.api-key-modal-body{margin-top:12px;display:flex;flex-direction:column;gap:10px}.modal-message,.modal-instruction{margin:0;color:#cbd5e1}.modal-message strong{color:#e5e7eb}.api-key-input-group{display:flex;align-items:center;gap:10px}.api-key-input{flex:1 1 auto;background:#0a1020;color:#e2e8f0;border:1px solid #334155;border-radius:8px;padding:10px 12px}.api-key-input:focus{outline:none;border-color:#2563eb;box-shadow:0 0 0 3px #2563eb33}.api-key-load-btn{background:#2563eb;color:#fff;border:none;border-radius:8px;padding:10px 12px;cursor:pointer;font-weight:600}.api-key-load-btn:hover{background:#1d4ed8}.modal-hint{display:flex;align-items:center;gap:8px;color:#94a3b8}.modal-hint a{color:#93c5fd}\n"], dependencies: [{ kind: "directive", type: i2.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i2.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i3.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { kind: "directive", type: i3.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i3.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }] });
2741
+ }
2742
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ProfileComparisonLibComponent, decorators: [{
2743
+ type: Component,
2744
+ args: [{ selector: 'lib-profile-comparison', standalone: false, template: "<!-- API Quota Exhausted Modal (Independent) -->\r\n<div class=\"api-key-modal-overlay\" *ngIf=\"showApiKeyModal\">\r\n <div class=\"api-key-modal\" (click)=\"$event.stopPropagation()\">\r\n <div class=\"api-key-modal-header\">\r\n <div class=\"warning-icon\">\r\n <svg\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"48\"\r\n height=\"48\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"currentColor\"\r\n stroke-width=\"2\"\r\n stroke-linecap=\"round\"\r\n stroke-linejoin=\"round\"\r\n >\r\n <path\r\n d=\"M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z\"\r\n ></path>\r\n <line x1=\"12\" y1=\"9\" x2=\"12\" y2=\"13\"></line>\r\n <line x1=\"12\" y1=\"17\" x2=\"12.01\" y2=\"17\"></line>\r\n </svg>\r\n </div>\r\n <h2>API Quota Limit Exceeded</h2>\r\n <button class=\"close-btn\" (click)=\"closeApiKeyModal()\" aria-label=\"Close\">\r\n <svg\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"24\"\r\n height=\"24\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"currentColor\"\r\n stroke-width=\"2\"\r\n stroke-linecap=\"round\"\r\n stroke-linejoin=\"round\"\r\n >\r\n <line x1=\"18\" y1=\"6\" x2=\"6\" y2=\"18\"></line>\r\n <line x1=\"6\" y1=\"6\" x2=\"18\" y2=\"18\"></line>\r\n </svg>\r\n </button>\r\n </div>\r\n <!-- Face++ modal moved outside to top-level sibling overlay -->\r\n\r\n <div class=\"api-key-modal-body\">\r\n <p class=\"modal-message\">\r\n The default API Ninjas key has reached its free monthly quota limit of\r\n <strong>3,000 API calls</strong>.\r\n </p>\r\n <p class=\"modal-instruction\">\r\n Please enter your own API Ninjas key below to continue using the profile\r\n comparison features:\r\n </p>\r\n\r\n <div class=\"api-key-input-group\">\r\n <input\r\n type=\"text\"\r\n [(ngModel)]=\"apiKeyInputValue\"\r\n placeholder=\"Paste your API Ninjas key here\"\r\n class=\"api-key-input\"\r\n autofocus\r\n />\r\n <button class=\"api-key-load-btn\" (click)=\"updateApiKey()\">\r\n Apply Key\r\n </button>\r\n </div>\r\n\r\n <div class=\"modal-hint\">\r\n <p>\r\n <svg\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"16\"\r\n height=\"16\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"currentColor\"\r\n stroke-width=\"2\"\r\n stroke-linecap=\"round\"\r\n stroke-linejoin=\"round\"\r\n >\r\n <circle cx=\"12\" cy=\"12\" r=\"10\"></circle>\r\n <line x1=\"12\" y1=\"16\" x2=\"12\" y2=\"12\"></line>\r\n <line x1=\"12\" y1=\"8\" x2=\"12.01\" y2=\"8\"></line>\r\n </svg>\r\n Don't have an API key? Get your free key at\r\n <a href=\"https://api-ninjas.com/\" target=\"_blank\" rel=\"noopener\"\r\n >api-ninjas.com</a\r\n >\r\n </p>\r\n </div>\r\n </div>\r\n </div>\r\n</div>\r\n\r\n<!-- Face++ API Credentials Modal (Independent) -->\r\n<div class=\"api-key-modal-overlay facepp-overlay\" *ngIf=\"showFaceppKeyModal\">\r\n <div class=\"api-key-modal\" (click)=\"$event.stopPropagation()\">\r\n <div class=\"api-key-modal-header\">\r\n <div class=\"warning-icon\">\r\n <svg\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"48\"\r\n height=\"48\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"currentColor\"\r\n stroke-width=\"2\"\r\n stroke-linecap=\"round\"\r\n stroke-linejoin=\"round\"\r\n >\r\n <path\r\n d=\"M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z\"\r\n ></path>\r\n <line x1=\"12\" y1=\"9\" x2=\"12\" y2=\"13\"></line>\r\n <line x1=\"12\" y1=\"17\" x2=\"12.01\" y2=\"17\"></line>\r\n </svg>\r\n </div>\r\n <h2>Face++ Credentials Required</h2>\r\n <button\r\n class=\"close-btn\"\r\n (click)=\"closeFaceppKeyModal()\"\r\n aria-label=\"Close\"\r\n >\r\n <svg\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"24\"\r\n height=\"24\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"currentColor\"\r\n stroke-width=\"2\"\r\n stroke-linecap=\"round\"\r\n stroke-linejoin=\"round\"\r\n >\r\n <line x1=\"18\" y1=\"6\" x2=\"6\" y2=\"18\"></line>\r\n <line x1=\"6\" y1=\"6\" x2=\"18\" y2=\"18\"></line>\r\n </svg>\r\n </button>\r\n </div>\r\n\r\n <div class=\"api-key-modal-body\">\r\n <p class=\"modal-message\">\r\n The Face++ API returned an authorization or rate-limit error. Please\r\n enter your Face++ API Key and API Secret.\r\n </p>\r\n\r\n <div\r\n class=\"api-key-input-group\"\r\n style=\"flex-direction: column; align-items: stretch; gap: 10px\"\r\n >\r\n <input\r\n type=\"text\"\r\n [(ngModel)]=\"faceppKeyInputValue\"\r\n placeholder=\"Face++ API Key\"\r\n class=\"api-key-input\"\r\n />\r\n <input\r\n type=\"text\"\r\n [(ngModel)]=\"faceppSecretInputValue\"\r\n placeholder=\"Face++ API Secret\"\r\n class=\"api-key-input\"\r\n />\r\n <button class=\"api-key-load-btn\" (click)=\"updateFaceppCredentials()\">\r\n Apply Face++ Credentials\r\n </button>\r\n </div>\r\n </div>\r\n </div>\r\n</div>\r\n\r\n<!-- OpenAI API Key Modal (Independent) -->\r\n<div class=\"api-key-modal-overlay openai-overlay\" *ngIf=\"showOpenAIKeyModal\">\r\n <div class=\"api-key-modal\" (click)=\"$event.stopPropagation()\">\r\n <div class=\"api-key-modal-header\">\r\n <div class=\"warning-icon\">\r\n <svg\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"48\"\r\n height=\"48\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"currentColor\"\r\n stroke-width=\"2\"\r\n stroke-linecap=\"round\"\r\n stroke-linejoin=\"round\"\r\n >\r\n <path\r\n d=\"M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z\"\r\n ></path>\r\n <line x1=\"12\" y1=\"9\" x2=\"12\" y2=\"13\"></line>\r\n <line x1=\"12\" y1=\"17\" x2=\"12.01\" y2=\"17\"></line>\r\n </svg>\r\n </div>\r\n <h2>OpenAI API Key Required</h2>\r\n <button\r\n class=\"close-btn\"\r\n (click)=\"closeOpenAIKeyModal()\"\r\n aria-label=\"Close\"\r\n >\r\n <svg\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"24\"\r\n height=\"24\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"currentColor\"\r\n stroke-width=\"2\"\r\n stroke-linecap=\"round\"\r\n stroke-linejoin=\"round\"\r\n >\r\n <line x1=\"18\" y1=\"6\" x2=\"6\" y2=\"18\"></line>\r\n <line x1=\"6\" y1=\"6\" x2=\"18\" y2=\"18\"></line>\r\n </svg>\r\n </button>\r\n </div>\r\n\r\n <div class=\"api-key-modal-body\">\r\n <p class=\"modal-message\">\r\n The OpenAI API returned an authorization or rate-limit error. Please\r\n enter your OpenAI API key to continue using the text similarity features.\r\n </p>\r\n <p class=\"modal-instruction\">\r\n Text matching now uses OpenAI embeddings (text-embedding-3-small) for improved semantic similarity.\r\n </p>\r\n\r\n <div class=\"api-key-input-group\">\r\n <input\r\n type=\"text\"\r\n [(ngModel)]=\"openaiKeyInputValue\"\r\n placeholder=\"Paste your OpenAI API key here\"\r\n class=\"api-key-input\"\r\n autofocus\r\n />\r\n <button class=\"api-key-load-btn\" (click)=\"updateOpenAIKey()\">\r\n Apply OpenAI Key\r\n </button>\r\n </div>\r\n\r\n <div class=\"modal-hint\">\r\n <p>\r\n <svg\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"16\"\r\n height=\"16\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"currentColor\"\r\n stroke-width=\"2\"\r\n stroke-linecap=\"round\"\r\n stroke-linejoin=\"round\"\r\n >\r\n <circle cx=\"12\" cy=\"12\" r=\"10\"></circle>\r\n <line x1=\"12\" y1=\"16\" x2=\"12\" y2=\"12\"></line>\r\n <line x1=\"12\" y1=\"8\" x2=\"12.01\" y2=\"8\"></line>\r\n </svg>\r\n Don't have an API key? Get your free key at\r\n <a href=\"https://platform.openai.com/api-keys\" target=\"_blank\" rel=\"noopener\"\r\n >platform.openai.com/api-keys</a\r\n >\r\n </p>\r\n </div>\r\n </div>\r\n </div>\r\n</div>\r\n\r\n<div class=\"blank-box\"></div>\r\n\r\n<div class=\"profile-screen\">\r\n <div class=\"profile-flex\">\r\n <div class=\"profile-img left\" [class.fade-all]=\"fadeAllEdges\">\r\n <img\r\n [src]=\"user1Image\"\r\n alt=\"User 1\"\r\n [style.transform]=\"user1Transform\"\r\n [style.object-position]=\"user1ObjectPosition\"\r\n (load)=\"onUserImageLoad(1, $event)\"\r\n />\r\n </div>\r\n <div class=\"profile-img right\" [class.fade-all]=\"fadeAllEdges\">\r\n <img\r\n [src]=\"user2Image\"\r\n alt=\"User 2\"\r\n [style.transform]=\"user2Transform\"\r\n [style.object-position]=\"user2ObjectPosition\"\r\n (load)=\"onUserImageLoad(2, $event)\"\r\n />\r\n <!-- [style.object-fit]=\"'cover'\" -->\r\n </div>\r\n </div>\r\n\r\n <div class=\"shape\">\r\n <div class=\"shape-bg\">\r\n <img src=\"./assets/s1.svg\" alt=\"Shape\" height=\"350\" class=\"shape-bg1\" />\r\n <img src=\"./assets/s2.svg\" alt=\"Shape\" height=\"350\" class=\"shape-bg2\" />\r\n </div>\r\n\r\n <div class=\"shape-text\">\r\n <div class=\"shape-text-left\">\r\n <div class=\"scroll-container\" #leftContainer>\r\n <ng-container\r\n *ngFor=\"\r\n let interest of alignedPerson1Interests.length > 0\r\n ? alignedPerson1Interests\r\n : displayPerson1Interests.length > 0\r\n ? displayPerson1Interests\r\n : person1Interests;\r\n let i = index\r\n \"\r\n >\r\n <p\r\n class=\"shape-p\"\r\n *ngIf=\"!centerItem.includes(interest)\"\r\n [ngClass]=\"{'dash-item': interest === '-', 'spacer-item': interest === '----', 'aligned-item': interest !== '-' && interest !== '----'}\"\r\n >\r\n {{ interest === \"-\" ? \"\u2014\" : interest }}\r\n </p>\r\n </ng-container>\r\n </div>\r\n <h2 class=\"shape-h2\" (click)=\"onViewProfile()\">View Profile</h2>\r\n </div>\r\n\r\n <div class=\"shape-text-center\">\r\n <ng-container *ngFor=\"let item of centerItem\">\r\n <p class=\"shape-p-center\">{{ item }}</p>\r\n </ng-container>\r\n </div>\r\n <div class=\"shape-text-right\">\r\n <div class=\"scroll-container\" #rightContainer>\r\n <ng-container\r\n *ngFor=\"\r\n let interest of alignedPerson2Interests.length > 0\r\n ? alignedPerson2Interests\r\n : displayPerson2Interests.length > 0\r\n ? displayPerson2Interests\r\n : person2Interests;\r\n let i = index\r\n \"\r\n >\r\n <p\r\n class=\"shape-p-right\"\r\n *ngIf=\"!centerItem.includes(interest)\"\r\n [ngClass]=\"{'dash-item': interest === '-', 'spacer-item': interest === '----', 'aligned-item': interest !== '-' && interest !== '----'}\"\r\n >\r\n {{ interest === \"-\" ? \"\u2014\" : interest }}\r\n </p>\r\n </ng-container>\r\n </div>\r\n <h2 class=\"shape-h2-right\" (click)=\"onViewProfile()\">View Profile</h2>\r\n </div>\r\n </div>\r\n </div>\r\n\r\n <!-- Loading indicator for alignment process -->\r\n <div *ngIf=\"isAligning\" class=\"loading-indicator\">\r\n <div class=\"loading-spinner\"></div>\r\n </div>\r\n</div>\r\n\r\n\r\n", styles: [".profile-img.left{-webkit-mask-image:linear-gradient(to right,rgb(0,0,0) 0%,rgb(0,0,0) calc(100% - var(--overlap-size, 40px)),rgba(0,0,0,0) 100%);-webkit-mask-size:100% 100%;-webkit-mask-repeat:no-repeat;mask-image:linear-gradient(to right,#fff 0% calc(100% - var(--overlap-size, 40px)),#fff0);mask-size:100% 100%;mask-repeat:no-repeat;margin-right:calc(-.5 * var(--overlap-size, 40px))}.profile-img.right{-webkit-mask-image:linear-gradient(to left,rgb(0,0,0) 0%,rgb(0,0,0) calc(100% - var(--overlap-size, 40px)),rgba(0,0,0,0) 100%);-webkit-mask-size:100% 100%;-webkit-mask-repeat:no-repeat;mask-image:linear-gradient(to left,#fff 0% calc(100% - var(--overlap-size, 40px)),#fff0);mask-size:100% 100%;mask-repeat:no-repeat;margin-left:calc(-.5 * var(--overlap-size, 40px))}.profile-img.fade-all{-webkit-mask-image:linear-gradient(to bottom,transparent 75px,black 125px,black 425px,transparent 475px),linear-gradient(to right,transparent,black 15%,black 85%,transparent);-webkit-mask-composite:source-in;mask-image:linear-gradient(to bottom,transparent 75px,black 125px,black 425px,transparent 475px),linear-gradient(to right,transparent,black 15%,black 85%,transparent);mask-composite:intersect}.profile-img.fade-all.left{-webkit-mask-image:linear-gradient(to bottom,transparent 75px,black 125px,black 425px,transparent 475px),linear-gradient(to right,transparent 0%,black 15%,black calc(100% - var(--overlap-size, 40px)),transparent 100%);mask-image:linear-gradient(to bottom,transparent 75px,black 125px,black 425px,transparent 475px),linear-gradient(to right,transparent 0%,black 15%,black calc(100% - var(--overlap-size, 40px)),transparent 100%)}.profile-img.fade-all.right{-webkit-mask-image:linear-gradient(to bottom,transparent 75px,black 125px,black 425px,transparent 475px),linear-gradient(to left,transparent 0%,black 15%,black calc(100% - var(--overlap-size, 40px)),transparent 100%);mask-image:linear-gradient(to bottom,transparent 75px,black 125px,black 425px,transparent 475px),linear-gradient(to left,transparent 0%,black 15%,black calc(100% - var(--overlap-size, 40px)),transparent 100%)}.profile-screen{width:320px;max-width:100%;margin:0 auto;overflow:hidden;position:relative}.blank-box{background-color:#000;height:20px;width:100%;position:absolute;left:0;top:26%;transform:translateY(-50%);z-index:-5}.profile-flex{display:flex;width:100%;height:400px;overflow:hidden;background:linear-gradient(135deg,#3a3a3a,#2a2a2a);gap:0;margin:0;padding:0;position:relative;box-shadow:none;border:none;--overlap-size: 40px}.shape-bg1,.shape-bg2{position:absolute;max-width:250px;opacity:.5;transition:transform .2s ease-out}.shape-bg1{z-index:1;left:0}.shape-bg2{right:0}.shape-bg img{cursor:grab}.shape-bg img:active{cursor:grabbing}.profile-img{width:180px;height:550px;position:relative;flex-shrink:0;background:linear-gradient(135deg,#3a3a3a,#2a2a2a);margin:0;padding:0;left:0;top:-75px;border:none;box-sizing:border-box;overflow:hidden;box-shadow:none;outline:none}.profile-img img{transition:transform .3s ease;width:100%;height:100%;object-fit:cover;display:block;object-position:center center;opacity:.6;transform-origin:center center;border:none;outline:none;box-shadow:none;position:relative;top:0;left:0;filter:contrast(1.2) brightness(1.1) saturate(1.1);image-rendering:auto}.shape{position:absolute;top:40px;width:100%}.shape-bg{position:relative}.shape-text{position:absolute;top:0;left:0;right:0;width:100%;height:100%;z-index:1;padding:0 1.25rem;box-sizing:border-box;pointer-events:none}.shape-text-left{margin-top:40px;text-align:left;position:absolute;top:0;bottom:0;right:calc(50% + 75px);z-index:2;pointer-events:auto;display:flex;flex-direction:column;align-items:flex-end}.shape-text-left .scroll-container{width:100%}.shape-text-center{position:absolute;left:0;right:0;margin:0 auto;width:fit-content;font-family:Gilroy,sans-serif;font-weight:700;font-size:.75rem;line-height:100%;letter-spacing:0%;color:#fff;text-align:center;display:flex;justify-content:center;align-items:center;flex-direction:column;text-shadow:2px 2px 4px rgba(0,0,0,.3);z-index:10;transition:transform .3s ease;pointer-events:none;top:50%;transform:translateY(-50%);padding:0 .625rem;margin-top:3.8125rem}.shape-text-right{margin-top:40px;text-align:right;position:absolute;top:0;bottom:0;left:calc(50% + 70px);z-index:2;pointer-events:auto;display:flex;flex-direction:column;align-items:flex-start}.shape-text-right .scroll-container{width:100%}.shape-p{font-family:Gilroy,sans-serif;font-weight:400;font-size:.75rem;line-height:100%;letter-spacing:0%;color:#fff;padding-bottom:.125rem;text-align:right;text-shadow:2px 2px 4px rgba(0,0,0,.3);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;direction:rtl;-webkit-user-select:none;user-select:none;margin:0}.shape-p-center{font-family:Gilroy,sans-serif;font-weight:700;font-size:.75rem;line-height:100%;letter-spacing:0%;color:#fff;margin:0;text-align:center;display:flex;justify-content:center;align-items:center;text-shadow:2px 2px 4px rgba(0,0,0,.3)}.shape-p-right{font-family:Gilroy,sans-serif;font-weight:400;font-size:.75rem;line-height:100%;letter-spacing:0%;color:#fff;padding-bottom:.125rem;text-align:left;text-shadow:2px 2px 4px rgba(0,0,0,.3);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;margin:0;-webkit-user-select:none;user-select:none}.dash-item{color:#ffffff4d!important;font-size:.625rem!important;height:.625rem}.spacer-item{height:auto!important;color:#fffc!important;font-weight:700;font-size:.75rem!important;letter-spacing:2px}.shape-h2{font-family:Calistoga,serif;font-weight:400;font-size:.625rem;letter-spacing:0%;color:#ffffff80;text-shadow:2px 2px 4px rgba(0,0,0,.3);cursor:pointer;transition:all .3s ease;border:none;outline:none;white-space:nowrap;pointer-events:auto;z-index:100;position:relative;margin-left:.5rem}.shape-h2:hover{opacity:.8;transform:translateY(-2px)}.shape-h2-right{font-family:Calistoga,serif;font-weight:400;font-size:.625rem;letter-spacing:0%;color:#ffffff80;text-shadow:2px 2px 4px rgba(0,0,0,.3);cursor:pointer;transition:all .3s ease;border:none;outline:none;white-space:nowrap;pointer-events:auto;z-index:100;position:relative;margin-right:3.75rem}.shape-h2-right:hover{opacity:.8;transform:translateY(-2px)}.scroll-when-long{display:inline-block;max-width:4.5625rem;white-space:nowrap;overflow-x:auto;overflow-y:hidden;-webkit-overflow-scrolling:touch}.scroll-when-long::-webkit-scrollbar{height:2px}.scroll-when-long::-webkit-scrollbar-track{background:#eee;border-radius:.625rem}.scroll-when-long::-webkit-scrollbar-thumb{background:#888;border-radius:.625rem}.scroll-when-long::-webkit-scrollbar-thumb:hover{background:#555}.shape-text p{cursor:default;-webkit-user-select:none;user-select:none}.shape-text p:active{cursor:default}#drag-preview{position:absolute;pointer-events:none;display:none;background:#000000b3;color:#fff;padding:5px 10px;border-radius:5px;z-index:9999;max-width:200px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.shape-text-container{width:70px;position:relative;overflow:hidden}.draggable{width:70px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;cursor:grab;-webkit-user-select:none;user-select:none;position:relative;display:inline-block}.dragging{width:auto;text-overflow:clip;cursor:grabbing;position:absolute;z-index:1000}.p-wrapper{width:70px;overflow-x:hidden}.shape-text-center{display:flex;justify-content:space-between;flex-direction:column;gap:10px;height:100%;margin-top:55px}.loading-indicator{position:fixed;top:25%;left:50%;transform:translate(-50%,-50%);background:transparent;color:#fff;padding:20px 30px;border-radius:10px;text-align:center;z-index:1000;display:flex;flex-direction:column;align-items:center;gap:8px}.loading-spinner{width:40px;height:40px;border:4px solid rgba(255,255,255,.3);border-top:4px solid #667eea;border-radius:50%;animation:spin 1s linear infinite}@keyframes spin{0%{transform:rotate(0)}to{transform:rotate(360deg)}}.loading-indicator p{margin:0;font-size:16px;font-weight:500}.api-key-modal-overlay{position:fixed;inset:0;background:#0f172a99;-webkit-backdrop-filter:blur(2px);backdrop-filter:blur(2px);display:flex;align-items:center;justify-content:center;z-index:2000}.api-key-modal{width:min(640px,92vw);background:#0f172a;color:#e2e8f0;border:1px solid #334155;border-radius:12px;box-shadow:0 20px 50px #00000073;padding:16px 18px}.api-key-modal-header{display:flex;align-items:center;justify-content:space-between;gap:12px}.api-key-modal-header h2{margin:0;font-size:20px;font-weight:700;color:#e2e8f0}.warning-icon{color:#fbbf24}.api-key-modal .close-btn{background:#0b1220;border:1px solid #334155;color:#94a3b8;border-radius:8px;padding:6px;cursor:pointer;line-height:0}.api-key-modal-body{margin-top:12px;display:flex;flex-direction:column;gap:10px}.modal-message,.modal-instruction{margin:0;color:#cbd5e1}.modal-message strong{color:#e5e7eb}.api-key-input-group{display:flex;align-items:center;gap:10px}.api-key-input{flex:1 1 auto;background:#0a1020;color:#e2e8f0;border:1px solid #334155;border-radius:8px;padding:10px 12px}.api-key-input:focus{outline:none;border-color:#2563eb;box-shadow:0 0 0 3px #2563eb33}.api-key-load-btn{background:#2563eb;color:#fff;border:none;border-radius:8px;padding:10px 12px;cursor:pointer;font-weight:600}.api-key-load-btn:hover{background:#1d4ed8}.modal-hint{display:flex;align-items:center;gap:8px;color:#94a3b8}.modal-hint a{color:#93c5fd}\n"] }]
2745
+ }], ctorParameters: () => [{ type: ProfileService }, { type: OpenAIEmbeddingService }, { type: i0.Renderer2 }, { type: FileConversionService }, { type: ImageCompressionService }], propDecorators: { config: [{
2746
+ type: Input
2747
+ }], apiNinjasKey: [{
2748
+ type: Input
2749
+ }], faceplusKey: [{
2750
+ type: Input
2751
+ }], faceplusSecret: [{
2752
+ type: Input
2753
+ }], openaiApiKey: [{
2754
+ type: Input
2755
+ }], fadeAllEdges: [{
2756
+ type: Input
2757
+ }], matrixDataChange: [{
2758
+ type: Output
2759
+ }], leftContainer: [{
2760
+ type: ViewChild,
2761
+ args: ['leftContainer']
2762
+ }], rightContainer: [{
2763
+ type: ViewChild,
2764
+ args: ['rightContainer']
2765
+ }] } });
2766
+
2767
+ class CustomInputComponent {
2768
+ router;
2769
+ person1Text = '';
2770
+ person2Text = '';
2771
+ person3Text = '';
2772
+ user1File = null;
2773
+ user2File = null;
2774
+ user1Preview = null;
2775
+ user2Preview = null;
2776
+ saving = false;
2777
+ error = null;
2778
+ static MAX_WIDTH = 1600;
2779
+ static MAX_HEIGHT = 1600;
2780
+ static COMPRESSION_FORMAT = 'image/jpeg';
2781
+ static QUALITY_START = 0.85;
2782
+ static QUALITY_MIN = 0.5;
2783
+ static QUALITY_STEP = 0.05;
2784
+ static MAX_BYTES = 2 * 1024 * 1024;
2785
+ static DOWNSCALE_STEP = 0.9;
2786
+ static MIN_WIDTH = 400;
2787
+ static MIN_HEIGHT = 400;
2788
+ // Image compression settings (configurable)
2789
+ // Note: Images are later stored as Data URLs in localStorage (base64 ~33% larger).
2790
+ // If you plan to store multiple images, consider lowering maxBytes to ~1.6–1.8 MB.
2791
+ compressionConfig = {
2792
+ maxWidth: CustomInputComponent.MAX_WIDTH,
2793
+ maxHeight: CustomInputComponent.MAX_HEIGHT,
2794
+ format: CustomInputComponent.COMPRESSION_FORMAT,
2795
+ qualityStart: CustomInputComponent.QUALITY_START,
2796
+ qualityMin: CustomInputComponent.QUALITY_MIN,
2797
+ qualityStep: CustomInputComponent.QUALITY_STEP,
2798
+ maxBytes: CustomInputComponent.MAX_BYTES,
2799
+ downscaleStep: CustomInputComponent.DOWNSCALE_STEP,
2800
+ minWidth: CustomInputComponent.MIN_WIDTH,
2801
+ minHeight: CustomInputComponent.MIN_HEIGHT,
2802
+ };
2803
+ constructor(router) {
2804
+ this.router = router;
2805
+ }
2806
+ async onUser1FileChange(event) {
2807
+ const input = event.target;
2808
+ const file = input.files && input.files.length ? input.files[0] : null;
2809
+ if (!file) {
2810
+ this.user1File = null;
2811
+ this.user1Preview = null;
2812
+ return;
2813
+ }
2814
+ try {
2815
+ const { file: compressed, dataUrl } = await firstValueFrom(this.compressImageFile(file));
2816
+ this.user1File = compressed;
2817
+ this.user1Preview = dataUrl;
2818
+ }
2819
+ catch (e) {
2820
+ console.error('User1 image compression failed:', e);
2821
+ // Fallback to original if compression fails
2822
+ this.user1File = file;
2823
+ this.previewFile(file, (dataUrl) => (this.user1Preview = dataUrl));
2824
+ }
2825
+ }
2826
+ async onUser2FileChange(event) {
2827
+ const input = event.target;
2828
+ const file = input.files && input.files.length ? input.files[0] : null;
2829
+ if (!file) {
2830
+ this.user2File = null;
2831
+ this.user2Preview = null;
2832
+ return;
2833
+ }
2834
+ try {
2835
+ const { file: compressed, dataUrl } = await firstValueFrom(this.compressImageFile(file));
2836
+ this.user2File = compressed;
2837
+ this.user2Preview = dataUrl;
2838
+ }
2839
+ catch (e) {
2840
+ console.error('User2 image compression failed:', e);
2841
+ // Fallback to original if compression fails
2842
+ this.user2File = file;
2843
+ this.previewFile(file, (dataUrl) => (this.user2Preview = dataUrl));
2844
+ }
2845
+ }
2846
+ async saveAndView() {
2847
+ this.error = null;
2848
+ this.saving = true;
2849
+ try {
2850
+ const img1 = this.user1File
2851
+ ? await firstValueFrom(this.fileToDataURL(this.user1File))
2852
+ : this.user1Preview || '';
2853
+ const img2 = this.user2File
2854
+ ? await firstValueFrom(this.fileToDataURL(this.user2File))
2855
+ : this.user2Preview || '';
2856
+ const cfg = {
2857
+ person1Interests: this.toList(this.person1Text),
2858
+ person2Interests: this.toList(this.person2Text),
2859
+ person3Interests: this.toList(this.person3Text),
2860
+ user1Image: img1,
2861
+ user2Image: img2,
2862
+ };
2863
+ localStorage.setItem('customProfileConfig', JSON.stringify(cfg));
2864
+ // Navigate to home to render with the new config
2865
+ await this.router.navigateByUrl('/');
2866
+ window.location.reload();
2867
+ }
2868
+ catch (e) {
2869
+ this.error = e?.message || 'Failed to save configuration';
2870
+ }
2871
+ finally {
2872
+ this.saving = false;
2873
+ }
2874
+ }
2875
+ clearAll() {
2876
+ this.person1Text = '';
2877
+ this.person2Text = '';
2878
+ this.person3Text = '';
2879
+ this.user1File = null;
2880
+ this.user2File = null;
2881
+ this.user1Preview = null;
2882
+ this.user2Preview = null;
2883
+ try {
2884
+ localStorage.removeItem('customProfileConfig');
2885
+ window.location.reload();
2886
+ }
2887
+ catch { }
2888
+ }
2889
+ // Close the overlay and return to the main view
2890
+ close() {
2891
+ this.router.navigateByUrl('/');
2892
+ }
2893
+ toList(block) {
2894
+ return (block || '')
2895
+ .split(/[\n,]+/)
2896
+ .map((s) => s.trim())
2897
+ .filter((s) => !!s);
2898
+ }
2899
+ fileToDataURL(file) {
2900
+ return new Observable(observer => {
2901
+ const reader = new FileReader();
2902
+ reader.onload = () => {
2903
+ observer.next(String(reader.result));
2904
+ observer.complete();
2905
+ };
2906
+ reader.onerror = (err) => observer.error(err);
2907
+ reader.readAsDataURL(file);
2908
+ });
2909
+ }
2910
+ previewFile(file, cb) {
2911
+ if (!file) {
2912
+ cb(null);
2913
+ return;
2914
+ }
2915
+ const reader = new FileReader();
2916
+ reader.onload = () => cb(String(reader.result));
2917
+ reader.readAsDataURL(file);
2918
+ }
2919
+ // Compress an image file according to compressionConfig (or overrides)
2920
+ compressImageFile(file, overrides = {}) {
2921
+ const cfg = { ...this.compressionConfig, ...overrides };
2922
+ return this.loadImageFromFile(file).pipe(switchMap((img) => {
2923
+ let width = img.naturalWidth || img.width;
2924
+ let height = img.naturalHeight || img.height;
2925
+ // Initial fit within max dimensions
2926
+ const scale = Math.min(cfg.maxWidth / width, cfg.maxHeight / height, 1);
2927
+ width = Math.max(1, Math.round(width * scale));
2928
+ height = Math.max(1, Math.round(height * scale));
2929
+ const canvas = document.createElement('canvas');
2930
+ const ctx = canvas.getContext('2d');
2931
+ const render = (w, h) => {
2932
+ canvas.width = w;
2933
+ canvas.height = h;
2934
+ ctx.clearRect(0, 0, w, h);
2935
+ ctx.drawImage(img, 0, 0, w, h);
2936
+ };
2937
+ const process = (w, h, q, attempts) => {
2938
+ render(w, h);
2939
+ return this.canvasToBlob(canvas, cfg.format, q).pipe(switchMap((blob) => {
2940
+ if (blob.size > cfg.maxBytes && attempts < 25) {
2941
+ if (q > cfg.qualityMin + 0.005) {
2942
+ return process(w, h, Math.max(cfg.qualityMin, q - cfg.qualityStep), attempts + 1);
2943
+ }
2944
+ else {
2945
+ const nextW = Math.max(Math.round(w * cfg.downscaleStep), cfg.minWidth);
2946
+ const nextH = Math.max(Math.round(h * cfg.downscaleStep), cfg.minHeight);
2947
+ if (nextW === w && nextH === h) {
2948
+ return of(blob);
2949
+ }
2950
+ return process(nextW, nextH, cfg.qualityStart, attempts + 1);
2951
+ }
2952
+ }
2953
+ return of(blob);
2954
+ }));
2955
+ };
2956
+ return process(width, height, cfg.qualityStart, 0).pipe(switchMap((finalBlob) => {
2957
+ const newName = this.renameFileForFormat(file.name, cfg.format);
2958
+ const compressedFile = new File([finalBlob], newName, {
2959
+ type: cfg.format,
2960
+ lastModified: Date.now(),
2961
+ });
2962
+ return this.blobToDataURL(finalBlob).pipe(map((dataUrl) => ({ file: compressedFile, dataUrl })));
2963
+ }));
2964
+ }));
2965
+ }
2966
+ loadImageFromFile(file) {
2967
+ return new Observable(observer => {
2968
+ const url = URL.createObjectURL(file);
2969
+ const img = new Image();
2970
+ img.onload = () => {
2971
+ URL.revokeObjectURL(url);
2972
+ observer.next(img);
2973
+ observer.complete();
2974
+ };
2975
+ img.onerror = () => {
2976
+ URL.revokeObjectURL(url);
2977
+ observer.error(new Error('Failed to load image for compression'));
2978
+ };
2979
+ img.src = url;
2980
+ });
2981
+ }
2982
+ canvasToBlob(canvas, type, quality) {
2983
+ return new Observable(observer => {
2984
+ if (canvas.toBlob) {
2985
+ canvas.toBlob((blob) => {
2986
+ if (blob) {
2987
+ observer.next(blob);
2988
+ observer.complete();
2989
+ }
2990
+ else {
2991
+ observer.error(new Error('Canvas.toBlob returned null'));
2992
+ }
2993
+ }, type, quality);
2994
+ }
2995
+ else {
2996
+ // Fallback via DataURL
2997
+ try {
2998
+ const dataUrl = canvas.toDataURL(type, quality);
2999
+ const base64 = dataUrl.split(',')[1] || '';
3000
+ const binary = atob(base64);
3001
+ const len = binary.length;
3002
+ const u8 = new Uint8Array(len);
3003
+ for (let i = 0; i < len; i++)
3004
+ u8[i] = binary.charCodeAt(i);
3005
+ observer.next(new Blob([u8], { type }));
3006
+ observer.complete();
3007
+ }
3008
+ catch (e) {
3009
+ observer.error(e);
3010
+ }
3011
+ }
3012
+ });
3013
+ }
3014
+ blobToDataURL(blob) {
3015
+ return new Observable(observer => {
3016
+ const reader = new FileReader();
3017
+ reader.onload = () => {
3018
+ observer.next(String(reader.result));
3019
+ observer.complete();
3020
+ };
3021
+ reader.onerror = (err) => observer.error(err);
3022
+ reader.readAsDataURL(blob);
3023
+ });
3024
+ }
3025
+ renameFileForFormat(name, mime) {
3026
+ const ext = mime === 'image/webp' ? '.webp' : mime === 'image/png' ? '.png' : '.jpg';
3027
+ return name.replace(/\.[^/.]+$/, '') + ext;
3028
+ }
3029
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: CustomInputComponent, deps: [{ token: i1$1.Router }], target: i0.ɵɵFactoryTarget.Component });
3030
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "20.3.16", type: CustomInputComponent, isStandalone: true, selector: "lib-custom-input", ngImport: i0, template: "<div class=\"try-overlay\">\r\n <div class=\"try-container\" (click)=\"$event.stopPropagation()\">\r\n <header class=\"try-header\">\r\n <h1>Try Your Own Data</h1>\r\n <a class=\"close-btn\" (click)=\"close()\" aria-label=\"Close\">\u00D7</a>\r\n </header>\r\n <p class=\"lead\">\r\n Paste interests (one per line) and upload two images. Click \"Save & View\"\r\n to preview in the main comparison view.\r\n </p>\r\n\r\n <section class=\"try-grid\">\r\n <div class=\"card\">\r\n <h2>Person 1</h2>\r\n <div class=\"field\">\r\n <label>Image</label>\r\n <input\r\n type=\"file\"\r\n accept=\"image/*\"\r\n (change)=\"onUser1FileChange($event)\"\r\n />\r\n <div class=\"preview\" *ngIf=\"user1Preview\">\r\n <img [src]=\"user1Preview\" alt=\"User 1 preview\" />\r\n </div>\r\n </div>\r\n <div class=\"field\">\r\n <label>Interests (one per line)</label>\r\n <textarea\r\n [(ngModel)]=\"person1Text\"\r\n rows=\"10\"\r\n placeholder=\"e.g.Photography,Travel,Hiking\"\r\n ></textarea>\r\n </div>\r\n </div>\r\n\r\n <div class=\"card\">\r\n <h2>Person 2</h2>\r\n <div class=\"field\">\r\n <label>Image</label>\r\n <input\r\n type=\"file\"\r\n accept=\"image/*\"\r\n (change)=\"onUser2FileChange($event)\"\r\n />\r\n <div class=\"preview\" *ngIf=\"user2Preview\">\r\n <img [src]=\"user2Preview\" alt=\"User 2 preview\" />\r\n </div>\r\n </div>\r\n <div class=\"field\">\r\n <label>Interests (one per line)</label>\r\n <textarea\r\n [(ngModel)]=\"person2Text\"\r\n rows=\"10\"\r\n placeholder=\"e.g.Music,Movies,Soccer\"\r\n ></textarea>\r\n </div>\r\n </div>\r\n\r\n <!-- <div class=\"card\">\r\n <h2>Person 3 (Optional)</h2>\r\n <div class=\"field\">\r\n <label>Interests (one per line)</label>\r\n <textarea [(ngModel)]=\"person3Text\" rows=\"10\" placeholder=\"Optional third list\"></textarea>\r\n </div>\r\n <div class=\"hint\">\r\n <p>Tip: Leave this empty if you only want to compare two lists.</p>\r\n </div>\r\n </div> -->\r\n </section>\r\n\r\n <footer class=\"try-actions\">\r\n <button class=\"btn primary\" (click)=\"saveAndView()\" [disabled]=\"saving\">\r\n {{ saving ? \"Saving...\" : \"Save & View\" }}\r\n </button>\r\n <button class=\"btn\" (click)=\"clearAll()\" [disabled]=\"saving\">\r\n Clear\r\n </button>\r\n <a class=\"btn link\" (click)=\"close()\">Back to Home</a>\r\n </footer>\r\n\r\n <p class=\"error\" *ngIf=\"error\">{{ error }}</p>\r\n </div>\r\n</div>\r\n", styles: [".try-overlay{position:fixed;inset:0;background:#0a0e17bf;z-index:2000;overflow:auto}.try-container{box-sizing:border-box;max-width:900px;margin:40px auto;background:#0f172a;color:#e2e8f0;border-radius:12px;padding:24px 28px;box-shadow:0 12px 40px #00000059}.try-header{display:flex;align-items:center;justify-content:space-between;gap:16px;margin-bottom:8px}.try-header h1{margin:0;font-size:22px}.close-btn{color:#94a3b8;text-decoration:none;font-size:24px;line-height:1;border:1px solid #334155;border-radius:8px;padding:2px 10px;background:#0b1220;cursor:pointer}.lead{margin:0 0 12px;color:#cbd5e1}.try-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(280px,1fr));gap:16px}.card{background:#0b1220;border:1px solid #334155;border-radius:12px;padding:16px;box-shadow:0 4px 12px #0000000a}.field{margin:12px 0}.field label{font-weight:600;display:block;margin-bottom:6px}.field textarea{background:#0a1020;color:#e2e8f0;border:1px solid #334155;border-radius:8px;padding:8px 10px;resize:vertical;min-height:160px}.preview{margin-top:8px}.preview img{width:100%;max-height:220px;object-fit:cover;border-radius:8px;border:1px solid #334155}.try-actions{display:flex;gap:10px;margin-top:18px;align-items:center}.btn{background:#2563eb;color:#fff;padding:8px 14px;border-radius:8px;border:none;cursor:pointer;text-decoration:none}.btn.primary{background:#2563eb;color:#fff;border-color:transparent}.btn[disabled]{opacity:.6;cursor:not-allowed}.error{color:#b91c1c;font-weight:600;margin-top:12px}.hint{color:#6b7280;font-size:13px}\n"], dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "directive", type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "ngmodule", type: FormsModule }, { kind: "directive", type: i3.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { kind: "directive", type: i3.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i3.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }] });
3031
+ }
3032
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: CustomInputComponent, decorators: [{
3033
+ type: Component,
3034
+ args: [{ selector: 'lib-custom-input', standalone: true, imports: [CommonModule, FormsModule], template: "<div class=\"try-overlay\">\r\n <div class=\"try-container\" (click)=\"$event.stopPropagation()\">\r\n <header class=\"try-header\">\r\n <h1>Try Your Own Data</h1>\r\n <a class=\"close-btn\" (click)=\"close()\" aria-label=\"Close\">\u00D7</a>\r\n </header>\r\n <p class=\"lead\">\r\n Paste interests (one per line) and upload two images. Click \"Save & View\"\r\n to preview in the main comparison view.\r\n </p>\r\n\r\n <section class=\"try-grid\">\r\n <div class=\"card\">\r\n <h2>Person 1</h2>\r\n <div class=\"field\">\r\n <label>Image</label>\r\n <input\r\n type=\"file\"\r\n accept=\"image/*\"\r\n (change)=\"onUser1FileChange($event)\"\r\n />\r\n <div class=\"preview\" *ngIf=\"user1Preview\">\r\n <img [src]=\"user1Preview\" alt=\"User 1 preview\" />\r\n </div>\r\n </div>\r\n <div class=\"field\">\r\n <label>Interests (one per line)</label>\r\n <textarea\r\n [(ngModel)]=\"person1Text\"\r\n rows=\"10\"\r\n placeholder=\"e.g.Photography,Travel,Hiking\"\r\n ></textarea>\r\n </div>\r\n </div>\r\n\r\n <div class=\"card\">\r\n <h2>Person 2</h2>\r\n <div class=\"field\">\r\n <label>Image</label>\r\n <input\r\n type=\"file\"\r\n accept=\"image/*\"\r\n (change)=\"onUser2FileChange($event)\"\r\n />\r\n <div class=\"preview\" *ngIf=\"user2Preview\">\r\n <img [src]=\"user2Preview\" alt=\"User 2 preview\" />\r\n </div>\r\n </div>\r\n <div class=\"field\">\r\n <label>Interests (one per line)</label>\r\n <textarea\r\n [(ngModel)]=\"person2Text\"\r\n rows=\"10\"\r\n placeholder=\"e.g.Music,Movies,Soccer\"\r\n ></textarea>\r\n </div>\r\n </div>\r\n\r\n <!-- <div class=\"card\">\r\n <h2>Person 3 (Optional)</h2>\r\n <div class=\"field\">\r\n <label>Interests (one per line)</label>\r\n <textarea [(ngModel)]=\"person3Text\" rows=\"10\" placeholder=\"Optional third list\"></textarea>\r\n </div>\r\n <div class=\"hint\">\r\n <p>Tip: Leave this empty if you only want to compare two lists.</p>\r\n </div>\r\n </div> -->\r\n </section>\r\n\r\n <footer class=\"try-actions\">\r\n <button class=\"btn primary\" (click)=\"saveAndView()\" [disabled]=\"saving\">\r\n {{ saving ? \"Saving...\" : \"Save & View\" }}\r\n </button>\r\n <button class=\"btn\" (click)=\"clearAll()\" [disabled]=\"saving\">\r\n Clear\r\n </button>\r\n <a class=\"btn link\" (click)=\"close()\">Back to Home</a>\r\n </footer>\r\n\r\n <p class=\"error\" *ngIf=\"error\">{{ error }}</p>\r\n </div>\r\n</div>\r\n", styles: [".try-overlay{position:fixed;inset:0;background:#0a0e17bf;z-index:2000;overflow:auto}.try-container{box-sizing:border-box;max-width:900px;margin:40px auto;background:#0f172a;color:#e2e8f0;border-radius:12px;padding:24px 28px;box-shadow:0 12px 40px #00000059}.try-header{display:flex;align-items:center;justify-content:space-between;gap:16px;margin-bottom:8px}.try-header h1{margin:0;font-size:22px}.close-btn{color:#94a3b8;text-decoration:none;font-size:24px;line-height:1;border:1px solid #334155;border-radius:8px;padding:2px 10px;background:#0b1220;cursor:pointer}.lead{margin:0 0 12px;color:#cbd5e1}.try-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(280px,1fr));gap:16px}.card{background:#0b1220;border:1px solid #334155;border-radius:12px;padding:16px;box-shadow:0 4px 12px #0000000a}.field{margin:12px 0}.field label{font-weight:600;display:block;margin-bottom:6px}.field textarea{background:#0a1020;color:#e2e8f0;border:1px solid #334155;border-radius:8px;padding:8px 10px;resize:vertical;min-height:160px}.preview{margin-top:8px}.preview img{width:100%;max-height:220px;object-fit:cover;border-radius:8px;border:1px solid #334155}.try-actions{display:flex;gap:10px;margin-top:18px;align-items:center}.btn{background:#2563eb;color:#fff;padding:8px 14px;border-radius:8px;border:none;cursor:pointer;text-decoration:none}.btn.primary{background:#2563eb;color:#fff;border-color:transparent}.btn[disabled]{opacity:.6;cursor:not-allowed}.error{color:#b91c1c;font-weight:600;margin-top:12px}.hint{color:#6b7280;font-size:13px}\n"] }]
3035
+ }], ctorParameters: () => [{ type: i1$1.Router }] });
3036
+
3037
+ class DocumentationComponent {
3038
+ openaiEmbeddingService;
3039
+ profileService;
3040
+ router;
3041
+ // API key state
3042
+ apiKeyInput = '';
3043
+ // Embedding similarity demo
3044
+ textA = 'football';
3045
+ textB = 'soccer';
3046
+ embeddingScore = null;
3047
+ embeddingLoading = false;
3048
+ embeddingError = null;
3049
+ // Best matching pairs demo
3050
+ listAInput = 'music\nphotography\ntravel\nrock climbing';
3051
+ listBInput = 'hiking\nsoccer\nphotograph\nmovies';
3052
+ matchingPairs = [];
3053
+ pairsLoading = false;
3054
+ pairsError = null;
3055
+ // Face detect demo
3056
+ faceFile = null;
3057
+ faceResult = null;
3058
+ faceLoading = false;
3059
+ faceError = null;
3060
+ // Text similarity via API Ninjas
3061
+ compareA = 'football';
3062
+ compareB = 'soccer';
3063
+ apiSimilarity = null;
3064
+ apiLoading = false;
3065
+ apiError = null;
3066
+ // OpenAI key state
3067
+ openaiKeyInput = '';
3068
+ defaultApiNinjasKey = '';
3069
+ defaultOpenaiApiKey = '';
3070
+ constructor(openaiEmbeddingService, profileService, router) {
3071
+ this.openaiEmbeddingService = openaiEmbeddingService;
3072
+ this.profileService = profileService;
3073
+ this.router = router;
3074
+ }
3075
+ ngOnInit() {
3076
+ // Prefill API key from env or localStorage
3077
+ const saved = this.getStoredKey();
3078
+ if (saved) {
3079
+ this.apiKeyInput = saved;
3080
+ }
3081
+ else {
3082
+ this.apiKeyInput = this.defaultApiNinjasKey || '';
3083
+ }
3084
+ }
3085
+ // --- API Key helpers ---
3086
+ getActiveApiKey() {
3087
+ return this.apiKeyInput?.trim() || this.defaultApiNinjasKey;
3088
+ }
3089
+ saveKey() {
3090
+ try {
3091
+ localStorage.setItem('apiNinjasCustomKey', this.apiKeyInput.trim());
3092
+ }
3093
+ catch (e) {
3094
+ console.warn('Could not persist API key', e);
3095
+ }
3096
+ }
3097
+ getStoredKey() {
3098
+ try {
3099
+ return localStorage.getItem('apiNinjasCustomKey');
3100
+ }
3101
+ catch {
3102
+ return null;
3103
+ }
3104
+ }
3105
+ // --- OpenAI Key helpers ---
3106
+ getActiveOpenAIKey() {
3107
+ return this.openaiKeyInput?.trim() || this.defaultOpenaiApiKey;
3108
+ }
3109
+ saveOpenAIKey() {
3110
+ try {
3111
+ localStorage.setItem('openaiCustomKey', this.openaiKeyInput.trim());
3112
+ }
3113
+ catch (e) {
3114
+ console.warn('Could not persist OpenAI key', e);
3115
+ }
3116
+ }
3117
+ // --- Embedding similarity demo (now uses OpenAI) ---
3118
+ runEmbeddingSimilarity() {
3119
+ this.embeddingLoading = true;
3120
+ this.embeddingError = null;
3121
+ this.embeddingScore = null;
3122
+ this.openaiEmbeddingService
3123
+ .calculateSimilarity(this.textA, this.textB, this.getActiveOpenAIKey())
3124
+ .subscribe({
3125
+ next: (score) => {
3126
+ this.embeddingScore = score;
3127
+ this.embeddingLoading = false;
3128
+ },
3129
+ error: (err) => {
3130
+ this.embeddingError = err?.message || 'Failed to compute similarity';
3131
+ this.embeddingLoading = false;
3132
+ },
3133
+ });
3134
+ }
3135
+ // --- Best matching pairs demo ---
3136
+ runBestPairs() {
3137
+ const listA = this.listAInput
3138
+ .split('\n')
3139
+ .map((s) => s.trim())
3140
+ .filter(Boolean);
3141
+ const listB = this.listBInput
3142
+ .split('\n')
3143
+ .map((s) => s.trim())
3144
+ .filter(Boolean);
3145
+ if (listA.length === 0 || listB.length === 0) {
3146
+ this.pairsError = 'Both lists must contain at least one item.';
3147
+ return;
3148
+ }
3149
+ this.pairsLoading = true;
3150
+ this.pairsError = null;
3151
+ this.matchingPairs = [];
3152
+ // Note: Best matching pairs demo is currently disabled as it's not needed for showcase functionality
3153
+ // The OpenAI service focuses on core text similarity for the main alignment algorithm
3154
+ this.pairsError = 'This demo is currently disabled. The showcase uses OpenAI embeddings for core text similarity.';
3155
+ this.pairsLoading = false;
3156
+ /* Original TensorFlow implementation:
3157
+ this.embeddingService
3158
+ .findBestMatchingPairs(listA, listB, 0.15)
3159
+ .subscribe({
3160
+ next: (pairs: IWordPair[]) => {
3161
+ this.matchingPairs = pairs;
3162
+ this.pairsLoading = false;
3163
+ },
3164
+ error: (err: Error | HttpErrorResponse) => {
3165
+ this.pairsError = (err as Error)?.message || 'Failed to compute pairs';
3166
+ this.pairsLoading = false;
3167
+ },
3168
+ });
3169
+ */
3170
+ }
3171
+ // --- Face detection demo ---
3172
+ onFaceFileChange(event) {
3173
+ const input = event.target;
3174
+ const file = input.files && input.files.length ? input.files[0] : null;
3175
+ this.faceFile = file;
3176
+ }
3177
+ runFaceDetect() {
3178
+ this.faceResult = null;
3179
+ this.faceError = null;
3180
+ if (!this.faceFile) {
3181
+ this.faceError = 'Please choose an image file.';
3182
+ return;
3183
+ }
3184
+ this.faceLoading = true;
3185
+ this.profileService.detectFace(this.faceFile).subscribe({
3186
+ next: (res) => {
3187
+ this.faceResult = res;
3188
+ this.faceLoading = false;
3189
+ },
3190
+ error: (err) => {
3191
+ this.faceError = err?.error?.message || err?.message || 'Face detection failed';
3192
+ this.faceLoading = false;
3193
+ }
3194
+ });
3195
+ }
3196
+ // --- API Ninjas text similarity demo ---
3197
+ runApiSimilarity() {
3198
+ this.apiSimilarity = null;
3199
+ this.apiError = null;
3200
+ this.apiLoading = true;
3201
+ this.profileService
3202
+ .compareInterests(this.compareA, this.compareB, this.getActiveApiKey())
3203
+ .subscribe({
3204
+ next: (res) => {
3205
+ this.apiSimilarity = res.similarity;
3206
+ this.apiLoading = false;
3207
+ },
3208
+ error: (err) => {
3209
+ this.apiError = err?.error?.message || err?.message || 'API call failed';
3210
+ this.apiLoading = false;
3211
+ },
3212
+ });
3213
+ }
3214
+ // --- Helper label/format methods to simplify template expressions ---
3215
+ similarityLabel() {
3216
+ if (this.embeddingScore === null)
3217
+ return '';
3218
+ return `Similarity: ${this.formatNumber(this.embeddingScore, 2, 4)}`;
3219
+ }
3220
+ apiSimilarityLabel() {
3221
+ if (this.apiSimilarity === null)
3222
+ return '';
3223
+ return `Similarity: ${this.formatNumber(this.apiSimilarity, 2, 4)}`;
3224
+ }
3225
+ pairLabel(p) {
3226
+ return `${p.wordA} ⇄ ${p.wordB} — ${this.formatNumber(p.score * 100, 1, 1)}%`;
3227
+ }
3228
+ faceResultJson() {
3229
+ try {
3230
+ return JSON.stringify(this.faceResult, null, 2);
3231
+ }
3232
+ catch {
3233
+ return String(this.faceResult);
3234
+ }
3235
+ }
3236
+ formatNumber(value, min = 2, max = 2) {
3237
+ try {
3238
+ return new Intl.NumberFormat('en-US', { minimumFractionDigits: min, maximumFractionDigits: max }).format(value);
3239
+ }
3240
+ catch {
3241
+ return value.toFixed(max);
3242
+ }
3243
+ }
3244
+ // --- Navigation ---
3245
+ goHome(event) {
3246
+ event.preventDefault();
3247
+ this.router.navigateByUrl('/');
3248
+ }
3249
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: DocumentationComponent, deps: [{ token: OpenAIEmbeddingService }, { token: ProfileService }, { token: i1$1.Router }], target: i0.ɵɵFactoryTarget.Component });
3250
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "20.3.16", type: DocumentationComponent, isStandalone: true, selector: "lib-documentation", inputs: { defaultApiNinjasKey: "defaultApiNinjasKey", defaultOpenaiApiKey: "defaultOpenaiApiKey" }, ngImport: i0, template: "<div class=\"docs-overlay\">\r\n <div class=\"docs-container\">\r\n <div class=\"docs-header\">\r\n <h1>Profile Comparison Library \u2014 Developer Docs</h1>\r\n <a class=\"close-btn\" href=\"/\" (click)=\"goHome($event)\" aria-label=\"Close\">\u00D7</a>\r\n </div>\r\n\r\n <p class=\"lead\">\r\n This page documents how to use the library services and lets you test them live inside the showcase.\r\n Existing showcase UI remains unchanged \u2014 this is a standalone docs screen reachable from the \"Docs\" link.\r\n </p>\r\n\r\n <section class=\"section\">\r\n <h2>Quickstart</h2>\r\n <ol>\r\n <li><strong>Build the library</strong>: run <code>ng build profile-comparison-lib</code>.</li>\r\n <li><strong>Import</strong>: <code>import &#123; ProfileComparisonLibModule, EmbeddingService, ProfileService &#125; from 'profile-comparison-lib';</code></li>\r\n <li><strong>Provide HttpClient</strong>: ensure <code>HttpClientModule</code> is imported in your app module.</li>\r\n <li><strong>API Key</strong>: for text similarity/face detect APIs, pass an API Ninjas key.</li>\r\n <li><strong>Run Showcase</strong>: <code>ng serve showcase</code> (or <code>npm run serve:showcase</code>), then open <code>/docs</code>.</li>\r\n </ol>\r\n <p>\r\n Full README is in <code>README.md</code> at the repository root.\r\n </p>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>Set API Key</h2>\r\n <div class=\"row\">\r\n <input type=\"text\" [(ngModel)]=\"apiKeyInput\" placeholder=\"API Ninjas Key\" />\r\n <button (click)=\"saveKey()\">Save</button>\r\n </div>\r\n <p class=\"hint\">Your key is stored in localStorage. Provide a valid key; rate limits apply.</p>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>Embedding Similarity (TensorFlow.js)</h2>\r\n <div class=\"row\">\r\n <input type=\"text\" [(ngModel)]=\"textA\" placeholder=\"Text A\" />\r\n <input type=\"text\" [(ngModel)]=\"textB\" placeholder=\"Text B\" />\r\n <button (click)=\"runEmbeddingSimilarity()\" [disabled]=\"embeddingLoading\">Run</button>\r\n </div>\r\n <div class=\"result\" *ngIf=\"embeddingLoading\">Computing\u2026</div>\r\n <div class=\"result\" *ngIf=\"embeddingError\" [textContent]=\"'Error: ' + embeddingError\"></div>\r\n <div class=\"result\" *ngIf=\"embeddingScore !== null\" [textContent]=\"similarityLabel()\"></div>\r\n <details>\r\n <summary>Usage</summary>\r\n <pre ngNonBindable><code>constructor(private embedding: EmbeddingService) &#123;&#125;\r\nthis.embedding.calculateSimilarity('football','soccer')\r\n .subscribe(score =&gt; console.log(score));</code></pre>\r\n </details>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>Best Matching Pairs</h2>\r\n <div class=\"cols\">\r\n <textarea [(ngModel)]=\"listAInput\" rows=\"6\" spellcheck=\"false\"></textarea>\r\n <textarea [(ngModel)]=\"listBInput\" rows=\"6\" spellcheck=\"false\"></textarea>\r\n </div>\r\n <button class=\"spaced-btn\" (click)=\"runBestPairs()\" [disabled]=\"pairsLoading\">Find Pairs</button>\r\n <div class=\"result\" *ngIf=\"pairsLoading\">Processing\u2026</div>\r\n <div class=\"result\" *ngIf=\"pairsError\" [textContent]=\"'Error: ' + pairsError\"></div>\r\n <ul class=\"pairs\" *ngIf=\"!pairsLoading && matchingPairs.length\">\r\n <li *ngFor=\"let p of matchingPairs\" [textContent]=\"pairLabel(p)\"></li>\r\n </ul>\r\n <details>\r\n <summary>Usage</summary>\r\n <pre ngNonBindable><code>this.embedding.findBestMatchingPairs(listA, listB, 0.15)\r\n .subscribe(pairs =&gt; console.log(pairs));</code></pre>\r\n </details>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>API Ninjas: Text Similarity</h2>\r\n <div class=\"row\">\r\n <input type=\"text\" [(ngModel)]=\"compareA\" placeholder=\"Text A\" />\r\n <input type=\"text\" [(ngModel)]=\"compareB\" placeholder=\"Text B\" />\r\n <button (click)=\"runApiSimilarity()\" [disabled]=\"apiLoading\">Compare</button>\r\n </div>\r\n <div class=\"result\" *ngIf=\"apiLoading\">Calling API\u2026</div>\r\n <div class=\"result\" *ngIf=\"apiError\" [textContent]=\"'Error: ' + apiError\"></div>\r\n <div class=\"result\" *ngIf=\"apiSimilarity !== null\" [textContent]=\"apiSimilarityLabel()\"></div>\r\n <details>\r\n <summary>Usage</summary>\r\n <pre><code>this.profileService.compareInterests(a, b, apiKey)\r\n .subscribe((res: &#123; similarity: number &#125;) =&gt; console.log(res.similarity));</code></pre>\r\n </details>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>Face Detection</h2>\r\n <div class=\"row\">\r\n <input type=\"file\" (change)=\"onFaceFileChange($event)\" accept=\"image/*\" />\r\n <button (click)=\"runFaceDetect()\" [disabled]=\"faceLoading\">Detect Face</button>\r\n </div>\r\n <div class=\"result\" *ngIf=\"faceLoading\">Calling API\u2026</div>\r\n <div class=\"result\" *ngIf=\"faceError\" [textContent]=\"'Error: ' + faceError\"></div>\r\n <pre class=\"result\" *ngIf=\"!faceLoading && faceResult\" [textContent]=\"faceResultJson()\"></pre>\r\n <details>\r\n <summary>Usage</summary>\r\n <pre ngNonBindable><code>this.profileService.detectFace(file, apiKey)\r\n .subscribe(res =&gt; console.log(res));</code></pre>\r\n </details>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>Module Import</h2>\r\n <pre ngNonBindable><code>import &#123; HttpClientModule &#125; from '@angular/common/http';\r\nimport &#123; ProfileComparisonLibModule &#125; from 'profile-comparison-lib';\r\n\r\n@NgModule(&#123;\r\n imports: [HttpClientModule, ProfileComparisonLibModule]\r\n&#125;)\r\nexport class AppModule &#123;&#125;</code></pre>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>Services Overview</h2>\r\n <ul>\r\n <li><strong>EmbeddingService</strong>: <code>calculateSimilarity(textA, textB)</code>, <code>findBestMatchingPairs(listA, listB, threshold?)</code></li>\r\n <li><strong>ProfileService</strong>: <code>compareInterests(a, b, apiKey)</code>, <code>detectFace(file, apiKey)</code></li>\r\n </ul>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>Exports</h2>\r\n <ul>\r\n <li><code>ProfileComparisonLibModule</code></li>\r\n <li><code>EmbeddingService</code></li>\r\n <li><code>ProfileService</code></li>\r\n <li><code>WordPair</code> (interface)</li>\r\n <li><code>WordAlignment</code> (interface)</li>\r\n </ul>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>Troubleshooting</h2>\r\n <ul>\r\n <li><strong>API key/Quota</strong>: If calls fail, verify the API key and check rate limits.</li>\r\n <li><strong>Network</strong>: Ensure CORS and network connectivity are OK.</li>\r\n <li><strong>Build</strong>: Run <code>ng build profile-comparison-lib</code> after library changes.</li>\r\n </ul>\r\n </section>\r\n </div>\r\n</div>\r\n", styles: [".docs-overlay{position:fixed;inset:0;background:#0a0e17bf;z-index:2000;overflow:auto}.docs-container{box-sizing:border-box;max-width:900px;margin:40px auto;background:#0f172a;color:#e2e8f0;border-radius:12px;padding:24px 28px;box-shadow:0 12px 40px #00000059}.docs-header{display:flex;align-items:center;justify-content:space-between;gap:16px;margin-bottom:8px}.docs-header h1{margin:0;font-size:22px}.close-btn{color:#94a3b8;text-decoration:none;font-size:24px;line-height:1;border:1px solid #334155;border-radius:8px;padding:2px 10px;background:#0b1220}.lead{margin:0 0 12px;color:#cbd5e1}.section{border:1px solid #334155;border-radius:10px;padding:16px;margin:14px 0;background:#0b1220}.section h2{margin:0 0 12px;font-size:18px}.row{display:flex;gap:10px;align-items:center;flex-wrap:wrap}.row input[type=text],.row input[type=file],textarea{flex:1 1 280px;background:#0a1020;color:#e2e8f0;border:1px solid #334155;border-radius:8px;padding:8px 10px}textarea{min-height:100px}button{background:#2563eb;color:#fff;border:none;border-radius:8px;padding:8px 12px;cursor:pointer}.result{margin-top:8px;color:#cbd5e1}.cols{display:grid;grid-template-columns:1fr 1fr;gap:10px}.pairs{margin:10px 0 0;padding-left:18px}.hint{color:#94a3b8;margin-top:4px;font-size:12px}.spaced-btn{margin-top:10px;margin-bottom:10px}\n"], dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "directive", type: i2.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "ngmodule", type: FormsModule }, { kind: "directive", type: i3.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { kind: "directive", type: i3.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i3.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }] });
3251
+ }
3252
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: DocumentationComponent, decorators: [{
3253
+ type: Component,
3254
+ args: [{ selector: 'lib-documentation', standalone: true, imports: [CommonModule, FormsModule], template: "<div class=\"docs-overlay\">\r\n <div class=\"docs-container\">\r\n <div class=\"docs-header\">\r\n <h1>Profile Comparison Library \u2014 Developer Docs</h1>\r\n <a class=\"close-btn\" href=\"/\" (click)=\"goHome($event)\" aria-label=\"Close\">\u00D7</a>\r\n </div>\r\n\r\n <p class=\"lead\">\r\n This page documents how to use the library services and lets you test them live inside the showcase.\r\n Existing showcase UI remains unchanged \u2014 this is a standalone docs screen reachable from the \"Docs\" link.\r\n </p>\r\n\r\n <section class=\"section\">\r\n <h2>Quickstart</h2>\r\n <ol>\r\n <li><strong>Build the library</strong>: run <code>ng build profile-comparison-lib</code>.</li>\r\n <li><strong>Import</strong>: <code>import &#123; ProfileComparisonLibModule, EmbeddingService, ProfileService &#125; from 'profile-comparison-lib';</code></li>\r\n <li><strong>Provide HttpClient</strong>: ensure <code>HttpClientModule</code> is imported in your app module.</li>\r\n <li><strong>API Key</strong>: for text similarity/face detect APIs, pass an API Ninjas key.</li>\r\n <li><strong>Run Showcase</strong>: <code>ng serve showcase</code> (or <code>npm run serve:showcase</code>), then open <code>/docs</code>.</li>\r\n </ol>\r\n <p>\r\n Full README is in <code>README.md</code> at the repository root.\r\n </p>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>Set API Key</h2>\r\n <div class=\"row\">\r\n <input type=\"text\" [(ngModel)]=\"apiKeyInput\" placeholder=\"API Ninjas Key\" />\r\n <button (click)=\"saveKey()\">Save</button>\r\n </div>\r\n <p class=\"hint\">Your key is stored in localStorage. Provide a valid key; rate limits apply.</p>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>Embedding Similarity (TensorFlow.js)</h2>\r\n <div class=\"row\">\r\n <input type=\"text\" [(ngModel)]=\"textA\" placeholder=\"Text A\" />\r\n <input type=\"text\" [(ngModel)]=\"textB\" placeholder=\"Text B\" />\r\n <button (click)=\"runEmbeddingSimilarity()\" [disabled]=\"embeddingLoading\">Run</button>\r\n </div>\r\n <div class=\"result\" *ngIf=\"embeddingLoading\">Computing\u2026</div>\r\n <div class=\"result\" *ngIf=\"embeddingError\" [textContent]=\"'Error: ' + embeddingError\"></div>\r\n <div class=\"result\" *ngIf=\"embeddingScore !== null\" [textContent]=\"similarityLabel()\"></div>\r\n <details>\r\n <summary>Usage</summary>\r\n <pre ngNonBindable><code>constructor(private embedding: EmbeddingService) &#123;&#125;\r\nthis.embedding.calculateSimilarity('football','soccer')\r\n .subscribe(score =&gt; console.log(score));</code></pre>\r\n </details>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>Best Matching Pairs</h2>\r\n <div class=\"cols\">\r\n <textarea [(ngModel)]=\"listAInput\" rows=\"6\" spellcheck=\"false\"></textarea>\r\n <textarea [(ngModel)]=\"listBInput\" rows=\"6\" spellcheck=\"false\"></textarea>\r\n </div>\r\n <button class=\"spaced-btn\" (click)=\"runBestPairs()\" [disabled]=\"pairsLoading\">Find Pairs</button>\r\n <div class=\"result\" *ngIf=\"pairsLoading\">Processing\u2026</div>\r\n <div class=\"result\" *ngIf=\"pairsError\" [textContent]=\"'Error: ' + pairsError\"></div>\r\n <ul class=\"pairs\" *ngIf=\"!pairsLoading && matchingPairs.length\">\r\n <li *ngFor=\"let p of matchingPairs\" [textContent]=\"pairLabel(p)\"></li>\r\n </ul>\r\n <details>\r\n <summary>Usage</summary>\r\n <pre ngNonBindable><code>this.embedding.findBestMatchingPairs(listA, listB, 0.15)\r\n .subscribe(pairs =&gt; console.log(pairs));</code></pre>\r\n </details>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>API Ninjas: Text Similarity</h2>\r\n <div class=\"row\">\r\n <input type=\"text\" [(ngModel)]=\"compareA\" placeholder=\"Text A\" />\r\n <input type=\"text\" [(ngModel)]=\"compareB\" placeholder=\"Text B\" />\r\n <button (click)=\"runApiSimilarity()\" [disabled]=\"apiLoading\">Compare</button>\r\n </div>\r\n <div class=\"result\" *ngIf=\"apiLoading\">Calling API\u2026</div>\r\n <div class=\"result\" *ngIf=\"apiError\" [textContent]=\"'Error: ' + apiError\"></div>\r\n <div class=\"result\" *ngIf=\"apiSimilarity !== null\" [textContent]=\"apiSimilarityLabel()\"></div>\r\n <details>\r\n <summary>Usage</summary>\r\n <pre><code>this.profileService.compareInterests(a, b, apiKey)\r\n .subscribe((res: &#123; similarity: number &#125;) =&gt; console.log(res.similarity));</code></pre>\r\n </details>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>Face Detection</h2>\r\n <div class=\"row\">\r\n <input type=\"file\" (change)=\"onFaceFileChange($event)\" accept=\"image/*\" />\r\n <button (click)=\"runFaceDetect()\" [disabled]=\"faceLoading\">Detect Face</button>\r\n </div>\r\n <div class=\"result\" *ngIf=\"faceLoading\">Calling API\u2026</div>\r\n <div class=\"result\" *ngIf=\"faceError\" [textContent]=\"'Error: ' + faceError\"></div>\r\n <pre class=\"result\" *ngIf=\"!faceLoading && faceResult\" [textContent]=\"faceResultJson()\"></pre>\r\n <details>\r\n <summary>Usage</summary>\r\n <pre ngNonBindable><code>this.profileService.detectFace(file, apiKey)\r\n .subscribe(res =&gt; console.log(res));</code></pre>\r\n </details>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>Module Import</h2>\r\n <pre ngNonBindable><code>import &#123; HttpClientModule &#125; from '@angular/common/http';\r\nimport &#123; ProfileComparisonLibModule &#125; from 'profile-comparison-lib';\r\n\r\n@NgModule(&#123;\r\n imports: [HttpClientModule, ProfileComparisonLibModule]\r\n&#125;)\r\nexport class AppModule &#123;&#125;</code></pre>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>Services Overview</h2>\r\n <ul>\r\n <li><strong>EmbeddingService</strong>: <code>calculateSimilarity(textA, textB)</code>, <code>findBestMatchingPairs(listA, listB, threshold?)</code></li>\r\n <li><strong>ProfileService</strong>: <code>compareInterests(a, b, apiKey)</code>, <code>detectFace(file, apiKey)</code></li>\r\n </ul>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>Exports</h2>\r\n <ul>\r\n <li><code>ProfileComparisonLibModule</code></li>\r\n <li><code>EmbeddingService</code></li>\r\n <li><code>ProfileService</code></li>\r\n <li><code>WordPair</code> (interface)</li>\r\n <li><code>WordAlignment</code> (interface)</li>\r\n </ul>\r\n </section>\r\n\r\n <section class=\"section\">\r\n <h2>Troubleshooting</h2>\r\n <ul>\r\n <li><strong>API key/Quota</strong>: If calls fail, verify the API key and check rate limits.</li>\r\n <li><strong>Network</strong>: Ensure CORS and network connectivity are OK.</li>\r\n <li><strong>Build</strong>: Run <code>ng build profile-comparison-lib</code> after library changes.</li>\r\n </ul>\r\n </section>\r\n </div>\r\n</div>\r\n", styles: [".docs-overlay{position:fixed;inset:0;background:#0a0e17bf;z-index:2000;overflow:auto}.docs-container{box-sizing:border-box;max-width:900px;margin:40px auto;background:#0f172a;color:#e2e8f0;border-radius:12px;padding:24px 28px;box-shadow:0 12px 40px #00000059}.docs-header{display:flex;align-items:center;justify-content:space-between;gap:16px;margin-bottom:8px}.docs-header h1{margin:0;font-size:22px}.close-btn{color:#94a3b8;text-decoration:none;font-size:24px;line-height:1;border:1px solid #334155;border-radius:8px;padding:2px 10px;background:#0b1220}.lead{margin:0 0 12px;color:#cbd5e1}.section{border:1px solid #334155;border-radius:10px;padding:16px;margin:14px 0;background:#0b1220}.section h2{margin:0 0 12px;font-size:18px}.row{display:flex;gap:10px;align-items:center;flex-wrap:wrap}.row input[type=text],.row input[type=file],textarea{flex:1 1 280px;background:#0a1020;color:#e2e8f0;border:1px solid #334155;border-radius:8px;padding:8px 10px}textarea{min-height:100px}button{background:#2563eb;color:#fff;border:none;border-radius:8px;padding:8px 12px;cursor:pointer}.result{margin-top:8px;color:#cbd5e1}.cols{display:grid;grid-template-columns:1fr 1fr;gap:10px}.pairs{margin:10px 0 0;padding-left:18px}.hint{color:#94a3b8;margin-top:4px;font-size:12px}.spaced-btn{margin-top:10px;margin-bottom:10px}\n"] }]
3255
+ }], ctorParameters: () => [{ type: OpenAIEmbeddingService }, { type: ProfileService }, { type: i1$1.Router }], propDecorators: { defaultApiNinjasKey: [{
3256
+ type: Input
3257
+ }], defaultOpenaiApiKey: [{
3258
+ type: Input
3259
+ }] } });
3260
+
3261
+ class ProfileComparisonLibModule {
3262
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ProfileComparisonLibModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
3263
+ static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.3.16", ngImport: i0, type: ProfileComparisonLibModule, declarations: [ProfileComparisonLibComponent], imports: [CommonModule,
3264
+ HttpClientModule,
3265
+ FormsModule,
3266
+ ReactiveFormsModule,
3267
+ CustomInputComponent,
3268
+ DocumentationComponent,
3269
+ RouterModule], exports: [ProfileComparisonLibComponent,
3270
+ CustomInputComponent,
3271
+ DocumentationComponent] });
3272
+ static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ProfileComparisonLibModule, providers: [
3273
+ ProfileService,
3274
+ EmbeddingService,
3275
+ OpenAIEmbeddingService
3276
+ ], imports: [CommonModule,
3277
+ HttpClientModule,
3278
+ FormsModule,
3279
+ ReactiveFormsModule,
3280
+ CustomInputComponent,
3281
+ DocumentationComponent,
3282
+ RouterModule] });
3283
+ }
3284
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ProfileComparisonLibModule, decorators: [{
3285
+ type: NgModule,
3286
+ args: [{
3287
+ declarations: [
3288
+ ProfileComparisonLibComponent
3289
+ ],
3290
+ imports: [
3291
+ CommonModule,
3292
+ HttpClientModule,
3293
+ FormsModule,
3294
+ ReactiveFormsModule,
3295
+ CustomInputComponent,
3296
+ DocumentationComponent,
3297
+ RouterModule
3298
+ ],
3299
+ exports: [
3300
+ ProfileComparisonLibComponent,
3301
+ CustomInputComponent,
3302
+ DocumentationComponent
3303
+ ],
3304
+ providers: [
3305
+ ProfileService,
3306
+ EmbeddingService,
3307
+ OpenAIEmbeddingService
3308
+ ]
3309
+ }]
3310
+ }] });
3311
+
3312
+ /*
3313
+ * Public API Surface of profile-comparison-lib
3314
+ */
3315
+
3316
+ /**
3317
+ * Generated bundle index. Do not edit.
3318
+ */
3319
+
3320
+ export { CustomInputComponent, DocumentationComponent, EmbeddingService, FileConversionService, ImageCompressionService, OpenAIEmbeddingService, ProfileComparisonLibComponent, ProfileComparisonLibModule, ProfileComparisonLibService, ProfileService };
3321
+ //# sourceMappingURL=naniteninja-profile-comparison-lib.mjs.map