Spaces:
Running on Zero
Running on Zero
Cheikh Niang commited on
Deploiement GuindiMa AI (interface Gradio)
Browse files- app.py +15 -8
- src/matching/geo.py +51 -7
app.py
CHANGED
|
@@ -619,8 +619,7 @@ def _note_sens_inverse(match: dict) -> str:
|
|
| 619 |
return (
|
| 620 |
"<div style='color:#6B7A72;font-size:12px;font-style:italic;"
|
| 621 |
"margin-top:6px;'>"
|
| 622 |
-
f"{intro} Au retour, le bus peut emprunter d'autres rues
|
| 623 |
-
"centre-ville.</div>"
|
| 624 |
)
|
| 625 |
|
| 626 |
|
|
@@ -731,14 +730,21 @@ def _bloc_arrets(match: dict) -> str:
|
|
| 731 |
)
|
| 732 |
|
| 733 |
|
| 734 |
-
def _une_carte(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 735 |
"""Une ligne : compagnie, numéro, trajet conseillé, arrêts parcourus.
|
| 736 |
|
| 737 |
On ne prétend pas connaître l'arrêt exact de l'utilisateur : on conseille
|
| 738 |
l'arrêt le plus proche du lieu qu'il a dit, et on rappelle que tout arrêt de
|
| 739 |
-
la ligne parcouru convient.
|
| 740 |
-
|
| 741 |
-
|
|
|
|
| 742 |
"""
|
| 743 |
numero = numero_de(match["ligne"])
|
| 744 |
affiche = re.sub(r"^Ligne\s*", "", str(match["ligne"]))
|
|
@@ -751,6 +757,7 @@ def _une_carte(match: dict, etape: str = "", depart: str = "", arrivee: str = ""
|
|
| 751 |
descend_a, _ = _nom_arret(match["descend_a"])
|
| 752 |
depart_texte = depart if depart else match["monte_a"]
|
| 753 |
arrivee_texte = arrivee if arrivee else match["descend_a"]
|
|
|
|
| 754 |
trajet = (
|
| 755 |
f"<div style='color:#11301C;font-size:22px;font-weight:800;"
|
| 756 |
f"line-height:1.35;margin-top:8px;'>"
|
|
@@ -759,7 +766,7 @@ def _une_carte(match: dict, etape: str = "", depart: str = "", arrivee: str = ""
|
|
| 759 |
f"<span style='font-weight:600;font-size:15px;color:#3D5A48;'>"
|
| 760 |
f"(ou tout arrêt de la ligne {affiche} près de toi)</span><br>"
|
| 761 |
f"Descends à <b style='color:#081F11;'>{arrivee_texte}</b> — arrêt conseillé : "
|
| 762 |
-
f"<b style='color:#081F11;'>{descend_a}</b></div>"
|
| 763 |
)
|
| 764 |
arrets = _bloc_arrets(match)
|
| 765 |
# Les terminus de la ligne, en petit : le parcours réel du bus.
|
|
@@ -816,7 +823,7 @@ def _carte(
|
|
| 816 |
for numero, trajet in enumerate(correspondances, start=1):
|
| 817 |
if numero > 1:
|
| 818 |
blocs.append(f"\n**Autre possibilité {numero}**\n")
|
| 819 |
-
blocs.append(_une_carte(trajet["etape_1"], "1er bus", depart=depart))
|
| 820 |
blocs.append(
|
| 821 |
f"<div style='text-align:center;color:#6B7A72;margin:2px 0;'>"
|
| 822 |
f"🔁 Change à <b>{trajet['changement']}</b></div>"
|
|
|
|
| 619 |
return (
|
| 620 |
"<div style='color:#6B7A72;font-size:12px;font-style:italic;"
|
| 621 |
"margin-top:6px;'>"
|
| 622 |
+
f"{intro} Au retour, le bus peut emprunter d'autres rues.</div>"
|
|
|
|
| 623 |
)
|
| 624 |
|
| 625 |
|
|
|
|
| 730 |
)
|
| 731 |
|
| 732 |
|
| 733 |
+
def _une_carte(
|
| 734 |
+
match: dict,
|
| 735 |
+
etape: str = "",
|
| 736 |
+
depart: str = "",
|
| 737 |
+
arrivee: str = "",
|
| 738 |
+
changement: bool = False,
|
| 739 |
+
) -> str:
|
| 740 |
"""Une ligne : compagnie, numéro, trajet conseillé, arrêts parcourus.
|
| 741 |
|
| 742 |
On ne prétend pas connaître l'arrêt exact de l'utilisateur : on conseille
|
| 743 |
l'arrêt le plus proche du lieu qu'il a dit, et on rappelle que tout arrêt de
|
| 744 |
+
la ligne parcouru convient. `changement=True` (1er bus d'une correspondance)
|
| 745 |
+
ajoute « pour changer de bus » à la descente. Les couleurs sont portées en
|
| 746 |
+
propre par chaque bloc (fond clair fixe + texte sombre) : la carte reste
|
| 747 |
+
lisible en mode clair comme en mode sombre, quel que soit le thème Gradio.
|
| 748 |
"""
|
| 749 |
numero = numero_de(match["ligne"])
|
| 750 |
affiche = re.sub(r"^Ligne\s*", "", str(match["ligne"]))
|
|
|
|
| 757 |
descend_a, _ = _nom_arret(match["descend_a"])
|
| 758 |
depart_texte = depart if depart else match["monte_a"]
|
| 759 |
arrivee_texte = arrivee if arrivee else match["descend_a"]
|
| 760 |
+
fin = " pour changer de bus" if changement else ""
|
| 761 |
trajet = (
|
| 762 |
f"<div style='color:#11301C;font-size:22px;font-weight:800;"
|
| 763 |
f"line-height:1.35;margin-top:8px;'>"
|
|
|
|
| 766 |
f"<span style='font-weight:600;font-size:15px;color:#3D5A48;'>"
|
| 767 |
f"(ou tout arrêt de la ligne {affiche} près de toi)</span><br>"
|
| 768 |
f"Descends à <b style='color:#081F11;'>{arrivee_texte}</b> — arrêt conseillé : "
|
| 769 |
+
f"<b style='color:#081F11;'>{descend_a}</b>{fin}</div>"
|
| 770 |
)
|
| 771 |
arrets = _bloc_arrets(match)
|
| 772 |
# Les terminus de la ligne, en petit : le parcours réel du bus.
|
|
|
|
| 823 |
for numero, trajet in enumerate(correspondances, start=1):
|
| 824 |
if numero > 1:
|
| 825 |
blocs.append(f"\n**Autre possibilité {numero}**\n")
|
| 826 |
+
blocs.append(_une_carte(trajet["etape_1"], "1er bus", depart=depart, changement=True))
|
| 827 |
blocs.append(
|
| 828 |
f"<div style='text-align:center;color:#6B7A72;margin:2px 0;'>"
|
| 829 |
f"🔁 Change à <b>{trajet['changement']}</b></div>"
|
src/matching/geo.py
CHANGED
|
@@ -61,6 +61,17 @@ COMPAGNIE_VOISINS = "Tata AFTU"
|
|
| 61 |
# porte en plus une localité (« Rue 10 Pikine Ouest ») reste situable.
|
| 62 |
_RUE_NUMEROTEE = re.compile(r"^rue\s+\d+\s*(?:bis|ter)?$", re.IGNORECASE)
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
def nom_rue_numerotee(arret: str) -> bool:
|
| 66 |
"""Vrai si le nom d'arrêt est une rue numérotée seule (« Rue 9 », « Rue 9bis »)."""
|
|
@@ -68,6 +79,13 @@ def nom_rue_numerotee(arret: str) -> bool:
|
|
| 68 |
return bool(_RUE_NUMEROTEE.match(normaliser(sans_annotation)))
|
| 69 |
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
def _hors_voisinage(
|
| 72 |
positions: dict[str, tuple[float, float]],
|
| 73 |
bonnes: set[str],
|
|
@@ -116,8 +134,13 @@ def _hors_voisinage(
|
|
| 116 |
|
| 117 |
|
| 118 |
def _non_fiables(arrets: dict[str, dict], itineraires_csv: Path) -> set[str]:
|
| 119 |
-
"""Arrêts à position non fiable : rues numérotées
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
positions = {
|
| 122 |
arret: info["point"]
|
| 123 |
for arret, info in arrets.items()
|
|
@@ -128,7 +151,7 @@ def _non_fiables(arrets: dict[str, dict], itineraires_csv: Path) -> set[str]:
|
|
| 128 |
for arret, info in arrets.items()
|
| 129 |
if info.get("confiance") == "bonne"
|
| 130 |
}
|
| 131 |
-
return
|
| 132 |
|
| 133 |
|
| 134 |
def arrets_non_fiables(
|
|
@@ -169,6 +192,13 @@ GENERIQUES = {
|
|
| 169 |
# que l'arrêt est dans la ville de Guédiawaye (il peut être à Pikine).
|
| 170 |
NIVEAUX_LARGES = ("arrondissement", "departement")
|
| 171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
def normaliser(texte: str) -> str:
|
| 174 |
"""Minuscules, sans accents ni ponctuation : « Dakar-Plateau » -> « dakar plateau »."""
|
|
@@ -184,6 +214,22 @@ def _niveau_precis(valeur: str) -> bool:
|
|
| 184 |
return bool(valeur) and not normaliser(valeur).startswith(NIVEAUX_LARGES)
|
| 185 |
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
def distance_metres(a: tuple[float, float], b: tuple[float, float]) -> float:
|
| 188 |
"""Distance à vol d'oiseau entre deux points (latitude, longitude)."""
|
| 189 |
rayon = 6_371_000.0
|
|
@@ -292,10 +338,8 @@ class LieuxGeo:
|
|
| 292 |
quartier = rangee.get("quartier", "")
|
| 293 |
commune = rangee.get("commune", "")
|
| 294 |
cible: set[str] = set()
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
if _niveau_precis(commune):
|
| 298 |
-
cible |= set(_mots(commune))
|
| 299 |
self._arrets[arret] = {
|
| 300 |
"point": point,
|
| 301 |
"quartier": quartier,
|
|
|
|
| 61 |
# porte en plus une localité (« Rue 10 Pikine Ouest ») reste situable.
|
| 62 |
_RUE_NUMEROTEE = re.compile(r"^rue\s+\d+\s*(?:bis|ter)?$", re.IGNORECASE)
|
| 63 |
|
| 64 |
+
# Noms d'équipement sans localité (« Préfecture », « Police », « Marché »…) : le
|
| 65 |
+
# nom seul ne dit pas de quel quartier il s'agit, et le géocodage le pose souvent
|
| 66 |
+
# au mauvais endroit. Cas réel : le « Préfecture » de la ligne 46 est en banlieue
|
| 67 |
+
# mais géocodé au Plateau, ce qui fait monter Sandaga → Pikine au 46 à Préfecture.
|
| 68 |
+
# Un nom qui porte en plus une localité (« Préfecture Guédiawaye », « Marché
|
| 69 |
+
# Jeudi », « Hôpital Roi Baudouin ») reste fiable.
|
| 70 |
+
NOMS_GENERIQUES = {
|
| 71 |
+
"prefecture", "police", "poste", "mairie", "marche", "gare", "hopital",
|
| 72 |
+
"stade", "lycee", "pharmacie", "station",
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
|
| 76 |
def nom_rue_numerotee(arret: str) -> bool:
|
| 77 |
"""Vrai si le nom d'arrêt est une rue numérotée seule (« Rue 9 », « Rue 9bis »)."""
|
|
|
|
| 79 |
return bool(_RUE_NUMEROTEE.match(normaliser(sans_annotation)))
|
| 80 |
|
| 81 |
|
| 82 |
+
def nom_generique(arret: str) -> bool:
|
| 83 |
+
"""Vrai si le nom d'arrêt est un équipement seul, sans localité qui le situe."""
|
| 84 |
+
sans_annotation = re.sub(r"\([^)]*\)", " ", str(arret))
|
| 85 |
+
mots = normaliser(sans_annotation).split()
|
| 86 |
+
return bool(mots) and all(mot in NOMS_GENERIQUES for mot in mots)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
def _hors_voisinage(
|
| 90 |
positions: dict[str, tuple[float, float]],
|
| 91 |
bonnes: set[str],
|
|
|
|
| 134 |
|
| 135 |
|
| 136 |
def _non_fiables(arrets: dict[str, dict], itineraires_csv: Path) -> set[str]:
|
| 137 |
+
"""Arrêts à position non fiable : rues numérotées, équipements sans localité,
|
| 138 |
+
et isolés sur une ligne AFTU."""
|
| 139 |
+
douteuses = {
|
| 140 |
+
arret
|
| 141 |
+
for arret in arrets
|
| 142 |
+
if nom_rue_numerotee(arret) or nom_generique(arret)
|
| 143 |
+
}
|
| 144 |
positions = {
|
| 145 |
arret: info["point"]
|
| 146 |
for arret, info in arrets.items()
|
|
|
|
| 151 |
for arret, info in arrets.items()
|
| 152 |
if info.get("confiance") == "bonne"
|
| 153 |
}
|
| 154 |
+
return douteuses | _hors_voisinage(positions, bonnes, itineraires_csv)
|
| 155 |
|
| 156 |
|
| 157 |
def arrets_non_fiables(
|
|
|
|
| 192 |
# que l'arrêt est dans la ville de Guédiawaye (il peut être à Pikine).
|
| 193 |
NIVEAUX_LARGES = ("arrondissement", "departement")
|
| 194 |
|
| 195 |
+
# Villes dont le département porte le nom, et que les voyageurs demandent tels
|
| 196 |
+
# quels : un arrêt « Département de Guédiawaye » compte alors pour « Guédiawaye ».
|
| 197 |
+
# C'est ce qui rend directe la ligne 78 (Golf, Lycée Canada…). « Dakar » n'y est
|
| 198 |
+
# jamais : le département de Dakar ne dit pas la ville, et « Dakar » ramènerait
|
| 199 |
+
# toute la banlieue.
|
| 200 |
+
DEPARTEMENTS_COMME_VILLE = {"guediawaye", "pikine", "rufisque"}
|
| 201 |
+
|
| 202 |
|
| 203 |
def normaliser(texte: str) -> str:
|
| 204 |
"""Minuscules, sans accents ni ponctuation : « Dakar-Plateau » -> « dakar plateau »."""
|
|
|
|
| 214 |
return bool(valeur) and not normaliser(valeur).startswith(NIVEAUX_LARGES)
|
| 215 |
|
| 216 |
|
| 217 |
+
def _mots_niveau(valeur: str) -> set[str]:
|
| 218 |
+
"""Mots d'un quartier/commune à retenir pour comparer à un lieu dit.
|
| 219 |
+
|
| 220 |
+
Un « Département de X » ne situe pas l'arrêt dans la ville X — sauf quand X
|
| 221 |
+
est justement la ville demandée (Guédiawaye, Pikine, Rufisque, jamais Dakar).
|
| 222 |
+
"""
|
| 223 |
+
if _niveau_precis(valeur):
|
| 224 |
+
return set(_mots(valeur))
|
| 225 |
+
bas = normaliser(valeur)
|
| 226 |
+
if bas.startswith("departement"):
|
| 227 |
+
ville = [mot for mot in bas.split() if mot not in GENERIQUES]
|
| 228 |
+
if len(ville) == 1 and ville[0] in DEPARTEMENTS_COMME_VILLE:
|
| 229 |
+
return set(ville)
|
| 230 |
+
return set()
|
| 231 |
+
|
| 232 |
+
|
| 233 |
def distance_metres(a: tuple[float, float], b: tuple[float, float]) -> float:
|
| 234 |
"""Distance à vol d'oiseau entre deux points (latitude, longitude)."""
|
| 235 |
rayon = 6_371_000.0
|
|
|
|
| 338 |
quartier = rangee.get("quartier", "")
|
| 339 |
commune = rangee.get("commune", "")
|
| 340 |
cible: set[str] = set()
|
| 341 |
+
cible |= _mots_niveau(quartier)
|
| 342 |
+
cible |= _mots_niveau(commune)
|
|
|
|
|
|
|
| 343 |
self._arrets[arret] = {
|
| 344 |
"point": point,
|
| 345 |
"quartier": quartier,
|