Advanced Product Review Aggregation Techniques

Introduction

Hello, and welcome to today's lesson! Today, we are going to dive into the world of managing product reviews and applying data aggregation in practice. We will start with a relatively simple Starter Task to set up our base and then gradually build up to a more complex solution involving data aggregation. Let's jump in!

Starter Task: Methods and Their Definitions

For our starter task, we will lay the foundation by implementing basic operations for managing product reviews. These are the methods we will need to implement:

  • add_review(self, product_id: str, review_id: str, review_text: str, rating: int) -> bool — adds a review to the product specified by product_id. If a review with review_id already exists, it updates the existing review. Returns True if the review was added or updated successfully, False otherwise.

  • get_review(self, product_id: str, review_id: str) -> dict | None — returns the review details (review_text, rating, and flagged fields) for the review specified by review_id under the given product_id. If the review or product does not exist, returns None.

  • delete_review(self, product_id: str, review_id: str) -> bool — deletes the review specified by review_id under the given product_id. Returns True if the review was deleted, False otherwise.

Starter Task Implementation

Let's look at the code that implements these functionalities:

Python
class ReviewManager:
    def __init__(self):
        self.products = {}

    def add_review(self, product_id: str, review_id: str, review_text: str, rating: int) -> bool:
        if rating < 1 or rating > 5:
            return False  # Invalid rating
        if product_id not in self.products:
            self.products[product_id] = {}
        self.products[product_id][review_id] = {"text": review_text, "rating": rating, "flagged": False}
        return True

    def get_review(self, product_id: str, review_id: str) -> dict | None:
        if product_id in self.products and review_id in self.products[product_id]:
            review = self.products[product_id][review_id]
            return {"text": review["text"], "rating": review["rating"], "flagged": review["flagged"]}
        return None

    def delete_review(self, product_id: str, review_id: str) -> bool:
        if product_id in self.products and review_id in self.products[product_id]:
            del self.products[product_id][review_id]
            if not self.products[product_id]:
                del self.products[product_id]  # Remove product if no reviews left
            return True
        return False

# Instantiate the ReviewManager
review_manager = ReviewManager()

# Adding some reviews
review_manager.add_review("p1", "r1", "Great product!", 5)
review_manager.add_review("p1", "r2", "Not bad", 3)

# Testing get_review method
print(review_manager.get_review("p1", "r1"))  # Expected: {"text": "Great product!", "rating": 5, "flagged": false}
print(review_manager.get_review("p1", "r3"))  # Expected: None

# Testing delete_review method
print(review_manager.delete_review("p1", "r2"))  # Expected: True
print(review_manager.get_review("p1", "r2"))  # Expected: None

This code establishes the foundational methods needed for managing product reviews within a ReviewManager class. The add_review method allows for adding a new review or updating an existing one, ensuring each review contains valid rating values between 1 and 5. The get_review method retrieves the review details for a specific product, including the review text and rating, returning None if the product or review doesn't exist. The delete_review method facilitates the removal of a specific review, and if no reviews are left for a product, the product itself is removed from the product list. Together, these methods form the basic operations required to manage a collection of product reviews efficiently.

Now, let's extend this with new features.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal