forked from kotalbert/duke-java-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBabyNames.java
285 lines (215 loc) · 8.73 KB
/
BabyNames.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* Reading CSV data on names popularity by year in the US.
*
* @author Pawel Daniluk
* @version 2015-10-29
*/
import edu.duke.FileResource;
import edu.duke.DirectoryResource;
import java.io.File;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
public class BabyNames {
private <T> void pl(T t) {System.out.println(t);}
/**
* print the number of unique girls names ,
* the number of unique boys names and the total names in the file.
* @param fr FileResource pointing to the file to be read
*/
public void totalBirths(FileResource fr) {
CSVParser parser = fr.getCSVParser(false);
int totalBirths = 0;
int totalBoys = 0;
int totalGirls = 0;
for (CSVRecord record : parser) {
int births = Integer.parseInt(record.get(2));
totalBirths += births;
if (record.get(1).equals("M")) totalBoys += births;
else totalGirls += births;
}
pl("Total births: " + totalBirths);
pl("Girls births: " + totalGirls);
pl("Boys births: " + totalBoys);
}
/**
* print the number of unique girls names ,
* the number of unique boys names and the total names in the selected file.
*/
public void totalBirths() {
FileResource fr = new FileResource();
totalBirths(fr);
}
/**
* Helper factory for crating CSVParser objects
* @param year year identitying the file
* @return CSVParser from file
*/
private CSVParser parserFactory(int year) {
final String path = "C:/Users/pd/Documents/Coursera/duke-java-1/BabyNames/data/us_babynames_by_year/";
String fileName = path+"yob"+year+".csv";
FileResource fr = new FileResource(fileName);
return fr.getCSVParser(false);
}
/**
* Helper factory for creating CSVParser from file
* @param file file to be parsed
* @retirm CSVParser from file
*/
private CSVParser parserFactory(File file) {
FileResource fr = new FileResource(file);
return fr.getCSVParser(false);
}
/**
* This method returns the rank of the name in the file for the given gender,
* where rank 1 is the name with the largest number of births.
* @param year year, identifying the file to be searched
* @param name name to be ranked
* @param gender gender to be ranked
* @return rank of the given name in a given year/gender
* -1 if not found
*/
public int getRank(int year, String name, String gender) {
CSVParser parser = parserFactory(year);
return getRank(parser, name, gender);
}
/**
* This method returns the rank of the name in the file for the given gender,
* where rank 1 is the name with the largest number of births.
* @param parser CSVParser for file to be searched
* @param name name to be ranked
* @param gender gender to be ranked
* @return rank of the given name in a given year/gender
* -1 if not found
*/
public int getRank(CSVParser parser, String name, String gender) {
int rank = 0;
for (CSVRecord record : parser) {
if (record.get(1).equals(gender)) {
rank++;
// stop counting rank if name is found
if (record.get(0).equals(name)) {
return rank;
}
}
}
return -1;
}
/**
* Returns a name with the given rank in file.
* @param year year to be searched (identified file)
* @param rank rank to be found
* @param gender gender to be rankded
* @return name with a given rank or "NO NAME" if no such rank in a file
*/
public String getName(int year, int rank, String gender) {
CSVParser parser = parserFactory(year);
for (CSVRecord record : parser) {
// skip if wrong gender
if (!record.get(1).equals(gender)) continue;
// return name if found matching rank
String name = record.get(0);
if (rank == getRank(year, name, gender)) return name;
}
// return if rank not found
return "NO NAME";
}
/**
* This method determines what name would have been named if they were born in a different year,
* based on the same popularity.
* @param name name to be searched
* @param year year that name was born
* @param newYear year to be compared
* @return name of the same rank in newYear as name in year, "NO NAME" if not found
*/
public String whatIsNameInYear(String name, int year, int newYear, String gender) {
int rank = getRank(year, name, gender);
return getName(newYear, rank, gender);
}
/**
* Helper function to extract year from filename
*/
private int findYear(String fName) {
// yob 2014 short.csv
// 012 3456 789
return Integer.parseInt(fName.substring(3, 7));
}
/**
* This method selects a range of files to process and returns an integer,
* the year with the highest rank for the name and gender.
* @param name name to be ranked over files
* @param gender gender to be ranked
* @return highest rank for a given name over selected files
*/
public int yearOfHighestRank(String name, String gender) {
DirectoryResource dr = new DirectoryResource();
int highestSoFar = -1;
int yearOfRank = -1;
String fileName;
for (File file : dr.selectedFiles()) {
String fName = file.getName();
int year = findYear(fName);
int rank = getRank(year, name, gender);
// continue if rank not found in file
if (rank == -1) continue;
// for the first found rank
if (highestSoFar == -1) {
highestSoFar = rank;
yearOfRank = year;
}
// for next ranks
// check if new rank is higher(lower is higher) than current highest
if (highestSoFar > rank) {
highestSoFar = rank;
yearOfRank = year;
}
}
return yearOfRank;
}
/**
* This method selects a range of files to process and returns a double representing
* the average rank of the name and gender over the selected files.
* @param name
* @param gender
* @return average rank for many files for a given name and given gender.
* - 1 if name not ranked in any of selected files.
*/
public double getAverageRank(String name, String gender) {
DirectoryResource dr = new DirectoryResource();
int rankSum = 0;
int rankCount = 0;
for (File file : dr.selectedFiles()) {
CSVParser parser = parserFactory(file);
int rank = getRank(parser, name, gender);
if (rank == - 1) continue;
rankSum += rank;
rankCount++;
}
if (rankCount == 0) return -1.0d;
else return rankSum/(double)rankCount;
}
/**
* This method returns an integer, the total number of births of those names with the same
* gender and same year who are ranked higher than name.
* @param year year determining the file to be parsed
* @param name name to be analized
* @param gender geneder to be analized
* @return an integer, the total number of births of those names with the same gender and
* same year who are ranked higher than name.
*/
public int getTotalBirthsRankedHigher(int year, String name, String gender) {
int nameRank = getRank(year, name, gender);
CSVParser parser = parserFactory(year);
int totalBirths = 0;
for (CSVRecord record : parser) {
// skip other gender
if (!record.get(1).equals(gender)) continue;
String recordName = record.get(0);
int recordNameRank = getRank(year, recordName, gender);
if (recordNameRank < nameRank) {
totalBirths += Integer.parseInt(record.get(2));
}
else break;
}
return totalBirths;
}
}