forked from Early-Modern-OCR/RETAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextPreprocessor.java
executable file
·248 lines (215 loc) · 8.07 KB
/
TextPreprocessor.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
/* Copyright (C) <2013> University of Massachusetts Amherst
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 3 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, see <http://www.gnu.org/licenses/>.
*/
/**
*
* @author Ismet Zeki Yalniz
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Locale;
public abstract class TextPreprocessor {
public Locale locale = null;
public static String IGNORED_CHARS = ".,;:=-+/'`&|$#@!%^*()[]_\"}{\\<>?~";
public abstract String processText(String s);
public abstract boolean isValidChar(char a);
public String toLowerCase(String s) {
return s.toLowerCase(locale);
}
public static String getEncoding(String filename) {
FileInputStream fi;
InputStreamReader ir;
String encoding = "";
try {
fi = new FileInputStream(filename);
ir = new InputStreamReader(fi);
encoding = ir.getEncoding();
ir.close();
fi.close();
} catch (IOException ex) {
System.out.println("TextPreprocessor: can not read file ->" + filename);
}
// System.out.println("encoding = " + encoding);
return encoding;
}
public static void convertFileFormat(String inputFile, String outputFile, String inputEncoding, String outputEncoding) {
Reader input = null;
try {
input = new InputStreamReader(new FileInputStream(inputFile), inputEncoding);
Writer output = new OutputStreamWriter(new FileOutputStream(outputFile), outputEncoding);
char[] buffer = new char[1000];
int charsRead;
while ((charsRead = input.read(buffer)) != -1) {
output.write(buffer, 0, charsRead);
}
input.close();
output.close();
} catch (Exception ex) {
} finally {
try {
input.close();
} catch (IOException ex) {
}
}
}
public static ArrayList<String> readFilenames(String inputfilename, int from, int to) {
BufferedReader reader = null;
ArrayList<String> list = new ArrayList<String>(1000);
String line = "";
try {
reader = new BufferedReader(new FileReader(new File(inputfilename)));
int count = 0;
while ((line = reader.readLine()) != null) {
if (count == to) {
break;
} else {
if (!line.equals("") && count >= from) {
list.add(line);
}
}
count++;
}
} catch (Exception e) {
}
return list;
}
public static String[] getFilenames(String folder, String contains) {
File folderfile = new File(folder);
File[] files = null;
MyFileFilter filter = new MyFileFilter(contains);
String result[] = null;
if (folderfile.isDirectory()) {
files = folderfile.listFiles(filter);
result = new String[files.length];
for (int i = 0; i < files.length; i++) {
result[i] = files[i].getAbsolutePath();
}
} else {
System.out.println("wrong input folder for foreign language books");
}
return result;
}
public static ArrayList<String> getFilenamesRecursive(String rootFolder) {
ArrayList<String> result = new ArrayList<String>(10000);
File[] files = null;
// MyFileFilter filter = new MyFileFilter(contains);
File folderfile = new File(rootFolder);
if (folderfile.isDirectory()) {
files = folderfile.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
result.addAll(getFilenamesRecursive(files[i].getAbsolutePath()));
} else {
String filename = files[i].getAbsolutePath();
if (filename.substring(filename.lastIndexOf('.') + 1).equalsIgnoreCase("unique")) {
result.add(filename);
}
}
}
} else {
System.out.println("TextPreprocessor:getFilenamesRecursive: input folder is not valid");
}
return result;
}
public static boolean isWesternEuropeanLanguage(String s) {
//Portuguese, French, German, Dutch, English, Danish, Swedish, Norwegian, and Icelandic,
if (s.equalsIgnoreCase("english")
|| s.equalsIgnoreCase("french")
|| s.equalsIgnoreCase("spanish")
|| s.equalsIgnoreCase("italian")
|| s.equalsIgnoreCase("latin")
|| s.equalsIgnoreCase("german")
|| s.equalsIgnoreCase("portuguese")
|| s.equalsIgnoreCase("dutch")
|| s.equalsIgnoreCase("danish")
|| s.equalsIgnoreCase("swedish")
|| s.equalsIgnoreCase("norwegian")
|| s.equalsIgnoreCase("icelandic")) {
return true;
}
return false;
}
public static String extractFilename(String s) {
if (s == null || s.equals("")) {
return "";
} else {
int start = s.lastIndexOf("\\");
if (start == -1) {
start = s.lastIndexOf('/');
} else {
start++;
}
if (start == -1) {
start = 0;
}
int end = s.lastIndexOf('.');
if (end == -1) {
end = s.length();
}
return s.substring(start, end);
}
}
public static String readFile(String filename) {
File file = new File(filename);
return readFile(file);
}
public static String readFile(File file) {
if (file == null || !file.exists()) {
System.out.println("TextPreprocessor: Can not read the file " + file.getAbsolutePath());
return null;
}
StringBuffer bufc = null;
FileReader fr = null;
try {
fr = new FileReader(file, StandardCharsets.UTF_8);
int theChar;
bufc = new StringBuffer();
while (((theChar = fr.read()) != -1)) {
bufc.append((char) theChar);
}
} catch (IOException ioe) {
System.out.println("TextPreprocessor: Can not read the file " + file.getAbsolutePath());
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException ioe) {
}
}
}
fr = null;
return bufc.toString();
}
public static class MyFileFilter implements FileFilter {
String contains = "";
public MyFileFilter(String c) {
contains = c;
}
public boolean accept(File file) {
if (file.getName().toLowerCase().contains(contains)) {
return true;
}
return false;
}
}
}