英文:
Discrepancy in Calculating Average Ratings from Java Spring boot Rest api
问题
I am working on a project where I need to calculate the average ratings from MongoDB data using Java. However, I'm facing an issue where the calculated average does not match the expected result. I have reviewed the code and the data, but I'm unable to identify the cause of the discrepancy.
The average rating I obtained from the calculation is 1.309375 However, based on the provided values, I expected the average rating to be 3.325 if I haven't forgotten how to average the results.
this is the document in mongodb review summary
Here is the code I'm using:
public Page<Movie> allMovies(Pageable pageable) throws Exception {
Page<Movie> movies = movieRepository.findAll(pageable);
List<ReviewSummary> reviewSummaries = reviewSummaryRepository.findAll();
int numRatings = reviewSummaries.size(); // Moved this line outside the loop
Map<String, Double> avgRatings = new HashMap<>();
for (ReviewSummary reviewSummary : reviewSummaries) {
String imdbId = reviewSummary.getMovieImdbId();
double rating = reviewSummary.getRating();
if (!avgRatings.containsKey(imdbId)) {
avgRatings.put(imdbId, rating);
} else {
double totalRating = avgRatings.get(imdbId) + rating;
double avgRating = totalRating / numRatings; // Changed reviewSummaries.size() to numRatings
avgRatings.put(imdbId, avgRating);
}
}
for (Movie movie : movies) {
String imdbId = movie.getImdbId();
if (avgRatings.containsKey(imdbId)) {
double avgRating = avgRatings.get(imdbId);
movie.setAvgRating(avgRating);
} else {
movie.setAvgRating(0.0);
}
}
return movies;
}
this is what i get in my postman get request for a movie
"imdbId": "tt8760708",
"title": "M3GAN",
"releaseDate": "2023-01-06",
"trailerLink": "https://www.youtube.com/watch?v=BRb4U99OU80",
"poster": "https://image.tmdb.org/t/p/w500/xBl5AGw7HXZcv1nNXPlzGgO4Cfo.jpg",
"genres": [
"Science Fiction",
"Horror",
"Comedy"
],
"backdrops": [
"https://image.tmdb.org/t/p/original/5kAGbi9MFAobQTVfK4kWPnIfnP0.jpg",
"https://image.tmdb.org/t/p/original/dlxzUj7z1MqEcFiwvvrj0bvBKDY.jpg",
"https://image.tmdb.org/t/p/original/q2fY4kMXKoGv4CQf310MCxpXlRI.jpg",
"https://image.tmdb.org/t/p/original/pTxwFdsdDWzpCRYuk1QbggdaOlL.jpg",
"https://image.tmdb.org/t/p/original/1zuz2RgFoOjulkjjNHHFc3WiHGB.jpg",
"https://image.tmdb.org/t/p/original/7HqxI1IXMloT9VTSuDC8ikaj810.jpg",
"https://image.tmdb.org/t/p/original/vpK2rp3J5LiC01HoNM0j9DEHQ1T.jpg",
"https://image.tmdb.org/t/p/original/cNHXdmr4amP6EPCMa0dqD8rwzDV.jpg",
"https://image.tmdb.org/t/p/original/txQLFd6rfQrskQhFENkS1jElptt.jpg",
"https://image.tmdb.org/t/p/original/dC4tj1ONdlZ2TWv4XD2SA1KUnJN.jpg"
],
"reviewIds": [
{
"id": {
"timestamp": 1683393168,
"date": "2023-05-06T17:12:48.000+00:00"
},
"userEmail": "arnold@gmail.com",
"movieImdbId": "tt8760708",
"rating": {
"id": {
"timestamp": 1683393168,
"date": "2023-05-06T17:12:48.000+00:00"
},
"userEmail": "arnold@gmail.com",
"movieImdbId": "tt8760708",
"value": 3.5
},
"body": "Seem a bit meh "
},
{
"id": {
"timestamp": 1683393413,
"date": "2023-05-06T17:16:53.000+00:00"
},
"userEmail": "john@gmail.com",
"movieImdbId": "tt8760708",
"rating": {
"id": {
"timestamp": 1683393413,
"date": "2023-05-06T17:16:53.000+00:00"
},
"userEmail": "john@gmail.com",
"movieImdbId": "tt8760708",
"value": 3.5
},
"body": "Seem a bit meh "
},
{
"id": {
"timestamp": 1683393446,
"date": "2023-05-06T17:17:26.000+00:00"
},
"userEmail": "jim@gmail.com",
"movieImdbId": "tt8760708",
"rating": {
"id": {
"timestamp": 1683393446,
"date": "2023-05-06T17:17:26.000+00:00"
},
"userEmail": "jim@gmail.com",
"movieImdbId": "tt8760708",
"value": 2.0
},
"body": "good enough "
},
{
"id": {
"timestamp": 1683393489,
"date": "2023-05-06T17:18:09.000+00:00"
},
"userEmail": "will@gmail.com",
"movieImdbId": "tt8760708",
"rating": {
"id": {
"timestamp": 1683393489,
"date": "2023-05-06T17:18:09.000+00:00"
},
"userEmail": "will@gmail.com",
"movieImdbId": "tt8760708",
"value": 4.3
},
"body": "very good"
}
],
---> here is the issue "avgRating": 1.309375,
"predictedRating": 0.0,
"similarity": 0.0
英文:
I am working on a project where I need to calculate the average ratings from MongoDB data using Java. However, I'm facing an issue where the calculated average does not match the expected result. I have reviewed the code and the data, but I'm unable to identify the cause of the discrepancy.
The average rating I obtained from the calculation is 1.309375 However, based on the provided values, I expected the average rating to be 3.325 if I haven't forgotten how to average the results.
this is the document in mongodb review summary
Here is the code I'm using:
public Page<Movie> allMovies(Pageable pageable) throws Exception {
Page<Movie> movies = movieRepository.findAll(pageable);
List<ReviewSummary> reviewSummaries = reviewSummaryRepository.findAll();
int numRatings = reviewSummaries.size(); // Moved this line outside the loop
Map<String, Double> avgRatings = new HashMap<>();
for (ReviewSummary reviewSummary : reviewSummaries) {
String imdbId = reviewSummary.getMovieImdbId();
double rating = reviewSummary.getRating();
if (!avgRatings.containsKey(imdbId)) {
avgRatings.put(imdbId, rating);
} else {
double totalRating = avgRatings.get(imdbId) + rating;
double avgRating = totalRating / numRatings; // Changed reviewSummaries.size() to numRatings
avgRatings.put(imdbId, avgRating);
}
}
for (Movie movie : movies) {
String imdbId = movie.getImdbId();
if (avgRatings.containsKey(imdbId)) {
double avgRating = avgRatings.get(imdbId);
movie.setAvgRating(avgRating);
} else {
movie.setAvgRating(0.0);
}
}
return movies;
}
this is what i get in my postman get request for a movie
"imdbId": "tt8760708",
"title": "M3GAN",
"releaseDate": "2023-01-06",
"trailerLink": "https://www.youtube.com/watch?v=BRb4U99OU80",
"poster": "https://image.tmdb.org/t/p/w500/xBl5AGw7HXZcv1nNXPlzGgO4Cfo.jpg",
"genres": [
"Science Fiction",
"Horror",
"Comedy"
],
"backdrops": [
"https://image.tmdb.org/t/p/original/5kAGbi9MFAobQTVfK4kWPnIfnP0.jpg",
"https://image.tmdb.org/t/p/original/dlxzUj7z1MqEcFiwvvrj0bvBKDY.jpg",
"https://image.tmdb.org/t/p/original/q2fY4kMXKoGv4CQf310MCxpXlRI.jpg",
"https://image.tmdb.org/t/p/original/pTxwFdsdDWzpCRYuk1QbggdaOlL.jpg",
"https://image.tmdb.org/t/p/original/1zuz2RgFoOjulkjjNHHFc3WiHGB.jpg",
"https://image.tmdb.org/t/p/original/7HqxI1IXMloT9VTSuDC8ikaj810.jpg",
"https://image.tmdb.org/t/p/original/vpK2rp3J5LiC01HoNM0j9DEHQ1T.jpg",
"https://image.tmdb.org/t/p/original/cNHXdmr4amP6EPCMa0dqD8rwzDV.jpg",
"https://image.tmdb.org/t/p/original/txQLFd6rfQrskQhFENkS1jElptt.jpg",
"https://image.tmdb.org/t/p/original/dC4tj1ONdlZ2TWv4XD2SA1KUnJN.jpg"
],
"reviewIds": [
{
"id": {
"timestamp": 1683393168,
"date": "2023-05-06T17:12:48.000+00:00"
},
"userEmail": "arnold@gmail.com",
"movieImdbId": "tt8760708",
"rating": {
"id": {
"timestamp": 1683393168,
"date": "2023-05-06T17:12:48.000+00:00"
},
"userEmail": "arnold@gmail.com",
"movieImdbId": "tt8760708",
"value": 3.5
},
"body": "Seem a bit meh "
},
{
"id": {
"timestamp": 1683393413,
"date": "2023-05-06T17:16:53.000+00:00"
},
"userEmail": "john@gmail.com",
"movieImdbId": "tt8760708",
"rating": {
"id": {
"timestamp": 1683393413,
"date": "2023-05-06T17:16:53.000+00:00"
},
"userEmail": "john@gmail.com",
"movieImdbId": "tt8760708",
"value": 3.5
},
"body": "Seem a bit meh "
},
{
"id": {
"timestamp": 1683393446,
"date": "2023-05-06T17:17:26.000+00:00"
},
"userEmail": "jim@gmail.com",
"movieImdbId": "tt8760708",
"rating": {
"id": {
"timestamp": 1683393446,
"date": "2023-05-06T17:17:26.000+00:00"
},
"userEmail": "jim@gmail.com",
"movieImdbId": "tt8760708",
"value": 2.0
},
"body": "good enough "
},
{
"id": {
"timestamp": 1683393489,
"date": "2023-05-06T17:18:09.000+00:00"
},
"userEmail": "will@gmail.com",
"movieImdbId": "tt8760708",
"rating": {
"id": {
"timestamp": 1683393489,
"date": "2023-05-06T17:18:09.000+00:00"
},
"userEmail": "will@gmail.com",
"movieImdbId": "tt8760708",
"value": 4.3
},
"body": "very good"
}
],
"ratings": [
{
"id": {
"timestamp": 1683393168,
"date": "2023-05-06T17:12:48.000+00:00"
},
"userEmail": "arnold@gmail.com",
"movieImdbId": "tt8760708",
"value": 3.5
},
{
"id": {
"timestamp": 1683393413,
"date": "2023-05-06T17:16:53.000+00:00"
},
"userEmail": "john@gmail.com",
"movieImdbId": "tt8760708",
"value": 3.5
},
{
"id": {
"timestamp": 1683393446,
"date": "2023-05-06T17:17:26.000+00:00"
},
"userEmail": "jim@gmail.com",
"movieImdbId": "tt8760708",
"value": 2.0
},
{
"id": {
"timestamp": 1683393489,
"date": "2023-05-06T17:18:09.000+00:00"
},
"userEmail": "will@gmail.com",
"movieImdbId": "tt8760708",
"value": 4.3
}
],
----> here is the issue "avgRating": 1.309375,
"predictedRating": 0.0,
"similarity": 0.0
答案1
得分: 1
你不应该通过总评分数(numRatimgs)进行除法运算,这是导致问题的原因。
double avgRating = totalRating / numRatings;
相反,你应该将它除以直到那个时间点的特定电影的实际发生次数。
也许你可以创建一个名为 MoviRating 的 POJO,其中可以包含属性,例如 - totalReviews、totalRating 和 averageRating。
在你的映射中,你可以将数据设置为 <String, MovieRating>,然后不断更新 POJO 中的数据。
希望这对你有帮助。
英文:
You shouldn't be dividing by total number of ratings (numRatimgs) [this is what causing issue for you]
double avgRating = totalRating / numRatings;
Instead you should divide it by actual number of occurrences of that particular movie until that time.
May be you can have a pojo called MoviRating where you can have properties inside it like - totalReviews,totalRating and averageRating
In your map you can have data as <String, MovieRating>
And keep updating the data in pojo.
Hope this helps you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论