diff --git a/example.tex b/example.tex new file mode 100644 index 0000000..dd270f8 --- /dev/null +++ b/example.tex @@ -0,0 +1,10 @@ +$$x=a+b$$ a+c=b $$a+b=\phi$$asdasdas +$$n=v+n*b$$ +\begin{equation}b=a+t+y*c\end{equation} 123123123 +\begin{equation} +m=n+k*k +\end{equation} +$$x=\integrate(x/(x**2+2*x+1), x)$$ +skjvbbksjvk $$h=k+i*i$$ ekvjeorjvasdekvj +$j=k+y*y$ +1231231$$n=\phi+y$$123123 \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..7451275 --- /dev/null +++ b/main.py @@ -0,0 +1,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') diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..e6dc077 --- /dev/null +++ b/test.txt @@ -0,0 +1,16 @@ +$$l=t+i*f/s$$ +dxhbzrar +$a-b=c+f*r$ +dfgbfnst +\begin{equation}r=g+g+o+m*k\end{equation} +\(r-f=z+v\)dvsv +sedtgb +\begin{equation}\frac{7*u*j}{47 - \beta} + s*d*s*m*t -\frac{1 - x}{2132 - \phi} + \frac{1 - x}{232 - \alpha}\end{equation} +skjvbbksjvk $$h=k+i*i$$ ekvjeorjvekvj +$j=r+d*y$ +fbsnrhagr +$$x=a+b*y/s$$ +$$n-p=v+n*b$$ +\begin{equation}b=a+g+t+y*c\end{equation} +skjvbbksjvk $$h=k+i*i$$ ekvjeorjvekvj +$j=k+y*y$ \ No newline at end of file diff --git a/versionV1.py b/versionV1.py new file mode 100644 index 0000000..3cec5a5 --- /dev/null +++ b/versionV1.py @@ -0,0 +1,33 @@ +import sympy.abc +import re + + +def parsing(): + with open('example.tex') as f: + data = f.read() + formulas = list() + data = data.replace('$$', '$') + formulas.append(re.findall(r'\\begin{equation}([\s\S]*?)\\end{equation}', data)) + formulas.append(re.findall(r'\$([^$]+)\$', data)) + + i = 0 + while i < len(formulas): + if len(formulas[i]) == 0: + formulas.pop(i) + i -= 1 + i += 1 + + for i in range(len(formulas)): + for j in range(len(formulas[i])): + temp = formulas[i][j].replace('\\', '') + formulas[i][j] = temp + print(formulas) + return formulas + + +if __name__ == '__main__': + data = parsing() + for i in range(len(data)): + for j in range(len(data[i])): + expr = sympy.Eq(*map(sympy.sympify, (data[i][j].split("=")))) + print(expr) \ No newline at end of file