forked from santifer/career-ops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdedup-tracker.mjs
More file actions
206 lines (181 loc) · 6 KB
/
dedup-tracker.mjs
File metadata and controls
206 lines (181 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env node
/**
* dedup-tracker.mjs — Remove duplicate entries from applications.md
*
* Groups by normalized company + fuzzy role match.
* Keeps entry with highest score. If discarded entry had more advanced status,
* preserves that status. Merges notes.
*
* Run: node career-ops/dedup-tracker.mjs [--dry-run]
*/
import { readFileSync, writeFileSync, copyFileSync, existsSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const CAREER_OPS = dirname(fileURLToPath(import.meta.url));
// Support both layouts: data/applications.md (boilerplate) and applications.md (original)
const APPS_FILE = existsSync(join(CAREER_OPS, 'data/applications.md'))
? join(CAREER_OPS, 'data/applications.md')
: join(CAREER_OPS, 'applications.md');
const DRY_RUN = process.argv.includes('--dry-run');
// Status advancement order (higher = more advanced in pipeline)
// Aplicado > Rechazado because active application > terminal state
const STATUS_RANK = {
// English canonicals (states.yml labels)
'skip': 0,
'discarded': 0,
'rejected': 1,
'evaluated': 2,
'applied': 3,
'responded': 4,
'interview': 5,
'offer': 6,
// Spanish aliases — kept for backwards compat with existing tracker data
'no_aplicar': 0,
'no aplicar': 0,
'descartado': 0,
'descartada': 0,
'rechazado': 1, // Terminal — below active states
'rechazada': 1,
'evaluada': 2,
'aplicado': 3,
'respondido': 4,
'entrevista': 5,
'oferta': 6,
};
function normalizeCompany(name) {
return name.toLowerCase()
.replace(/[()]/g, '')
.replace(/\s+/g, ' ')
.replace(/[^a-z0-9 ]/g, '')
.trim();
}
function normalizeRole(role) {
return role.toLowerCase()
.replace(/[()]/g, ' ')
.replace(/\s+/g, ' ')
.replace(/[^a-z0-9 /]/g, '')
.trim();
}
function roleMatch(a, b) {
const wordsA = normalizeRole(a).split(/\s+/).filter(w => w.length > 3);
const wordsB = normalizeRole(b).split(/\s+/).filter(w => w.length > 3);
const overlap = wordsA.filter(w => wordsB.some(wb => wb.includes(w) || w.includes(wb)));
return overlap.length >= 2;
}
function parseScore(s) {
const m = s.replace(/\*\*/g, '').match(/([\d.]+)/);
return m ? parseFloat(m[1]) : 0;
}
function parseAppLine(line) {
const parts = line.split('|').map(s => s.trim());
if (parts.length < 9) return null;
const num = parseInt(parts[1]);
if (isNaN(num)) return null;
return {
num,
date: parts[2],
company: parts[3],
role: parts[4],
score: parts[5],
status: parts[6],
pdf: parts[7],
report: parts[8],
notes: parts[9] || '',
raw: line,
};
}
// Read
if (!existsSync(APPS_FILE)) {
console.log('No applications.md found. Nothing to dedup.');
process.exit(0);
}
const content = readFileSync(APPS_FILE, 'utf-8');
const lines = content.split('\n');
// Parse all entries
const entries = [];
const entryLineMap = new Map(); // num → line index
for (let i = 0; i < lines.length; i++) {
if (!lines[i].startsWith('|')) continue;
const app = parseAppLine(lines[i]);
if (app && app.num > 0) {
entries.push(app);
entryLineMap.set(app.num, i);
}
}
console.log(`📊 ${entries.length} entries loaded`);
// Group by company+role
const groups = new Map();
for (const entry of entries) {
const key = normalizeCompany(entry.company);
if (!groups.has(key)) groups.set(key, []);
groups.get(key).push(entry);
}
// Find duplicates
let removed = 0;
const linesToRemove = new Set();
for (const [company, companyEntries] of groups) {
if (companyEntries.length < 2) continue;
// Within same company, find role matches
const processed = new Set();
for (let i = 0; i < companyEntries.length; i++) {
if (processed.has(i)) continue;
const cluster = [companyEntries[i]];
processed.add(i);
for (let j = i + 1; j < companyEntries.length; j++) {
if (processed.has(j)) continue;
if (roleMatch(companyEntries[i].role, companyEntries[j].role)) {
cluster.push(companyEntries[j]);
processed.add(j);
}
}
if (cluster.length < 2) continue;
// Keep the one with highest score
cluster.sort((a, b) => parseScore(b.score) - parseScore(a.score));
const keeper = cluster[0];
// Check if any removed entry has more advanced status
let bestStatusRank = STATUS_RANK[keeper.status.toLowerCase()] || 0;
let bestStatus = keeper.status;
for (let k = 1; k < cluster.length; k++) {
const rank = STATUS_RANK[cluster[k].status.toLowerCase()] || 0;
if (rank > bestStatusRank) {
bestStatusRank = rank;
bestStatus = cluster[k].status;
}
}
// Update keeper's status if a removed entry had a more advanced one
if (bestStatus !== keeper.status) {
const lineIdx = entryLineMap.get(keeper.num);
if (lineIdx !== undefined) {
const parts = lines[lineIdx].split('|').map(s => s.trim());
parts[6] = bestStatus;
lines[lineIdx] = '| ' + parts.slice(1, -1).join(' | ') + ' |';
console.log(` 📝 #${keeper.num}: status promoted to "${bestStatus}" (from #${cluster.find(e => e.status === bestStatus)?.num})`);
}
}
// Remove duplicates
for (let k = 1; k < cluster.length; k++) {
const dup = cluster[k];
const lineIdx = entryLineMap.get(dup.num);
if (lineIdx !== undefined) {
linesToRemove.add(lineIdx);
removed++;
console.log(`🗑️ Remove #${dup.num} (${dup.company} — ${dup.role}, ${dup.score}) → kept #${keeper.num} (${keeper.score})`);
}
}
}
}
// Remove lines (in reverse order to preserve indices)
const sortedRemoveIndices = [...linesToRemove].sort((a, b) => b - a);
for (const idx of sortedRemoveIndices) {
lines.splice(idx, 1);
}
console.log(`\n📊 ${removed} duplicates removed`);
if (!DRY_RUN && removed > 0) {
copyFileSync(APPS_FILE, APPS_FILE + '.bak');
writeFileSync(APPS_FILE, lines.join('\n'));
console.log('✅ Written to applications.md (backup: applications.md.bak)');
} else if (DRY_RUN) {
console.log('(dry-run — no changes written)');
} else {
console.log('✅ No duplicates found');
}