/
TunnistinPalveluFast.java
429 lines (358 loc) · 11.6 KB
/
TunnistinPalveluFast.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
HeLIfast, general purpose language identifier for digital text
See: Jauhiainen, Lindén, Jauhiainen 2016: "HeLI, a Word-Based Backoff Method for Language Identification" In Proceedings of the 3rd Workshop on Language Technology for Closely Related Languages, Varieties and Dialects (VarDial)
Copyright (C) 2019 Tommi Jauhiainen
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.io.*;
import java.util.*;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.google.common.collect.Table.Cell;
import java.lang.Math.*;
import java.io.*;
import java.net.*;
import java.security.*;
class TunnistinPalveluFast {
// global table holding the language models for all the languages
private static Table<String, String, Double> gramDict;
private static Table<String, String, Double> wordDict;
// global variable containing all the languages known by the identifier
private static List<String> kielilista = new ArrayList<String>();
// These variables should be moved to a configuration file
// They set the relative frequency to read from each model file
private static double usedmonos = 0.0000005;
private static double usedbis = 0.0000005;
private static double usedtris = 0.0000005;
private static double usedquads = 0.0000005;
private static double usedcinqs = 0.0000005;
private static double usedwords = 0.0000005;
// this is the penalty value for unseen tokens
private static double gramsakko = 7.0;
// This is the maximum length of used character n-grams (setting them to 0 gives the same outcome, but the identifier still divides the words)
private static int maksimingram = 5;
private static int port=8082, maxConnections=0;
// We read the file languagelist which includes list of the languages to be included in the repertoire of the language identifier.
public static void main(String[] args) {
File file = new File("languagelist");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null) {
kielilista.add(text);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
}
}
gramDict = HashBasedTable.create();
wordDict = HashBasedTable.create();
ListIterator gramiterator = kielilista.listIterator();
while(gramiterator.hasNext()) {
Object element = gramiterator.next();
String kieli = (String) element;
kayLapi(usedmonos, kieli, "mono");
kayLapi(usedbis, kieli, "bi");
kayLapi(usedtris, kieli, "tri");
kayLapi(usedquads, kieli, "quad");
kayLapi(usedcinqs, kieli, "cinq");
kayLapi(usedwords, kieli, "word");
}
System.out.println("Ready to accept queries.");
doServe();
}
private static void kayLapi(double usedgrams, String kieli, String tyyppi) {
Table<String, String, Double> tempDict;
tempDict = HashBasedTable.create();
String seuraava = null;
String pituustiedosto = null;
if (tyyppi.equals("word")) {
seuraava = "LanguageModels/" + kieli + ".wordcount";
pituustiedosto = "LanguageModels/" + kieli + ".words.count";
}
else {
seuraava = "LanguageModels/" + kieli + "-" + tyyppi + ".X";
pituustiedosto = "LanguageModels/" + kieli + "-" + tyyppi + ".count";
}
double grampituus = 0;
double langamount = 0;
File file = new File(pituustiedosto);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null) {
grampituus = Double.parseDouble(text);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
}
}
file = new File(seuraava);
reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null) {
String gram = text;
if (tyyppi.equals("word")) {
gram = gram.replaceAll(".*[1-90]", "");
gram = gram.replaceAll("$", " ");
}
else {
gram = text.replaceAll(".*[1-90]=", "");
}
int amount = Integer.parseInt(text.replaceAll("[^1-90]", ""));
if (amount/grampituus > usedgrams) {
tempDict.put(gram, kieli, (double) amount);
langamount = langamount + (double) amount;
}
else {
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
}
}
for (Cell<String, String, Double> cell: tempDict.cellSet()){
double probability = -Math.log10(cell.getValue() / langamount);
if (tyyppi.equals("word")) {
wordDict.put(cell.getRowKey(), kieli, probability);
}
else {
gramDict.put(cell.getRowKey(), kieli, probability);
}
}
}
public static String tunnistaTeksti(String teksti) {
teksti = teksti.toLowerCase();
teksti = teksti.replaceAll("[^\\p{L}\\p{M}′']", " ");
teksti = teksti.replaceAll(" *", " ");
teksti = teksti.replaceAll("^ ", "");
int strLength = teksti.length();
if (strLength == 0) {
return("xxx0.0");
}
Boolean viimeinensana = false;
if (teksti.substring(strLength - 1, strLength).equals(" ")) {
teksti = teksti.replaceAll(" $", "");
viimeinensana = true;
}
String[] sanat = teksti.split(" ");
List<String> sanalista = new ArrayList<String>();
double sanamaara = 0;
double uniquesanat = 0;
for (String ss : sanat) {
if (sanalista.contains(ss)) {
sanamaara = sanamaara +1 ;
}
else {
sanalista.add(ss);
uniquesanat = uniquesanat + 1;
sanamaara = sanamaara +1 ;
}
}
double suhde = sanamaara / uniquesanat;
if (suhde > 15) {
return("xxx0.0");
}
Map<String, Double> kielipisteet = new HashMap();
ListIterator pisteiterator = kielilista.listIterator();
while(pisteiterator.hasNext()) {
Object element = pisteiterator.next();
String pistekieli = (String) element;
kielipisteet.put(pistekieli, 0.0);
}
double monesko = 0;
for (String sana : sanat) {
Boolean olisana = false;
Map<String, Double> sanapisteet = new HashMap();
monesko = monesko + 1;
sana = " " + sana;
if (monesko == sanamaara) {
if (viimeinensana) {
sana = sana + " ";
}
}
else {
sana = sana + " ";
}
if (usedwords < 1) {
if (wordDict.containsRow(sana)) {
olisana = true;
pisteiterator = kielilista.listIterator();
while(pisteiterator.hasNext()) {
Object element = pisteiterator.next();
String pistekieli = (String) element;
if (wordDict.contains(sana,pistekieli)) {
sanapisteet.put(pistekieli, wordDict.get(sana,pistekieli));
}
else {
sanapisteet.put(pistekieli, gramsakko);
}
}
}
}
if (!olisana) {
pisteiterator = kielilista.listIterator();
while(pisteiterator.hasNext()) {
Object element = pisteiterator.next();
String pistekieli = (String) element;
sanapisteet.put(pistekieli, 0.0);
}
}
int t = maksimingram;
while (t > 0) {
if (olisana) {
break;
}
else {
int pituus = sana.length();
int x = 0;
int grammaara = 0;
if (pituus > (t-1)) {
while (x < pituus - t + 1) {
String gram = sana.substring(x,x+t);
if (gramDict.containsRow(gram)) {
grammaara = grammaara + 1;
olisana = true;
pisteiterator = kielilista.listIterator();
while(pisteiterator.hasNext()) {
Object element = pisteiterator.next();
String pistekieli = (String) element;
if (gramDict.contains(gram,pistekieli)) {
sanapisteet.put(pistekieli, (sanapisteet.get(pistekieli)+gramDict.get(gram,pistekieli)));
}
else {
sanapisteet.put(pistekieli, (sanapisteet.get(pistekieli)+gramsakko));
}
}
}
x = x + 1;
}
}
if (olisana) {
pisteiterator = kielilista.listIterator();
while(pisteiterator.hasNext()) {
Object element = pisteiterator.next();
String pistekieli = (String) element;
sanapisteet.put(pistekieli, (sanapisteet.get(pistekieli)/grammaara));
}
}
}
t = t -1 ;
}
pisteiterator = kielilista.listIterator();
while(pisteiterator.hasNext()) {
Object element = pisteiterator.next();
String pistekieli = (String) element;
kielipisteet.put(pistekieli, (kielipisteet.get(pistekieli) + sanapisteet.get(pistekieli)));
}
}
String voittaja = "xxx";
Double pienin = gramsakko + 1;
pisteiterator = kielilista.listIterator();
while(pisteiterator.hasNext()) {
Object element = pisteiterator.next();
String pistekieli = (String) element;
kielipisteet.put(pistekieli, (kielipisteet.get(pistekieli)/sanamaara));
if (kielipisteet.get(element) < pienin) {
voittaja = pistekieli;
pienin = kielipisteet.get(element);
}
}
return (voittaja);
}
private static void doServe() {
int i=0;
try{
ServerSocket listener = new ServerSocket(port);
Socket server;
while((i++ < maxConnections) || (maxConnections == 0)){
doCommsFast connection;
server = listener.accept();
doCommsFast conn_c= new doCommsFast(server);
Thread t = new Thread(conn_c);
t.start();
}
} catch (IOException ioe) {
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
}
}
}
class doCommsFast implements Runnable {
private Socket server;
private String line,input;
doCommsFast(Socket server) {
this.server=server;
}
public void run () {
input="";
List<String> uralilaiset = Arrays.asList("enf", "enh", "fit", "fkv", "izh", "kca", "koi", "kom", "kpv", "krl", "liv", "lud", "mdf", "mhr", "mns", "mrj", "mtm", "myv", "nio", "olo", "sel", "sia", "sjd", "sje", "sjk", "sjt", "sju", "sma", "sme", "smj", "smn", "sms", "udm", "vep", "vot", "vro", "xas", "yrk");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
PrintStream out = new PrintStream(server.getOutputStream());
line = in.readLine();
if (line.length() == 300) {
String lang;
lang = TunnistinPalveluFast.tunnistaTeksti(line.substring(0,100));
if (uralilaiset.contains(lang.substring(0,3))) {
out.println(lang);
}
else {
lang = TunnistinPalveluFast.tunnistaTeksti(line.substring(100,200));
if (uralilaiset.contains(lang.substring(0,3))) {
out.println(lang);
}
else {
lang = TunnistinPalveluFast.tunnistaTeksti(line.substring(200,300));
out.println(lang);
}
}
}
else {
out.println(TunnistinPalveluFast.tunnistaTeksti(line));
}
server.close();
} catch (IOException ioe) {
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
}
}
}