#Importing the sklearn’s CountVectorizer and Cosine Similarity functions
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import pandas as pd
import numpy as np

#Reading the articles.csv as DataFrame and dropping the NA values in the title column
df = pd.read_csv('articles.csv')
df = df[df['title'].notna()]

#Creating the count_matrix and creating cosine_sim classifier
count = CountVectorizer(stop_words='english')
count_matrix = count.fit_transform(df['title'])
cosine_sim2 = cosine_similarity(count_matrix, count_matrix)

#Resetting the indexes of the DataFrame to the article's title
df = df.reset_index()
indices = pd.Series(df.index, index=df['contentId'])

#Create the function to get a recommendation based on the contentId of the article.
def get_recommendations(contentId):
    idx = indices[int(contentId)]
    sim_scores = list(enumerate(cosine_sim2[idx]))
    sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
    sim_scores = sim_scores[1:11]
    article_indices = [i[0] for i in sim_scores]
    return df[["url", "title", "text", "lang", "total_events"]].iloc[article_indices].values.tolist()