PythonMindset commited on
Commit
4e3642c
·
verified ·
1 Parent(s): a53d477

Deploiement GuindiMa AI (interface Gradio) (part 2)

Browse files
Files changed (1) hide show
  1. src/intent/conversation.py +68 -3
src/intent/conversation.py CHANGED
@@ -17,9 +17,9 @@ from dataclasses import dataclass
17
  from difflib import SequenceMatcher
18
 
19
  try: # import à plat (app.py) ou en paquet
20
- from intent.extract_intent import _normaliser_phrase
21
  except ImportError: # pragma: no cover
22
- from .extract_intent import _normaliser_phrase
23
 
24
  # Une forme proche d'une variante connue (faute de l'ASR) compte à partir de ce
25
  # score, sur des variantes d'au moins LONGUEUR_FLOUE lettres. Plus court, la
@@ -193,6 +193,71 @@ def _reconnue(phrase: str, variante: str) -> bool:
193
  return False
194
 
195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  def detecter(phrase: str) -> Intention | None:
197
  """L'intention de petite conversation de la phrase, ou None.
198
 
@@ -207,4 +272,4 @@ def detecter(phrase: str) -> Intention | None:
207
  return None
208
 
209
 
210
- __all__ = ["HORS_SUJET", "INTENTIONS", "Intention", "TOUTES", "detecter"]
 
17
  from difflib import SequenceMatcher
18
 
19
  try: # import à plat (app.py) ou en paquet
20
+ from intent.extract_intent import MOTS_DE_LA_PHRASE, _normaliser_phrase, _sans_compagnie
21
  except ImportError: # pragma: no cover
22
+ from .extract_intent import MOTS_DE_LA_PHRASE, _normaliser_phrase, _sans_compagnie
23
 
24
  # Une forme proche d'une variante connue (faute de l'ASR) compte à partir de ce
25
  # score, sur des variantes d'au moins LONGUEUR_FLOUE lettres. Plus court, la
 
193
  return False
194
 
195
 
196
+ # Mots qui suivent « maa ngi » ou « dem » dans une phrase de politesse, jamais
197
+ # un lieu : « maa ngi fi rekk » (ça va), « maa ngi ci jàmm », « maa ngi tudd
198
+ # Moussa », « maa ngi dem, ba beneen yoon » (au revoir). Les mots des formules
199
+ # de conversation ci-dessus s'y ajoutent (« maa ngi dem, jërëjëf »).
200
+ _NON_LIEUX = {
201
+ "fi", "fii", "fee", "fale", "rekk", "jamm", "tudd", "lay", "def", "dem", "yoon",
202
+ "beneen", "benen", "suba", "sant", "nuyu", "yaw", "nag", "alxamdulilaay",
203
+ "alhamdoulilah", "alhamdulillah", "waaw", "deedeet", "xam", "deglu",
204
+ }
205
+ _NON_LIEUX |= {
206
+ mot
207
+ for intention in INTENTIONS
208
+ for texte in intention.variantes
209
+ for mot in _normaliser_phrase(texte).split()
210
+ }
211
+ # « dem ci Petersen » : « ci » se saute pour trouver le lieu.
212
+ _SAUTES = {"ci"}
213
+ _MARQUEURS_ICI = re.compile(
214
+ r"(?:^| )(?:maa ngi|ma ngi|mangi|maangi|mangui|man gui|fi ma nekk|fii ma nekk|dama nekk)"
215
+ r"(?: nekk)?(?: mooy| moy| mooye)?(?= |$)"
216
+ )
217
+
218
+
219
+ def _peut_etre_un_lieu(mot: str) -> bool:
220
+ return (
221
+ len(mot) >= 3
222
+ and mot not in MOTS_DE_LA_PHRASE
223
+ and mot not in _NON_LIEUX
224
+ )
225
+
226
+
227
+ def _premier_mot_utile(mots: list[str], debut: int) -> str:
228
+ for mot in mots[debut:]:
229
+ if mot not in _SAUTES:
230
+ return mot
231
+ return ""
232
+
233
+
234
+ def signal_trajet(phrase: str) -> bool:
235
+ """La phrase demande-t-elle un trajet, même si aucun lieu n'est reconnu ?
236
+
237
+ Vu en production : « Salaam aleekum, man damay dem Petersen te maa ngi
238
+ Ouakam, ban bus laa wara jël ? » a reçu trois fois la salutation. L'ASR
239
+ avait déformé les noms de lieux, l'extraction locale n'en trouvait aucun,
240
+ et la salutation passait avant le LLM. Un mot qui peut être un lieu après
241
+ « dem / jëm », ou après « maa ngi », « fi ma nekk (mooy) », « dama nekk »,
242
+ suffit : le trajet passe alors avant la petite conversation (le LLM a sa
243
+ chance, la seconde chance ASR aussi). « maa ngi fi rekk », « maa ngi dem »
244
+ (au revoir) ou « fan nga bëgga dem ? » n'en sont pas.
245
+ """
246
+ mots = _sans_compagnie(phrase or "").split()
247
+ for i, mot in enumerate(mots):
248
+ if mot == "dem" and _peut_etre_un_lieu(_premier_mot_utile(mots, i + 1)):
249
+ return True
250
+ # « Ouakam laa nekk » : le lieu avant « laa nekk ».
251
+ if mot in {"laa", "la"} and i > 0 and mots[i + 1 : i + 2] == ["nekk"]:
252
+ if _peut_etre_un_lieu(mots[i - 1]):
253
+ return True
254
+ texte = " ".join(mots)
255
+ for trouve in _MARQUEURS_ICI.finditer(texte):
256
+ if _peut_etre_un_lieu(_premier_mot_utile(texte[trouve.end():].split(), 0)):
257
+ return True
258
+ return False
259
+
260
+
261
  def detecter(phrase: str) -> Intention | None:
262
  """L'intention de petite conversation de la phrase, ou None.
263
 
 
272
  return None
273
 
274
 
275
+ __all__ = ["HORS_SUJET", "INTENTIONS", "Intention", "TOUTES", "detecter", "signal_trajet"]