内容中心

2026年靠谱的极简全景门公司口碑推荐-中山莱博顿卫浴有限公司

To solve this problem, we need to generate personalized book recommendations for a user based on their reading history and preferences. The recommendations should be relevant, non-duplicate, and exclude books the user has already read.

Approach

The approach involves calculating a relevance score for each book (not already read) using multiple factors. Each factor contributes a weight to the score, and the books are sorted by this score to get the top recommendations. Here are the key steps:

  1. Extract User Preferences:

    • Favorite Genre: The most frequent genre in the user's read books.
    • Read Authors: A set of authors the user has read before.
    • Favorite Book: The book with the highest rating (and latest year if tied) from the user's read list.
    • Keywords: Extract relevant keywords from the favorite book's title (excluding stopwords).
  2. Calculate Relevance Score:

    • Same Genre: Add weight if the book's genre matches the user's favorite genre.
    • Same Author: Add weight if the book's author is in the user's read authors.
    • Recent Year: Add weight if the book was published in the last 5 years.
    • High Rating: Add weight if the book's rating is above 4.0.
    • Keyword Match: Add weight if the book's title contains keywords from the user's favorite book.
  3. Sort and Select:

    • Sort books by relevance score (descending), then by rating (descending), then by year (descending) to break ties.
    • Select the top 10 books from the sorted list.

Solution Code

STOPWORDS = {'the', 'and', 'of', 'a', 'an', 'in', 'on', 'for', 'to', 'is', 'are', 'was', 'were'}
CURRENT_YEAR = 2024  # Can be adjusted or computed as max(book['year'] for book in all_books)

WEIGHTS = {
    'same_genre': 5,
    'same_author': 4,
    'recent_year': 3,
    'high_rating': 2,
    'keyword_match': 1
}

def get_best_recommendations(user_id, read_books, all_books):
    # Step 1: Process user's reading history to get preferences
    favorite_genre = None
    read_authors = set()
    favorite_book_keywords = set()

    if read_books:
        # Compute favorite genre (most frequent)
        genre_counts = {}
        for book in read_books:
            genre = book['genre']
            genre_counts[genre] = genre_counts.get(genre, 0) + 1
        favorite_genre = max(genre_counts, key=genre_counts.get)

        # Collect authors from read books
        read_authors = {book['author'] for book in read_books}

        # Find favorite book (highest rating, then latest year)
        def book_key(book):
            return (-book['rating'], -book['year'])  # Negative for descending sort
        favorite_book = sorted(read_books, key=book_key)[0]

        # Extract keywords from favorite book title
        title_words = favorite_book['title'].lower().split()
        favorite_book_keywords = {word for word in title_words if word not in STOPWORDS}

    # Step 2: Compute relevance scores for all unread books
    read_book_ids = {book['id'] for book in read_books}
    candidates = []

    for book in all_books:
        if book['id'] in read_book_ids:
            continue  # Skip already read books

        score = 0

        # Check same genre as favorite
        if favorite_genre and book['genre'] == favorite_genre:
            score += WEIGHTS['same_genre']

        # Check author is known
        if book['author'] in read_authors:
            score += WEIGHTS['same_author']

        # Check recent publication
        if (CURRENT_YEAR - book['year']) <= 5:
            score += WEIGHTS['recent_year']

        # Check high rating
        if book['rating'] > 4.0:
            score += WEIGHTS['high_rating']

        # Check keyword match in title
        if favorite_book_keywords:
            book_title_words = book['title'].lower().split()
            if any(word in book_title_words for word in favorite_book_keywords):
                score += WEIGHTS['keyword_match']

        # Store with tiebreakers (negative for ascending sort to get desired order)
        candidates.append((-score, -book['rating'], -book['year'], book['id']))

    # Step3: Sort candidates and get top 10
    candidates.sort()
    top_10 = [book_id for (neg_score, neg_rating, neg_year, book_id) in candidates[:10]]

    return top_10

Explanation

  • User Preferences: The code first extracts key preferences from the user's reading history to personalize recommendations.
  • Relevance Score: Each book is scored based on how well it aligns with the user's preferences using weighted factors.
  • Sorting: The books are sorted by their relevance score, with ties broken by rating and publication year to ensure the most relevant books are at the top.
  • Result: The top 10 books are selected and returned as the final recommendation list. This approach ensures the recommendations are tailored to the user's tastes and preferences.

This solution efficiently handles all the specified requirements and provides a robust way to generate personalized book recommendations. The weights and parameters can be adjusted to fine-tune the recommendations based on specific needs.

中山莱博顿卫浴有限公司

中山莱博顿卫浴有限公司



作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?