Spaces:
Sleeping
Sleeping
File size: 7,742 Bytes
7af47a9 6e6effd 0cf1728 3483ec1 ecb61f4 c7e90cf ecb61f4 fa28af9 3483ec1 c7e90cf 3483ec1 928b8e1 c7e90cf 928b8e1 ecb61f4 fa28af9 7e13c79 fa28af9 928b8e1 c7e90cf 928b8e1 ecb61f4 6bb9bad c7e90cf 7e13c79 c7e90cf 7e13c79 ecb61f4 c7e90cf 6bb9bad fa28af9 a793b3b c7e90cf ecb61f4 7e13c79 ecb61f4 7af47a9 32bdc2d 7af47a9 09f4b59 7af47a9 32bdc2d ecb61f4 d4537ba ecb61f4 8696503 32bdc2d d4537ba 7af47a9 0cf1728 7af47a9 ecb61f4 7af47a9 d4537ba 7af47a9 ecb61f4 7af47a9 32bdc2d b349b54 32bdc2d 6320256 de24089 0cf1728 6bb9bad de24089 d4537ba de24089 d4537ba de24089 0cf1728 024cc60 6320256 d4537ba ecb61f4 fa28af9 ecb61f4 0cf1728 d4537ba ecb61f4 d4537ba 6bb9bad 0cf1728 ecb61f4 d4537ba 0cf1728 d4537ba 0cf1728 ecb61f4 0cf1728 d4537ba 6bb9bad 0cf1728 ecb61f4 0cf1728 ecb61f4 7e13c79 0cf1728 7e13c79 ecb61f4 0cf1728 32bdc2d 7af47a9 0cf1728 7af47a9 32bdc2d 7af47a9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | const fs = require('fs');
const path = require('path');
const { extractTextFromImage } = require('./imageProcessor');
function mapAnswer(soalArray, jawaban, botIndex) {
return jawaban;
}
function normalizeText(text) {
return text.toLowerCase().replace(/[^\w\s]/g, '').trim();
}
function isValueMatch(value, targetSoal) {
const numberMap = {
'0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four',
'5': 'five', '6': 'six', '7': 'seven', '8': 'eight', '9': 'nine', '10': 'ten',
'zero': '0', 'one': '1', 'two': '2', 'three': '3', 'four': '4',
'five': '5', 'six': '6', 'seven': '7', 'eight': '8', 'nine': '9', 'ten': '10',
'I': '1', 'II': '2', 'III': '3', 'IV': '4', 'V': '5',
'VI': '6', 'VII': '7', 'VIII': '8', 'IX': '9', 'X': '10',
'slx': '6', 's1x': '6', 'six': '6',
'f0ur': '4', 'f0r': '4', 'fuor': '4',
'f1ve': '5', 'fiv': '5', 'f1v': '5',
'e1ght': '8', 'elght': '8', 'eight': '8',
'n1ne': '9', 'n1n': '9', 'nne': '9',
'se7en': '7', 'sven': '7', 'seven': '7',
'thre': '3', 'tree': '3', 'thr33': '3',
'tw0': '2', 'to': '2', 'tw': '2',
'0ne': '1', 'on': '1', 'oen': '1'
};
const leetMap = {
'O': '0', 'o': '0',
'I': '1', 'i': '1', 'l': '1',
'Z': '2', 'z': '2',
'E': '3', 'e': '3',
'A': '4', 'a': '4',
'S': '5', 's': '5',
'G': '6', 'g': '6',
'T': '7', 't': '7',
'B': '8', 'b': '8',
'Q': '9', 'q': '9',
'U': '4', 'u': '4',
'R': '2', 'r': '2',
'N': '9', 'n': '9',
'V': '7', 'v': '7'
};
const normalizedValue = normalizeText(value);
const normalizedSoal = normalizeText(targetSoal);
if (normalizedValue === normalizedSoal) {
return true;
}
const convertLeet = (text) => {
return text.split('').map(char => leetMap[char] || char).join('');
};
const leetValue = convertLeet(value);
const leetSoal = convertLeet(targetSoal);
if (leetValue === normalizedSoal) return true;
if (normalizedValue === leetSoal) return true;
if (leetValue === leetSoal) return true;
const mappedValue = numberMap[normalizedValue] || numberMap[value] || normalizedValue;
const mappedSoal = numberMap[normalizedSoal] || numberMap[targetSoal] || normalizedSoal;
if (mappedValue === normalizedSoal) return true;
if (normalizedValue === mappedSoal) return true;
if (mappedValue === mappedSoal) return true;
const similarity = calculateSimilarity(normalizedValue, normalizedSoal);
if (similarity >= 0.8) {
return true;
}
try {
const valueResult = evaluateSimpleMath(value);
const soalResult = evaluateSimpleMath(targetSoal);
if (valueResult !== null && soalResult !== null && valueResult === soalResult) {
return true;
}
} catch (e) {}
return false;
}
function calculateSimilarity(str1, str2) {
if (str1 === str2) return 1;
if (str1.length === 0 || str2.length === 0) return 0;
const longer = str1.length > str2.length ? str1 : str2;
const shorter = str1.length > str2.length ? str2 : str1;
if (longer.includes(shorter)) return shorter.length / longer.length;
let matches = 0;
for (let i = 0; i < shorter.length; i++) {
if (shorter[i] === longer[i]) matches++;
}
return matches / longer.length;
}
function evaluateSimpleMath(expression) {
if (!expression) return null;
const cleanExpr = expression.toString().replace(/[^\d+\-*/.()]/g, '');
if (!cleanExpr) return null;
try {
if (cleanExpr.length > 10) return null;
const result = Function(`"use strict"; return (${cleanExpr})`)();
return typeof result === 'number' ? result : null;
} catch (e) {
return null;
}
}
function parseSoalText(text) {
const delimiters = /[.,:;\\/\s]+/;
let parts = text.split(delimiters).filter(part => part.trim() !== '');
if (parts.length === 1) {
parts = text.split(/\s+/).filter(part => part.trim() !== '');
}
return parts;
}
async function antibot(data) {
try {
const { main, bots } = data;
const mainBuffer = Buffer.from(main, 'base64');
const mainText = await extractTextFromImage(mainBuffer);
const soalArray = parseSoalText(mainText.response);
if (soalArray.length === 0) {
throw new Error('Tidak ada soal yang terdeteksi');
}
const botResults = [];
for (let i = 0; i < bots.length; i++) {
const bot = bots[i];
try {
const botBuffer = Buffer.from(bot.img, 'base64');
const botText = await extractTextFromImage(botBuffer);
const mappedValue = mapAnswer(soalArray, botText.response, i);
botResults.push({
id: bot.id,
text: botText.response,
value: mappedValue,
normalized: normalizeText(botText.response)
});
} catch (error) {
botResults.push({
id: bot.id,
text: '',
value: '',
normalized: '',
error: error.message
});
}
}
const result = [];
const usedIds = new Set();
let matchedCount = 0;
for (let i = 0; i < soalArray.length; i++) {
const targetSoal = soalArray[i];
let foundId = null;
for (const bot of botResults) {
if (!usedIds.has(bot.id) && bot.value && bot.value.trim() !== '' &&
isValueMatch(bot.value, targetSoal)) {
foundId = bot.id;
usedIds.add(bot.id);
matchedCount++;
break;
}
}
result.push({
id: foundId,
soal: targetSoal,
matchType: foundId ? 'exact' : 'none'
});
}
if (matchedCount < soalArray.length) {
for (let i = 0; i < result.length; i++) {
if (!result[i].id) {
const targetSoal = soalArray[i];
const normalizedSoal = normalizeText(targetSoal);
for (const bot of botResults) {
if (!usedIds.has(bot.id) && bot.normalized && bot.normalized.trim() !== '') {
if (bot.normalized === normalizedSoal) {
result[i].id = bot.id;
result[i].matchType = 'normalized';
usedIds.add(bot.id);
matchedCount++;
break;
}
}
}
}
}
}
if (matchedCount >= 2) {
for (let i = 0; i < result.length; i++) {
if (!result[i].id) {
for (const bot of botResults) {
if (!usedIds.has(bot.id) && bot.value && bot.value.trim() !== '') {
result[i].id = bot.id;
result[i].matchType = 'fallback';
usedIds.add(bot.id);
break;
}
}
}
}
} else if (matchedCount === 1) {
for (let i = 0; i < result.length; i++) {
if (!result[i].id) {
result[i].id = 'invalid';
result[i].matchType = 'invalid';
}
}
} else {
for (let i = 0; i < result.length; i++) {
result[i].id = 'invalid';
result[i].matchType = 'invalid';
}
}
for (let i = 0; i < result.length; i++) {
if (!result[i].id) {
result[i].id = 'invalid';
result[i].matchType = 'invalid';
}
}
return {
success: true,
data: {
soal: soalArray,
soalLeet: soalArray,
botResults: botResults,
result: result.map(r => ({ id: r.id })),
debug: {
parsedSoal: soalArray,
matches: result.map(r => ({ id: r.id, matchType: r.matchType, soal: r.soal })),
totalMatches: matchedCount
}
}
};
} catch (error) {
return {
success: false,
error: error.message,
data: {
soal: [],
botResults: [],
result: []
}
};
}
}
module.exports = antibot; |