diff options
author | Malik Talha <talhamalik727x@gmail.com> | 2023-10-29 20:52:29 +0500 |
---|---|---|
committer | Malik Talha <talhamalik727x@gmail.com> | 2023-10-29 20:52:29 +0500 |
commit | 42a03d2550f60a8064078f19a743afb944f9ff69 (patch) | |
tree | c9a7b3d028737d5fecd2e05f69e1c744810ed5fb /agl_service_voiceagent/nlu/snips_interface.py | |
parent | a10c988b5480ca5b937a2793b450cfa01f569d76 (diff) |
Update voice agent service
Add new features such as an option to load service
using an external config file, enhanced kuksa client,
and a more robust mapper.
Signed-off-by: Malik Talha <talhamalik727x@gmail.com>
Change-Id: Iba3cfd234c0aabad67b293669d456bb73d8e3135
Diffstat (limited to 'agl_service_voiceagent/nlu/snips_interface.py')
-rw-r--r-- | agl_service_voiceagent/nlu/snips_interface.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/agl_service_voiceagent/nlu/snips_interface.py b/agl_service_voiceagent/nlu/snips_interface.py index f0b05d2..1febe92 100644 --- a/agl_service_voiceagent/nlu/snips_interface.py +++ b/agl_service_voiceagent/nlu/snips_interface.py @@ -19,10 +19,30 @@ from typing import Text from snips_inference_agl import SnipsNLUEngine class SnipsInterface: + """ + SnipsInterface is a class for interacting with the Snips Natural Language Understanding Engine (Snips NLU). + """ + def __init__(self, model_path: Text): + """ + Initialize the SnipsInterface instance with the provided Snips NLU model. + + Args: + model_path (Text): The path to the Snips NLU model. + """ self.engine = SnipsNLUEngine.from_path(model_path) def preprocess_text(self, text): + """ + Preprocess the input text by converting it to lowercase, removing leading/trailing spaces, + and removing special characters and punctuation. + + Args: + text (str): The input text to preprocess. + + Returns: + str: The preprocessed text. + """ # text to lower case and remove trailing and leading spaces preprocessed_text = text.lower().strip() # remove special characters, punctuation, and extra whitespaces @@ -30,11 +50,29 @@ class SnipsInterface: return preprocessed_text def extract_intent(self, text: Text): + """ + Extract the intent from preprocessed text using the Snips NLU engine. + + Args: + text (Text): The preprocessed input text. + + Returns: + dict: The intent extraction result as a dictionary. + """ preprocessed_text = self.preprocess_text(text) result = self.engine.parse(preprocessed_text) return result def process_intent(self, intent_output): + """ + Extract intent and slot values from Snips NLU output. + + Args: + intent_output (dict): The intent extraction result from Snips NLU. + + Returns: + tuple: A tuple containing the intent name (str) and a dictionary of intent actions (entity-value pairs). + """ intent_actions = {} intent = intent_output['intent']['intentName'] slots = intent_output.get('slots', []) |