text_processor2.py 11.65 KiB
import codecs
import json
def text_process(arr):
new_arr = []
new_arr.append(f"## **{arr.pop(0)}**")
for _ in range(4):
del arr[0]
stek = []
p = False
li_c = False
li = False
li_c2 = False
for s in arr:
if "Пример" in s:
if p:
new_arr.append(f"|||{'<br>'.join(stek).rstrip('<br>')}")
else:
new_arr.append("<br>".join(stek).rstrip("<br>"))
p = True
stek = []
new_arr.append(f"### **{s}**")
elif p:
if s == "Ввод\tВывод":
if stek:
new_arr.append(f"|||{'<br>'.join(stek).rstrip('<br>')}")
new_arr.append("<br>".join(stek).rstrip("<br>"))
stek = []
new_arr.append("|Ввод |Вывод|")
new_arr.append("|-----|-----|")
else:
stek.append(s)
elif s == "Формат ввода" or s == "Формат вывода" or s == "Примечание":
new_arr.append("<br>".join(stek).rstrip("<br>"))
stek = []
new_arr.append(f"### **{s}**")
else:
if s and s[-1] == ":":
li_c = True
stek.append(s)
elif li_c:
li_c = False
if s == "":
li = True
new_arr.append("<br>".join(stek) + "<br>")
stek = []
elif li:
if not li_c2:
if s[-1] == ";":
li_c2 = True
else:
li = False
stek.append(s)
if li_c2:
new_arr.append(f"* {s}")
if s[-1] == ".":
li = False
new_arr.append("")
li_c2 = False
else:
stek.append(s)
new_arr.append(f"|||{'<br>'.join(stek[:-1]).rstrip('<br>')}")
input_count = stek[-1]
if len(input_count.split()) > 1:
input_count = [int(i) for i in input_count.split()]
elif input_count != "_":
input_count = int(input_count)
c = 0
for i in range(len(new_arr)):
if new_arr[i][:3] == "|||":
data = new_arr[i][3:].split("<br>")
if input_count == "_":
data = f"|{'<br>'.join(data)}||"
elif isinstance(input_count, list):
in_c = input_count[c]
if in_c > 0:
if 2 * in_c > len(data):
data = f"|{'<br>'.join(data[:in_c])}<br>|{'<br>'.join(data[in_c:])}<br>{'<br>'.join([' '] * (2 * in_c - len(data)))}<br>|"
elif 2 * in_c == len(data):
data = f"|{'<br>'.join(data[:in_c])}<br>|{'<br>'.join(data[in_c:])}<br>|"
else:
data = f"|{'<br>'.join(data[:in_c])}<br>{'<br>'.join([' '] * (len(data) - 2 * in_c))}<br>|{'<br>'.join(data[in_c:])}<br>|"
else:
if -2 * in_c > len(data):
data = f"|{'<br>'.join(data[:len(data) + in_c])}<br>{'<br>'.join([' '] * (-2 * in_c - len(data)))}<br>|{'<br>'.join(data[:-1 * in_c])}<br>|"
elif -2 * in_c == len(data):
data = f"|{'<br>'.join(data[:-1 * in_c])}<br>|{'<br>'.join(data[-1 * in_c:])}<br>|"
else:
data = f"|{'<br>'.join(data[:len(data) + in_c])}<br>|{'<br>'.join(data[len(data) + in_c:])}<br>{'<br>'.join([' '] * (len(data) - -2 * in_c))}<br>|"
c += 1
elif input_count > 0:
if 2 * input_count > len(data):
data = f"|{'<br>'.join(data[:input_count])}<br>|{'<br>'.join(data[input_count:])}<br>{'<br>'.join([' '] * (2 * input_count - len(data)))}<br>|"
elif 2 * input_count == len(data):
data = f"|{'<br>'.join(data[:input_count])}<br>|{'<br>'.join(data[input_count:])}<br>|"
else:
data = f"|{'<br>'.join(data[:input_count])}<br>{'<br>'.join([' '] * (len(data) - 2 * input_count))}<br>|{'<br>'.join(data[input_count:])}<br>|"
else:
if -2 * input_count > len(data):
data = f"|{'<br>'.join(data[:len(data) + input_count])}<br>{'<br>'.join([' '] * (-2 * input_count - len(data)))}<br>|{'<br>'.join(data[:-1 * input_count])}<br>|"
elif -2 * input_count == len(data):
data = f"|{'<br>'.join(data[:-1 * input_count])}<br>|{'<br>'.join(data[-1 * input_count:])}<br>|"
else:
data = f"|{'<br>'.join(data[:len(data) + input_count])}<br>|{'<br>'.join(data[len(data) + input_count:])}<br>{'<br>'.join([' '] * (len(data) - -2 * input_count))}<br>|"
new_arr[i] = data
for i in range(len(new_arr)):
if new_arr[i] and (new_arr[i][0] == "|" or new_arr[i][0] == "#"):
continue
elems = new_arr[i].split("<br>")
# ВЫДЕЛАЕТ ОТДЕЛЬНЫЕ БУКВЫ
del_ind = []
for j in range(len(elems) - 1):
if len(elems[j]) == 1 and elems[j] == elems[j + 1][0]:
del_ind.append(j)
if del_ind:
for j in del_ind[::-1]:
if j:
buff = elems.pop(j + 1)
if elems[j - 1][-1] == ".":
elems[j] = "$" + buff[0] + "$" + buff[1:]
else:
elems[j - 1] = elems[j - 1] + "$" + buff[0] + "$" + buff[1:]
del elems[j]
# ВЫДЕЛЯЕТ ВЫРАЖЕНИЯ
del_ind = []
for j in range(len(elems)):
if not elems[j]:
continue
string = elems[j].split()
first_word = string[0]
if len(first_word) <= j and ''.join(elems[j - len(first_word):j]) == first_word:
f = False
signs = ["+", "-", "*", "/", "%", "=", "<", ">", "≤", "≥"]
for sign in signs:
if sign in first_word:
f = True
if not f:
continue
del_ind.append([count for count in range(j - len(first_word), j)])
if del_ind:
for j in del_ind[::-1]:
ind = j[-1] + 1
buff = elems[ind].split()
if elems[j[0] - 1][-1] == ".":
elems[ind] = "$" + buff[0] + "$ " + " ".join(buff[1:])
for k in j[::-1]:
if k:
del elems[k]
else:
elems[j[0] - 1] = elems[j[0] - 1] + "$" + buff[0] + "$ " + ' '.join(buff[1:])
del elems[ind]
for k in j[::-1]:
if k:
del elems[k]
# ВЫДЕЛЯЕТ ДРУГИЕ ВЫРАЖЕНИЯ
del_ind = []
for j in range(len(elems) - 1):
if not elems[j]:
continue
eq = elems[j].split()[0]
f = False
signs = ["+", "-", "*", "/", "%", "=", "<", ">", "≤", "≥"]
for num in range(10):
signs.append(str(num))
for sign in signs:
if sign in eq:
f = True
if not f or len(eq) == 1:
continue
if ")" in eq:
eq = eq[:eq.index(")")]
if " " in eq:
eq = eq[:-1]
if elems[j - 1] == elems[j + 1]:
f = False
for ind in range(j - 3, -1, -1):
if eq == "".join(elems[ind:j - 1]):
f = True
break
if not f:
continue
del_ind.append([count for count in range(ind, j - 1)] + [j - 1, False])
else:
f = False
for ind in range(j - 2, -1, -1):
if eq == "".join(elems[ind:j]):
f = True
break
if not f:
continue
del_ind.append([count for count in range(ind, j)] + [True])
if del_ind:
for j in del_ind[::-1]:
if j[-1]:
buff = elems.pop(j[-2] + 1)
if ")" in buff:
ind = buff.index(")")
elems[j[0] - 1] = elems[j[0] - 1] + "$" + buff[:ind] + "$" + buff[ind:]
else:
elems[j[0] - 1] = elems[j[0] - 1] + "$" + buff + "$"
for k in j[::-1][1:]:
del elems[k]
else:
buff = elems.pop(j[-2] + 1)
elems[j[0] - 1] = elems[j[0] - 1] + "$" + buff[:-1] + "^" + elems[j[-2]] + "$"
elems[j[0] - 1] = elems[j[0] - 1] + elems[j[-2] + 2][1:]
del elems[j[-2] + 2]
del elems[j[-2] + 1]
del elems[j[-2]]
for k in j[::-1][2:]:
del elems[k]
# ДЕЛАЕТ $x_i$
del_ind = []
for j in range(len(elems) - 4):
if [elems[j] + " ", elems[j + 1]] == [elems[j + 2], elems[j + 3]] and elems[j + 4] == "\u200b":
del_ind.append([j, j + 1])
if del_ind:
for j in del_ind[::-1]:
if j:
buff = f"${elems[j[0]]}_{elems[j[1]]}$"
if j[0] != 0:
elems[j[0] - 1] = elems[j[0] - 1] + buff + elems[j[1] + 4][1:]
else:
elems[j[0]] = buff + elems[j[1] + 4][1:]
for count in range(4, -1, -1):
del elems[j[1] + count]
if j[0] != 0:
del elems[j[0]]
new_arr[i] = "<br>".join(elems)
return new_arr
f = codecs.open("2.4/24.ipynb", 'r', encoding="utf-8")
source = f.read()
y = json.loads(source)
f.close()
inp_data = open("md.txt", encoding="utf-8")
arr = []
tarr = []
for i in inp_data:
if i == "/py\n":
arr.append(tarr)
tarr = []
elif i == "/md\n":
arr.append(tarr)
tarr = []
else:
tarr.append(i.strip("\n"))
arr.append(tarr)
del arr[0]
data = {'cells':[],
'metadata': {'kernelspec': {'display_name': 'Python 3', 'language': 'python', 'name': 'python3'},
'language_info': {'name': 'python', 'version': '3.11.1'}},
'nbformat': 4,
'nbformat_minor': 2}
arr1, arr2 = arr[:len(arr) // 2], arr[len(arr) // 2:]
task_ammount = int(input("Введите количество заданий: "))
for i in range(0, 2 * task_ammount - 1, 2):
arr[i] = arr1[i // 2]
arr[i + 1] = arr2[i // 2]
# print(*arr, sep="\n")
for i in range(len(arr)):
if i % 2 == 0:
py_data = {'cell_type': 'markdown',
'metadata': {},
'source': [],}
new_arr = text_process(arr[i])
for s in new_arr:
py_data['source'] += [s]
else:
py_data = {'cell_type': 'code',
'execution_count': None,
'metadata': {},
'outputs': [],
'source': [],}
for s in arr[i]:
py_data['source'] += [s]
data['cells'] += [py_data]
with open('output.ipynb', 'w') as file:
# Write the JSON data to the file
json.dump(data, file)
file.close()