|
|
|
|
|
import re |
|
|
|
|
|
def extract_and_parse_persons(input_file: str = "kbis_extracted.txt") -> dict: |
|
|
""" |
|
|
1. Extrait la section des personnes du K-bis. |
|
|
2. Parse gérants et associés avec gestion des deux formats (compact + ligne par ligne). |
|
|
Retourne un dictionnaire avec les résultats. |
|
|
""" |
|
|
try: |
|
|
with open(input_file, "r", encoding="utf-8") as f: |
|
|
full_text = f.read() |
|
|
except FileNotFoundError: |
|
|
print("Erreur : fichier non trouvé.") |
|
|
return {"Gérant(s)": "Erreur", "Associé(s)": "Erreur"} |
|
|
|
|
|
|
|
|
pattern = r"(Gestion, Direction, Administration, Contrôle, Associés ou Membres)\s+(.*?)\s+(Renseignements sur l['’]établissement principal)" |
|
|
match = re.search(pattern, full_text, re.I | re.DOTALL) |
|
|
|
|
|
if not match: |
|
|
print("Section personnes non trouvée.") |
|
|
return {"Gérant(s)": "Non détecté", "Associé(s)": "Non détecté"} |
|
|
|
|
|
persons_text = match.group(2).strip() |
|
|
|
|
|
|
|
|
gerants = [] |
|
|
associes = [] |
|
|
|
|
|
|
|
|
compact_pattern = re.finditer( |
|
|
r"Qualité\s+(gérant|Associé)\s+Nom[,;\s]*prénoms?\s*[:;,]?\s*([A-ZÀÂÄÇÉÈÊËÎÏÔÖÙÛÜŸ][A-Za-zàâäçéèêëîïôöùûüÿ\s,'’;-]+?)(?:\s+Date et lieu|\s+Nationalité|\s+Adresse|\s+Qualité|$)", |
|
|
persons_text, |
|
|
re.I |
|
|
) |
|
|
for m in compact_pattern: |
|
|
role = m.group(1).lower() |
|
|
name = m.group(2).strip().rstrip(";,.") |
|
|
if "gérant" in role: |
|
|
gerants.append(name) |
|
|
else: |
|
|
associes.append(name) |
|
|
|
|
|
|
|
|
lines = [line.strip() for line in persons_text.split("\n") if line.strip()] |
|
|
i = 0 |
|
|
while i < len(lines): |
|
|
line = lines[i] |
|
|
if re.search(r"Qualité\s+Nom[;,]?\s*prénoms?\s+Date et lieu de naissance\s+Nationalité\s+Adresse", line, re.I): |
|
|
i += 1 |
|
|
if i < len(lines): |
|
|
next_line = lines[i] |
|
|
standalone_match = re.match( |
|
|
r"(gérant|Associé)\s*([A-ZÀÂÄÇÉÈÊËÎÏÔÖÙÛÜŸ][A-Za-zàâäçéèêëîïôöùûüÿ\s,'’;-]+)", |
|
|
next_line, |
|
|
re.I |
|
|
) |
|
|
if standalone_match: |
|
|
role = standalone_match.group(1).lower() |
|
|
name = standalone_match.group(2).strip().rstrip(";,.") |
|
|
if "gérant" in role: |
|
|
gerants.append(name) |
|
|
else: |
|
|
associes.append(name) |
|
|
i += 1 |
|
|
|
|
|
|
|
|
gerants = list(dict.fromkeys(gerants)) |
|
|
associes = list(dict.fromkeys(associes)) |
|
|
|
|
|
result = { |
|
|
"Gérant(s)": ", ".join(gerants) if gerants else "Non détecté", |
|
|
"Associé(s)": ", ".join(associes) if associes else "Non détecté" |
|
|
} |
|
|
|
|
|
|
|
|
print("=== RÉSULTAT FINAL ===") |
|
|
for key, value in result.items(): |
|
|
print(f"{key}: {value}") |
|
|
|
|
|
return result |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
extract_and_parse_persons("kbis_extracted.txt") |