Commit d18626fe authored by Elias's avatar Elias

script added

parent 2aa2104e
import re
import ast
import os
from pympi import Praat
dossier_textgrid = 'textGrid'
fichier_txt = 'resTrainValTest2_Random.txt'
fichier_txt_backup = 'resTrainValTest2_Random_backup.txt'
output_mots = set()
# Sauvegarder le fichier original
with open(fichier_txt, 'r', encoding='utf-8') as f:
contenu_original = f.read()
with open(fichier_txt_backup, 'w', encoding='utf-8') as f_backup:
f_backup.write(contenu_original)
# Regex pour trouver chaque bloc .csv + Train : [(xmin, xmax, texte)]
regex_bloc = re.compile(r'(?P<nom_csv>\S+\.csv)\s*Train\s*:\s*(?P<train_data>\[\(.*?\)\])', re.DOTALL)
contenu_modifie = contenu_original # On travaille sur une copie
# Parcourir tous les blocs Train
for match in regex_bloc.finditer(contenu_original):
nom_csv = match.group('nom_csv')
train_data_str = match.group('train_data')
base_name = nom_csv.split('-')[0]
textgrid_path = os.path.join(dossier_textgrid, f"{base_name}.TextGrid")
if not os.path.exists(textgrid_path):
print(f"Fichier TextGrid manquant : {textgrid_path}")
continue
# Charger TextGrid
tg = Praat.TextGrid(textgrid_path)
try:
phrases = ast.literal_eval(train_data_str)
except Exception as e:
print(f"Erreur Train pour {nom_csv} : {e}")
continue
# Accès aux tiers locuteurs
tier_locuteur1 = tg.tiers[1].intervals
tier_locuteur2 = tg.tiers[4].intervals
nouvelles_phrases = []
for xmin_phrase, xmax_phrase, _ in phrases:
mots = []
def extraire_mots(intervals):
for xmin_mot, xmax_mot, mot in intervals:
if xmin_mot >= xmin_phrase and xmax_mot <= xmax_phrase:
mot = mot.strip()
if mot:
mots.append(f'"{mot}"')
output_mots.add(mot)
extraire_mots(tier_locuteur1)
extraire_mots(tier_locuteur2)
nouvelle_chaine = " ".join(mots)
nouvelles_phrases.append((xmin_phrase, xmax_phrase, nouvelle_chaine))
nouveau_bloc = f"{nom_csv} Train : {repr(nouvelles_phrases)}"
contenu_modifie = contenu_modifie.replace(match.group(0), nouveau_bloc)
# Réécrire le fichier texte modifié
with open(fichier_txt, 'w', encoding='utf-8') as f_modif:
f_modif.write(contenu_modifie)
# Écrire les mots extraits dans res3_Random.txt
with open('res3_Random.txt', 'w', encoding='utf-8') as f_mots:
for mot in sorted(output_mots):
f_mots.write(f'"{mot}"\n')
print("Fichier mis à jour :", fichier_txt)
print("Sauvegarde créée :", fichier_txt_backup)
print("Mots extraits dans res3_Random.txt")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment