forked from AdamMeyers/The_Termolator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetNounChunks.java
181 lines (127 loc) · 4.63 KB
/
getNounChunks.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
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
//USER INPUT: file containing list of all foreground files (having been chunked by TreeTagger)
public class getNounChunks {
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, IOException {
boolean start = false;
boolean endNP = false;
boolean APstart = false;
//open file
String inname = args[0];
File in = new File(inname);
String[] dirnamesplit = inname.split("\\.");
Scanner read = new Scanner(in, "UTF-8");
//new dir file to write all new terms files to
String outname = dirnamesplit[0] + ".chunklist";
OutputStreamWriter listwrite = new OutputStreamWriter(new FileOutputStream(outname), Charset.forName("UTF-8").newEncoder());
//for every line in input file aka for every doc
while (read.hasNextLine()) {
//extract docname from list
//remove path components and ext
String fulldocname = read.nextLine();
//System.out.println("This line: " + fulldocname);
//System.out.println(fulldocname.charAt(0));
if (fulldocname.equals("")) {
continue;
}
//identify if first character of file is a weird char due to encoding
if ((fulldocname.length() != 0) && !Character.isLetterOrDigit(fulldocname.charAt(0))) {
//System.out.println("y");
fulldocname = fulldocname.substring(1);
}
String[] namesplit = fulldocname.split("/|\\.");
//System.out.println("Split name: " + Arrays.deepToString(namesplit));
String docname = namesplit[namesplit.length - 2];
//System.out.println("Bare doc name: " + docname);
String pathname = "";
for (int i = 0; i < namesplit.length - 2; i++) {
pathname += (namesplit[i] + "/");
}
//System.out.println("Path name: " + pathname);
String ext = namesplit[namesplit.length - 1];
//System.out.println("Extension: " + ext);
File thisdoc = new File(fulldocname);
Scanner doc = new Scanner(thisdoc, "UTF-8");
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(pathname + docname + ".chunks"), Charset.forName("UTF-8").newEncoder());
start = false;
endNP = false;
APstart = false;
String chunk = "";
String[] thisLine;
while(doc.hasNextLine()) {
thisLine = doc.nextLine().split("\t");
//write to file if have endNP and APstart
if (thisLine[0].equals("</AP>")) {
APstart = false;
//remove trailing punctuation
if (chunk.length() > 1 && !Character.isLetter(chunk.charAt(chunk.length()-1))) {
chunk = chunk.substring(0, chunk.length()-1);
}
//remove bugs from nounchunker
else if (chunk.length() > 5 && chunk.substring(chunk.length()-5).equals("et/ou")){
chunk = chunk.substring(0, chunk.length()-5);
}
//System.out.println(chunk);
if (!chunk.isEmpty()) {
writer.write(chunk + "\r\n");
}
chunk = "";
endNP = false;
}
if (thisLine[0].equals("</NP>")) {
start = false;
endNP = true;
continue;
}
if (start || APstart) {
if (thisLine[0].length() == 1) {
if (Character.isLetter(thisLine[0].charAt(0))) {
chunk = chunk.concat(" " + thisLine[0]);
}
}
else {
chunk = chunk.concat(thisLine[0] + " ");
}
}
if (thisLine[0].equals("<NP>")) {
start = true;
endNP = false;
}
if (thisLine[0].equals("<AP>")) {
if (endNP) {
APstart = true;
}
}
if (endNP) {
if (!APstart) { //no AP: write
//remove trailing punctuation
if (chunk.length() > 1 && !Character.isLetter(chunk.charAt(chunk.length()-1))) {
chunk = chunk.substring(0, chunk.length()-1);
}
//remove bugs from nounchunker
else if (chunk.length() > 5 && chunk.substring(chunk.length()-5).equals("et/ou")){
chunk = chunk.substring(0, chunk.length()-5);
}
//System.out.println(chunk);
if (!chunk.isEmpty()) {
writer.write(chunk + "\r\n");
}
chunk = "";
endNP = false;
}
}
}
writer.flush();
writer.close();
doc.close();
//write new chunk file name to dir file
listwrite.write(pathname + docname +".chunks");
listwrite.write("\n");
}
listwrite.flush();
listwrite.close();
read.close();
}
}