-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathParser.java
More file actions
155 lines (132 loc) · 4.82 KB
/
Copy pathParser.java
File metadata and controls
155 lines (132 loc) · 4.82 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
import java.io.*;
import java.util.stream.*;
import org.apache.commons.io.FileUtils;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
public class Parser
{
private String data;
// Number of files to parse, if not specified parse all
private int num = -1;
// Number of flies parsed in a single call to Parse()
private int num_parsed = 0;
public Parser(String[] args)
{
data = System.getenv("PROJECT_HOME") + File.separator + "data";
if(args.length > 1)
throw new IllegalArgumentException("There can be maximum of one command line argument");
else if(args.length == 1)
{
num = Integer.parseInt(args[0]);
}
}
private boolean Extract(Path path)
{
try(BufferedReader br = new BufferedReader(new FileReader(path.toString())))
{
String dir_parsed = data + File.separator + "parsed";
Files.createDirectories(Paths.get(dir_parsed));
String parsed_summary = dir_parsed + File.separator + "summary" + File.separator + path.getFileName().toString();
String parsed_text = dir_parsed + File.separator + "text" + File.separator + path.getFileName().toString();
try
{
File out_summary = new File(parsed_summary);
out_summary.getParentFile().mkdirs();
if(out_summary.exists())
return false;
out_summary.createNewFile();
BufferedWriter bw_summary = new BufferedWriter(new FileWriter(out_summary));
// Assuming there are at least two lines in each file
String line = br.readLine();
// Headline (which is typically first line) starts with
// "(CNN) --", remove it
String token = "(CNN)";
int idx = line.indexOf(token);
if(idx != -1)
line = line.substring(idx + token.length());
token = "-- ";
idx = line.indexOf(token);
if(idx != -1)
line = line.substring(idx + token.length());
bw_summary.write(line);
bw_summary.flush();
bw_summary.close();
File out_text = new File(parsed_text);
out_text.getParentFile().mkdirs();
if(out_text.exists())
return false;
out_text.createNewFile();
BufferedWriter bw_text = new BufferedWriter(new FileWriter(out_text));
line = br.readLine();
while(line != null && !line.equals("@highlight"))
{
if(line.length()>0)
{
bw_text.write(line + " ");
}
line = br.readLine();
}
bw_text.flush();
bw_text.close();
}
catch(Exception e)
{
throw e;
}
}
catch(Exception e)
{
System.out.println("Error during data extraction: " + e.getMessage());
}
num_parsed++;
if(num_parsed % 1000 == 0)
{
int x = num_parsed/1000;
System.out.println("Parsed " + Integer.toString(x) + "k files...");
}
return true;
}
public void Parse() throws Exception
{
num_parsed = 0;
String dir = data + File.separator + "stories";
FileUtils.cleanDirectory(new File(System.getenv("PROJECT_HOME") + File.separator + "data" + File.separator + "parsed" + File.separator + "summary"));
FileUtils.cleanDirectory(new File(System.getenv("PROJECT_HOME") + File.separator + "data" + File.separator + "parsed" + File.separator + "text"));
try(Stream<Path> paths = Files.walk( Paths.get(dir) ))
{
System.out.println("Parsing started...");
if(num == -1)
{
paths
.filter(Files::isRegularFile)
.forEach( path -> Extract(path) );
}
else
{
paths
.filter(Files::isRegularFile)
.filter( path -> Extract(path) )
.limit(num)
.forEach( path -> {} );
}
System.out.println(Integer.toString(num_parsed) + " files parsed.");
}
catch(Exception e)
{
throw e;
}
}
public static void main(String[] args) throws Exception
{
try
{
Parser P = new Parser(args);
P.Parse();
}
catch(Exception e)
{
System.out.println("Error during parsing: " + e.getMessage());
}
}
}