<jupyter_start><jupyter_text>Core> This module contains core classes and functions to be reused by other modules<jupyter_code>#export
from typing import List, NamedTuple, Tuple
from dataclasses import dataclass
from pathlib import Path
import logging

import pandas as pd
import matplotlib.pyplot as plt
#export
# constants
RANDOM_SEED = 31

logger = logging.getLogger(__name__)
#export
class Recommendation(NamedTuple):
    """
    Represents a recommendation for a movie
    """
    movie_id: int
    score: float
#export
@dataclass
class Movie:
    """
    Simple dataclass to hold the content for a Movie 
    """
    movie_id: int
    title: str
    genres: str
#export
class MovieRepository:
    def __init__(
        self,
        movies_df: pd.DataFrame):
        self.movies_df = movies_df
        self.movie_id_dict = movies_df.set_index("movieId").to_dict('index')
    
    def find_by_movie_id(self, movie_id: int) -> Movie:
        movie_row = self.movie_id_dict[movie_id]
        return Movie(
            movie_id=movie_id,
            title=movie_row['title'],
            genres=movie_row['genres']
        )

# export
def print_recommendations(movie_repository: MovieRepository, seed_id: int, recommendations: List[Recommendation]):
    seed = movie_repository.find_by_movie_id(seed_id)
    print(seed)
    print("> Recommendations:")
    for rec in recommendations:
        movie = movie_repository.find_by_movie_id(rec.movie_id)
        print(">>", movie, f"score={rec.score}")
movies_df = pd.read_csv('../data/ml-latest-small/movies.csv')
movies_df.head()
movie_repository = MovieRepository(
    movies_df=movies_df
)

movie_repository.find_by_movie_id(1)
print_recommendations(movie_repository, seed_id=1210, recommendations=[Recommendation(movie_id=10, score=0.1), Recommendation(movie_id=1, score=0.2)])<jupyter_output>Movie(movie_id=1210, title='Star Wars: Episode VI - Return of the Jedi (1983)', genres='Action|Adventure|Sci-Fi')
> Recommendations:
>> Movie(movie_id=10, title='GoldenEye (1995)', genres='Action|Adventure|Thriller') score=0.1
>> Movie(movie_id=1, title='Toy Story (1995)', genres='Adventure|Animation|Children|Comedy|Fantasy') score=0.2
{'fraction_non_alphanumeric': 0.11009174311926606, 'fraction_numerical': 0.016972477064220184, 'mean_word_length': 4.466165413533835, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '329355', 'license': 'Apache-2.0', 'n_tokens_mistral': 770, 'n_tokens_neox': 713, 'n_words': 157, 'path': 'notebooks/00_core.ipynb', 'repo_name': 'agacera/udacity_mle_word2vec_recommender', 'size': '2180'}