-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathRDRsegmenter.java
executable file
·317 lines (270 loc) · 10.7 KB
/
RDRsegmenter.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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author DatQuocNguyen
*
*/
public class RDRsegmenter
{
private Node root;
public RDRsegmenter()
throws IOException
{
this.constructTreeFromRulesFile("Model.RDR");
}
private void constructTreeFromRulesFile(String rulesFilePath)
throws IOException
{
BufferedReader buffer = new BufferedReader(
new InputStreamReader(new FileInputStream(new File(rulesFilePath)), "UTF-8"));
String line = buffer.readLine();
this.root = new Node(new FWObject(false), "NN", null, null, null, 0);
Node currentNode = this.root;
int currentDepth = 0;
for (; (line = buffer.readLine()) != null;) {
int depth = 0;
for (int i = 0; i <= 6; i++) { // Supposed that the maximum
// exception level is up to 6.
if (line.charAt(i) == '\t')
depth += 1;
else
break;
}
line = line.trim();
if (line.length() == 0)
continue;
if (line.contains("cc:"))
continue;
FWObject condition = Utils.getCondition(line.split(" : ")[0].trim());
String conclusion = Utils.getConcreteValue(line.split(" : ")[1].trim());
Node node = new Node(condition, conclusion, null, null, null, depth);
if (depth > currentDepth) {
currentNode.setExceptNode(node);
}
else if (depth == currentDepth) {
currentNode.setIfnotNode(node);
}
else {
while (currentNode.depth != depth)
currentNode = currentNode.fatherNode;
currentNode.setIfnotNode(node);
}
node.setFatherNode(currentNode);
currentNode = node;
currentDepth = depth;
}
buffer.close();
}
private Node findFiredNode(FWObject object)
{
Node currentN = root;
Node firedN = null;
while (true) {
if (currentN.satisfy(object)) {
firedN = currentN;
if (currentN.exceptNode == null) {
break;
}
else {
currentN = currentN.exceptNode;
}
}
else {
if (currentN.ifnotNode == null) {
break;
}
else {
currentN = currentN.ifnotNode;
}
}
}
return firedN;
}
// An initial word segmenter based on longest matching
public List<WordTag> getInitialSegmentation(String sentence)
{
List<WordTag> wordtags = new ArrayList<WordTag>();
for (String regex : Utils.NORMALIZER_KEYS)
if (sentence.contains(regex))
sentence = sentence.replaceAll(regex, Utils.NORMALIZER.get(regex));
List<String> tokens = Arrays.asList(sentence.split("\\s+"));
List<String> lowerTokens = Arrays.asList(sentence.toLowerCase().split("\\s+"));
int senLength = tokens.size();
int i = 0;
while (i < senLength) {
String token = tokens.get(i);
if (token.chars().allMatch(Character::isLetter)) {
if (Character.isLowerCase(token.charAt(0)) && (i + 1) < senLength) {
if (Character.isUpperCase(tokens.get(i + 1).charAt(0))) {
wordtags.add(new WordTag(token, "B"));
i++;
continue;
}
}
boolean isSingleSyllabel = true;
for (int j = Math.min(i + 4, senLength); j > i + 1; j--) {
String word = String.join(" ", lowerTokens.subList(i, j));
if (Vocabulary.VN_DICT.contains(word) || Vocabulary.VN_LOCATIONS.contains(word)
|| Vocabulary.COUNTRY_L_NAME.contains(word)) {
wordtags.add(new WordTag(token, "B"));
for (int k = i + 1; k < j; k++)
wordtags.add(new WordTag(tokens.get(k), "I"));
i = j - 1;
isSingleSyllabel = false;
break;
}
}
if (isSingleSyllabel) {
String lowercasedToken = lowerTokens.get(i);
if (Vocabulary.VN_FIRST_SENT_WORDS.contains(lowercasedToken)
|| Character.isLowerCase(token.charAt(0))
|| token.chars().allMatch(Character::isUpperCase)
|| Vocabulary.COUNTRY_S_NAME.contains(lowercasedToken)
|| Vocabulary.WORLD_COMPANY.contains(lowercasedToken)) {
wordtags.add(new WordTag(token, "B"));
i++;
continue;
}
// Capitalized
int ilower = i + 1;
for (ilower = i + 1; ilower < Math.min(i + 4, senLength); ilower++) {
String ntoken = tokens.get(ilower);
if (Character.isLowerCase(ntoken.charAt(0))
|| !ntoken.chars().allMatch(Character::isLetter)
|| ntoken.equals("LBKT") || ntoken.equals("RBKT")) {
break;
}
}
if (ilower > i + 1) {
boolean isNotMiddleName = true;
if (Vocabulary.VN_MIDDLE_NAMES.contains(lowercasedToken) && (i >= 1)) {
String prevT = tokens.get(i - 1);
if (Character.isUpperCase(prevT.charAt(0))) {
if (Vocabulary.VN_FAMILY_NAMES.contains(prevT.toLowerCase())) {
wordtags.add(new WordTag(token, "I"));
isNotMiddleName = false;
}
}
}
if (isNotMiddleName)
wordtags.add(new WordTag(token, "B"));
for (int k = i + 1; k < ilower; k++)
wordtags.add(new WordTag(tokens.get(k), "I"));
i = ilower - 1;
}
else {
wordtags.add(new WordTag(token, "B"));
}
}
}
else {
wordtags.add(new WordTag(token, "B"));
}
i++;
}
return wordtags;
}
public String segmentTokenizedString(String str)
throws IOException
{
StringBuilder sb = new StringBuilder();
String line = str.trim();
if (line.length() == 0) {
return "\n";
}
List<WordTag> wordtags = this.getInitialSegmentation(line);
int size = wordtags.size();
for (int i = 0; i < size; i++) {
FWObject object = Utils.getObject(wordtags, size, i);
Node firedNode = findFiredNode(object);
if (firedNode.depth > 0) {
if (firedNode.conclusion.equals("B"))
sb.append(" " + wordtags.get(i).form);
else
sb.append("_" + wordtags.get(i).form);
}
else {// Fired at root, return initialized tag
if (wordtags.get(i).tag.equals("B"))
sb.append(" " + wordtags.get(i).form);
else
sb.append("_" + wordtags.get(i).form);
}
}
return sb.toString().trim();
}
public String segmentRawString(String str)
throws IOException
{
return segmentTokenizedString(String.join(" ", Tokenizer.tokenize(str)));
}
public void segmentTokenizedCorpus(String inFilePath)
throws IOException
{
BufferedReader buffer = new BufferedReader(
new InputStreamReader(new FileInputStream(new File(inFilePath)), "UTF-8"));
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(inFilePath + ".WS"), "UTF-8"));
for (String line; (line = buffer.readLine()) != null;) {
bw.write(segmentTokenizedString(line) + "\n");
}
buffer.close();
bw.close();
}
public void segmentRawCorpus(String inFilePath)
throws IOException
{
BufferedReader buffer = new BufferedReader(
new InputStreamReader(new FileInputStream(new File(inFilePath)), "UTF-8"));
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(inFilePath + ".WSeg"), "UTF-8"));
for (String line; (line = buffer.readLine()) != null;) {
// bw.write(segmentTokenizedString(String.join(" ", Tokenizer.tokenize(line))) + "\n");
for (String sentence : Tokenizer.joinSentences(Tokenizer.tokenize(line))) {
bw.write(segmentTokenizedString(sentence) + "\n");
}
}
buffer.close();
bw.close();
}
public void segmentDirectory(String inDirectoryPath, String suffix)
throws IOException
{
File directoryPath = new File(inDirectoryPath);
if (!directoryPath.exists()) {
throw new FileNotFoundException("Requested directory " + directoryPath + " does not exist!");
}
FilenameFilter textFilefilter = new FilenameFilter(){
public boolean accept(File dir, String name) {
if (name.endsWith(suffix)) {
return true;
} else {
return false;
}
}
};
File filesList[] = directoryPath.listFiles(textFilefilter);
for(File file : filesList) {
segmentRawCorpus(file.getAbsolutePath());
}
}
public static void main(String[] args)
throws IOException
{
RDRsegmenter segmenter = new RDRsegmenter();
// segmenter.segmentRawCorpus(args[0]);
segmenter.segmentDirectory(args[0], args[1]);
// Get output of input test set for evaluation
// segmenter.segmentTokenizedCorpus("data/Test.txt");
}
}