-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFinalPipeline.java
206 lines (168 loc) · 5.71 KB
/
FinalPipeline.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
package finalProject.fp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class FinalPipeline {
public static void main(String[] args) {
String pathOriginal = "src/main/resources/original/";
String pathModified = "src/main/resources/modified/";
String pathTerm = "src/main/resources/term/";
File file = new File(pathOriginal);
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
modify(pathOriginal+filelist[i],filelist[i]);
}
File termFile = new File(pathTerm+"word_index");
List<String> terms = new ArrayList<>();
try {
BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(termFile)));
String line = "";
while((line=reader.readLine())!=null) {
terms.add(line.split(" ")[1]);
}
}
catch (IOException e)
{
e.printStackTrace();
}
List<List<List<String>>> textss = new ArrayList<>();
for (int i = 0; i < filelist.length; i++) {
try {
File fileModified = new File(pathModified);
String[] filelistModified = fileModified.list();
BufferedReader aReader=new BufferedReader(new InputStreamReader(new FileInputStream(pathModified+filelistModified[i])));
String line = "";
line = aReader.readLine();
List<List<String>> texts = new ArrayList<>();
while(line.length()>0) {
line = aReader.readLine();
}
List<String> paragraph = new ArrayList<>();
while((line = aReader.readLine())!=null) {
if(line.length()!=0) {
String[] s = line.split(" ");
for(int t = 0;t<s.length;t++) {
paragraph.add(s[t]);
}
texts.add(paragraph);
paragraph = new ArrayList<>();
paragraph.clear();
}
line = aReader.readLine();
}
textss.add(texts);
texts = new ArrayList<>();
}
catch (IOException e)
{
e.printStackTrace();
}
}
List<List<List<Double>>> tfIdfForAll = new ArrayList<>();
for(List<List<String>> texts: textss) {
List<List<Double>> tfIdfForFile = new ArrayList<>();
for(String term: terms) {
List<Double> tfIdfForTerm = tfIdf(texts, term);
System.out.println(tfIdfForTerm);
tfIdfForFile.add(tfIdfForTerm);
}
tfIdfForAll.add(tfIdfForFile);
tfIdfForFile = new ArrayList<>();
}
}
public static List<Double> tfIdf(List<List<String>> texts, String term)
{
double wordAppear = 0;
double docAppear = 0;
double wordSum = 0;
List<Double> list = new ArrayList<>();
for(int i=0;i<texts.size();i++) {
for(String word: texts.get(i)) {
if(word.equalsIgnoreCase(term)) {
docAppear++;
break;
}
}
}
for(int i = 0;i<texts.size();i++) {
for(String word: texts.get(i)) {
if(word.equalsIgnoreCase(term)) {
wordAppear++;
}
wordSum++;
}
if(wordAppear>0) {
list.add(1+Math.log10(wordAppear));
}
else {
list.add(0.0);
}
wordAppear = 0;
wordSum = 0;
}
for(int i = 0; i<list.size();i++) {
list.set(i, list.get(i)*Math.log10(texts.size()/docAppear));
}
return list;
// System.out.println(wordAppear+" "+wordSum+" "+docAppear+" "+texts.size());
// if(wordAppear>0) {
// return Math.log10(texts.size()/docAppear)*(1+Math.log10(wordAppear/wordSum));
// }
// return 0;
}
public static void modify(String path, String fileName)
{
try {
String overallRating = "";
String location = "";
String hotelName = "";
boolean noURL = false;
BufferedReader aReader=new BufferedReader(new InputStreamReader(new FileInputStream(path)));
List<String[]> words = new ArrayList<String[]>();
String line = "";
line = aReader.readLine();
overallRating = line.split(">")[1];
while((line=aReader.readLine()).length()<6|!(line.contains("<URL>"))) {
if(line.contains("<Author>")) {
noURL = true;
break;
}
}
if(line.length()!=0&&!noURL) {
line = line+aReader.readLine();
String[] locationExtract = line.split("-");
location = locationExtract[locationExtract.length-1].split("\\.")[0];
hotelName = locationExtract[locationExtract.length-2];
if(locationExtract.length==1) {
System.out.println(fileName);
}
}
String path0 = "src/main/resources/modified/" + fileName;
File newFile = new File(path0);
newFile.createNewFile();
BufferedWriter writer = new BufferedWriter(new FileWriter(newFile));
writer.write("hotelName "+hotelName+"\n");
writer.write("location: "+location+"\n");
writer.write("overallRating "+overallRating+"\n");
writer.write("\n");
while((line = aReader.readLine())!=null) {
if(!(line.length()>2&&line.substring(0, 1).equals("<")&&!line.contains("<Content>"))) {
writer.write(line+"\n");
}
}
writer.flush();
writer.close();
aReader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}