Discrepancy in Calculating Average Ratings from Java Spring boot Rest api

huangapple go评论69阅读模式
英文:

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
Discrepancy in Calculating Average Ratings from Java Spring boot Rest api

Here is the code I'm using:

 public Page&lt;Movie&gt; allMovies(Pageable pageable) throws Exception {
        Page&lt;Movie&gt; movies = movieRepository.findAll(pageable);
        List&lt;ReviewSummary&gt; reviewSummaries = reviewSummaryRepository.findAll();
        int numRatings = reviewSummaries.size(); // Moved this line outside the loop
        Map&lt;String, Double&gt; avgRatings = new HashMap&lt;&gt;();

        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

&quot;imdbId&quot;: &quot;tt8760708&quot;,
&quot;title&quot;: &quot;M3GAN&quot;,
&quot;releaseDate&quot;: &quot;2023-01-06&quot;,
&quot;trailerLink&quot;: &quot;https://www.youtube.com/watch?v=BRb4U99OU80&quot;,
&quot;poster&quot;: &quot;https://image.tmdb.org/t/p/w500/xBl5AGw7HXZcv1nNXPlzGgO4Cfo.jpg&quot;,
&quot;genres&quot;: [
&quot;Science Fiction&quot;,
&quot;Horror&quot;,
&quot;Comedy&quot;
],
&quot;backdrops&quot;: [
&quot;https://image.tmdb.org/t/p/original/5kAGbi9MFAobQTVfK4kWPnIfnP0.jpg&quot;,
&quot;https://image.tmdb.org/t/p/original/dlxzUj7z1MqEcFiwvvrj0bvBKDY.jpg&quot;,
&quot;https://image.tmdb.org/t/p/original/q2fY4kMXKoGv4CQf310MCxpXlRI.jpg&quot;,
&quot;https://image.tmdb.org/t/p/original/pTxwFdsdDWzpCRYuk1QbggdaOlL.jpg&quot;,
&quot;https://image.tmdb.org/t/p/original/1zuz2RgFoOjulkjjNHHFc3WiHGB.jpg&quot;,
&quot;https://image.tmdb.org/t/p/original/7HqxI1IXMloT9VTSuDC8ikaj810.jpg&quot;,
&quot;https://image.tmdb.org/t/p/original/vpK2rp3J5LiC01HoNM0j9DEHQ1T.jpg&quot;,
&quot;https://image.tmdb.org/t/p/original/cNHXdmr4amP6EPCMa0dqD8rwzDV.jpg&quot;,
&quot;https://image.tmdb.org/t/p/original/txQLFd6rfQrskQhFENkS1jElptt.jpg&quot;,
&quot;https://image.tmdb.org/t/p/original/dC4tj1ONdlZ2TWv4XD2SA1KUnJN.jpg&quot;
],
&quot;reviewIds&quot;: [
{
&quot;id&quot;: {
&quot;timestamp&quot;: 1683393168,
&quot;date&quot;: &quot;2023-05-06T17:12:48.000+00:00&quot;
},
&quot;userEmail&quot;: &quot;arnold@gmail.com&quot;,
&quot;movieImdbId&quot;: &quot;tt8760708&quot;,
&quot;rating&quot;: {
&quot;id&quot;: {
&quot;timestamp&quot;: 1683393168,
&quot;date&quot;: &quot;2023-05-06T17:12:48.000+00:00&quot;
},
&quot;userEmail&quot;: &quot;arnold@gmail.com&quot;,
&quot;movieImdbId&quot;: &quot;tt8760708&quot;,
&quot;value&quot;: 3.5
},
&quot;body&quot;: &quot;Seem a bit meh &quot;
},
{
&quot;id&quot;: {
&quot;timestamp&quot;: 1683393413,
&quot;date&quot;: &quot;2023-05-06T17:16:53.000+00:00&quot;
},
&quot;userEmail&quot;: &quot;john@gmail.com&quot;,
&quot;movieImdbId&quot;: &quot;tt8760708&quot;,
&quot;rating&quot;: {
&quot;id&quot;: {
&quot;timestamp&quot;: 1683393413,
&quot;date&quot;: &quot;2023-05-06T17:16:53.000+00:00&quot;
},
&quot;userEmail&quot;: &quot;john@gmail.com&quot;,
&quot;movieImdbId&quot;: &quot;tt8760708&quot;,
&quot;value&quot;: 3.5
},
&quot;body&quot;: &quot;Seem a bit meh &quot;
},
{
&quot;id&quot;: {
&quot;timestamp&quot;: 1683393446,
&quot;date&quot;: &quot;2023-05-06T17:17:26.000+00:00&quot;
},
&quot;userEmail&quot;: &quot;jim@gmail.com&quot;,
&quot;movieImdbId&quot;: &quot;tt8760708&quot;,
&quot;rating&quot;: {
&quot;id&quot;: {
&quot;timestamp&quot;: 1683393446,
&quot;date&quot;: &quot;2023-05-06T17:17:26.000+00:00&quot;
},
&quot;userEmail&quot;: &quot;jim@gmail.com&quot;,
&quot;movieImdbId&quot;: &quot;tt8760708&quot;,
&quot;value&quot;: 2.0
},
&quot;body&quot;: &quot;good enough &quot;
},
{
&quot;id&quot;: {
&quot;timestamp&quot;: 1683393489,
&quot;date&quot;: &quot;2023-05-06T17:18:09.000+00:00&quot;
},
&quot;userEmail&quot;: &quot;will@gmail.com&quot;,
&quot;movieImdbId&quot;: &quot;tt8760708&quot;,
&quot;rating&quot;: {
&quot;id&quot;: {
&quot;timestamp&quot;: 1683393489,
&quot;date&quot;: &quot;2023-05-06T17:18:09.000+00:00&quot;
},
&quot;userEmail&quot;: &quot;will@gmail.com&quot;,
&quot;movieImdbId&quot;: &quot;tt8760708&quot;,
&quot;value&quot;: 4.3
},
&quot;body&quot;: &quot;very good&quot;
}
],
&quot;ratings&quot;: [
{
&quot;id&quot;: {
&quot;timestamp&quot;: 1683393168,
&quot;date&quot;: &quot;2023-05-06T17:12:48.000+00:00&quot;
},
&quot;userEmail&quot;: &quot;arnold@gmail.com&quot;,
&quot;movieImdbId&quot;: &quot;tt8760708&quot;,
&quot;value&quot;: 3.5
},
{
&quot;id&quot;: {
&quot;timestamp&quot;: 1683393413,
&quot;date&quot;: &quot;2023-05-06T17:16:53.000+00:00&quot;
},
&quot;userEmail&quot;: &quot;john@gmail.com&quot;,
&quot;movieImdbId&quot;: &quot;tt8760708&quot;,
&quot;value&quot;: 3.5
},
{
&quot;id&quot;: {
&quot;timestamp&quot;: 1683393446,
&quot;date&quot;: &quot;2023-05-06T17:17:26.000+00:00&quot;
},
&quot;userEmail&quot;: &quot;jim@gmail.com&quot;,
&quot;movieImdbId&quot;: &quot;tt8760708&quot;,
&quot;value&quot;: 2.0
},
{
&quot;id&quot;: {
&quot;timestamp&quot;: 1683393489,
&quot;date&quot;: &quot;2023-05-06T17:18:09.000+00:00&quot;
},
&quot;userEmail&quot;: &quot;will@gmail.com&quot;,
&quot;movieImdbId&quot;: &quot;tt8760708&quot;,
&quot;value&quot;: 4.3
}
],
----&gt; here is the issue     &quot;avgRating&quot;: 1.309375,
&quot;predictedRating&quot;: 0.0,
&quot;similarity&quot;: 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.

huangapple
  • 本文由 发表于 2023年5月7日 02:23:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190468.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定