Created
February 4, 2024 05:11
-
-
Save ehzawad/f1d7b4086a4a8b36bd44894234c8079b to your computer and use it in GitHub Desktop.
actions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActionAnalyzePreviousMessages(Action): | |
def name(self) -> Text: | |
return "action_analyze_previous_messages" | |
def run(self, dispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[EventType]: | |
# Your predefined list or dictionary of keywords | |
keywords = ["খতিয়ান", "পর্চা", "পর্চার", "খাজনা"] | |
# Get the list of events | |
events = tracker.events | |
# Function to find message events | |
def find_message_events(events): | |
return [event for event in events if event["event"] == "user"] | |
# Extracting the last two user messages | |
message_events = find_message_events(events) | |
previous_message = message_events[-2]["text"] if len(message_events) > 1 else "" | |
previous_of_previous_message = message_events[-3]["text"] if len(message_events) > 2 else "" | |
# Check for keywords in messages | |
keyword_in_previous = any(keyword in previous_message for keyword in keywords) | |
keyword_in_previous_of_previous = any(keyword in previous_of_previous_message for keyword in keywords) | |
# Logic based on keyword findings | |
if keyword_in_previous: | |
# Handle scenario for keyword found in previous message | |
# pass | |
print("previous") | |
elif keyword_in_previous_of_previous: | |
# Handle scenario for keyword found in previous of previous message | |
# pass | |
print("previous of previous") | |
else: | |
# Handle scenario if no relevant keyword is found | |
# pass | |
print("no relevant word found") | |
# Return an empty list if no events are to be triggered | |
return [] | |
You, Thu 6:01 PM | |
```- rule: Ask the user to rephrase whenever they send a message with low NLU confidence | |
steps: | |
- intent: nlu_fallback | |
- action: action_analyze_previous_messages | |
- rule: Ask the user to rephrase whenever they send a message with low NLU confidence out of scope | |
steps: | |
- intent: out_of_scope | |
- action: action_analyze_previous_messages``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActionAnalyzePreviousMessages(Action): | |
def name(self) -> Text: | |
return "action_analyze_previous_messages" | |
def run(self, dispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[EventType]: | |
# Defining keywords and their corresponding Rasa intents | |
keyword_intent_mapping = { | |
"নামজারি": "ApplyNamzari", | |
"Namzari fee": "NamzariFee", | |
# Add other keywords and their corresponding intents here | |
} | |
# Get the list of events | |
events = tracker.events | |
# Function to find message events | |
def find_message_events(events): | |
return [event for event in events if event["event"] == "user"] | |
# Extracting the last two user messages | |
message_events = find_message_events(events) | |
previous_message = message_events[-2]["text"] if len(message_events) > 1 else "" | |
previous_of_previous_message = message_events[-3]["text"] if len(message_events) > 2 else "" | |
# Check for keywords in messages and map to corresponding intent | |
for keyword, intent in keyword_intent_mapping.items(): | |
if keyword in previous_message or keyword in previous_of_previous_message: | |
# Dispatch a custom action or message based on the identified intent | |
dispatcher.utter_message(text=f"Found keyword related to {intent}. Redirecting to the appropriate action.") | |
# Here you would typically return a FollowupAction with the name of the action corresponding to the intent | |
# For illustration, we'll just return an empty list | |
return [FollowupAction(name=f"action_for_{intent}")] | |
# Handle scenario if no relevant keyword is found | |
dispatcher.utter_message(text="No relevant keyword found. Please rephrase or ask another question.") | |
return [] |
Author
ehzawad
commented
Feb 4, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment