Skip to content

Commit 7a44903

Browse files
authored
feat:multilang_match (#646)
* feat:multilang_match if enabled attempt to match intents across all configured langs this allows OVOS to match even if lang was incorrectly tagged, as long as the transcription is correct use cases aretext chatbots (eg, matrix hivemind both) or misclassifications of audio transformer plugins * ensure lang in session * handle old ovos-plugin-manager versions
1 parent 6bc1cca commit 7a44903

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

ovos_core/intent_services/__init__.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def _handle_deactivate(self, message):
267267
skill_id = message.data.get("skill_id")
268268
self._deactivations[sess.session_id].append(skill_id)
269269

270-
def _emit_match_message(self, match: Union[IntentHandlerMatch, PipelineMatch], message: Message):
270+
def _emit_match_message(self, match: Union[IntentHandlerMatch, PipelineMatch], message: Message, lang: str):
271271
"""
272272
Emit a reply message for a matched intent, updating session and skill activation.
273273
@@ -278,6 +278,7 @@ def _emit_match_message(self, match: Union[IntentHandlerMatch, PipelineMatch], m
278278
match (Union[IntentHandlerMatch, PipelineMatch]): The matched intent object containing
279279
utterance and matching information.
280280
message (Message): The original messagebus message that triggered the intent match.
281+
lang (str): The language of the pipeline plugin match
281282
282283
Details:
283284
- Handles two types of matches: PipelineMatch and IntentHandlerMatch
@@ -295,7 +296,12 @@ def _emit_match_message(self, match: Union[IntentHandlerMatch, PipelineMatch], m
295296
None
296297
"""
297298
reply = None
298-
sess = match.updated_session or SessionManager.get(message)
299+
try:
300+
sess = match.updated_session or SessionManager.get(message)
301+
except AttributeError: # old ovos-plugin-manager version
302+
LOG.warning("outdated ovos-plugin-manager detected! please update to version 0.8.0")
303+
sess = SessionManager.get(message)
304+
sess.lang = lang # ensure it is updated
299305

300306
# utterance fully handled by pipeline matcher
301307
if isinstance(match, PipelineMatch):
@@ -310,6 +316,7 @@ def _emit_match_message(self, match: Union[IntentHandlerMatch, PipelineMatch], m
310316

311317
if reply is not None:
312318
reply.data["utterance"] = match.utterance
319+
reply.data["lang"] = lang
313320

314321
# update active skill list
315322
if match.skill_id:
@@ -408,26 +415,33 @@ def handle_utterance(self, message: Message):
408415
match = None
409416
with stopwatch:
410417
self._deactivations[sess.session_id] = []
411-
412418
# Loop through the matching functions until a match is found.
413419
for pipeline, match_func in self.get_pipeline(session=sess):
414-
match = match_func(utterances, lang, message)
415-
if match:
416-
LOG.info(f"{pipeline} match: {match}")
417-
if match.skill_id and match.skill_id in sess.blacklisted_skills:
418-
LOG.debug(
419-
f"ignoring match, skill_id '{match.skill_id}' blacklisted by Session '{sess.session_id}'")
420-
continue
421-
if isinstance(match, IntentHandlerMatch) and match.match_type in sess.blacklisted_intents:
422-
LOG.debug(
423-
f"ignoring match, intent '{match.match_type}' blacklisted by Session '{sess.session_id}'")
424-
continue
425-
try:
426-
self._emit_match_message(match, message)
427-
break
428-
except:
429-
LOG.exception(f"{match_func} returned an invalid match")
430-
LOG.debug(f"no match from {match_func}")
420+
langs = [lang]
421+
if self.config.get("multilingual_matching"):
422+
# if multilingual matching is enabled, attempt to match all user languages if main fails
423+
langs += [l for l in get_valid_languages() if l != lang]
424+
for intent_lang in langs:
425+
match = match_func(utterances, intent_lang, message)
426+
if match:
427+
LOG.info(f"{pipeline} match ({intent_lang}): {match}")
428+
if match.skill_id and match.skill_id in sess.blacklisted_skills:
429+
LOG.debug(
430+
f"ignoring match, skill_id '{match.skill_id}' blacklisted by Session '{sess.session_id}'")
431+
continue
432+
if isinstance(match, IntentHandlerMatch) and match.match_type in sess.blacklisted_intents:
433+
LOG.debug(
434+
f"ignoring match, intent '{match.match_type}' blacklisted by Session '{sess.session_id}'")
435+
continue
436+
try:
437+
self._emit_match_message(match, message, intent_lang)
438+
break
439+
except:
440+
LOG.exception(f"{match_func} returned an invalid match")
441+
else:
442+
LOG.debug(f"no match from {match_func}")
443+
continue
444+
break
431445
else:
432446
# Nothing was able to handle the intent
433447
# Ask politely for forgiveness for failing in this vital task

0 commit comments

Comments
 (0)