Repository navigation
Expand file tree
/
Copy pathOnlineCoursesAnalyzer.java
More file actions
502 lines (434 loc) · 13.7 KB
/
Copy pathOnlineCoursesAnalyzer.java
File metadata and controls
502 lines (434 loc) · 13.7 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
/**
* This is just a demo for you, please run it on JDK17. This is just a demo, and you can extend and
* implement functions based on this demo, or implement it in a different way.
*/
public class OnlineCoursesAnalyzer {
List<Course> courses = new ArrayList<>();
/**
* read courses.
*
* @param datasetPath the dataset path
*/
public OnlineCoursesAnalyzer(String datasetPath) {
BufferedReader br = null;
String line;
try {
br = new BufferedReader(new FileReader(datasetPath, StandardCharsets.UTF_8));
br.readLine();
while ((line = br.readLine()) != null) {
String[] info = line.split(",(?=([^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)", -1);
Course course = new Course(info[0], info[1], new Date(info[2]), info[3], info[4],
info[5],
Integer.parseInt(info[6]), Integer.parseInt(info[7]), Integer.parseInt(info[8]),
Integer.parseInt(info[9]), Integer.parseInt(info[10]),
Double.parseDouble(info[11]),
Double.parseDouble(info[12]), Double.parseDouble(info[13]),
Double.parseDouble(info[14]),
Double.parseDouble(info[15]), Double.parseDouble(info[16]),
Double.parseDouble(info[17]),
Double.parseDouble(info[18]), Double.parseDouble(info[19]),
Double.parseDouble(info[20]),
Double.parseDouble(info[21]), Double.parseDouble(info[22]));
courses.add(course);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Participants count by Institution.
*
* @return (institution, count) map
*/
public Map<String, Integer> getPtcpCountByInst() {
return courses.stream()
.sorted((Comparator.comparing(o -> o.institution)))
.collect(Collectors.groupingBy(o -> o.institution, LinkedHashMap::new,
Collectors.summingInt(o -> o.participants)));
}
/**
* Participants count by Institution and Course Subject.
*
* @return (institution-course Subject, count) map
*/
public Map<String, Integer> getPtcpCountByInstAndSubject() {
Map<String, Integer> group = courses.stream()
.collect(Collectors.groupingBy(o -> o.institution + "-" + o.subject,
Collectors.summingInt(o -> o.participants)));
return group.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldV, newV) -> oldV,
LinkedHashMap::new));
}
/**
* Course list by Instructor.
*
* @return (Instructor, [[course1, course2,...],[coursek,coursek+1,...]]) map
*/
public Map<String, List<List<String>>> getCourseListOfInstructor() {
Map<String, List<List<String>>> res = new HashMap<>();
courses.forEach(c -> {
String[] instructors = c.instructors.split(",");
for (String instructor : instructors) {
instructor = instructor.strip();
if (!res.containsKey(instructor)) {
List<List<String>> both = new ArrayList<>();
both.add(new ArrayList<>());
both.add(new ArrayList<>());
res.put(instructor, both);
}
if (instructors.length == 1) {
res.get(instructor).get(0).add(c.title);
} else {
res.get(instructor).get(1).add(c.title);
}
}
});
Map<String, List<List<String>>> ret = new HashMap<>();
res.forEach((k, v) -> {
List<String> one = v.get(0).stream()
.distinct()
.sorted()
.toList();
List<String> two = v.get(1).stream()
.distinct()
.sorted()
.toList();
List<List<String>> val = new ArrayList<>();
val.add(one);
val.add(two);
ret.put(k, val);
});
return ret;
}
/**
* Top courses.
*
* @param topK the top K courses
* @param by the given criterion
* @return a list of Course titles
*/
public List<String> getCourses(int topK, String by) {
if (by.equals("hours")) {
return courses.stream()
.sorted(Comparator.comparing(Course::getTotalHours, Comparator.reverseOrder())
.thenComparing(Course::getTitle))
.map(Course::getTitle)
.distinct()
.limit(topK)
.collect(Collectors.toList());
} else {
return courses.stream()
.sorted(Comparator.comparing(Course::getParticipants, Comparator.reverseOrder())
.thenComparing(Course::getTitle))
.map(Course::getTitle)
.distinct()
.limit(topK)
.collect(Collectors.toList());
}
}
/**
* Search courses.
*
* @param courseSubject Fuzzy matching course subject
* @param percentAudited the percent of the audited should >= percentAudited
* @param totalCourseHours the Total Course Hours (Thousands) should <= totalCourseHours
* @return a list of Course titles
*/
public List<String> searchCourses(String courseSubject, double percentAudited,
double totalCourseHours) {
return courses.stream()
.filter(o -> o.subject.toUpperCase().contains(courseSubject.toUpperCase())
&& o.percentAudited >= percentAudited && o.totalHours <= totalCourseHours)
.map(Course::getTitle)
.distinct()
.sorted()
.collect(Collectors.toList());
}
/**
* Recommend courses.
*
* @param age age of the user
* @param gender 0-female, 1-male
* @param isBachelorOrHigher 0-Not get bachelor degree, 1- Bachelor degree or higher
* @return a list of Course titles
*/
public List<String> recommendCourses(int age, int gender, int isBachelorOrHigher) {
class Similarity {
String title;
double similarity;
public Similarity(String title, double similarity) {
this.title = title;
this.similarity = similarity;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getSimilarity() {
return similarity;
}
public void setSimilarity(double similarity) {
this.similarity = similarity;
}
}
ArrayList<Similarity> target = new ArrayList<>();
// get average
Map<String, Double> number2age = courses.stream().collect(
Collectors.groupingBy(Course::getNumber,
Collectors.averagingDouble(Course::getMedianAge)));
Map<String, Double> number2male = courses.stream().collect(
Collectors.groupingBy(Course::getNumber,
Collectors.averagingDouble(Course::getPercentMale)));
Map<String, Double> number2degree = courses.stream().collect(
Collectors.groupingBy(Course::getNumber,
Collectors.averagingDouble(Course::getPercentDegree)));
// get latest(max) title of course number
Map<String, Optional<Course>> number2latest = courses.stream()
.collect(Collectors.groupingBy(Course::getNumber,
Collectors.maxBy((d1, d2) -> d1.launchDate.compareTo(d2.getLaunchDate()))));
for (String k : number2age.keySet()) {
target.add(new Similarity(number2latest.get(k).get().title,
Math.pow(number2age.get(k) - age, 2) + Math.pow(number2male.get(k) - gender * 100,
2)
+ Math.pow(number2degree.get(k) - isBachelorOrHigher * 100, 2)));
}
return target.stream()
.sorted(
Comparator.comparing(Similarity::getSimilarity).thenComparing(Similarity::getTitle))
.map(o -> o.title)
.distinct()
.limit(10)
.collect(Collectors.toList());
}
}
class Course {
String institution;
String number;
Date launchDate;
String title;
String instructors;
String subject;
int year;
int honorCode;
int participants;
int audited;
int certified;
double percentAudited;
double percentCertified;
double percentCertified50;
double percentVideo;
double percentForum;
double gradeHigherZero;
double totalHours;
double medianHoursCertification;
double medianAge;
double percentMale;
double percentFemale;
double percentDegree;
public Course(String institution, String number, Date launchDate,
String title, String instructors, String subject,
int year, int honorCode, int participants,
int audited, int certified, double percentAudited,
double percentCertified, double percentCertified50,
double percentVideo, double percentForum, double gradeHigherZero,
double totalHours, double medianHoursCertification,
double medianAge, double percentMale, double percentFemale,
double percentDegree) {
this.institution = institution;
this.number = number;
this.launchDate = launchDate;
if (title.startsWith("\"")) {
title = title.substring(1);
}
if (title.endsWith("\"")) {
title = title.substring(0, title.length() - 1);
}
this.title = title;
if (instructors.startsWith("\"")) {
instructors = instructors.substring(1);
}
if (instructors.endsWith("\"")) {
instructors = instructors.substring(0, instructors.length() - 1);
}
this.instructors = instructors;
if (subject.startsWith("\"")) {
subject = subject.substring(1);
}
if (subject.endsWith("\"")) {
subject = subject.substring(0, subject.length() - 1);
}
this.subject = subject;
this.year = year;
this.honorCode = honorCode;
this.participants = participants;
this.audited = audited;
this.certified = certified;
this.percentAudited = percentAudited;
this.percentCertified = percentCertified;
this.percentCertified50 = percentCertified50;
this.percentVideo = percentVideo;
this.percentForum = percentForum;
this.gradeHigherZero = gradeHigherZero;
this.totalHours = totalHours;
this.medianHoursCertification = medianHoursCertification;
this.medianAge = medianAge;
this.percentMale = percentMale;
this.percentFemale = percentFemale;
this.percentDegree = percentDegree;
}
public String getInstitution() {
return institution;
}
public void setInstitution(String institution) {
this.institution = institution;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Date getLaunchDate() {
return launchDate;
}
public void setLaunchDate(Date launchDate) {
this.launchDate = launchDate;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getInstructors() {
return instructors;
}
public void setInstructors(String instructors) {
this.instructors = instructors;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getHonorCode() {
return honorCode;
}
public void setHonorCode(int honorCode) {
this.honorCode = honorCode;
}
public int getParticipants() {
return participants;
}
public void setParticipants(int participants) {
this.participants = participants;
}
public int getAudited() {
return audited;
}
public void setAudited(int audited) {
this.audited = audited;
}
public int getCertified() {
return certified;
}
public void setCertified(int certified) {
this.certified = certified;
}
public double getPercentAudited() {
return percentAudited;
}
public void setPercentAudited(double percentAudited) {
this.percentAudited = percentAudited;
}
public double getPercentCertified() {
return percentCertified;
}
public void setPercentCertified(double percentCertified) {
this.percentCertified = percentCertified;
}
public double getPercentCertified50() {
return percentCertified50;
}
public void setPercentCertified50(double percentCertified50) {
this.percentCertified50 = percentCertified50;
}
public double getPercentVideo() {
return percentVideo;
}
public void setPercentVideo(double percentVideo) {
this.percentVideo = percentVideo;
}
public double getPercentForum() {
return percentForum;
}
public void setPercentForum(double percentForum) {
this.percentForum = percentForum;
}
public double getGradeHigherZero() {
return gradeHigherZero;
}
public void setGradeHigherZero(double gradeHigherZero) {
this.gradeHigherZero = gradeHigherZero;
}
public double getTotalHours() {
return totalHours;
}
public void setTotalHours(double totalHours) {
this.totalHours = totalHours;
}
public double getMedianHoursCertification() {
return medianHoursCertification;
}
public void setMedianHoursCertification(double medianHoursCertification) {
this.medianHoursCertification = medianHoursCertification;
}
public double getMedianAge() {
return medianAge;
}
public void setMedianAge(double medianAge) {
this.medianAge = medianAge;
}
public double getPercentMale() {
return percentMale;
}
public void setPercentMale(double percentMale) {
this.percentMale = percentMale;
}
public double getPercentFemale() {
return percentFemale;
}
public void setPercentFemale(double percentFemale) {
this.percentFemale = percentFemale;
}
public double getPercentDegree() {
return percentDegree;
}
public void setPercentDegree(double percentDegree) {
this.percentDegree = percentDegree;
}
}