Skip to content

explainable

EXPLAINABLE

Source code in engines/contentFilterEngine/fairness_explainability/explainable.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class EXPLAINABLE:
    def __init__(self):
        """
        Initialize the explainable module.

        Attributes:
            explanations (dict): A dictionary to store explanations with keys as 
            (user_id, item_id) tuples and values as explanation strings.
        """
        self.explanations = {}

    def generate_explanation(self, user_id: int, item_id: int, context: Optional[Dict[str, Any]] = None) -> str:
        """
        Generate an explanation for why a particular item was recommended to a user.

        Parameters:
            user_id (int): The ID of the user for whom the recommendation was made.
            item_id (int): The ID of the recommended item.
            context (dict, optional): Additional context in which the recommendation was made, 
            such as user preferences or item features.

        Returns:
            str: A textual explanation of the recommendation, detailing the factors that 
            influenced the recommendation decision.
        """
        explanation = f"Item {item_id} was recommended to User {user_id} because "
        if context:
            explanation += f"of the context {context} and "
        explanation += "based on similar items and user preferences."
        self.explanations[(user_id, item_id)] = explanation
        return explanation

    def get_explanation(self, user_id: int, item_id: int) -> str:
        """
        Retrieve a previously generated explanation for a recommendation.

        Parameters:
            user_id (int): The ID of the user for whom the recommendation was made.
            item_id (int): The ID of the recommended item.

        Returns:
            str: The explanation of the recommendation if available, otherwise a default 
            message indicating that no explanation is available.
        """
        return self.explanations.get((user_id, item_id), "No explanation available.")

__init__()

Initialize the explainable module.

Attributes:

Name Type Description
explanations dict

A dictionary to store explanations with keys as

Source code in engines/contentFilterEngine/fairness_explainability/explainable.py
 5
 6
 7
 8
 9
10
11
12
13
def __init__(self):
    """
    Initialize the explainable module.

    Attributes:
        explanations (dict): A dictionary to store explanations with keys as 
        (user_id, item_id) tuples and values as explanation strings.
    """
    self.explanations = {}

generate_explanation(user_id, item_id, context=None)

Generate an explanation for why a particular item was recommended to a user.

Parameters:

Name Type Description Default
user_id int

The ID of the user for whom the recommendation was made.

required
item_id int

The ID of the recommended item.

required
context dict

Additional context in which the recommendation was made,

None

Returns:

Name Type Description
str str

A textual explanation of the recommendation, detailing the factors that

str

influenced the recommendation decision.

Source code in engines/contentFilterEngine/fairness_explainability/explainable.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def generate_explanation(self, user_id: int, item_id: int, context: Optional[Dict[str, Any]] = None) -> str:
    """
    Generate an explanation for why a particular item was recommended to a user.

    Parameters:
        user_id (int): The ID of the user for whom the recommendation was made.
        item_id (int): The ID of the recommended item.
        context (dict, optional): Additional context in which the recommendation was made, 
        such as user preferences or item features.

    Returns:
        str: A textual explanation of the recommendation, detailing the factors that 
        influenced the recommendation decision.
    """
    explanation = f"Item {item_id} was recommended to User {user_id} because "
    if context:
        explanation += f"of the context {context} and "
    explanation += "based on similar items and user preferences."
    self.explanations[(user_id, item_id)] = explanation
    return explanation

get_explanation(user_id, item_id)

Retrieve a previously generated explanation for a recommendation.

Parameters:

Name Type Description Default
user_id int

The ID of the user for whom the recommendation was made.

required
item_id int

The ID of the recommended item.

required

Returns:

Name Type Description
str str

The explanation of the recommendation if available, otherwise a default

str

message indicating that no explanation is available.

Source code in engines/contentFilterEngine/fairness_explainability/explainable.py
36
37
38
39
40
41
42
43
44
45
46
47
48
def get_explanation(self, user_id: int, item_id: int) -> str:
    """
    Retrieve a previously generated explanation for a recommendation.

    Parameters:
        user_id (int): The ID of the user for whom the recommendation was made.
        item_id (int): The ID of the recommended item.

    Returns:
        str: The explanation of the recommendation if available, otherwise a default 
        message indicating that no explanation is available.
    """
    return self.explanations.get((user_id, item_id), "No explanation available.")