Menu

Building the Recommendation Function

Building the Recommendation Function

The recommendation function is the core component of the project. It accepts a news headline as input, identifies the corresponding article in the dataset, calculates similarity scores, and returns the most similar news articles.

Code

def recommend_news(title):
index = indices[title]
similarity_scores = list(
enumerate(similarity_matrix[index])
)
similarity_scores = sorted(
similarity_scores,
key=lambda x: x[1],
reverse=True
)
return similarity_scores

Explanation

The function performs the following operations:

  • Finds the index of the selected news article.
  • Retrieves similarity scores from the similarity matrix.
  • Sorts the articles based on similarity scores.
  • Returns the sorted list of similar news articles.

Output

The recommendation function is successfully created and is ready to generate personalized recommendations.