File size: 2,797 Bytes
b599481
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""Recommender and metadata integration component for CRB-CRS model.

This component is responsible for replacing placeholders, if any, in the
retrieved response with appropriate movie information.
"""

from __future__ import annotations

import os
import pickle
from abc import ABC, abstractmethod
from typing import List


class Recommender(ABC):
    def __init__(self) -> None:
        """Initializes the recommender."""
        pass

    @abstractmethod
    def get_recommendations(self, context: List[str]) -> List[str]:
        """Gets recommendations based on the conversation context.

        Args:
            context: Conversation context.

        Raises:
            NotImplementedError: If the method is not implemented in the
              subclass.

        Returns:
            List of recommended items.
        """
        raise NotImplementedError

    @abstractmethod
    def replace_item_ids_with_recommendations(
        self, response: str, recommended_items: List[str] = []
    ) -> str:
        """Replaces item ids in a response with recommended items.

        If no recommended items are available, the item ids are replaced with
        their original titles.

        Args:
            response: Response containing item ids.
            recommended_items: List of recommended items.

        Raises:
            NotImplementedError: If the method is not implemented in the
              subclass.
        Returns:
            Response with item ids replaced by recommended items.
        """
        raise NotImplementedError

    @abstractmethod
    def integrate_domain_metadata(
        self,
        context: List[str],
        response: str,
    ) -> str:
        """Integrates domain metadata into the response.

        Args:
            context: Conversation context
            response: Response to integrate domain metadata into.

        Raises:
            NotImplementedError: If the method is not implemented in the
              subclass.

        Returns:
            Response with domain metadata integrated.
        """
        raise NotImplementedError

    @classmethod
    def load(cls, path: str) -> Recommender:
        """Loads the recommender from the given path.

        Args:
            path: Path to the recommender.

        Raises:
            FileNotFoundError: If the recommender file is not found.

        Returns:
            Loaded recommender.
        """
        if not os.path.exists(path):
            raise FileNotFoundError(f"Recommender file not found: {path}")

        return pickle.load(open(path, "rb"))

    def save(self, path: str) -> None:
        """Saves the recommender to the given path.

        Args:
            path: Path to save the recommender.
        """
        pickle.dump(self, open(path, "wb"))