-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcor.py
180 lines (153 loc) · 7.01 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
import re
from datetime import datetime
from pathlib import Path
from color import *
class CleanGABC:
def __init__(self):
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"}
self.header = [
"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",
]
self.string_dic = {
0:["(;)", "(:)", "(::)", "(,)", "(;3)", "(;4)", "(;6)"],
1:["(c1)","(c2)","(c3)","(c4)", "(f3)","(f4)","(cb3)","(c2@c4)"],
}
self.exception = {
r"<v>gre(height)?star</v>": "*",
#r"(\([,;:]\))\s?\*": r"*\1",
r"(\(([\w_.!])+?\)?)([,;:?!.])":r"\3\1",
r"\*([^(])": r"*()\1",
r"(\(::\))\s?(\n)":r"\1\2",
r"^(\d+)\.\s*" : r"<sp>\1.</sp>\n",
r"^(\d+)\s*" : r"<sp>\1</sp> ",
r"\n(<sp>[\d\.tpvra]+\.</sp>)\s*":r"\n\1\n",
r"\[([ou])h:[hl]\]":r"",
r"([aA]l.+?le.+?l)[uú](.+?)\{?[ij]a\}?":r"\1ú\2{ja}",
r"(\s*)((<[ib]>)?[Euoae]\.?(</[ib]>?\(.+?\)?){6}\s*(\(::\))?)":r"\1#eu#",
}
self.spe={
"<sp>ae</sp>":"æ",
"<sp>oe</sp>":"œ",
"<sp>'ae</sp>":"ǽ",
"<sp>'oe</sp>":"œ́",
"<sp>tsp</sp>":"",
"<sp>V/</sp>":"<sp>v.</sp>/",
"<i>T.P.</i>":"<sp>tp</sp>",
"&flx":"<sp>flx</sp>",
"&dbl":"<sp>dbl</sp>",
"&vel":"<sp>vel</sp>",
"&v/":"<sp>v.</sp>/",
"&r/":"<sp>r.</sp>/",
"&a/":"<sp>a.</sp>/",
"&ps":"<sp>ps.</sp>",
"&tp":"<sp>tp.</sp>",
"&ij":"<sp>ij.</sp>",
"&iij":"<sp>iij.</sp>",
"ae":"æ",
"oe":"œ",
"aé":"ǽ",
"oé":"œ́",
}
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, path_file, output_file):
print(ORANGE+" Gabc Cleaner"+RESET)
file = Path(path_file)
ext = file.suffix
final = ""
if ext == '.gabc':
with open(file, 'r', encoding='utf-8') as file:
content = file.read()
post = content.find("%%")
before = content[:post + 2]
after = str(content[post + 2:])
final = "" # Réinitialiser final
processed_fields = set() # Pour suivre les champs déjà traités
# Header
for type in self.header:
if type in processed_fields:
continue # Sauter si déjà traité
var = re.search(r"{}:\s*(.*?);".format(type), before)
value = ""
if var:
# Si le champ existe déjà dans le fichier
value = var.group(1).strip()
var = var.group(0)
else:
# Si le champ n'existe pas, demander la saisie
value = input(ORANGE + "[warn]"+ RESET + f" Input score {type}: ")
if value:
var = f"{type}: {value};"
else:
continue # Skip empty values
# Cas spécial pour office-part
if type == "office-part":
value = self.office_part()
var = f"{type}: {value};"
# Cas spécial pour Antiphona - vérifier le mode-differentia
if value == "Antiphona":
diff = re.search(r"mode-differentia:\s*(.*?);", before)
if not diff:
diff_value = input(ORANGE + "[warn]" + RESET + " Input mode-differentia: ")
if diff_value:
var += f"\nmode-differentia: {diff_value};"
if var: # N'ajouter que si var contient quelque chose
final += var + "\n"
processed_fields.add(type)
# gabc
for excep, corec in self.exception.items(): # avant les sauts de ligne?
after = re.sub(excep, corec, after, re.IGNORECASE, re.MULTILINE)
# Parcourir chaque catégorie (0 et 1) dans string_dic
for category in self.string_dic:
# Parcourir chaque string dans la catégorie
for string in self.string_dic[category]:
if string in after:
# Déterminer le nombre de sauts de ligne à ajouter
newlines = "\n\n" if category == 1 else "\n"
# Vérifier si le string est déjà suivi d'un saut de ligne
current_pos = 0
while True:
pos = after.find(string, current_pos)
if pos == -1:
break
current_pos = pos + 1
# Vérifier si un saut de ligne suit déjà
if not after[pos + len(string):].startswith("\n"):
after = after[:pos + len(string)] + newlines + after[pos + len(string):]
for spe, corec in self.spe.items():
after = after.replace(spe, corec)
# Fichier final
final = final + "\n" +'%%\n' + after
else:
print(RED+"[eror]"+RESET+" The file is not .gabc")
with open(output_file, 'w', encoding="utf-8") as f:
f.write(final)
return final