-
Notifications
You must be signed in to change notification settings - Fork 1
/
translation.py
168 lines (150 loc) · 5.58 KB
/
translation.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
dictionary = {"alif": "أ",
"ba": "ب",
"jeem": "جـ",
"dal": "د",
"ra": "ر",
".": ",",
"sen": "س",
"sad": "ص",
"pi": "ط",
"ayn": "ع",
"qaf": "ق",
"lam": "ل",
"mim": "م",
"nun": "ن",
"ha": "هـ",
"waw": "و",
"ya": "ى",
"E": "هـ",
"I": "ت",
"-": " -", # To handle issue with html when converting from english ltr to arabic rtl
"(": "(",
")": ")",
"[": "[",
"]": "]",
"{": "{",
"}": "}",
"+": "+",
"div": "\\",
"/": "\\",
"geq": "≥",
"gt": ">",
"csc": "قتا",
"infty": "∞",
"int": "∫",
"leq": "≤",
"lt": "<",
"neq": "≠",
"phi": "Φ",
"sigma": "Σ",
"sqrt": "√",
"sum": "Σ",
"theta": "θ",
"times": "×",
"larr": "←",
"cos": "جتا",
"lim": "نها",
"log": "لـو",
"sin": "جا",
"tan": "ظا",
"0": "٠",
"1": "١",
"2": "٢",
"3": "٣",
"4": "٤",
"5": "٥",
"6": "٦",
"7": "٧",
"8": "٨",
"9": "٩",
"cot": "ظتا",
"sec": "قا",
" ": "",
"*": "*",
"=": "=",
",": ","
}
stop_symbols = ["(", ")", "[", "]", "*", "+", "-", "^", "/", "=", ",", " "]
def toArabicExpr(equation, mapped_symbols):
arabic_expr = ''
i = 0
while i < len(equation):
curr_symbol = ''
if str.isalpha(equation[i]) and equation[i] not in ['I', 'E']:
while i < len(equation) and equation[i] not in stop_symbols:
curr_symbol += equation[i]
i += 1
if curr_symbol in mapped_symbols and mapped_symbols[curr_symbol] in dictionary:
arabic_expr += dictionary[mapped_symbols[curr_symbol]]
elif curr_symbol == 'log':
log_arg = ''
base = ''
if equation[i] == '(':
i += 1
while i < len(equation) and equation[i] not in [',', ')']:
log_arg += equation[i]
i += 1
if i < len(equation) and equation[i] == ')':
arabic_expr += dictionary[curr_symbol] + '(' + toArabicExpr(log_arg, mapped_symbols) + ')'
i += 1
else:
i += 1
while i < len(equation) and equation[i] != ')':
base += equation[i]
i += 1
i += 1
arabic_expr += dictionary[curr_symbol] + "<sub>" + toArabicExpr(base,
mapped_symbols) + "</sub>" + \
'(' + toArabicExpr(log_arg, mapped_symbols) + ')'
else:
print("Error in log parsing")
elif curr_symbol in dictionary:
arabic_expr += dictionary[curr_symbol]
else:
arabic_expr += curr_symbol
i -= 1
elif equation[i] == '^':
i += 1
exp_eqn = ''
if equation[i] == '(':
i += 1
open_close_brackets = 1
# Assumption: brackets in exponent are valid.
while i < len(equation) and open_close_brackets > 0:
if equation[i] == '(':
open_close_brackets += 1
elif equation[i] == ')':
open_close_brackets -= 1
if open_close_brackets > 0:
exp_eqn += equation[i]
i += 1
i -= 1 # Cancel effect of outer loop
arabic_expr += " <sup>" + toArabicExpr(exp_eqn, mapped_symbols) + "</sup>"
else:
exp_eqn += equation[i]
arabic_expr += " <sup>" + toArabicExpr(exp_eqn, mapped_symbols) + "</sup>"
elif equation[i] in dictionary:
arabic_expr += dictionary[equation[i]]
else:
arabic_expr += equation[i]
i += 1
return arabic_expr
def refine_expr(expression):
return expression.replace("**", "^")
def translate_to_arabic_html(expression, solution, mapping):
expression = refine_expr(str(expression))
reversed_mapping = {v: k for k, v in mapping.items()}
arabic_expr = toHtml(toArabicExpr(expression, reversed_mapping))
arabic_sol = []
if not isinstance(solution, list):
curr_arabic_sol = refine_expr(str(solution))
curr_arabic_sol = toArabicExpr(curr_arabic_sol, reversed_mapping)
arabic_sol.append(toHtml(curr_arabic_sol))
else:
for sol in solution:
curr_arabic_sol = refine_expr(str(sol))
curr_arabic_sol = toArabicExpr(curr_arabic_sol, reversed_mapping)
arabic_sol.append(toHtml(curr_arabic_sol))
return arabic_expr, arabic_sol
def toHtml(expression):
return "<html><body>" + expression + "</body></html>"