File size: 1,300 Bytes
82a5d51
 
 
 
98f5046
82a5d51
068431a
 
 
 
 
 
82a5d51
068431a
 
 
 
 
 
82a5d51
068431a
 
 
 
 
 
82a5d51
068431a
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import pandas as pd
from rapidfuzz import process

# Load the dataset
df = pd.read_csv("./sci_fi_books.csv")

# Function to get author by book title
def get_author_by_title(title):
    match = df[df["title"].str.lower() == title.lower()]
    if not match.empty:
        return match.iloc[0]["author"]
    return f"Author not found for book '{title}'."

# Function to get year by book title
def get_year_by_title(title):
    match = df[df["title"].str.lower() == title.lower()]
    if not match.empty:
        return int(match.iloc[0]["year"])
    return f"Year not found for book '{title}'."

# Function to get summary by book title
def get_summary_by_title(title):
    match = df[df["title"].str.lower() == title.lower()]
    if not match.empty:
        return match.iloc[0]["summary"]
    return f"Summary not found for book '{title}'."

# Function to find a book by summary similarity
def find_book_by_description(description):
    summaries = df["summary"].tolist()
    match = process.extractOne(description, summaries)
    if match and match[1] > 80:  # Threshold for similarity
        matched_title = df[df["summary"] == match[0]]["title"].iloc[0]
        return f"The closest book based on your description is '{matched_title}'."
    return "No book matches your description closely enough."