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

Create explainable user model classes #223

Merged
merged 4 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ data/users.db

# local experimentation
local/
.vscode/
4 changes: 2 additions & 2 deletions moviebot/explainability/explainable_user_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class ExplainableUserModel(ABC):
def generate_explanation(
self, user_preferences: UserPreferences
) -> AnnotatedUtterance:
"""Generate an explanation based on the provided input data.
"""Generates an explanation based on the provided input data.

Args:
input_data: The input data for which an explanation is to be
generated.

Returns:
The generated explanation.
A system utterance containing an explanation.

Raises:
NotImplementedError: This method must be implemented by a subclass.
Expand Down
30 changes: 21 additions & 9 deletions moviebot/explainability/explainable_user_model_tag_based.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
"""This module contains the ExplainableUserModelTagBased class.
"""Class for creating a tag-based user model explanations.

The class generates explanations for user preferences in the movie domain based
on templates loaded from a YAML file.
The class generates explanations for user preferences in the movie domain.
Currently, the explanations are based on the explicit movie tags/attributes.
Future versions of the class will also support implicit tags/attributes, which
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: "... are based on movie tags/attributes that were explicitly mentioned by the user in the conversation."

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

are inferred from the movie recommendation feedback. Explanations are based on
the templates loaded from a YAML file.
"""

import os
import random
import re

Expand All @@ -16,27 +20,35 @@
UserPreferences,
)

_DEFAULT_TEMPLATE_FILE = "moviebot/explainability/explanation_templates.yaml"
_DEFAULT_TEMPLATE_FILE = "data/explainability/explanation_templates.yaml"


class ExplainableUserModelTagBased(ExplainableUserModel):
def __init__(self, template_file: str = _DEFAULT_TEMPLATE_FILE):
"""Initialize the ExplainableUserModelTagBased class.
"""Initializes the ExplainableUserModelTagBased class.

Args:
template_file: Path to the YAML file containing explanation
templates. Defaults to _DEFAULT_TEMPLATE_FILE.
templates. Defaults to _DEFAULT_TEMPLATE_FILE.

Raises:
FileNotFoundError: The template file could not be found.
"""
if not os.isfile(template_file):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: check if file exists raise exception otherwise.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

raise FileNotFoundError(
f"Could not find template file {template_file}."
)

with open(template_file, "r") as f:
self.templates = yaml.safe_load(f)

def generate_explanation(
self, user_preferences: UserPreferences
) -> AnnotatedUtterance:
"""Generate an explanation based on the provided user preferences.
"""Generates an explanation based on the provided user preferences.

Args:
user_preferences: Nested dictionary of user preferences.
user_preferences: User preferences.

Returns:
The generated explanation.
Expand Down Expand Up @@ -68,7 +80,7 @@ def _clean_negative_keyword(

Args:
template: Template containing negative keyword.
remove: If True, remove the negative keyword.
remove: If True, remove the negative keyword. Defaults to True.

Returns:
Template with negative keyword removed or replaced.
Expand Down
6 changes: 2 additions & 4 deletions tests/explainability/test_explainable_user_model_tag_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@


@pytest.fixture
def explainable_model():
return ExplainableUserModelTagBased(
"moviebot/explainability/explanation_templates.yaml"
)
def explainable_model() -> ExplainableUserModelTagBased:
return ExplainableUserModelTagBased()


def test_generate_explanation_positive(explainable_model):
Expand Down
Loading