Commit c068bacc authored by Shaik Afreed's avatar Shaik Afreed

Rolled back the bayesian formulae to old formulae for average rating changes implemented

parent 1d3fa3de
...@@ -19,6 +19,7 @@ class FeatureUpdater: ...@@ -19,6 +19,7 @@ class FeatureUpdater:
existing_total_reviews = self.product_data.get("total_reviews", 0) existing_total_reviews = self.product_data.get("total_reviews", 0)
current_overall_rating = self.product_data.get("overall_rating", 0.0) current_overall_rating = self.product_data.get("overall_rating", 0.0)
for feature, new_data in feature_data.items(): for feature, new_data in feature_data.items():
if feature not in features: if feature not in features:
features[feature] = { features[feature] = {
...@@ -30,37 +31,25 @@ class FeatureUpdater: ...@@ -30,37 +31,25 @@ class FeatureUpdater:
current_feature = features[feature] current_feature = features[feature]
existing_review_count = current_feature["review_count"] existing_review_count = current_feature["review_count"]
M = 4
C = max(2, 10 - (existing_review_count // 20)) # Reduce prior influence faster
# if existing_review_count < 5:
# C = 5
# elif existing_review_count < 50:
# C = 10
# elif existing_review_count < 500:
# C = 15
# else:
# C = 20
new_ratings = new_data.get("ratings", []) new_ratings = new_data.get("ratings", [])
new_rating = sum(new_ratings) / len(new_ratings) if new_ratings else 0 new_rating = sum(new_ratings) / len(new_ratings) if new_ratings else 0
new_positive = new_data.get("positive_count", 0) or 0 new_positive = new_data.get("positive_count", 0) or 0
new_negative = new_data.get("negative_count", 0) or 0 new_negative = new_data.get("negative_count", 0) or 0
# Apply the Bayesian Average Formula
new_average_rating = ( new_average_rating = (
(current_feature["average_rating"] * existing_review_count) + ( (current_feature["average_rating"] * existing_review_count) + new_rating
C * M) + new_rating ) / (existing_review_count + 1) if existing_review_count > 0 else new_rating
) / (existing_review_count + C + 1) if existing_review_count > 0 else new_rating
print(f"{new_average_rating}+--------------------------$$$----------------")
print(f"{new_average_rating}--------------------------$$$----------------")
features[feature]["average_rating"] = new_average_rating features[feature]["average_rating"] = new_average_rating
features[feature]["review_count"] += 1 features[feature]["review_count"] += 1
features[feature]["positive_count"] += new_positive features[feature]["positive_count"] += new_positive
features[feature]["negative_count"] += new_negative features[feature]["negative_count"] += new_negative
ratings_distribution[str(new_overall_rating)] += 1 ratings_distribution[str(new_overall_rating)] += 1
updated_total_reviews = existing_total_reviews + 1 updated_total_reviews = existing_total_reviews + 1
...@@ -68,9 +57,10 @@ class FeatureUpdater: ...@@ -68,9 +57,10 @@ class FeatureUpdater:
(current_overall_rating * existing_total_reviews) + new_overall_rating (current_overall_rating * existing_total_reviews) + new_overall_rating
) / updated_total_reviews if existing_total_reviews > 0 else new_overall_rating ) / updated_total_reviews if existing_total_reviews > 0 else new_overall_rating
self.product_data["features"] = features self.product_data["features"] = features
self.product_data["ratings_distribution"] = ratings_distribution self.product_data["ratings_distribution"] = ratings_distribution
self.product_data["overall_rating"] = round(new_overall_rating, 1) self.product_data["overall_rating"] = round(new_overall_rating, 1)
self.product_data["total_reviews"] = updated_total_reviews self.product_data["total_reviews"] = updated_total_reviews
return self.product_data return self.product_data
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment