-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
235 lines (193 loc) · 14.2 KB
/
test.py
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
import unittest
from app import predict, solve
from evaluation import polynomial
from translation import translate_to_arabic_html
def get_Prediction(img_path):
expression, mapping = predict(img_path)
solution, error = polynomial(expression, mapping)
arabic_expr, arabic_sol = translate_to_arabic_html(expression, solution, mapping)
prediction = {'expression': expression, 'mapping': str(mapping), 'solution': str(solution), 'error': str(error),
'arabic_expr': arabic_expr, 'arabic_sol': arabic_sol}
return prediction
class MyTestCase(unittest.TestCase):
def test_pow(self):
prediction = get_Prediction('test_images/IMG_20220406_164831_24.jpg')
self.assertEqual(prediction['expression'], 'x^(3*x+5)')
self.assertEqual(prediction['solution'], '[0]')
self.assertEqual(prediction['arabic_expr'], '<html><body>س <sup>٣*س+٥</sup></body></html>')
self.assertEqual(prediction['arabic_sol'], ['<html><body>٠</body></html>'])
prediction = get_Prediction('test_images/IMG_20220517_125215_763.png')
self.assertEqual(prediction['expression'], '(x+3)^5')
self.assertEqual(prediction['solution'], '[-3]')
self.assertEqual(prediction['arabic_expr'], '<html><body>(ب+٣) <sup>٥</sup></body></html>')
self.assertEqual(prediction['arabic_sol'], ['<html><body> -٣</body></html>'])
prediction = get_Prediction('test_images/IMG_5375.png')
self.assertEqual(prediction['expression'], '10^(12)')
self.assertEqual(prediction['solution'], '1000000000000.00')
self.assertEqual(prediction['arabic_expr'], '<html><body>١٠ <sup>١٢</sup></body></html>')
self.assertEqual(prediction['arabic_sol'], ['<html><body>١٠٠٠٠٠٠٠٠٠٠٠٠,٠٠</body></html>'])
prediction = get_Prediction('test_images/IMG_2.png')
self.assertEqual(prediction['expression'], '2^x-3')
self.assertEqual(prediction['solution'], '[log(3)/log(2)]')
self.assertEqual(prediction['arabic_expr'], '<html><body>٢ <sup>س</sup> -٣</body></html>')
self.assertEqual(prediction['arabic_sol'], ['<html><body>لـو(٣)\\لـو(٢)</body></html>'])
prediction = get_Prediction('test_images/IMG_4.png')
self.assertEqual(prediction['expression'], '2^3-5')
self.assertEqual(prediction['solution'], '3.00000000000000')
self.assertEqual(prediction['arabic_expr'], '<html><body>٢ <sup>٣</sup> -٥</body></html>')
self.assertEqual(prediction['arabic_sol'], ['<html><body>٣,٠٠٠٠٠٠٠٠٠٠٠٠٠٠</body></html>'])
def test_fraction(self):
prediction = get_Prediction('test_images/IMG_9.png')
self.assertEqual(prediction['expression'], '(x+1/2)/(x^2+3/4)=4/5')
self.assertEqual(prediction['solution'], '[5/8 - sqrt(17)/8, sqrt(17)/8 + 5/8]')
self.assertEqual(prediction['arabic_expr'],
'<html><body>(س+١\\٢)\\(س <sup>٢</sup>+٣\\٤)=٤\\٥</body></html>')
self.assertEqual(prediction['arabic_sol'], ['<html><body>٥\\٨ -√(١٧)\\٨</body></html>',
'<html><body>√(١٧)\\٨+٥\\٨</body></html>'])
def test_sqrt(self):
prediction = get_Prediction('test_images/IMG_20220325_180702_831.jpg')
self.assertEqual(prediction['expression'], 'sqrt(x^2+2*x+4)=0')
self.assertEqual(prediction['solution'], '[-1 - sqrt(3)*I, -1 + sqrt(3)*I]')
self.assertEqual(prediction['arabic_expr'], '<html><body>√(س <sup>٢</sup>+٢*س+٤)=٠</body></html>')
self.assertEqual(prediction['arabic_sol'], ['<html><body> -١ -√(٣)*ت</body></html>',
'<html><body> -١+√(٣)*ت</body></html>'])
def test_decimal_point(self):
prediction = get_Prediction('test_images/IMG_20220325_183755_522.jpg')
self.assertEqual(prediction['expression'], '(5.3+7)/5+1')
self.assertEqual(prediction['solution'], '3.46000000000000')
def test_times(self):
prediction = get_Prediction('test_images/IMG_10.png')
self.assertEqual(prediction['expression'], '2*x*sin(x^2)')
self.assertEqual(prediction['solution'], '[0, -sqrt(pi), sqrt(pi)]')
self.assertEqual(prediction['arabic_expr'], '<html><body>٢*س*جا(س <sup>٢</sup>)</body></html>')
self.assertEqual(prediction['arabic_sol'], ['<html><body>٠</body></html>', '<html><body> -√(ط)</body></html>',
'<html><body>√(ط)</body></html>'])
prediction = get_Prediction('test_images/IMG_11.png')
self.assertEqual(prediction['expression'], '(1+1)*(2+3)')
self.assertEqual(prediction['solution'], '10.0000000000000')
self.assertEqual(prediction['arabic_expr'], '<html><body>(١+١)*(٢+٣)</body></html>')
self.assertEqual(prediction['arabic_sol'], ['<html><body>١٠,٠٠٠٠٠٠٠٠٠٠٠٠٠</body></html>'])
def test_trig_functions(self):
prediction = get_Prediction('test_images/IMG_20220330_185200_343.jpg')
self.assertEqual(prediction['expression'], 'sin(2*pi)+1')
self.assertEqual(prediction['solution'], '1.00000000000000')
prediction = get_Prediction('test_images/IMG_20220406_175855_152.jpg')
self.assertEqual(prediction['expression'], 'tan(pi*E)')
self.assertEqual(prediction['solution'], '-1.22216467181901')
prediction = get_Prediction('test_images/IMG_20220406_190500_200.jpg')
self.assertEqual(prediction['expression'], 'sin(6*pi+3)')
self.assertEqual(prediction['solution'], '0.141120008059867')
prediction = get_Prediction('test_images/IMG_20220406_212851_465.jpg')
self.assertEqual(prediction['expression'], 'sec(6*pi+3)')
self.assertEqual(prediction['solution'], '-1.01010866590799')
prediction = get_Prediction('test_images/IMG_12.png')
self.assertEqual(prediction['expression'], 'sin(E)^2+cos(E)^2')
self.assertEqual(prediction['solution'], '1.00000000000000')
self.assertEqual(prediction['arabic_expr'],
'<html><body>جا(هـ) <sup>٢</sup>+جتا(هـ) <sup>٢</sup></body></html>')
self.assertEqual(prediction['arabic_sol'], ['<html><body>١,٠٠٠٠٠٠٠٠٠٠٠٠٠٠</body></html>'])
prediction = get_Prediction('test_images/IMG_20220517_141952_471.jpg')
self.assertEqual(prediction['expression'], '(sec(3*pi))/(csc(3*pi))')
self.assertEqual(prediction['solution'], '0')
self.assertEqual(prediction['arabic_expr'], '<html><body>(قا(٣*ط))\\(قتا(٣*ط))</body></html>')
self.assertEqual(prediction['arabic_sol'], ['<html><body>٠</body></html>'])
def test_poly_equations(self):
prediction = get_Prediction('test_images/IMG_20220404_001419_252.jpg')
self.assertEqual(prediction['expression'], 'x^2+3*x=-2')
self.assertEqual(prediction['solution'], '[-2, -1]')
prediction = get_Prediction('test_images/IMG_20220404_003309_121.jpg')
self.assertEqual(prediction['expression'], 'x^2+3*x+2')
self.assertEqual(prediction['solution'], '[-2, -1]')
self.assertEqual(prediction['arabic_expr'], '<html><body>س <sup>٢</sup>+٣*س+٢</body></html>')
self.assertEqual(prediction['arabic_sol'],
['<html><body> -٢</body></html>', '<html><body> -١</body></html>'])
prediction = get_Prediction('test_images/IMG_1.png')
self.assertEqual(prediction['expression'], 'x^3-x^2-7')
self.assertEqual(prediction['solution'],
'[1/3 + (-1/2 - sqrt(3)*I/2)*(sqrt(4053)/18 + 191/54)**(1/3) + 1/(9*(-1/2 - sqrt(3)*I/2)*('
'sqrt(4053)/18 + 191/54)**(1/3)), 1/3 + 1/(9*(-1/2 + sqrt(3)*I/2)*(sqrt(4053)/18 + '
'191/54)**(1/3)) + (-1/2 + sqrt(3)*I/2)*(sqrt(4053)/18 + 191/54)**(1/3), 1/(9*(sqrt(4053)/18 '
'+ 191/54)**(1/3)) + 1/3 + (sqrt(4053)/18 + 191/54)**(1/3)]')
self.assertEqual(prediction['arabic_expr'],
'<html><body>س <sup>٣</sup> -س <sup>٢</sup> -٧</body></html>')
self.assertEqual(prediction['arabic_sol'], ['<html><body>١\\٣+( -١\\٢ -√(٣)*ت\\٢)*(√(٤٠٥٣)\\١٨+١٩١\\٥٤) <sup>١\\٣</sup>+١\\(٩*( -١\\٢ -√(٣)*ت\\٢)*(√(٤٠٥٣)\\١٨+١٩١\\٥٤) <sup>١\\٣</sup>)</body></html>',
'<html><body>١\\٣+١\\(٩*( -١\\٢+√(٣)*ت\\٢)*(√(٤٠٥٣)\\١٨+١٩١\\٥٤) <sup>١\\٣</sup>)+( -١\\٢+√(٣)*ت\\٢)*(√(٤٠٥٣)\\١٨+١٩١\\٥٤) <sup>١\\٣</sup></body></html>',
'<html><body>١\\(٩*(√(٤٠٥٣)\\١٨+١٩١\\٥٤) <sup>١\\٣</sup>)+١\\٣+(√(٤٠٥٣)\\١٨+١٩١\\٥٤) <sup>١\\٣</sup></body></html>'])
prediction = get_Prediction('test_images/IMG_3.png')
self.assertEqual(prediction['expression'], 'x^2-4=0')
self.assertEqual(prediction['solution'], '[-2, 2]')
self.assertEqual(prediction['arabic_expr'], '<html><body>س <sup>٢</sup> -٤=٠</body></html>')
self.assertEqual(prediction['arabic_sol'],
['<html><body> -٢</body></html>', '<html><body>٢</body></html>'])
prediction = get_Prediction('test_images/IMG_6.png')
self.assertEqual(prediction['expression'], 'x^2-9=0')
self.assertEqual(prediction['solution'], '[-3, 3]')
self.assertEqual(prediction['arabic_expr'], '<html><body>س <sup>٢</sup> -٩=٠</body></html>')
self.assertEqual(prediction['arabic_sol'],
['<html><body> -٣</body></html>', '<html><body>٣</body></html>'])
def test_log(self):
prediction = get_Prediction('test_images/IMG_20220406_185940_704.jpg')
self.assertEqual(prediction['expression'], 'log(pi,pi)')
self.assertEqual(prediction['solution'], '1.00000000000000')
prediction = get_Prediction('test_images/IMG_5.png')
self.assertEqual(prediction['expression'], 'log(2*2-3,10)')
self.assertEqual(prediction['solution'], '0')
self.assertEqual(prediction['arabic_expr'], '<html><body>لـو<sub>١٠</sub>(٢*٢ -٣)</body></html>')
self.assertEqual(prediction['arabic_sol'], ['<html><body>٠</body></html>'])
prediction = get_Prediction('test_images/IMG_13.png')
self.assertEqual(prediction['expression'], 'log(log(log(16,10),10),10)')
self.assertEqual(prediction['solution'], '-1.09328921534395')
self.assertEqual(prediction['arabic_expr'],
'<html><body>لـو<sub>١٠</sub>(لـو<sub></sub>(لـو<sub></sub>(١٦))),١٠),١٠)</body></html>')
self.assertEqual(prediction['arabic_sol'], ['<html><body> -١,٠٩٣٢٨٩٢١٥٣٤٣٩٥</body></html>'])
def test_complex_expr(self):
prediction = get_Prediction('test_images/IMG_20220515_205442_998.jpg')
self.assertEqual(prediction['expression'], '(-x+sqrt(x^2+4*x*pi))/(sin(pi)+log(10,10))')
self.assertEqual(prediction['solution'], '[0]')
def test_more_than_1_variable(self):
prediction = get_Prediction('test_images/IMG_7.png')
self.assertEqual(prediction['expression'], 'x^2+y^2=0')
self.assertEqual(prediction['solution'], '[(-I*y, y), (I*y, y)]')
self.assertEqual(prediction['arabic_expr'], '<html><body>س <sup>٢</sup>+ص <sup>٢</sup>=٠</body></html>')
self.assertEqual(prediction['arabic_sol'],
['<html><body>( -ت*ص,ص)</body></html>', '<html><body>(ت*ص,ص)</body></html>'])
prediction = get_Prediction('test_images/IMG_8.png')
self.assertEqual(prediction['expression'], 'x+y-z+a')
self.assertEqual(prediction['solution'], '[(-a - y + z, y, z, a)]')
self.assertEqual(prediction['arabic_expr'], '<html><body>أ+ب -جـ+د</body></html>')
self.assertEqual(prediction['arabic_sol'], ['<html><body>( -د -ب+جـ,ب,جـ,د)</body></html>'])
def test_simplification(self):
prediction = get_Prediction('test_images/IMG_11.png')
self.assertEqual(prediction['expression'], '(1+1)*(2+3)')
self.assertEqual(prediction['solution'], '10.0000000000000')
prediction = get_Prediction('test_images/IMG_5375.png')
self.assertEqual(prediction['expression'], '10^(12)')
self.assertEqual(prediction['solution'], '1000000000000.00')
prediction = get_Prediction('test_images/IMG_20220325_183755_522.jpg')
self.assertEqual(prediction['expression'], '(5.3+7)/5+1')
self.assertEqual(prediction['solution'], '3.46000000000000')
prediction = get_Prediction('test_images/IMG_12.png')
self.assertEqual(prediction['expression'], 'sin(E)^2+cos(E)^2')
self.assertEqual(prediction['solution'], '1.00000000000000')
def test_differentiation(self):
eng_pred, arab_pred = solve('test_images/IMG_1.png', problem='d')
self.assertEqual(eng_pred['expression'], 'x^3-x^2-7')
self.assertEqual(eng_pred['solution'], '3*x**2 - 2*x')
self.assertEqual(arab_pred['expression'], '<html><body>س <sup>٣</sup> -س <sup>٢</sup> -٧</body></html>')
self.assertEqual(arab_pred['solution'], ['<html><body>٣*س <sup>٢</sup> -٢*س</body></html>'])
eng_pred, arab_pred = solve('test_images/IMG_4.png', problem='d')
self.assertEqual(eng_pred['expression'], '2^3-5')
self.assertEqual(eng_pred['solution'], '0')
def test_integration(self):
eng_pred, arab_pred = solve('test_images/IMG_20220404_003309_121.jpg', problem='i')
self.assertEqual(eng_pred['expression'], 'x^2+3*x+2')
self.assertEqual(eng_pred['solution'], 'x**3/3 + 3*x**2/2 + 2*x')
self.assertEqual(arab_pred['expression'], '<html><body>س <sup>٢</sup>+٣*س+٢</body></html>')
self.assertEqual(arab_pred['solution'], ['<html><body>س <sup>٣</sup>\\٣+٣*س <sup>٢</sup>\\٢+٢*س</body></html>'])
eng_pred, arab_pred = solve('test_images/integ_trig.jpg', problem='i')
self.assertEqual(eng_pred['expression'], 'sin(2*x)')
self.assertEqual(eng_pred['solution'], '-cos(2*x)/2')
self.assertEqual(arab_pred['expression'], '<html><body>جا(٢*س)</body></html>')
self.assertEqual(arab_pred['solution'], ['<html><body> -جتا(٢*س)\\٢</body></html>'])
if __name__ == '__main__':
unittest.main()