Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testim #69

Merged
merged 10 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 83 additions & 4 deletions Rasa_Bot/actions/actions_goal_setting_dialog.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
"""
Contains custom actions related to the relapse dialogs
"""
from virtual_coach_db.helper import ExecutionInterventionComponents, PreparationInterventionComponents
from virtual_coach_db.dbschema.models import (Testimonials, Users)
from virtual_coach_db.helper import (ExecutionInterventionComponents,
PreparationInterventionComponents)
from virtual_coach_db.helper.helper_functions import get_db_session

from . import validator
from .definitions import TIMEZONE
from .definitions import DATABASE_URL, TIMEZONE
from .helper import (get_latest_bot_utterance)
from datetime import datetime, timedelta
from rasa_sdk import Action, Tracker
from rasa_sdk.events import SlotSet, FollowupAction
from rasa_sdk.events import FollowupAction, SlotSet
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.forms import FormValidationAction
from typing import Any, Dict, Text
Expand All @@ -26,6 +29,78 @@ async def run(self, dispatcher, tracker, domain):

return [SlotSet('first_possible_quit_date', first_date),
SlotSet('last_possible_quit_date', last_date)]


def goal_setting_testimonial_model_output(t, user_se, user_godin, user_c1,
user_c3):
"""
Get the output of the linear regression model used to predict motivation
ratings of testimonials that differs per testimonial.
We do not consider the terms that do not differ between testimonials as
they do not impact which testimonial is chosen.
The model is a simplified version of the one developed in the publicly
available MSc. thesis by Beyza Hizli
(http://resolver.tudelft.nl/uuid:b7225a91-6ae8-4a32-8441-38fb7ff74b4c).
The simplification was done to reduce the number of variables we need
to collect data on.
"""

t_godin = t.godin_activity_level
t_se = t.self_efficacy_pref
t_poc1 = int(t.part_of_cluster1)
t_poc3 = int(t.part_of_cluster3)
# Need to divide by 100 and 2 for scaling to interval [0, 1]
model_sim = -1.00491 * abs(user_se - t_se)/100 -0.93247 * abs(user_godin - t_godin)/2
model_cluster_member = -0.72352 * t_poc1 - 1.16833 * t_poc3
model_cluster_inter = 0.26407 * user_c1 * t_poc1 + 0.30176 * user_c3 * t_poc3

return model_cluster_member + model_cluster_inter + model_sim


class ActionGoalSettingChooseTestimonials(Action):
def name(self):
return "action_goal_setting_choose_testimonials"

async def run(self, dispatcher, tracker, domain):

# Get user ID
user_id = tracker.current_state()['sender_id']

# Create session object to connect db
session = get_db_session(db_url=DATABASE_URL)

selected = session.query(Users).filter_by(nicedayuid=user_id).one()

# Get self-efficacy, cluster ratings, and godin activity level of user
user_se = selected.testim_self_efficacy_pref
user_c1 = selected.testim_sim_cluster_1
user_c3 = selected.testim_sim_cluster_3
user_godin = selected.testim_godin_activity_level

# Get testimonials
selected = session.query(Testimonials).all()
# Compute motivation score (i.e., model output) for each testimonial
motiv_all = []
for t in selected:

model_output_t = goal_setting_testimonial_model_output(t, user_se,
user_godin,
user_c1,
user_c3)

motiv_all.append(model_output_t)


# Sort testimonials based on motivation rating since we want the
# 2 most motivating testimonials
motiv_all_sorted = sorted(range(len(motiv_all)),
key=lambda k: motiv_all[k],
reverse = True)

return [SlotSet('goal_setting_testimonial_1',
selected[motiv_all_sorted[0]].testimonial_text),
SlotSet('goal_setting_testimonial_2',
selected[motiv_all_sorted[1]].testimonial_text)]


class ActionGoalSettingContinueAfterPlan(Action):
Expand All @@ -42,6 +117,8 @@ async def run(self, dispatcher, tracker, domain):
return [FollowupAction('relapse_medication_info_form')]
elif current_dialog == PreparationInterventionComponents.GOAL_SETTING:
return [FollowupAction('utter_goal_setting_pa_expl_1')]
else:
return None # TODO (prospector complained)


class ValidateChosenQuitDateForm(FormValidationAction):
Expand All @@ -61,7 +138,9 @@ def validate_chosen_quit_date_slot(
start_date = tracker.get_slot('first_possible_quit_date')
stop_date = tracker.get_slot('last_possible_quit_date')

if not (validator.validate_date_format(value) and validator.validate_date_range(value, start_date, stop_date)):
if not (validator.validate_date_format(value) and validator.validate_date_range(value,
start_date,
stop_date)):
dispatcher.utter_message(response="utter_goal_setting_wrong_date")
return {"chosen_quit_date_slot": None}

Expand Down
3 changes: 2 additions & 1 deletion Rasa_Bot/data/rules/rules_goal_setting_dialog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,15 @@ rules:
- action: utter_goal_setting_pa_set_4
- action: which_sport_form
- active_loop: which_sport_form
- active_loop: null # TODO: store slot to database
- active_loop: null # TODO: store slot to the database
- action: utter_goal_setting_pa_set_5
- action: utter_goal_setting_pa_set_6
- action: first_pa_goal_form
- active_loop: first_pa_goal_form
- active_loop: null
- action: utter_goal_setting_pa_set_7
- action: utter_goal_setting_pa_set_8
- action: action_goal_setting_choose_testimonials
- action: utter_goal_setting_pa_set_9
- action: testimonial_one_read_form
- active_loop: testimonial_one_read_form
Expand Down
17 changes: 15 additions & 2 deletions Rasa_Bot/domain/domain_goal_setting_dialogs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ slots:
- type: from_text
conditions:
- active_loop: finished_writing_pa_form

goal_setting_testimonial_1:
type: text
influence_conversation: false
mappings:
- type: custom

goal_setting_testimonial_2:
type: text
influence_conversation: false
mappings:
- type: custom

responses:
# Branch 1 introduction
Expand Down Expand Up @@ -320,7 +332,7 @@ responses:
utter_goal_setting_pa_set_8:
- text: "Het voorbeeld is van iemand die zijn of haar beweegdoel heeft gehaald 🙋:"
utter_goal_setting_pa_set_9:
- text: "[Insert testimonial 1 Nele/Beyza]" # TODO: insert testimonial
- text: '🏁 "{goal_setting_testimonial_1}"'
utter_ask_testimonial_one_read:
- text: "Typ 'klaar' als je het voorbeeld gelezen hebt."
utter_ask_testimonial_two_read:
Expand All @@ -330,7 +342,7 @@ responses:
utter_second_testimonial_1:
- text: "Hier is nog een voorbeeld van iemand die zijn of haar beweegdoel heeft gehaald 🙋🏾:"
utter_second_testimonial_2:
- text: "[Insert testimonial 2 Nele/Beyza]" # TODO: insert testimonial
- text: '🏁 "{goal_setting_testimonial_2}"'
utter_ask_testimonial_evaluation:
- text: "Wat vind je van het voorbeeld / de voorbeelden? Zijn er dingen die jij hetzelfde zou willen doen? Typ je
antwoord naar mij. "
Expand Down Expand Up @@ -456,6 +468,7 @@ responses:
actions:
- action_get_first_last_date
- action_set_slot_goal_setting_dialog
- action_goal_setting_choose_testimonials
- action_goal_setting_continue_after_plan
- action_continue_testimonial_evaluation
- action_continue_change_pa_goal
Expand Down
Binary file not shown.