Closed saidhanush4422 closed 1 year ago
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}'")
Failed test
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
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.