-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReview.java
More file actions
63 lines (53 loc) · 1.17 KB
/
Review.java
File metadata and controls
63 lines (53 loc) · 1.17 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
package io.github.test;
import java.util.*;
public class Review {
//instance fields
private String id;
private String review;
private HashMap<String, Double> tf;
private HashMap<String, Double> tfidf;
/**
* Constructor
*/
public Review(String id, String review) {
this.id = id;
this.review = review;
tf = new HashMap<String, Double>();
tfidf = new HashMap<String, Double>();
}
public String getID() {
return id;
}
public String getReview() {
return review;
}
public HashMap<String, Double> getTF() {
return tf;
}
public HashMap<String, Double> getTFIDF() {
return tfidf;
}
/**
* Updates a review's term-frequency values
*/
public void updateTFs() {
String[] review_text = review.toLowerCase().split("\\W+");
for (int i = 1; i < review_text.length; i++) {
if (!tf.containsKey(review_text[i])) {
tf.put(review_text[i], 1.0);
}
else {
tf.put(review_text[i], 1.0 + tf.get(review_text[i]));
}
}
for (String word : tf.keySet()) {
tf.put(word, 1.0 + Math.log10(tf.get(word)));
}
}
/**
* Returns Review in json format
*/
public String toString() {
return "{id: " + id + ", review: " + review + "}";
}
}