Update endpoints/antibot.js
Browse files- endpoints/antibot.js +75 -65
endpoints/antibot.js
CHANGED
|
@@ -51,17 +51,11 @@ function normalizeWords(t) {
|
|
| 51 |
}
|
| 52 |
|
| 53 |
function convertMixedText(text) {
|
| 54 |
-
if (!text) return null;
|
| 55 |
-
|
| 56 |
const romanResult = romanToDecimal(text);
|
| 57 |
if (romanResult !== null) {
|
| 58 |
return romanResult;
|
| 59 |
}
|
| 60 |
|
| 61 |
-
if (WORD_NUM[text] !== undefined) {
|
| 62 |
-
return WORD_NUM[text];
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
let converted = '';
|
| 66 |
for (let char of text.toLowerCase()) {
|
| 67 |
if (CHAR_TO_NUM[char] !== undefined) {
|
|
@@ -81,30 +75,55 @@ function convertMixedText(text) {
|
|
| 81 |
function detectNumber(words) {
|
| 82 |
if (words.length === 0) return null;
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
const numbers = [];
|
| 85 |
for (let word of words) {
|
| 86 |
if (/^[0-9]+$/.test(word)) {
|
| 87 |
numbers.push(parseInt(word));
|
|
|
|
|
|
|
| 88 |
} else {
|
| 89 |
-
const
|
| 90 |
-
if (
|
| 91 |
-
numbers.push(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
}
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
-
if (numbers.length
|
| 97 |
-
return numbers[0];
|
| 98 |
-
}
|
| 99 |
-
|
| 100 |
-
if (numbers.length > 1) {
|
| 101 |
return numbers.reduce((a, b) => a + b, 0);
|
| 102 |
}
|
| 103 |
|
| 104 |
const combined = words.join('');
|
| 105 |
-
const
|
| 106 |
-
if (
|
| 107 |
-
return
|
| 108 |
}
|
| 109 |
|
| 110 |
return null;
|
|
@@ -114,7 +133,7 @@ async function readText(b64) {
|
|
| 114 |
const Jimp = await loadJimp();
|
| 115 |
const img = await Jimp.read(buf(b64));
|
| 116 |
|
| 117 |
-
img.greyscale().contrast(1.
|
| 118 |
|
| 119 |
if (img.bitmap.width > 500) {
|
| 120 |
img.resize(500, Jimp.AUTO);
|
|
@@ -124,8 +143,8 @@ async function readText(b64) {
|
|
| 124 |
await img.write(tmp);
|
| 125 |
|
| 126 |
const res = await Tesseract.recognize(tmp, "eng", {
|
| 127 |
-
tessedit_char_whitelist: '0123456789IVXLCDMivxlcdmOIZAESGTBQolizasgtbq|',
|
| 128 |
-
tessedit_pageseg_mode: '
|
| 129 |
});
|
| 130 |
|
| 131 |
fs.unlinkSync(tmp);
|
|
@@ -134,56 +153,47 @@ async function readText(b64) {
|
|
| 134 |
}
|
| 135 |
|
| 136 |
module.exports = async (data) => {
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
}
|
|
|
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
} else {
|
| 160 |
-
result.push(null);
|
| 161 |
-
}
|
| 162 |
} else {
|
| 163 |
-
const
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
return botText === soalText;
|
| 167 |
-
});
|
| 168 |
-
|
| 169 |
-
if (matches.length > 0) {
|
| 170 |
-
result.push(matches[0].id);
|
| 171 |
} else {
|
| 172 |
result.push(null);
|
| 173 |
}
|
| 174 |
}
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
};
|
| 181 |
-
} catch (error) {
|
| 182 |
-
console.error('Error in module:', error);
|
| 183 |
-
return {
|
| 184 |
-
soal: [],
|
| 185 |
-
botResults: [],
|
| 186 |
-
result: [null]
|
| 187 |
-
};
|
| 188 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
};
|
|
|
|
| 51 |
}
|
| 52 |
|
| 53 |
function convertMixedText(text) {
|
|
|
|
|
|
|
| 54 |
const romanResult = romanToDecimal(text);
|
| 55 |
if (romanResult !== null) {
|
| 56 |
return romanResult;
|
| 57 |
}
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
let converted = '';
|
| 60 |
for (let char of text.toLowerCase()) {
|
| 61 |
if (CHAR_TO_NUM[char] !== undefined) {
|
|
|
|
| 75 |
function detectNumber(words) {
|
| 76 |
if (words.length === 0) return null;
|
| 77 |
|
| 78 |
+
if (words.length === 1) {
|
| 79 |
+
const word = words[0];
|
| 80 |
+
|
| 81 |
+
if (/^[0-9]+$/.test(word)) {
|
| 82 |
+
return parseInt(word);
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
const romanResult = romanToDecimal(word);
|
| 86 |
+
if (romanResult !== null) {
|
| 87 |
+
return romanResult;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
if (WORD_NUM[word] !== undefined) {
|
| 91 |
+
return WORD_NUM[word];
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
const converted = convertMixedText(word);
|
| 95 |
+
if (converted !== null) {
|
| 96 |
+
return converted;
|
| 97 |
+
}
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
const numbers = [];
|
| 101 |
for (let word of words) {
|
| 102 |
if (/^[0-9]+$/.test(word)) {
|
| 103 |
numbers.push(parseInt(word));
|
| 104 |
+
} else if (WORD_NUM[word] !== undefined) {
|
| 105 |
+
numbers.push(WORD_NUM[word]);
|
| 106 |
} else {
|
| 107 |
+
const romanResult = romanToDecimal(word);
|
| 108 |
+
if (romanResult !== null) {
|
| 109 |
+
numbers.push(romanResult);
|
| 110 |
+
} else {
|
| 111 |
+
const converted = convertMixedText(word);
|
| 112 |
+
if (converted !== null) {
|
| 113 |
+
numbers.push(converted);
|
| 114 |
+
}
|
| 115 |
}
|
| 116 |
}
|
| 117 |
}
|
| 118 |
|
| 119 |
+
if (numbers.length > 0) {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
return numbers.reduce((a, b) => a + b, 0);
|
| 121 |
}
|
| 122 |
|
| 123 |
const combined = words.join('');
|
| 124 |
+
const converted = convertMixedText(combined);
|
| 125 |
+
if (converted !== null) {
|
| 126 |
+
return converted;
|
| 127 |
}
|
| 128 |
|
| 129 |
return null;
|
|
|
|
| 133 |
const Jimp = await loadJimp();
|
| 134 |
const img = await Jimp.read(buf(b64));
|
| 135 |
|
| 136 |
+
img.greyscale().contrast(1.0).normalize().brightness(0.1);
|
| 137 |
|
| 138 |
if (img.bitmap.width > 500) {
|
| 139 |
img.resize(500, Jimp.AUTO);
|
|
|
|
| 143 |
await img.write(tmp);
|
| 144 |
|
| 145 |
const res = await Tesseract.recognize(tmp, "eng", {
|
| 146 |
+
tessedit_char_whitelist: '0123456789IVXLCDMivxlcdmOIZAESGTBQolizasgtbq| ',
|
| 147 |
+
tessedit_pageseg_mode: '6'
|
| 148 |
});
|
| 149 |
|
| 150 |
fs.unlinkSync(tmp);
|
|
|
|
| 153 |
}
|
| 154 |
|
| 155 |
module.exports = async (data) => {
|
| 156 |
+
const mainText = await readText(data.main);
|
| 157 |
+
const soal = normalizeWords(mainText);
|
| 158 |
+
|
| 159 |
+
const mainNumber = detectNumber(soal);
|
| 160 |
+
|
| 161 |
+
const botResults = [];
|
| 162 |
+
for (let b of data.bots) {
|
| 163 |
+
const raw = await readText(b.img);
|
| 164 |
+
const tokens = normalizeWords(raw);
|
| 165 |
+
const num = detectNumber(tokens);
|
| 166 |
+
|
| 167 |
+
botResults.push({
|
| 168 |
+
id: b.id,
|
| 169 |
+
text: tokens.join(" "),
|
| 170 |
+
number: num
|
| 171 |
+
});
|
| 172 |
+
}
|
| 173 |
|
| 174 |
+
const result = [];
|
| 175 |
+
if (mainNumber !== null) {
|
| 176 |
+
const f = botResults.find(x => x.number === mainNumber);
|
| 177 |
+
if (f) {
|
| 178 |
+
result.push(f.id);
|
|
|
|
|
|
|
|
|
|
| 179 |
} else {
|
| 180 |
+
const totalBot = botResults.reduce((sum, bot) => sum + (bot.number || 0), 0);
|
| 181 |
+
if (totalBot === mainNumber) {
|
| 182 |
+
result.push(...botResults.map(bot => bot.id));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
} else {
|
| 184 |
result.push(null);
|
| 185 |
}
|
| 186 |
}
|
| 187 |
+
} else {
|
| 188 |
+
soal.forEach(word => {
|
| 189 |
+
const f = botResults.find(x => x.text.includes(word));
|
| 190 |
+
result.push(f ? f.id : null);
|
| 191 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
}
|
| 193 |
+
|
| 194 |
+
return {
|
| 195 |
+
soal,
|
| 196 |
+
botResults,
|
| 197 |
+
result
|
| 198 |
+
};
|
| 199 |
};
|