-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameReviews.java
127 lines (103 loc) · 3.81 KB
/
GameReviews.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class GameReviews {
private static class Review {
String gameTitle, platform, reaction, genre, editorsChoice;
double score;
int releaseYear;
Review(String[] attributes) {
this.gameTitle = attributes[0];
this.platform = attributes[1];
this.reaction = attributes[2];
this.score = Double.parseDouble(attributes[3]);
this.genre = attributes[4];
this.editorsChoice = attributes[5];
this.releaseYear = Integer.parseInt(attributes[6]);
}
}
private static class Group {
List<Review> reviews;
Map<String, Integer> reactionCount;
Map<Double, Integer> scoreCount;
double totalScoreSum;
Review bestGame;
Review worseGame;
Group() {
this.reviews = new ArrayList<>();
reactionCount = new HashMap<>();
scoreCount = new HashMap<>();
totalScoreSum = 0;
}
public void addReview(Review review) {
this.reviews.add(review);
incrementReactionCount(review);
incrementScoreCount(review);
if (bestGame == null || bestGame.score < review.score) {
bestGame = review;
}
if (worseGame == null || worseGame.score > review.score) {
worseGame = review;
}
}
private void incrementReactionCount(Review review) {
Integer count = reactionCount.get(review.reaction);
if (count == null) count = 0;
reactionCount.put(review.reaction, count + 1);
}
private void incrementScoreCount(Review review) {
Integer count = scoreCount.get(review.score);
if (count == null) count = 0;
scoreCount.put(review.score, count + 1);
totalScoreSum += review.score;
}
public double getScorePhrasePercentage(String scorePhrase) {
double count = 0;
if (reactionCount.containsKey(scorePhrase)) {
count = (double) reactionCount.get(scorePhrase);
}
return count / reviews.size();
}
public double getArithmeticAverage() {
return totalScoreSum / reviews.size();
}
public double getPopulationStandardDeviation() {
double total = 0;
double arithmeticAverage = getArithmeticAverage();
for(Double score : scoreCount.keySet()) {
total += Math.pow((score - arithmeticAverage), 2);
}
return Math.sqrt(total / reviews.size());
}
}
public static void main(String[] args) {
SimpleReader input = new SimpleReader("./game-reviews.csv");
Map<String, Group> map = new TreeMap<>();
// remove first line; a title line.
// title;platform;score_phrase;score;genre;editors_choice;release_year
input.readLine();
String line;
while ((line = input.readLine()) != null) {
Review review = new Review(line.split(";"));
if (review.genre == null || review.genre.equals("") || review.genre.equals("-")) review.genre = "Misc";
Group group = map.get(review.genre);
if (group == null) {
group = new Group();
map.put(review.genre, group);
}
group.addReview(review);
}
for (String genre : map.keySet()) {
Group group = map.get(genre);
System.out.println("=== Genre: " + genre);
System.out.println(" Total reviews: " + group.reviews.size());
System.out.println(" Amazing reviews percentage: " + String.format("%.2f", group.getScorePhrasePercentage("Amazing")) + "%");
System.out.println(" Score arithmetic average: " + String.format("%.2f", group.getArithmeticAverage()));
System.out.println(" One of the best games: " + group.bestGame.gameTitle + " with score " + group.bestGame.score);
System.out.println(" One of the worse games: " + group.worseGame.gameTitle + "with score " + group.worseGame.score);
System.out.println("");
}
}
}