Fourstore commited on
Commit
e2eeee0
·
verified ·
1 Parent(s): 11818ee

Update endpoints/antibot.js

Browse files
Files changed (1) hide show
  1. endpoints/antibot.js +25 -5
endpoints/antibot.js CHANGED
@@ -26,11 +26,14 @@ function normalize(x) {
26
  const roman = {i:1,ii:2,iii:3,iv:4,v:5,vi:6,vii:7,viii:8,ix:9,x:10};
27
  if (roman[t]) return roman[t];
28
 
29
- const word = {one:1,two:2,three:3,four:4,five:5,six:6,seven:7,eight:8,nine:9,ten:10};
 
 
 
30
  if (word[t]) return word[t];
31
 
32
  try {
33
- if (/[\d+\-*/]/.test(t)) {
34
  const v = eval(t);
35
  if (!isNaN(v)) return Number(v);
36
  }
@@ -43,6 +46,20 @@ function normalize(x) {
43
  .replace(/1/g,"i");
44
  }
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  async function preprocessSoal(b) {
47
  return sharp(b)
48
  .resize({ width: 1000 })
@@ -88,7 +105,8 @@ module.exports = async data => {
88
  const soalProcessed = await preprocessSoal(soalImg);
89
  const soalText = await ocr(soalProcessed, "soal");
90
  const soalRaw = cleanSoal(soalText);
91
- const soalNorm = soalRaw.map(normalize);
 
92
 
93
  const botResults = [];
94
  for (const b of data.bots) {
@@ -103,14 +121,16 @@ module.exports = async data => {
103
  });
104
  }
105
 
106
- const result = soalNorm.map(v => {
 
 
107
  const f = botResults.find(x => x.value === v);
108
  return f ? f.id : null;
109
  });
110
 
111
  return {
112
  soal: soalRaw,
113
- soalNormalized: soalNorm,
114
  botResults,
115
  result
116
  };
 
26
  const roman = {i:1,ii:2,iii:3,iv:4,v:5,vi:6,vii:7,viii:8,ix:9,x:10};
27
  if (roman[t]) return roman[t];
28
 
29
+ const word = {
30
+ zero:0, one:1, two:2, three:3, four:4, five:5,
31
+ six:6, seven:7, eight:8, nine:9, ten:10
32
+ };
33
  if (word[t]) return word[t];
34
 
35
  try {
36
+ if (/^[0-9+\-*/]+$/.test(t)) {
37
  const v = eval(t);
38
  if (!isNaN(v)) return Number(v);
39
  }
 
46
  .replace(/1/g,"i");
47
  }
48
 
49
+ function solveTokens(tokens) {
50
+ const n = tokens.map(normalize);
51
+
52
+ if (n.length === 3 && typeof n[0] === "number" && typeof n[2] === "number") {
53
+ const op = String(tokens[1]).replace(/[^+\-*/]/g, "");
54
+ try {
55
+ const v = eval(`${n[0]}${op}${n[2]}`);
56
+ if (!isNaN(v)) return v;
57
+ } catch {}
58
+ }
59
+
60
+ return n;
61
+ }
62
+
63
  async function preprocessSoal(b) {
64
  return sharp(b)
65
  .resize({ width: 1000 })
 
105
  const soalProcessed = await preprocessSoal(soalImg);
106
  const soalText = await ocr(soalProcessed, "soal");
107
  const soalRaw = cleanSoal(soalText);
108
+
109
+ let soalNormalized = solveTokens(soalRaw);
110
 
111
  const botResults = [];
112
  for (const b of data.bots) {
 
121
  });
122
  }
123
 
124
+ const target = Array.isArray(soalNormalized) ? soalNormalized : [soalNormalized];
125
+
126
+ const result = target.map(v => {
127
  const f = botResults.find(x => x.value === v);
128
  return f ? f.id : null;
129
  });
130
 
131
  return {
132
  soal: soalRaw,
133
+ soalNormalized: soalNormalized,
134
  botResults,
135
  result
136
  };