saidhanush4422 / Helium-3

0 stars 1 forks source link

Module 4.3: Create Book Suggestion System #16

Closed saidhanush4422 closed 1 year ago

saidhanush4422 commented 1 year ago

Failed test

data = pd.read_csv("database_books_analyzed.csv")

    # Define a function to recommend songs based on similar artists
    def recommend_books(Mood_Value):
      # Get all the songs by the artist
      books_by_author = data[data["Book_Title"] == Mood_Value]["Book_Short_Description"]

      # Get all the songs by similar artists
      similar_author = data[data["Book_Title"] != Mood_Value]["Book_Title"].unique()
      books_by_similar_authors = data[data["Book_Title"].isin(similar_author)]["Book_Title"]

      # Recommend the top 10 most popular songs by similar artist
      recommendations = books_by_similar_authors.value_counts().head(10)

      # Remove any songs by the original artist from the recommendations
      recommendations = recommendations[~recommendations.isin(books_by_author)]

      return recommendations

      print(recommend_books(a))

This recommendation system has been a failure so far

Generate a random data extraction from a given csv file, where the output should be from "Book_Title" cell and the given input shoul match with the cell " Mood_Value " from a database_books_analyzed.csv file

import csv
import random

# Define the input condition
input_mood = "happy"

# Open the CSV file and read the rows
with open("database_books_analyzed.csv", newline="") as csvfile:
    reader = csv.DictReader(csvfile)
    rows = [row for row in reader if row["Mood_Value"] == input_mood]

# Randomly select a row based on the input condition
if len(rows) > 0:
    selected_row = random.choice(rows)
    book_title = selected_row["Book_Title"]
    print(f"The book title for mood '{input_mood}' is '{book_title}'")
else:
    print(f"No books found for mood '{input_mood}'")

In this example, we first define the input condition as the value "happy". We then open the CSV file "database_books_analyzed.csv" using the csv.DictReader class, which reads the file and converts each row into a dictionary where the keys are the column headers and the values are the corresponding cell values.

We use a list comprehension to filter the rows based on the input condition. In this case, we only keep rows where the value of the "Mood_Value" column is equal to the input mood.

We then use the random.choice() function to randomly select one row from the filtered rows, if any exist. We extract the value of the "Book_Title" cell from the selected row using dictionary indexing, and print it out as the output.

If there are no rows that match the input condition, we print a message saying that no books were found.

saidhanush4422 commented 1 year ago

Successful Trail

input_mood = final_emotion

    # Open the CSV file and read the rows
    with open("database_books_analyzed.csv", newline="", encoding='utf-8') as csvfile:
        reader = csv.DictReader(csvfile)
        rows = [row for row in reader if row["Mood_Value"] == input_mood]

# Randomly select a row based on the input condition
    if len(rows) > 0:
        selected_row = random.choice(rows)
        book_title = selected_row["Book_Title"]
        print(f"The book title for mood '{input_mood}' is '{book_title}'")
    else:
        print(f"No books found for mood '{input_mood}'")