ExtractParagraphs.java
1.72 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
package is2.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.StringTokenizer;
public class ExtractParagraphs {
/**
* @param args
* @throws IOException
*/
public static void main(String args[]) throws IOException {
if (args.length<1) {
System.out.println("Please provide a file name.");
System.exit(0);
}
File file = new File(args[0]);
file.isDirectory();
String[] dirs = file.list();
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[1]),"UTF-8"),32768);
int cnt=0;
for (String fileName : dirs) {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]+fileName),"UTF-8"),32768);
int state =0;
String s;
while ((s = reader.readLine()) != null) {
if (s.startsWith("<P>")||s.startsWith("<p>")) {
state=1; // paragraph start
continue;
}
if (s.startsWith("</P>")||s.startsWith("</p>")) {
state=2; // paragraph end
write.newLine();
}
boolean lastNL =false;
if (state==1) {
String sp[] = s.split("\\. ");
for(String p : sp) {
write.write(p);
// if (sp.length>1) write.newLine();
}
cnt++;
}
}
//if (cnt>5000) break;
reader.close();
}
write.flush();
write.close();
System.out.println("Extract "+cnt+" lines ");
}
}