const Tesseract = require("tesseract.js"); const sharp = require("sharp"); function levenshtein(s, t) { if (s === t) { return 0; } var n = s.length, m = t.length; if (n === 0) return m; if (m === 0) return n; var d = []; for (var i = 0; i <= n; i++) d[i] = []; for (var i = 0; i <= n; i++) d[i][0] = i; for (var j = 0; j <= m; j++) d[0][j] = j; for (var i = 1; i <= n; i++) { for (var j = 1; j <= m; j++) { var cost = (s.charAt(i - 1) === t.charAt(j - 1)) ? 0 : 1; d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost); } } return d[n][m]; } const DICTIONARIES = { wordnumber: { ONE: '1', TWO: '2', THREE: '3', FOUR: '4', FIVE: '5', SIX: '6', SEVEN: '7', EIGHT: '8', NINE: '9', TEN: '10' }, numberword: { '1': 'ONE', '2': 'TWO', '3': 'THREE', '4': 'FOUR', '5': 'FIVE', '6': 'SIX', '7': 'SEVEN', '8': 'EIGHT', '9': 'NINE', '10': 'TEN' }, numberroman: { '1': 'I', '2': 'II', '3': 'III', '4': 'IV', '5': 'V', '6': 'VI', '7': 'VII', '8': 'VIII', '9': 'IX', '10': 'X' }, romannumber: { I: '1', II: '2', III: '3', IV: '4', V: '5', VI: '6', VII: '7', VIII: '8', IX: '9', X: '10', VILL: '7' }, mathans: { '2-1': '1', '1+1': '2', '141': '2', '1+2': '3', '2+2': '4', '242': '4', '3+2': '5', '342': '5', '2+4': '6', '244': '6', '3+4': '7', '344': '7', '4+4': '8', '444': '8', '1+8': '9', '148': '9', '5+6': '11' }, ansmath: { '1': '3-2', '2': '8-6', '3': '1+2', '4': '3+1', '5': '9-4', '6': '3+3', '7': '6+1', '8': '2*4', '9': '3+6', '10': '2+8' }, oox: { '--X': 'OOX', '-X-': 'OXO', 'X--': 'XOO', 'XX-': 'XXO', '-XX': 'OXX', 'X-X': 'XOX', '---': 'OOO', 'XXX': 'XXX', 'X-X-': 'XOXO', '-X-X': 'OXOX' }, xxx: { '--X': '--+', '-X-': '-+-', 'X--': '+--', 'XX-': '++-', '-XX': '-++', 'X-X': '+-+', '---': '---', 'XXX': '+++', 'X-X-': '+-+-', '-X-X': '-+-+' }, xox: { '--X': 'OO+', '-X-': 'O+O', 'X--': '+OO', 'XX-': '++O', '-XX': 'O++', 'X-X': '+O+', '---': 'OOO', 'XXX': '+++', 'X-X-': '+O+O', '-X-X': 'O+O+' }, oxo: { OOX: '--+', OXO: '-+-', XOO: '+--', XXO: '++-', OXX: '-++', XOX: '+-+', OOO: '---', XXX: '+++', XOXO: '+-+-', OXOX: '-+-+' }, zoo: { ZOO: '200', OZO: '020', OOZ: '002', SOO: '500', '0SO': '050', OOS: '005', LOL: '101', SOS: '505', ZOZ: '202', LLL: '111' }, ooz: { '200': 'ZOO', '020': 'OZO', '002': 'OOZ', '500': 'SOO', '050': 'OSO', '005': 'OOS', '101': 'LOL', '505': 'SOS', '202': 'ZOZ', '111': 'LLL' }, animals: { CAT: 'C@T', DOG: 'D0G', LION: '1!0N', TIGER: 'T!G3R', MONKEY: 'M0NK3Y', ELEPHANT: '31EPH@NT', COW: 'C0W', FOX: 'F0X', MOUSE: 'M0US3', ANT: '@NT' } }; function applyVariations(word, type) { let w = word.toUpperCase(); if (['numberword', 'mathans', 'ansmath'].includes(type)) { w = w.replace(/[LIF\|\}]/g, '1').replace(/ยข/g, '6').replace(/T/g, '+').replace(/Z/g, '2') .replace(/Q/g, '7').replace(/\(/g, '6').replace(/#/g, '1+') .replace(/FEN/g, 'TEN').replace(/4NREE/g, 'THREE') .replace(/142|W2/g, '1+2').replace(/343/g, '3+3').replace(/SVEN/g, 'SEVEN') .replace(/GIVE/g, 'FIVE').replace(/341/g, '3+1').replace(/1\+7/g, '1+1'); } if (['romannumber', 'numberroman'].includes(type)) { w = w.replace(/U/g, 'V').replace(/R/g, 'X').replace(/1/g, 'I') .replace(/\\\\/g, 'VI').replace(/[\\\|]/g, 'I').replace(/โ„ข/g, 'III').replace(/XX/g, 'X'); } if (['oxo', 'xox', 'xxx', 'oox'].includes(type)) { w = w.replace(/[0D]/g, 'O').replace(/[TF]/g, '+'); } if (type === 'zoo') { w = w.replace(/0/g, 'O').replace(/[I\|]/g, 'L').replace(/8/g, 'S') .replace(/\$/g, '5').replace(/SยฐS/g, 'SOS'); } if (type === 'ooz') { w = w.replace(/O/g, '0'); } if (type === 'animals') { w = w.replace(/ENT|@RT|ONT/g, 'ANT').replace(/TLG3R/g, 'TIGER').replace(/MOUSS/g, 'MOUSE') .replace(/CET/g, 'CAT').replace(/10/g, 'LION') .replace(/0/g, 'O').replace(/3/g, 'E').replace(/!/g, 'I').replace(/1/g, 'L').replace(/@/g, 'A'); } return w; } function normalize(text) { return text.replace(/[^a-zA-Z0-9+\-*@!]/g, "").toUpperCase(); } function findBestMatch(scannedText) { const cleanScanned = normalize(scannedText); let bestMatch = { type: 'unknown', value: cleanScanned, score: 0 }; for (const [type, dict] of Object.entries(DICTIONARIES)) { const variedText = applyVariations(cleanScanned, type); if (dict[variedText]) return { type, value: dict[variedText], original: variedText }; for (const [key, val] of Object.entries(dict)) { if (val === variedText) return { type, value: key, original: variedText }; const distKey = levenshtein(variedText, key); const distVal = levenshtein(variedText, val); if (distKey <= 1) return { type, value: val, original: key }; if (distVal <= 1) return { type, value: key, original: val }; } } return bestMatch; } const buf = b => Buffer.from(b.replace(/^data:image\/\w+;base64,/, ""), "base64"); async function preprocessImage(imageBuffer, type = 'main') { let pipeline = sharp(imageBuffer) .grayscale() .normalize(); if (type === 'main') { pipeline = pipeline .resize({ width: 800 }) .sharpen({ sigma: 1.5 }) .threshold(140); } else { pipeline = pipeline .resize({ height: 100 }) .sharpen({ sigma: 2.0 }) .median(1) .threshold(128); } return await pipeline.toBuffer(); } async function ocr(imageBuffer, psmMode = '6') { try { const result = await Tesseract.recognize(imageBuffer, "eng", { tessedit_char_whitelist: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-*=@!$,.:;", tessedit_pageseg_mode: psmMode }); return result.data.text.trim(); } catch (e) { console.error("OCR Failed: ", e); return ""; } } module.exports = async (data) => { try { console.log("๐Ÿš€ Starting AntiBot Analysis..."); const mainBuffer = buf(data.main); const processedMain = await preprocessImage(mainBuffer, 'main'); const rawMainText = await ocr(processedMain, '7'); const mainWords = rawMainText.replace(/[,.]/g, ' ').split(/\s+/).filter(w => w.length > 0); console.log("๐Ÿ“ Raw Main Text: ", rawMainText); console.log("๐Ÿ“ Parsed Words: ", mainWords); let targetConcepts = []; for (const word of mainWords) { const match = findBestMatch(word); if (match.type !== 'unknown') { targetConcepts.push(match); } } if (targetConcepts.length === 0) { targetConcepts = mainWords.map(w => ({ type: 'raw', value: normalize(w) })); } console.log("๐ŸŽฏ Target Concepts: ", targetConcepts.map(t => t.value)); const botResults = []; for (const bot of data.bots) { const botBuffer = buf(bot.img); const processedBot = await preprocessImage(botBuffer, 'bot'); const rawBotText = await ocr(processedBot, '6'); const cleanBotText = normalize(rawBotText); const botAnalysis = findBestMatch(cleanBotText); botResults.push({ id: bot.id, raw: rawBotText, clean: cleanBotText, analysis: botAnalysis }); } const finalResult = []; const usedBots = new Set(); for (const target of targetConcepts) { let bestBotId = null; let minDistance = 999; for (const bot of botResults) { if (usedBots.has(bot.id)) continue; if (target.value === bot.analysis.value || target.value === bot.clean) { bestBotId = bot.id; minDistance = -1; break; } const dist = levenshtein(target.value, bot.clean); if (dist < minDistance && dist <= 2) { minDistance = dist; bestBotId = bot.id; } } if (bestBotId) { finalResult.push(bestBotId); usedBots.add(bestBotId); } else { finalResult.push(null); } } for (let i = 0; i < finalResult.length; i++) { if (finalResult[i] === null) { for (const bot of botResults) { if (!usedBots.has(bot.id)) { finalResult[i] = bot.id; usedBots.add(bot.id); break; } } } } console.log("โœ… Final Result IDs: ", finalResult); return { success: true, soal: mainWords, bots_analysis: botResults.map(b => ({ id: b.id, text: b.clean })), result: finalResult.filter(x => x !== null) }; } catch (error) { console.error("๐Ÿ’ฅ Antibot Error: ", error); return { success: false, error: error.message, result: [] }; } };