У меня есть несколько re.compile для поиска регулярных выражений в тексте. Все работает, просто код слишком длинный, и я уверен, что есть лучший способ справиться с этим.
Код:
def find_regex(regex, text):
lista = []
for x in text:
matches_prima = re.findall(regex, x)
lunghezza = len(matches_prima)
lista.append(lunghezza)
print("The number of {} matches is ".format(regex), sum(lista))
def find_regex_fasi(regex, text):
matches_fasi = re.findall(regex, text)
print("Numero di corpo minore è", len(matches_fasi))
def main():
#find_prima = re.compile(r"\]\s*prima(?!\S)")
find_fase_base_2 = re.compile(r"\]\s([\w\s]+)\s[→]\sT") # ] parole → T
find_fase_12 = re.compile(r"\]\s1\s([\w\s]+)\s2\s([\w\s]+[^T])") # ] 1 parole 2 parole (esclude T)
find_fase_prima_12 = re.compile(r"\]\s+prima\s+1\s+([\w\s]+)\s+2([\w\s]+[^T])") # ] prima 1 parole 2 parole (esclude T)
find_fase_prima_123 = re.compile(r"\]\sprima\s1\s([\w\s]+)\s2([\w\s]+)\s3([\w\s]+)")
find_fase_prima_123T = re.compile(r"\]\sprima\s1\s([\w\s]+)\s2([\w\s]+)\s3\sT") #prima 1 parole 2 parole 3t
find_fase_prima_1freccia2 = re.compile(r"\]\s+prima\s1\s([\w\s]+)\s[→]\s2([\w\s]+[^T])") #] prima 1 parola → 2 parola
FIND_FASE12T = re.compile(r"\]\s1\s([\w\s]+)\s2\sT")
FIND_FASE123T_OPZ2 = re.compile(r"\]\s*prima\s*1([\w\s]+)\s*2([\w\s][^3|^3T]+) ")
FIND_FASE123T = re.compile(r"\]\s*1([\w\s]+)\s*2([\w\s]+)\s3\sT")
FIND_FASE_123FRECCIAT = re.compile(r"\]\s1\s([\w\s]+)\s2([\w\s]+)\s→\sT")
FIND_FASE_1FRECCIA23T = re.compile(r"\]\s1\s([\w\s]+)\s→\s2([\w\s]+)\s(T|3\sT)")
FIND_FASE_FRECCIA1F2FT = re.compile(r"\]\s1\s([\w\s]+)\s→\s2([\w\s]+)\s→\s(T|3\sT)")
FIND_FASE_PRIMA_123FRECCIAT = re.compile(r"\]\s*prima\s*1\s*([\w\s]+)\s*2([\w\s]+)\s*→\s*T")
FIND_FASE_PRIMA_1FRECCIA23T = re.compile(r"\]\s*prima\s*1\s*([\w\s]+)\s*→\s*2([\w\s]+)\s*(T|3\sT)")
FIND_FASE_PRIMA_FRECCIA1F2FT = re.compile(r"\]\s*prima\s*1\s*([\w\s]+)\s*→\s*2([\w\s]+)\s*→\s*(T|3\sT)")
FIND_FASE_PRIMA_1FRECCIA2 = re.compile(r"\]\s*prima\s*1\s*([\w\s]+)\s*→\s*2([\w\s]+)")
FIND_FASE_PRIMA_12345T = re.compile(r"\]\s*prima\s*1\s*([\w\s]+)\s*2([\w\s]+)\s*3([\w\s]+)\s*4([\w\s]+)\s*5\sT")
FIND_FASE_PRIMA_12345T_OPZ2 = re.compile(r"\]\s*prima\s*1\s*([\w\s]+)\s*2([\w\s]+)\s*3([\w\s]+)\s*4([\w\s][^5|^5\sT]+)")
FIND_FASE_12345T = re.compile(r"\]\s*1\s*([\w\s]+)\s*2([\w\s]+)\s*3([\w\s]+)\s*4([\w\s]+)\s*5\sT")
#find_da = re.compile(r"\]\s*da(?!\S)")
#find_da_cui = re.compile(r"\]\s*([\w\s]+)\s*da\scui")
#find_sps = re.compile(r"\]\s*([\w\s]+)\s*sps")
#find_su = re.compile(r"\]\s*([\w\s]+)\s*su")
#find_as = re.compile(r"\]\s*([\w\s]+)\s*as")
#find_ins = re.compile(r"\]\s*([\w\s]+)\s*ins")
#find_segue = re.compile(r"\]\s*([\w\s]+)\s*segue")
find_regex(FIND_FASE12T, testoo)
#find_regex(find_prima, testo)
find_regex(find_fase_base_2, testoo)
find_regex(find_fase_12, testoo)
find_regex(find_fase_prima_12, testoo)
find_regex(find_fase_prima_123, testoo)
find_regex(find_fase_prima_123T, testoo)
find_regex(find_fase_prima_1freccia2, testoo)
#find_regex(find_da, testoo)
#find_regex(find_da_cui, testoo)
#find_regex(find_sps, testoo)
#find_regex(find_su, testoo)
#find_regex(find_as, testoo)
#find_regex(find_ins, testoo)
#find_regex(find_segue, testoo)
#################
find_regex(FIND_FASE12T, testo_fasi)
find_regex(FIND_FASE123T_OPZ2, testo_fasi)
find_regex(FIND_FASE123T, testo_fasi)
find_regex(FIND_FASE_1FRECCIA23T, testo_fasi)
find_regex(FIND_FASE_123FRECCIAT, testo_fasi)
find_regex(FIND_FASE_FRECCIA1F2FT, testo_fasi)
find_regex(FIND_FASE_PRIMA_1FRECCIA23T, testo_fasi)
find_regex(FIND_FASE_PRIMA_123FRECCIAT, testo_fasi)
find_regex(FIND_FASE_PRIMA_FRECCIA1F2FT, testo_fasi)
find_regex(FIND_FASE_PRIMA_1FRECCIA2, testo_fasi)
find_regex(FIND_FASE_PRIMA_12345T, testo_fasi)
find_regex(FIND_FASE_PRIMA_12345T_OPZ2, testo_fasi)
find_regex(FIND_FASE_12345T, testo_fasi)
#find_regex(find_prima, testo_fasi)
find_regex(find_fase_base_2, testo_fasi)
find_regex(find_fase_12, testo_fasi)
find_regex(find_fase_prima_12, testo_fasi)
find_regex(find_fase_prima_123, testo_fasi)
find_regex(find_fase_prima_123T, testo_fasi)
find_regex(find_fase_prima_1freccia2, testo_fasi)
#find_regex(find_da, testo_fasi)
#find_regex(find_da_cui, testo_fasi)
#find_regex(find_sps, testo_fasi)
#find_regex(find_su, testo_fasi)
#find_regex(find_as, testo_fasi)
#find_regex(find_ins, testo_fasi)
#find_regex(find_segue, testo_fasi)
if __name__ == "__main__":
main()
testoo
и testo_fasi
это два текста, в которых я должен смотреть на одно и то же регулярное выражение. Поскольку структура текстов различна, для каждого текста есть две функции find_regex
. Как я могу сделать это лучше?