-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseMessages.java
320 lines (282 loc) · 10.5 KB
/
ParseMessages.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
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
package parser;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import entities.DataParser;
import entities.ForumPost;
/**
* Aggregates all mesage files to 1 big CSV file contianing UID, Message and stems:
*
* Stemming:
* Take all crawl results and chunk them in subfiles DIR_CHUNK/i.txt
* Remove special characters, authornames etc to DIR_PARSE/i.txt
* Stem the messages to DIR_CHUNK/i.txt.stemmed
* Aggregate everthing
* @author ejdfortu
*/
public class ParseMessages {
private static String DIR_OUTPUT= "output/";
private static String DIR_CRAWL = "output/crawled/";
private static String DIR_CHUNK = "output/chunked/";
private static String DIR_PARSE = "output/parsed/";
private static int MAX_CHUNKS = 500;
public static void main(String[] args) throws Exception {
createAggregateFile2();
if(true) return;
while(true) {
cleanUp();
copyMessages();
chunkMessages();
parseChunkedMessages();
createAggregateFile();
cleanUp();
System.out.println("Sleeping 30 minutes");
Thread.sleep(30*60*1000);
}
}
/********************************************************************************************************
*
* Utility functions
*
*********************************************************************************************************/
public static void copyMessages() throws IOException {
FileUtils.deleteDirectory(new File(DIR_CRAWL));
FileUtils.copyDirectory(new File("data"), new File(DIR_CRAWL));
}
public static void cleanUp() throws IOException {
FileUtils.deleteDirectory(new File(DIR_CRAWL));
} //TODO: implement
/*
Write results to short format [USERID, THREADID, MESSAGE]
In order to ensure mapping back, remember as well[USERID,USERNAME]
*/
public static void createAggregateFile2() throws IOException {
List<String> topics = new ArrayList<String>();
List<String> users = new ArrayList<String>();
FileOutputStream fstream = new FileOutputStream(DIR_OUTPUT+"dataset_users.csv");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fstream,"ASCII"));
FileOutputStream fstream2 = new FileOutputStream(DIR_OUTPUT+"dataset_short.csv");
BufferedWriter out2 = new BufferedWriter(new OutputStreamWriter(fstream2,"ASCII"));
for(int i = 0; i < MAX_CHUNKS; i++) {
System.out.println("Parsing chunk "+i);
File f = new File(DIR_PARSE + i + ".txt.stemmed");
if(f.exists()) {
FileInputStream lstream = new FileInputStream(f);
DataInputStream in = new DataInputStream(lstream);
String strLine="";
while ((strLine = in.readLine()) != null) {
String[] fields = strSplitNoQuotes(strLine);//strLine.split(";");
if(fields.length == 6) {
ForumPost fp = new ForumPost(Integer.parseInt(fields[1]),fields[4],fields[3],fields[5]);
fp.threadtitle = fields[2];
int topicid = topics.indexOf(fp.threadtitle);
if(topicid == -1) {
topics.add(fp.threadtitle);
topicid = topics.indexOf(fp.threadtitle);
}
if(!users.contains(fields[4])) {
users.add(fields[4]);
out.write(users.size() + "\t"+fields[4]+"\n");
}
out2.write(topicid+";"+fp.posterid+";"+fp.inhoud+"\n");
}
else
System.out.print("|");
}
in.close();
}
out2.write("\n");
}
out2.close();
out.close();
}
//Old version, less efficient because contains all data
public static void createAggregateFile() throws IOException {
FileOutputStream fstream2 = new FileOutputStream(DIR_OUTPUT+"dataset.csv");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fstream2,"ASCII"));
for(int i = 0; i < MAX_CHUNKS; i++) {
File f = new File(DIR_PARSE + i + ".txt.stemmed");
if(f.exists()) {
FileInputStream fstream = new FileInputStream(f);
DataInputStream in = new DataInputStream(fstream);
String strLine="";
while ((strLine = in.readLine()) != null) {
out.write(strLine+"\n");
}
in.close();
}
out.write("\n");
}
out.close();
}
/********************************************************************************************************
*
* CHUNKING
* Chunk in parts of linesize 1000
*
*********************************************************************************************************/
public static void chunkMessages() throws IOException {
int n_chunks = 0;
ArrayList<String> lines = new ArrayList<String>();
System.out.print("Chunking files.");
for (int i = 614; i > 0; i--) {
//Does there already exist a crawl result? chunk!
File crawFile = new File(DIR_CRAWL + "save" + i + ".csv");
if(crawFile.exists()) {
System.out.print(i+" ");
FileInputStream fstream = new FileInputStream(crawFile);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
lines.add(strLine);
if(lines.size() == 1000) {
File chunkFile =new File(DIR_CHUNK+n_chunks+".txt");
if(!chunkFile.exists()) {
//Save
FileOutputStream fstream2 = new FileOutputStream(chunkFile);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fstream2,"ASCII"));
for(String line : lines)
out.write(line+"\n");
System.out.println("chunked! "+chunkFile);
}
System.out.println(" .");
n_chunks++;
lines.clear();
}
}
}
}
System.out.println(" all chunking completed.");
}
/********************************************************************************************************
*
* PARSING
* Parse chunked files
*
*********************************************************************************************************/
private static ArrayList<String> forbidden = new ArrayList<String>();
public static void parseChunkedMessages() throws Exception {
int N_FILES = MAX_CHUNKS;
int start_i = 0;
for( ; start_i <= N_FILES; start_i++)
if(! new File(DIR_PARSE+start_i+".csv").exists())
break;
System.out.println("Parsing files "+start_i+" to "+N_FILES);
for(int i = start_i; i <= N_FILES;i++) {
if(new File(DIR_CHUNK+i+".txt").exists()) {
System.out.println("--- "+start_i+"+"+i+"/"+N_FILES+" ---");
//Stemming in python (catch things like don't, can't, ...)
stemChunkFile(DIR_CHUNK+i+".txt");
//Remove weird characters now
parseChunkedMessages(i);
//Stem again to make sure we didn't miss anything
stemChunkFile(DIR_PARSE+i+".txt");
}
}
}
public static void parseChunkedMessages(int i) throws Exception {
FileInputStream in = new FileInputStream(new File(DIR_CHUNK+i+".txt.stemmed"));
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
FileOutputStream fstream2 = new FileOutputStream(DIR_PARSE+i+".txt");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fstream2,"ASCII"));
while ((strLine = br.readLine()) != null) {
//Step 1: Find authornames to remove
int idx1 = strLine.indexOf(";") +1;
idx1 = strLine.indexOf(";",idx1)+1;
idx1 = strLine.indexOf("\"",idx1)+1;
idx1 = strLine.indexOf("\"",idx1)+1;
idx1 = strLine.indexOf(";",idx1)+1;
idx1 = strLine.indexOf(";",idx1)+1;
int idx2 = strLine.indexOf(";",idx1);
if(idx2 > idx1+1) {
String author = strLine.substring(idx1,idx2);
forbidden.add(filterSpecial(author));
//Step 2: Remove special characters and author names
String strText = strLine.substring(idx2+1);
strText = filterSpecial(strText);
for(String forbid : forbidden)
strText.replaceAll(forbid, "");
String outString = strLine.substring(0,idx2) + ";"+strText;
out.write(outString+"\n");
}
}
out.close();
}
//Remove all special characters (also use regexp to neuralize)
public static String filterSpecial(String strText) throws Exception {
strText = strText.replaceAll(";", " ");
strText = strText.replaceAll("['|]", "");
strText = strText.replaceAll("[?!\"--()\\[\\],.°ø®¤¸£ââⴽ~{©}<>@/
:â]"," ");
strText = strText.replaceAll("[ ][ ]+"," ");
strText = strText.toLowerCase();
return strText;
}
public static String stemChunkFile(String chunkfile) throws Exception {
//Apply python stemming library for Dutch and pipe results
String command = "python src/python/stem2.py "+chunkfile;
runCMD(command,false);
//Read result
InputStream imessagestream = DataParser.class.getResourceAsStream("/tmp/tmpmessage.txt.out");
BufferedReader imessagein = new BufferedReader(new InputStreamReader(new DataInputStream(imessagestream)));
String stemmed = "";
String strMessage = "";
while((strMessage = imessagein.readLine()) != null) {
stemmed = stemmed + " "+strMessage;
}
imessagestream.close();
return stemmed;
}
public static void runCMD(String cmd,boolean verbose) {
try {
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("cmd.exe /c "+cmd);
if(verbose) {
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;
while((line=input.readLine()) != null) {
System.out.println(line);
}
}
int exitVal = pr.waitFor();
if(verbose)
System.out.println("Exited with error code "+exitVal);
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
//Split a string in pieces, but don't parse anything in quotes
public static String[] strSplitNoQuotes(String line) {
String[] chunks = line.split(";");
if(chunks.length > 6) {
System.out.println(chunks);
//Merge topics that may not have been badly encoded
String id = chunks[1];
int i;
String topic="";
for(i=2; !topic.endsWith("\""); i++) {
topic = topic + chunks[i];
}
String date = chunks[i];
String author = chunks[i+1];
String text ="";
for(i = i+2; i < chunks.length;i++) {
text += chunks[i];
}
chunks = new String[]{"",id,topic,date,author,text};
}
return chunks;
}
}