Spaces:
Running on Zero
Running on Zero
Download scripts/generer_outils_rome.py from Myz0/CV-Matching: direct link, hf CLI and curl.
- Browser
- Download file 5.74 kB
-
https://huggingface.co/spaces/Myz0/CV-Matching/resolve/main/scripts/generer_outils_rome.py
- Command line
-
hf download hf://spaces/Myz0/CV-Matching/scripts/generer_outils_rome.py
-
curl -L -o generer_outils_rome.py https://huggingface.co/spaces/Myz0/CV-Matching/resolve/main/scripts/generer_outils_rome.py
5.74 kB
| """Génère app/outils_rome.json : les noms d'outils (logiciels) connus du ROME 4.0 et, pour chacun, | |
| les savoirs ROME qui le citent (« Excel » -> « Maîtrise des outils informatiques et bureautiques | |
| (Word, Excel, PowerPoint) »…). Le service s'en sert pour relier un outil cité dans un CV à la | |
| compétence demandée dans l'AVP (voir app/outils.py). | |
| Source : ROME 4.0, France Travail, open data sous Licence Ouverte | |
| (https://www.data.gouv.fr/fr/datasets/58da857388ee384902e505f5/). Le fichier JSON complet | |
| (~43 Mo) n'est pas embarqué dans le service : seul l'extrait utile (quelques Ko) l'est. | |
| Lancer depuis la racine du projet : | |
| python -m scripts.generer_outils_rome [dossier_json_rome] | |
| Sans dossier : télécharge l'archive open data du ROME dans donnees/rome/. | |
| """ | |
| from __future__ import annotations | |
| import io | |
| import json | |
| import re | |
| import sys | |
| import urllib.request | |
| import zipfile | |
| from pathlib import Path | |
| RACINE = Path(__file__).resolve().parent.parent | |
| URL_ROME = "https://api.francetravail.fr/api-nomenclatureemploi/v1/open-data/json" | |
| SORTIE = RACINE / "app" / "outils_rome.json" | |
| # « Microsoft Word » -> « Word » : préfixes qui annoncent un logiciel | |
| PREFIXES = re.compile(r"^(Microsoft|Progiciel de gestion|Système d'exploitation( informatique)?|Framework|Logiciel|Langage|" | |
| r"Outil|Plateforme|Base de données|SGBD|Suite)\s+", re.I) | |
| PASSERELLE = re.compile(r"bureauti|informatique|logiciel|tableur|outils", re.I) | |
| def charger(dossier: Path, prefixe: str): | |
| f = next(dossier.glob(f"unix_{prefixe}_v*.json")) | |
| raw = f.read_bytes() | |
| try: | |
| data = json.loads(raw.decode("utf-8")) | |
| except UnicodeDecodeError: # les fichiers ROME mêlent les encodages | |
| data = json.loads(raw.decode("cp1252")) | |
| version = re.search(r"_v(\d+)\.json$", f.name).group(1) | |
| return data, version | |
| def premiere_liste(x): | |
| if isinstance(x, list): | |
| return x | |
| for v in x.values(): | |
| r = premiere_liste(v) if isinstance(v, (dict, list)) else None | |
| if r: | |
| return r | |
| def ressemble_a_un_nom(x: str) -> bool: | |
| """« PowerPoint », « SAP », « Active Directory » : oui. « Gestion », « Base » : non | |
| (un mot ordinaire qui commence par une majuscule n'est pas un nom de logiciel).""" | |
| return bool(re.search(r"[A-Z0-9]", x[1:]) or (" " in x and re.search(r"\s[A-Z]", x))) | |
| def noms_outils(libelle: str) -> set[str]: | |
| noms = set() | |
| for dedans in re.findall(r"\(([^)]*)\)", libelle): # « (Word, Excel, PowerPoint) » | |
| for x in re.split(r"[,/;]| et | ou ", dedans): | |
| x = x.strip(" .") | |
| if x and ressemble_a_un_nom(x) and len(x.split()) <= 3 and "etc" not in x.lower(): | |
| noms.add(x) | |
| tete = libelle.split(" (")[0].strip() | |
| reste = PREFIXES.sub("", tete) | |
| if reste != tete and re.search(r"[A-Z]", reste) and len(reste.split()) <= 3: | |
| noms.add(reste) # logiciel explicite : « Microsoft Word » | |
| if tete.startswith("Microsoft "): | |
| noms.add(tete) | |
| noms |= {m.group(0) for m in re.finditer(r"(?<!\w)(?:pack|suite)\s+[A-Z]\w+", tete)} # « pack Office » | |
| return {n for n in noms if len(n) >= 2 and not n.isdigit()} | |
| def motif(nom: str) -> re.Pattern: | |
| """Mot entier ; « Power BI » accepte aussi « PowerBI » ; insensible à la casse si plusieurs mots.""" | |
| return re.compile(r"(?<![\w-])" + re.escape(nom).replace(r"\ ", r"\s?") + r"(?![\w-])", re.I if " " in nom else 0) | |
| def generer(dossier: Path) -> dict: | |
| savoirs = [s for s in premiere_liste(charger(dossier, "referentiel_savoir")[0]) if s.get("libelle")] | |
| fiche_version = dossier / "version.txt" # « Numero de version : 61 » | |
| trouve = re.search(r"version\s*:\s*(\d+)", fiche_version.read_bytes().decode("cp1252", "replace"), re.I) if fiche_version.exists() else None | |
| version = trouve.group(1) if trouve else charger(dossier, "referentiel_savoir")[1] | |
| logiciel = lambda s: s.get("libelle_sous_categorie") == "Logiciels, progiciels" | |
| noms = set() | |
| for s in savoirs: | |
| if logiciel(s) or "(" in s["libelle"]: | |
| noms |= {n for n in noms_outils(s["libelle"]) if logiciel(s) or PASSERELLE.search(s["libelle"])} | |
| motifs = {n: motif(n) for n in noms} | |
| def savoirs_citant(nom): | |
| """Les savoirs « passerelles » d'abord : ceux qui citent plusieurs outils ou un mot général.""" | |
| cites = {s["libelle"] for s in savoirs if motifs[nom].search(s["libelle"])} | |
| return sorted(cites, key=lambda l: (-sum(1 for m in motifs.values() if m.search(l)), | |
| -bool(re.search(r"bureauti|informatique|tableur", l, re.I)), -len(l)))[:2] | |
| outils = {n: savoirs_citant(n) for n in sorted(noms, key=str.lower)} | |
| return {"source": f"ROME 4.0 version {version}, France Travail, Licence Ouverte", | |
| "outils": {n: l for n, l in outils.items() if l}} | |
| if __name__ == "__main__": | |
| if len(sys.argv) > 1: | |
| dossier = Path(sys.argv[1]) | |
| else: | |
| dossier = RACINE / "donnees" / "rome" / "json" | |
| if not any(dossier.glob("unix_referentiel_savoir_v*.json")): | |
| print("Téléchargement du ROME (open data France Travail)…") | |
| with urllib.request.urlopen(URL_ROME, timeout=300) as r: | |
| zipfile.ZipFile(io.BytesIO(r.read())).extractall(dossier) | |
| donnees = generer(dossier) | |
| SORTIE.write_text(json.dumps(donnees, ensure_ascii=False, indent=1), encoding="utf-8") | |
| print(f"{len(donnees['outils'])} outils -> {SORTIE} ({donnees['source']})") | |