forked from vongostev/202-Advanced-Python-3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
119 lines (108 loc) · 3.33 KB
/
main.py
File metadata and controls
119 lines (108 loc) · 3.33 KB
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
import sympy.abc
import re
unicode_greek = ['Α', 'α',
'Β', 'β',
'Γ', 'γ',
'Δ', 'δ',
'Ε', 'ε',
'Ζ', 'ζ',
'Η', 'η',
'Θ', 'θ',
'Ι', 'ι',
'Κ', 'κ',
'Λ', 'λ',
'Μ', 'μ',
'Ν', 'ν',
'Ξ', 'ξ',
'Ο', 'ο',
'Π', 'π',
'Ρ', 'ρ',
'Σ', 'σ',
'Τ', 'τ',
'Υ', 'υ',
'Φ', 'φ',
'Χ', 'χ',
'Ψ', 'ψ',
'Ω', 'ω']
latex_greek = ['Alpha',
'alpha',
'Beta',
'beta',
'Gamma',
'gamma',
'Delta',
'delta',
'Epsilon',
'epsilon',
'Zeta',
'zeta',
'Eta',
'eta',
'Theta',
'theta',
'Iota',
'iota',
'Kappa',
'kappa',
'Lambda',
'lambda',
'Mu',
'mu',
'Nu',
'nu',
'Xi',
'xi',
'Omicron',
'omicron',
'Pi',
'pi',
'Rho',
'rho',
'Sigma',
'sigma',
'Tau',
'tau',
'Upsilon',
'upsilon',
'Phi',
'phi',
'Chi',
'chi',
'Psi',
'psi',
'Omega',
'omega']
greek_dict = dict(zip(latex_greek, unicode_greek))
def parsing(line : str):
with open(line) as f:
formulas = list()
for item in f:
formulas.append(re.findall(
r'\\begin{equation}(.*?)\\end{equation}', item))
formulas.append(re.findall(r'\$([^$]+)\$', item))
i = 0
while i < len(formulas):
if len(formulas[i]) == 0:
formulas.pop(i)
i -= 1
i += 1
for i in range(len(formulas)):
formulas[i] = formulas[i][0].replace('\\', '')
for greek_letter in latex_greek:
while greek_letter in formulas[i]:
formulas[i] = formulas[i].replace(
greek_letter, greek_dict[greek_letter])
while (formulas[i].find('frac{') != -1):
formulas[i] = formulas[i].replace('frac{', '(')
formulas[i] = formulas[i].replace('}{', ')/(')
formulas[i] = formulas[i].replace('}', ')')
if (formulas[i].find('=') != -1):
left = sympy.sympify(formulas[i][:formulas[i].find('=')])
right = sympy.sympify(formulas[i][(formulas[i].find('=') + 1):])
formulas[i] = (f'{left} = {right}')
else:
formulas[i] = sympy.sympify(formulas[i])
print(formulas)
return formulas
if __name__ == '__main__':
data = parsing('C:\\Users\\Ducky\\OneDrive\\Документы\\GitHub\\202-Advanced-Python-3\\test.txt')