|
| 1 | +/** |
| 2 | + * Military Resume Translator |
| 3 | + * Lightweight dictionary-based translation (no AI dependencies) |
| 4 | + * Fast, reliable, and bundle-size friendly |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Military-to-civilian terminology mappings |
| 9 | + */ |
| 10 | +const MILITARY_TERMINOLOGY: Record<string, string> = { |
| 11 | + // Ranks & Leadership |
| 12 | + 'squad leader': 'team supervisor', |
| 13 | + 'platoon sergeant': 'operations manager', |
| 14 | + 'first sergeant': 'senior operations manager', |
| 15 | + 'sergeant major': 'executive operations manager', |
| 16 | + 'commanding officer': 'chief executive', |
| 17 | + 'executive officer': 'deputy director', |
| 18 | + 'NCO': 'supervisor', |
| 19 | + 'NCOIC': 'operations supervisor', |
| 20 | + 'OIC': 'program manager', |
| 21 | + |
| 22 | + // Skills & Activities |
| 23 | + 'conducted': 'performed', |
| 24 | + 'executed': 'completed', |
| 25 | + 'deployed': 'traveled', |
| 26 | + 'mission': 'objective', |
| 27 | + 'operations': 'activities', |
| 28 | + 'tactical': 'strategic', |
| 29 | + 'reconnaissance': 'research', |
| 30 | + 'surveillance': 'monitoring', |
| 31 | + 'logistics': 'supply chain management', |
| 32 | + 'ordnance': 'equipment', |
| 33 | + |
| 34 | + // Military Branches & Units |
| 35 | + 'battalion': 'large organization', |
| 36 | + 'company': 'mid-size team', |
| 37 | + 'platoon': 'team', |
| 38 | + 'squad': 'small team', |
| 39 | + 'unit': 'department', |
| 40 | + |
| 41 | + // Common Military Terms |
| 42 | + 'personnel': 'employees', |
| 43 | + 'enlisted': 'staff members', |
| 44 | + 'subordinates': 'team members', |
| 45 | + 'superior': 'manager', |
| 46 | + 'briefed': 'presented to', |
| 47 | + 'debriefed': 'reviewed with', |
| 48 | + 'orders': 'directives', |
| 49 | + 'regulations': 'policies', |
| 50 | + 'standard operating procedure': 'company policy', |
| 51 | + 'SOP': 'policy', |
| 52 | + 'ROE': 'guidelines', |
| 53 | +}; |
| 54 | + |
| 55 | +/** |
| 56 | + * Common military job titles to civilian equivalents |
| 57 | + */ |
| 58 | +const JOB_TITLE_MAPPINGS: Record<string, string> = { |
| 59 | + // Infantry & Combat |
| 60 | + 'infantryman': 'team member', |
| 61 | + 'infantry squad leader': 'operations team lead', |
| 62 | + 'fire team leader': 'team supervisor', |
| 63 | + |
| 64 | + // Medical |
| 65 | + 'combat medic': 'emergency medical technician', |
| 66 | + 'field medic': 'paramedic', |
| 67 | + 'hospital corpsman': 'medical assistant', |
| 68 | + |
| 69 | + // Intelligence |
| 70 | + 'intelligence analyst': 'data analyst', |
| 71 | + 'signals intelligence analyst': 'communications analyst', |
| 72 | + |
| 73 | + // Administration |
| 74 | + 'personnel specialist': 'human resources specialist', |
| 75 | + 'administrative specialist': 'administrative coordinator', |
| 76 | + |
| 77 | + // Technical |
| 78 | + 'information technology specialist': 'IT specialist', |
| 79 | + 'network administrator': 'network administrator', |
| 80 | + 'communications specialist': 'telecommunications specialist', |
| 81 | + |
| 82 | + // Logistics |
| 83 | + 'supply specialist': 'inventory manager', |
| 84 | + 'logistics specialist': 'supply chain coordinator', |
| 85 | + 'quartermaster': 'logistics manager', |
| 86 | + |
| 87 | + // Vehicle & Equipment |
| 88 | + 'motor transport operator': 'truck driver', |
| 89 | + 'aircraft mechanic': 'aviation technician', |
| 90 | + 'wheeled vehicle mechanic': 'automotive technician', |
| 91 | +}; |
| 92 | + |
| 93 | +export interface TranslationResult { |
| 94 | + original: string; |
| 95 | + translated: string; |
| 96 | + suggestions: string[]; |
| 97 | + confidence: number; |
| 98 | +} |
| 99 | + |
| 100 | +export interface MilitaryProfile { |
| 101 | + jobTitle?: string; |
| 102 | + rank?: string; |
| 103 | + branch?: string; |
| 104 | + duties?: string; |
| 105 | + achievements?: string; |
| 106 | +} |
| 107 | + |
| 108 | +export interface TranslatedProfile { |
| 109 | + jobTitle: string; |
| 110 | + summary: string; |
| 111 | + keyResponsibilities: string[]; |
| 112 | + achievements: string[]; |
| 113 | + suggestions?: string[]; |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Replace military terminology with civilian equivalents |
| 118 | + */ |
| 119 | +function replaceTerminology(text: string): string { |
| 120 | + let result = text; |
| 121 | + |
| 122 | + // Sort by length (longest first) to avoid partial replacements |
| 123 | + const sortedTerms = Object.entries(MILITARY_TERMINOLOGY).sort( |
| 124 | + ([a], [b]) => b.length - a.length |
| 125 | + ); |
| 126 | + |
| 127 | + for (const [military, civilian] of sortedTerms) { |
| 128 | + // Case-insensitive replacement |
| 129 | + const regex = new RegExp(`\\b${military}\\b`, 'gi'); |
| 130 | + result = result.replace(regex, civilian); |
| 131 | + } |
| 132 | + |
| 133 | + return result; |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Translate military job title to civilian equivalent |
| 138 | + */ |
| 139 | +export function translateJobTitle(militaryTitle: string): string { |
| 140 | + const normalized = militaryTitle.toLowerCase().trim(); |
| 141 | + |
| 142 | + // Check for exact match |
| 143 | + if (JOB_TITLE_MAPPINGS[normalized]) { |
| 144 | + return JOB_TITLE_MAPPINGS[normalized]; |
| 145 | + } |
| 146 | + |
| 147 | + // Check for partial match |
| 148 | + for (const [military, civilian] of Object.entries(JOB_TITLE_MAPPINGS)) { |
| 149 | + if (normalized.includes(military)) { |
| 150 | + return civilian; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // Fallback: apply terminology replacement |
| 155 | + return replaceTerminology(militaryTitle); |
| 156 | +} |
| 157 | + |
| 158 | +/** |
| 159 | + * Translate a single military duty/responsibility to civilian language |
| 160 | + * Uses dictionary-based translation for instant, reliable results |
| 161 | + */ |
| 162 | +export async function translateDuty(duty: string): Promise<TranslationResult> { |
| 163 | + const translated = replaceTerminology(duty); |
| 164 | + |
| 165 | + // Generate simple suggestions based on the translation |
| 166 | + const suggestions = getSuggestions(translated); |
| 167 | + |
| 168 | + return { |
| 169 | + original: duty, |
| 170 | + translated: translated, |
| 171 | + suggestions: suggestions, |
| 172 | + confidence: 0.95, // High confidence with dictionary-based approach |
| 173 | + }; |
| 174 | +} |
| 175 | + |
| 176 | +/** |
| 177 | + * Translate entire military profile to civilian resume format using AI |
| 178 | + */ |
| 179 | +export async function translateMilitaryProfile( |
| 180 | + profile: MilitaryProfile |
| 181 | +): Promise<TranslatedProfile> { |
| 182 | + try { |
| 183 | + // Call API endpoint for AI-powered translation |
| 184 | + const response = await fetch('/api/military-resume/translate', { |
| 185 | + method: 'POST', |
| 186 | + headers: { |
| 187 | + 'Content-Type': 'application/json', |
| 188 | + }, |
| 189 | + body: JSON.stringify({ |
| 190 | + jobTitle: profile.jobTitle || '', |
| 191 | + rank: profile.rank || '', |
| 192 | + branch: profile.branch || '', |
| 193 | + duties: profile.duties || '', |
| 194 | + achievements: profile.achievements || '', |
| 195 | + }), |
| 196 | + }); |
| 197 | + |
| 198 | + if (!response.ok) { |
| 199 | + // Fallback to dictionary-based translation |
| 200 | + console.warn('AI translation failed, using fallback'); |
| 201 | + return fallbackTranslation(profile); |
| 202 | + } |
| 203 | + |
| 204 | + const translated = await response.json(); |
| 205 | + return translated; |
| 206 | + |
| 207 | + } catch (error) { |
| 208 | + console.error('Profile translation error:', error); |
| 209 | + // Fallback to dictionary-based translation |
| 210 | + return fallbackTranslation(profile); |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +/** |
| 215 | + * Fallback translation using dictionary-based approach |
| 216 | + */ |
| 217 | +function fallbackTranslation(profile: MilitaryProfile): TranslatedProfile { |
| 218 | + // Translate job title |
| 219 | + const civilianTitle = profile.jobTitle |
| 220 | + ? translateJobTitle(profile.jobTitle) |
| 221 | + : 'Professional'; |
| 222 | + |
| 223 | + // Create professional summary |
| 224 | + const summaryParts: string[] = []; |
| 225 | + if (profile.rank) { |
| 226 | + summaryParts.push(`Experienced professional with ${profile.rank} level responsibilities`); |
| 227 | + } |
| 228 | + if (profile.branch) { |
| 229 | + summaryParts.push(`in ${replaceTerminology(profile.branch)}`); |
| 230 | + } |
| 231 | + |
| 232 | + const summary = summaryParts.length > 0 |
| 233 | + ? summaryParts.join(' ') |
| 234 | + : 'Dedicated professional with proven leadership and operational experience'; |
| 235 | + |
| 236 | + // Translate duties/responsibilities |
| 237 | + const duties = profile.duties |
| 238 | + ? profile.duties.split('\n').filter((d) => d.trim()) |
| 239 | + : []; |
| 240 | + |
| 241 | + const translatedDuties = duties.map((duty) => replaceTerminology(duty)); |
| 242 | + |
| 243 | + // Translate achievements |
| 244 | + const achievements = profile.achievements |
| 245 | + ? profile.achievements.split('\n').filter((a) => a.trim()) |
| 246 | + : []; |
| 247 | + |
| 248 | + const translatedAchievements = achievements.map((achievement) => |
| 249 | + replaceTerminology(achievement) |
| 250 | + ); |
| 251 | + |
| 252 | + return { |
| 253 | + jobTitle: civilianTitle, |
| 254 | + summary, |
| 255 | + keyResponsibilities: translatedDuties, |
| 256 | + achievements: translatedAchievements, |
| 257 | + }; |
| 258 | +} |
| 259 | + |
| 260 | +/** |
| 261 | + * Batch translate multiple duties |
| 262 | + */ |
| 263 | +export async function translateDuties(duties: string[]): Promise<TranslationResult[]> { |
| 264 | + const results: TranslationResult[] = []; |
| 265 | + |
| 266 | + for (const duty of duties) { |
| 267 | + if (duty.trim()) { |
| 268 | + const result = await translateDuty(duty); |
| 269 | + results.push(result); |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + return results; |
| 274 | +} |
| 275 | + |
| 276 | +/** |
| 277 | + * Get suggestions for improving a translated duty |
| 278 | + */ |
| 279 | +export function getSuggestions(translatedDuty: string): string[] { |
| 280 | + const suggestions: string[] = []; |
| 281 | + |
| 282 | + // Suggest adding metrics |
| 283 | + if (!/\d+/.test(translatedDuty)) { |
| 284 | + suggestions.push('Consider adding specific numbers or metrics to quantify your impact'); |
| 285 | + } |
| 286 | + |
| 287 | + // Suggest using action verbs |
| 288 | + const actionVerbs = ['led', 'managed', 'developed', 'implemented', 'coordinated']; |
| 289 | + const startsWithActionVerb = actionVerbs.some((verb) => |
| 290 | + translatedDuty.toLowerCase().startsWith(verb) |
| 291 | + ); |
| 292 | + |
| 293 | + if (!startsWithActionVerb) { |
| 294 | + suggestions.push('Start with a strong action verb (e.g., Led, Managed, Developed)'); |
| 295 | + } |
| 296 | + |
| 297 | + // Suggest adding outcomes |
| 298 | + if (!translatedDuty.includes('result') && !translatedDuty.includes('improve')) { |
| 299 | + suggestions.push('Include the result or outcome of your work'); |
| 300 | + } |
| 301 | + |
| 302 | + return suggestions; |
| 303 | +} |
| 304 | + |
| 305 | +/** |
| 306 | + * Format translated profile for download/export |
| 307 | + */ |
| 308 | +export function formatForResume(profile: TranslatedProfile): string { |
| 309 | + let resume = ''; |
| 310 | + |
| 311 | + resume += `JOB TITLE: ${profile.jobTitle}\n\n`; |
| 312 | + resume += `PROFESSIONAL SUMMARY:\n${profile.summary}\n\n`; |
| 313 | + |
| 314 | + if (profile.keyResponsibilities.length > 0) { |
| 315 | + resume += `KEY RESPONSIBILITIES:\n`; |
| 316 | + profile.keyResponsibilities.forEach((resp) => { |
| 317 | + resume += `• ${resp}\n`; |
| 318 | + }); |
| 319 | + resume += '\n'; |
| 320 | + } |
| 321 | + |
| 322 | + if (profile.achievements.length > 0) { |
| 323 | + resume += `ACHIEVEMENTS:\n`; |
| 324 | + profile.achievements.forEach((achievement) => { |
| 325 | + resume += `• ${achievement}\n`; |
| 326 | + }); |
| 327 | + } |
| 328 | + |
| 329 | + return resume; |
| 330 | +} |
0 commit comments