-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileParser.java
186 lines (168 loc) · 6.17 KB
/
FileParser.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
/**
* Copyright(C) 2008
* Verena Henrich <verenah08@ru.is>, Timo Reuter <timo08@ru.is>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 US
*/
package is.iclt.ctagger;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* This class parses files.
*
* @author Verena Henrich <verenah08@ru.is>, Timo Reuter <timo08@ru.is>
* @version 0.9
*/
public class FileParser {
private String row = null;
private FileReader fileReader = null;
private BufferedReader bufferedReader = null;
private static String tagNotFoundSymbol = "##";
private int numLines;
private int nonEmptyLines;
public FileParser() {
init();
}
private void init() {
numLines = 0;
nonEmptyLines = 0;
}
public int getNumLines() { return numLines;}
public int getNonEmptyLines() { return nonEmptyLines;}
/**
* This method parses an input file and returns the containing words.
*
* @param textFile Input file which is parsed. This file needs to contain one word
* per line.
* @return The words that are contained in the input file are returned.
*/
public List<String> parseFile(File textFile, boolean onlyFirstCol) {
System.out.println("parse file: " + textFile);
init();
List<String> wordsInFile = new ArrayList<String>();
try {
fileReader = new FileReader(textFile);
bufferedReader = new BufferedReader(fileReader);
row = bufferedReader.readLine();
// Iterate over each row
while (row != null) {
numLines++;
row = row.replaceAll("^[ \t]+", "");
if (row.length() > 0) {
nonEmptyLines++;
if (onlyFirstCol) {
String[] columns = row.split("[ \t]+");
wordsInFile.add(columns[0]);
}
else
wordsInFile.add(row);
}
else // row can be empty
wordsInFile.add("");
row = bufferedReader.readLine();
}
bufferedReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return wordsInFile;
}
/**
* A tagger input file is parsed. Every entry (that corresponds to a word) gets
* related to one tag out of the given tagset.
*
* @param textFile Input file from a tagger which is parsed. This file need to
* contain one "entry" per line. These entries should start with the specified
* word (optional) followed by the corresponding tag and optional other
* contents.
* @param posTagset This is the tagset which specifies the tags that are searched
* in the input file.
* @return A list is returned. Each entry includes a tag and
* corresponds to one word of the input file.
*/
public List<String> parseTaggerInput(File textFile, POSTagset posTagset) {
System.out.println("parse tagger file: " + textFile);
init();
List<String> wordsInFile = new ArrayList<String>();
try {
fileReader = new FileReader(textFile);
bufferedReader = new BufferedReader(fileReader);
row = bufferedReader.readLine();
String longestMatch = "";
if (posTagset != null) {
while (row != null) {
numLines++;
row = row.replaceAll("^[ \t]+", "");
if (row.length() > 2)
{
nonEmptyLines++;
// Search for part-of-speech tags
for (String tag : posTagset.getTags()) {
if (row.contains(tag) && longestMatch.length() < tag.length()) {
longestMatch = tag;
}
}
if (longestMatch == "") {
longestMatch = tagNotFoundSymbol;
}
wordsInFile.add(longestMatch);
longestMatch = "";
}
else // row can be empty
wordsInFile.add("");
row = bufferedReader.readLine();
}
}
else
{
while (row != null)
{
numLines++;
row = row.replaceAll("^[ \t]+", "");
if (row.length() > 2)
{
nonEmptyLines++;
String[] actualWordTag = row.split("[ \t]+");
if (actualWordTag.length < 2) {
System.out.println("Missing tag for word: " + actualWordTag[0]);
wordsInFile.add(tagNotFoundSymbol);
}
else
wordsInFile.add(actualWordTag[1]);
}
else // row can be empty
wordsInFile.add("");
row = bufferedReader.readLine();
}
}
bufferedReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return wordsInFile;
}
public static String getTagNotFoundSymbol() {
return tagNotFoundSymbol;
}
}