-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_math.py
48 lines (39 loc) · 1.62 KB
/
extract_math.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
def text_to_latex(text):
# Словарь для соответствия слов и математических символов
replacements = {
"интеграл": r"\int",
"открывается": "(",
"закрывается": ")",
"плюс": "+",
"минус": "-",
"умножить": "*",
"делить": "/",
"и": ",",
"д": "d",
"икс": "x",
"степени": "^`",
"дробь": r"\frac",
"знаменатель": "`",
"числитель": "`",
"√": r"\sqrt`"
# добавьте другие соответствия по мере необходимости
}
# Преобразование текста в список слов
words = text.split()
# Преобразование каждого слова
latex_formula = ""
for word in words:
if word.lower() in replacements:
latex_formula += replacements[word.lower()]
else:
latex_formula += word
for sym_open in range(0, len(latex_formula)):
if latex_formula[sym_open-1] == "`" and latex_formula[sym_open] == "(":
latex_formula = latex_formula[:sym_open] + "{" + latex_formula[sym_open+1:]
for sym_close in range(sym_open, len(latex_formula)):
if latex_formula[sym_close] == ")":
latex_formula = latex_formula[:sym_close] + "}" + latex_formula[sym_close+1:]
break
latex_formula = latex_formula.replace("`", "")
print(latex_formula)
return "$" + latex_formula + "$"