-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcor.py
206 lines (167 loc) · 7.94 KB
/
cor.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from colorama import init, Fore, Style, just_fix_windows_console
import re
from datetime import datetime
RED = '\033[31m' # Rouge
GREEN = '\033[32m' # Vert
YELLOW = '\033[33m' # Jaune
RESET = '\033[0m' # Réinitialiser la couleur
class CleanGABC:
def __init__(self):
init(autoreset=True)
just_fix_windows_console()
self.office = {1:"Alleluia",2:"Antiphona",3:"Canticum", 4:"Communio", 5:"Graduale", 6:"Hymnus", 7:"Improperia", 8:"Introitus", 9:"Kyriale", 10:"Offertorium", 11:"Toni Communes", 12:"Prosa", 13:"Praefationes", 14:"Psalmus", 15:"Responsorium breve", 16:"Responsorium", 17:"Rhythmus", 18:"Sequentia", 19:"Supplicatio", 20:"Tropa", 21:"Tractus", 22:"Varia"}
def color_text(self, filegabc):
with open(filegabc, "r", encoding="utf-8") as file:
content = file.read()
# Remplacer "%%" par sa version colorée
content = content.replace("%%", Fore.GREEN + "%%")
pos = content.find("%%")
before = content[:pos + 2]
after = content[pos + 2:]
word_list = [
"name:",
"gabc-copyright:",
"score-copyright:",
"office-part:",
"occasion:",
"meter:",
"commentary:",
"arranger:",
"author:",
"date:",
"manuscript:",
"manuscript-reference:",
"manuscript-storage-place:",
"book:",
"language:",
"transcriber:",
"transcription-date:",
"mode:",
"user-notes:",
"annotation:",
";",
]
# Remplacement des mots en tenant compte de la casse
for word in word_list:
before = before.replace(word, Fore.YELLOW + word+ Fore.RESET )
if pos != -1:
result = []
in_para_spe = False
in_parentheses = False
in_cran = False
for i in range(len(after)):
char = after[i]
if char in r".*+?'^${}/!|_[]":
result.append(Fore.RED + char + Fore.RESET)
elif char == "(":
if i + 1 < len(after) and after[i + 1] in r'[,;:]':
result.append(Fore.YELLOW + char + Fore.RESET)
in_para_spe = True
else:
in_parentheses = True
result.append(Fore.CYAN + char + Fore.RESET)
elif char == ")":
if i - 1 >= 0 and after[i - 1] in ',;:0123456789':
result.append(Fore.YELLOW + char + Fore.RESET)
in_para_spe = False
else:
in_parentheses = False
result.append(Fore.CYAN + char + Fore.RESET)
elif char == ";":
result.append(Fore.YELLOW + char + Fore.RESET)
elif in_para_spe:
result.append(Fore.YELLOW + char + Fore.RESET)
elif in_parentheses:
if char in r"0123456789":
result.append(Fore.GREEN + char + Fore.RESET)
else:
result.append(Fore.CYAN + char + Fore.RESET)
elif char == "<":
in_cran = True
result.append(Fore.YELLOW + char + Fore.RESET)
elif char == ">":
in_cran = False
result.append(Fore.YELLOW + char + Fore.RESET)
elif in_cran:
result.append(Fore.YELLOW + char + Fore.RESET)
elif char == "\n":
result.append(char)
else:
result.append(Fore.MAGENTA + char + Fore.RESET)
content = before + "".join(result)
print()
print(content)
def office_part(self):
print(YELLOW+"[warn]"+RESET+" Enter the type of office partition: ")
for i, (key, valeur) in enumerate(self.office.items()):
print(f" {key} - {valeur}", end="\t")
if (i + 1) % 2 == 0:
print()
if len(self.office) % 2 !=0:
print()
office = input("Enter value(0-22): ")
print()
return self.office[int(office)]
def clean(self, input_file, output_file):
date = datetime.now()
date = date.strftime("%d/%m/%Y")
with open(input_file, "r", encoding="utf-8") as file:
lines = file.readlines()
filtred_lines = []
date_y = False
ano_i = 0
for line in lines:
if "date:" in line:
date_y = True
if "annotation:"in line:
ano_i += 1
for line in lines:
if not line.startswith(("transcriber")):
if "date:" in line:
filtred_lines.append(f"date:{date};\n")
print(GREEN+"[info]"+ RESET+f" Date update: {date}")
elif "%%"in line and ano_i == 1:
filtred_lines.append("annotation:;\n\n%%\n\n")
elif "%%"in line and ano_i == 0:
filtred_lines.append("annotation:;\nannotation:;\n\n%%\n\n")
else:
filtred_lines.append(line)
if line.startswith("name:"):
match = re.search(r"name:\s*(.*?);", line)
if match:
name = match.group(1).strip()
print(GREEN+"[info]"+ RESET + " Name:",name)
if not date_y:
print(GREEN+"[info]"+ RESET + f" Add date: {date}")
filtred_lines.append(f"date:{date};\n")
content = ''.join(filtred_lines)
for char in ["(;)", "(:)", "(::)", "(,)", "(;3)", "(;4)", "(;6)"]:
if char in content:
content = content.replace(char, char + "\n" if content[content.index(char) + len(char):].startswith("\n") is False else char)
for char in ["(c1)","(c2)","(c3)","(c4)", "(f3)","(f4)","(cb3)","(c2@c4)"]:
if char in content:
content = content.replace(char, char + "\n\n" if content[content.index(char) + len(char):].startswith("\n") is False else char)
match = re.search(r'office-part:\s*(.*?)\s*;', content)
if match:
content_between = match.group(1).strip()
if content_between:
print(GREEN+"[info]"+ RESET + " Office-part:", content_between)
else:
print(GREEN+"[info]"+ RESET + " Add office-part")
office = self.office_part()
#office_part = input("[warn]"+" Enter the type of office partition: ")
content = re.sub(r'date:\s*(.*?)\s*;', r'date: \1;\noffice-part: ' + office + ';', content)
else:
print(GREEN+"[info]"+ RESET + " Add office-part")
office = self.office_part()
#office_part = input("[warn]"+" Enter the type of office partition: ")
content = re.sub(r'date:\s*(.*?)\s*;', r'date: \1;\noffice-part: ' + office + ';', content)
for char in ["<eu>","<nlba>"]:
index = content.find(char)
while index != -1:
if index == 0 or content[index - 1] != "\n":
content = content.replace(char, "\n" + char, 1)
index = content.find(char, index + 1)
with open(output_file, "w", encoding="utf-8") as file:
file.write(content)
return output_file