-
Notifications
You must be signed in to change notification settings - Fork 1
/
RootExtrMain.java
712 lines (643 loc) · 26.5 KB
/
RootExtrMain.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
// You have to add weka.jar and libsvm.jar to the build path in order to compile.
// ALL ARABIC TEXT MUST BE IN UTF-8, input files, etc
// this file is in utf-8
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.core.converters.CSVLoader;
import weka.core.converters.ConverterUtils.DataSource;
import weka.classifiers.Evaluation;
import weka.classifiers.functions.LibSVM;
public class RootExtrMain {
// CONSTANTS
public static final String FATHA = "َ";
public static final String DAMMA = "ُ";
public static final String KASRA = "ِ";
public static final String SHADDA = "ّ";
public static final String ALIF = "ا";
public static final String HAMZMAD = "آ";
public static final String HAMZA = "ء";
public static final String[] DIACS_arr = {"َ","ً","ُ","ٌ","ِ","ٍ","ّ","ْ"};
public static final String DIACS_str = "ًٌٍَُِّْ";
public static final String DIACS_regex = "[ًٌٍَُِّْ]";
public static final int l = 3;
public static final char group[][] = {
{'ا','إ','ت','م'},
{'س','ن'},
{'ت','ا','و','ي'},
{'ا','ن','و','ي','ة'},
{'ا','ن','ة'},
{'ا','ة'},
{0}
};
// to avoid recompiling regex's every iteration, we define them here
public static Pattern regex_newline = Pattern.compile("\\n");
public static Pattern regex_hamza = Pattern.compile("[ء-ئ]");
public static Pattern regex_letterShadda = Pattern.compile("([ء-ي]"+SHADDA+")");
public static Pattern regex_shadda = Pattern.compile(SHADDA);
public static Pattern regex_hamzmad = Pattern.compile("آ");
public static Pattern regex_allLetters = Pattern.compile("[ء-ي]");
public static Pattern regex_kashidaDiac = Pattern.compile("ـ([َُِْ])");
public static Pattern regex_allDiacs = Pattern.compile(DIACS_regex);
public static Pattern regex_wawYa= Pattern.compile("[وي]");
//return regex_.matcher(string).replaceAll("replace by");
public static void main(String[]args) throws Exception {
LibSVM[] svms = new LibSVM[l];
boolean csvF = false; // determines format used.. false=>is arff
// setting class attribute
// Create vector to hold nominal values "first", "second", "third"
FastVector my_nominal_values = new FastVector(3);
my_nominal_values.addElement("pos0");
my_nominal_values.addElement("pos1");
my_nominal_values.addElement("pos2");
my_nominal_values.addElement("pos3");
my_nominal_values.addElement("pos4");
my_nominal_values.addElement("pos5");
my_nominal_values.addElement("pos6");
my_nominal_values.addElement("pos7");
my_nominal_values.addElement("unclassified");
// Create nominal attribute "position"
Attribute position = new Attribute("position", my_nominal_values);
// =======================READ========================
/* <------- '//*' to train.. '/*' to skip training
// if you have the model already trained from
// a previous run. the model can be found as 3 files
// with extension '.model'.
//
// ===================================================
//===========================
// FEATURE EXTRACTION
//===========================
// Read raw words from csv file previously cleaned by some regex operations
String rfName = "مصادر_ثلاثية.csv";
//String rfName = "inMicro.csv";
Scanner scnr = new Scanner (new File(rfName));
ArrayList<String> sroots = new ArrayList<String>(30000); // this is too expensive and perhaps stupid..
ArrayList<String> sderivs = new ArrayList<String>(30000);
int countLines = 0;
while(scnr.hasNextLine()) {
String tmp = scnr.nextLine().trim();
countLines++;
StringTokenizer x = new StringTokenizer(tmp, ",");
if(x.countTokens() != 2) {
System.out.println("Format error. "+rfName+":"+countLines+"\n "+tmp);
scnr.close();
System.exit(0);
}
sroots.add(x.nextToken());
sderivs.add(x.nextToken());
}
scnr.close();
String[] roots = (String[]) sroots.toArray(new String[sroots.size()]);
String[] derivs = (String[]) sderivs.toArray(new String[sderivs.size()]);
if(roots.length != derivs.length) {
System.out.println("Input Format error. Num of roots different than num of derivs. How did this happen ?!!!");
System.exit(0);
}
// some preprocessing
// not needed anymore.. done in featExtract
// for(int i=0; i<roots.length; i++) {
// roots[i] = hamzaNorm(roots[i]); // normalize hamza
// derivs[i] = hamzaNorm(derivs[i]); // normalize hamza
// derivs[i] = shaddaSub(derivs[i]); // resolve shadda
// derivs[i] = hamzmadSub(derivs[i]);// resolve hamzmad آ
// }
// do feature extraction and
// save the features to files
String[] ffNames = {"feat1.arff","feat2.arff","feat3.arff"};
if(csvF)
ffNames = new String[] {"feat1.csv","feat2.csv","feat3.csv"};
if(l!=ffNames.length) {
System.out.println("Wait wait... WHAT?!! Let me stop you here because, eventually, something will go wrong. I though we're dealing with 3 classifiers. I have "+l+" and "+ffNames.length);
System.exit(0);
}
File[] files = new File[l];
for(int i=0; i<l; i++) {
files[i] = new File(ffNames[i]);
}
PrintWriter[] outs = new PrintWriter[l];
for(int i=0; i<l; i++) {
outs[i] = new PrintWriter(files[i]);
}
// print datasets to files
String[] feats;
feats = getFeats(derivs, roots, csvF);
for(int i=0; i<l; i++) {
outs[i].print(feats[i]);
outs[i].close();
}
//===========================
// AFTER FEATURE EXTRACTION
// TRAIN SVM
//===========================
// Read Data.. three datasets one for each letter of the root => three classifiers
DataSource[] sources = new DataSource[l];
Instances[] data = new Instances[l];
for(int i=0; i<l; i++) {
sources[i] = new DataSource(ffNames[i]);
data[i] = sources[i].getDataSet();
}
// setting class attribute if the data format does not provide this information
for(int i=0; i<l; i++) {
if(csvF) data[i].setClass(position);
if (data[i].classIndex() == -1) {
data[i].setClassIndex(data[i].numAttributes()-1);
}
}
// setup parameters for SVM
String[] params = weka.core.Utils.splitOptions("-S 0 -K 2 -D 3 -G 0.0 -R 0.0 -N 0.5 -M 40.0 -C 1.0 -E 0.001 -P 0.1");
// Evaluate SVM (optional to test your features and parameters)
// evaluate using 10-fold.. test parameters
// setup parameters.. see report for choice of parameters
// LibSVM svmEval = new LibSVM();
// svmEval.setOptions(params);
// Evaluation eval = new Evaluation(data[2]);
// eval.crossValidateModel(svmEval, data[2], 10, new Random(1));
// System.out.println(eval.toSummaryString("\nResults\n======\n", false));
// setup the three classifiers.. use the same parameters from evaluation
for(int i=0; i<l; i++) {
svms[i] = new LibSVM();
svms[i].setOptions(params);
svms[i].buildClassifier(data[i]);
}
//*/
// load/save the trained models
String[] modelfn = new String[l];
for(int i=0; i<l; i++)
modelfn[i] = "svm"+(i+1)+".model";
if(svms[0]==null) {
System.out.println("Loading models");
for(int i=0; i<l; i++) {
svms[i] = (LibSVM) weka.core.SerializationHelper.read(modelfn[i]);
System.out.println("Loaded svm model from "+modelfn[i]);
}
} else { // save them
System.out.println("Saving models");
for(int i=0; i<l; i++) {
weka.core.SerializationHelper.write(modelfn[i], svms[i]);
System.out.println("Saved svm model in "+modelfn[i]);
}
}
//*
//===========================
// AFTER Training
// CLASSIFY unseen instances
//===========================
// classify new instances.. use the trained classifiers
// load unclassified data, note that everything will be done in threes because we have three feature sets
// deriv is the string to be analyzed
String deriv = "استفهام";
String[] fl = {"temp1", "temp2", "temp3"};
File[] tempFile = new File[l];
for(int i=0; i<l; i++) {
tempFile[i] = new File(fl[i]);
}
// get the features of our input
//String[] csvFeats = getCsvFeats(deriv);
//String[] arffFeats = getArffFeats(deriv);
String[] unclsdFeats = getFeats(deriv, csvF);
// convert them to ARFF
// if the feats are in CSV format, they must be written
// into a text file and then read again.. unfortunately,
// this is how weka works; it is explicitly stated in the
// documentation.
Instances[] unclsd = new Instances[l];
for(int i=0; i<l; i++) {
if(csvF) {
CSVLoader loader = new CSVLoader();
loader.setSource(new ByteArrayInputStream(unclsdFeats[i].getBytes("UTF-8")));
unclsd[i] = loader.getDataSet();
ArffSaver saver = new ArffSaver();
saver.setInstances(unclsd[i]);
saver.setFile(tempFile[i]);
saver.writeBatch();
unclsd[i] = new Instances(
new BufferedReader(
new FileReader(fl[i])));
} else {
unclsd[i] = new Instances(
new BufferedReader(
new StringReader(unclsdFeats[i])));
}
}
// setting class attribute if the data format does not provide this information
for(int i=0; i<l; i++) {
if(csvF) unclsd[i].setClass(position);
if (unclsd[i].classIndex() == -1) {
unclsd[i].setClassIndex(unclsd[i].numAttributes()-1);
}
}
// create copy
Instances clsd[] = new Instances[l];
for(int i=0; i<l; i++)
clsd[i] = new Instances(unclsd[i]);
// label instances (classify)
for(int i=0; i<l; i++) {
double clsLabel=0;
for (int j=0; j<unclsd[i].numInstances(); j++) {
clsLabel = svms[i].classifyInstance(unclsd[i].instance(j));
clsd[i].instance(j).setClassValue(clsLabel);
}
}
// show final output (only one instance is handled)
int classIdxs[] = {0,0,0};
char letters[] = {0,0,0};
for(int i=0; i<l; i++) {
classIdxs[i] = (int) clsd[i].instance(0).classValue();
String label = classLabels[i].trim().split(",")[classIdxs[i]];
try {
int realPos = Integer.parseInt(label.substring(label.length()-1))-1;
System.out.println(classIdxs[i]+": "+label+" -> charAt("+realPos+")");
letters[i] = removeDiacs(deriv).charAt(realPos);
} catch (java.lang.StringIndexOutOfBoundsException e) {
letters[i] = '?';
}
// i think this should be shadda-unfolded not just vocals-removed
// WARNING string operation not try-catched
}
String root = new String(letters);
System.out.println("SVM says: "+deriv+" is derived from "+root);
System.out.println("Guesser says: "+guessDerived(deriv,root));
// delete temp files.. not working probably because ArffSaver and unclsd have not freed them
for(int i=0; i<l; i++) {
tempFile[i].delete();
}
//*/
}
//===========================
// END OF MAIN
//===========================
// returns the features of a given word in CSV or ARFF format
private static String[] getFeats(String deriv, boolean csvF) throws FileNotFoundException {
if(csvF)
return getCsvFeats(deriv);
else
return getArffFeats(deriv);
}
// returns the features of a given word in ARFF format
private static String[] getArffFeats(String deriv) throws FileNotFoundException {
// append the header
String[]ss = getHeader(false);
// append the numbers
String[] f = featExtract(deriv);
for(int i=0; i<ss.length; i++)
ss[i] += f[i];
return ss;
}
// returns the features of a given word in CSV format
private static String[] getCsvFeats(String deriv) throws FileNotFoundException {
String[] h = featNamesWClassCsv;
String[] f = featExtract(deriv);
if(h.length != f.length) System.out.println("HOW DID THIS HAPPEN ?!! DEBUG NOW");
String[]ss = new String[h.length];
for (int i=0; i<ss.length; i++) {
ss[i] = h[i]+f[i];
}
return ss;
}
// returns the features of a given array of words in CSV or ARFF format
private static String[] getFeats(String[] derivs, boolean csvF) throws FileNotFoundException {
return getFeats(derivs, new String [derivs.length], csvF);
}
// returns the features of a given array of words in ARFF format
private static String[] getArffFeats(String[] derivs, String[] roots) throws FileNotFoundException {
return getFeats(derivs, roots, false);
}
// returns the features of a given array of words in CSV or ARFF format
private static String[] getFeats(String[] derivs, String[]roots, boolean csvF) throws FileNotFoundException {
// append the header
String[]sss = getHeader(csvF);
// append the numbers
for (int j=0; j<derivs.length; j++) { // for each deriv
String[] f = featExtract(derivs[j],roots[j]);
for(int i=0; i<sss.length; i++) // for 3
sss[i] += f[i];
}
return sss;
}
// returns the feature file header for CSV or ARFF formats
private static String[] getHeader(boolean csvF) {
if(csvF) {
String[] h = featNamesWClassCsv;
String[]header = new String[h.length];
for (int i=0; i<header.length; i++) {
header[i] = h[i];
}
return header;
} else { //ARFF
String[] h = featNamesCsv;
String[]header = new String[h.length];
for (int i=0; i<header.length; i++) {
header[i] = "% Features of derived words to root letter "+(i+1)+"\n%\n";
header[i] += "@RELATION rootL"+(i+1)+"\n\n";
String[] attribs = h[i].trim().split(",");
for (int j=0; j<attribs.length; j++)
header[i] +="@ATTRIBUTE "+attribs[j]+"\tNUMERIC\n";
header[i] +="@ATTRIBUTE position\t{"+classLabels[i]+"}\n";
header[i] +="\n@DATA\n";
}
return header;
}
}
private static String[] classLabels = {
"pos0,pos1,pos2,pos3,pos4",
"pos0,pos2,pos3,pos4,pos5",
"pos0,pos3,pos4,pos5,pos6,pos7"
};
private static String[] featNamesCsv = {
"length,isLetter1InCorrespondingGroup,isLetter2InCorrespondingGroup,isLetter3InCorrespondingGroup,isLetter4InCorrespondingGroup,WhatLetter1Haraka,WhatLetter2Haraka,WhatLetter3Haraka,WhatLetter4Haraka,isLetter1Vowel,isLetter2Vowel,isLetter3Vowel,isLetter4Vowel,isLetter1Hamza,isLetter2Hamza,isLetter3Hamza,isLetter4Hamza\n",
"length,isLetter2InCorrespondingGroup,isLetter3InCorrespondingGroup,isLetter4InCorrespondingGroup,isLetter5InCorrespondingGroup,WhatLetter2Haraka,WhatLetter3Haraka,WhatLetter4Haraka,WhatLetter5Haraka,isLetter2Vowel,isLetter3Vowel,isLetter4Vowel,isLetter5Vowel,isLetter2Hamza,isLetter3Hamza,isLetter4Hamza,isLetter5Hamza,rootL1Position\n",
"length,isLetter3InCorrespondingGroup,isLetter4InCorrespondingGroup,isLetter5InCorrespondingGroup,isLetter6InCorrespondingGroup,isLetter7InCorrespondingGroup,WhatLetter3Haraka,WhatLetter4Haraka,WhatLetter5Haraka,WhatLetter6Haraka,WhatLetter7Haraka,isLetter3Vowel,isLetter4Vowel,isLetter5Vowel,isLetter6Vowel,isLetter7Vowel,isLetter3Hamza,isLetter4Hamza,isLetter5Hamza,isLetter6Hamza,isLetter7Hamza,rootL1Position,rootL2Position\n"
};
private static String[] featNamesWClassCsv = {
"length,isLetter1InCorrespondingGroup,isLetter2InCorrespondingGroup,isLetter3InCorrespondingGroup,isLetter4InCorrespondingGroup,WhatLetter1Haraka,WhatLetter2Haraka,WhatLetter3Haraka,WhatLetter4Haraka,isLetter1Vowel,isLetter2Vowel,isLetter3Vowel,isLetter4Vowel,isLetter1Hamza,isLetter2Hamza,isLetter3Hamza,isLetter4Hamza,position\n",
"length,isLetter2InCorrespondingGroup,isLetter3InCorrespondingGroup,isLetter4InCorrespondingGroup,isLetter5InCorrespondingGroup,WhatLetter2Haraka,WhatLetter3Haraka,WhatLetter4Haraka,WhatLetter5Haraka,isLetter2Vowel,isLetter3Vowel,isLetter4Vowel,isLetter5Vowel,isLetter2Hamza,isLetter3Hamza,isLetter4Hamza,isLetter5Hamza,rootL1Position,position\n",
"length,isLetter3InCorrespondingGroup,isLetter4InCorrespondingGroup,isLetter5InCorrespondingGroup,isLetter6InCorrespondingGroup,isLetter7InCorrespondingGroup,WhatLetter3Haraka,WhatLetter4Haraka,WhatLetter5Haraka,WhatLetter6Haraka,WhatLetter7Haraka,isLetter3Vowel,isLetter4Vowel,isLetter5Vowel,isLetter6Vowel,isLetter7Vowel,isLetter3Hamza,isLetter4Hamza,isLetter5Hamza,isLetter6Hamza,isLetter7Hamza,rootL1Position,rootL2Position,position\n"
};
// Extracts 3 feature vectors from the input string, one for each classifier
// not every vector of the three is unique in features. For example, all three have
// a feature "length" (length of deriv)
// deriv is the word we want to find the root for.
// This method should not be called directly
private static String[] featExtract(String deriv) throws FileNotFoundException {
return featExtract(deriv,null);
}
private static String[] featExtract(String deriv, String root) throws FileNotFoundException {
// preprocess input string
// ---------------------------
String root_mod=null;
boolean training = false;
if(root!=null) {
root_mod=hamzaNorm(root);
training = true;
}
String deriv_mod = deriv;
deriv_mod = hamzaNorm(deriv_mod); // normalize hamza
deriv_mod = shaddaSub(deriv_mod); // resolve shadda
deriv_mod = hamzmadSub(deriv_mod);// resolve hamzmad آ
// separate the string from its diacritical/vocalization/tashkeel marks
String noT = removeDiacs(deriv_mod); // the input string without Tashkeel
String T = getDiacs(deriv_mod); // a string of only the Tashkeel of the input
// DONE preprocess
//----------------------------
// prepare output
// featVs is an array of 3 feature vectors each corresponding to a classifier.
String featVs[] = {"","",""};
int m=7; // max num of letters expected
// ignore if length > m
if(noT.length() > m) {
// TODO warning hardcoded
String featVsDummy[] = {
"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,pos0\n",
"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,pos0\n",
"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,pos0\n"
};
System.out.println("Warning: ("+deriv+") words longer than "+m+" letters are not supported. returned dummy feature vector.");
return featVsDummy; // should cause no trouble to caller
}
// setup the vars to be distributed among the three feature vectors
int[] isLetterInGroup = new int[m];
for(int i=0; i<m; i++)
isLetterInGroup[i] = isLetterInGroup(noT,i,i);
int[] WhatLetterHaraka= new int[m];
for(int i=0; i<m; i++)
WhatLetterHaraka[i] = WhatLetterHaraka(T,i);
int[] isLetterVowel= new int[m];
for(int i=0; i<m; i++)
isLetterVowel[i] = isLetterVowel(noT,i);
int[] isLetterHamza= new int[m];
for(int i=0; i<m; i++)
isLetterHamza[i] = isLetterHamza(noT,i);
String[] cls = new String[l];
int[] clsInt = new int[l]; // initialized to zeros by java
// TODO should I fill it with -1 for "unclassified" to keep 0 for "not found" ?
// I think not because in testing, I don't want to be surprised by a -1 !!
// plus it won't make a difference, because clsInt will take a new value very soon
//java.util.Arrays.fill(clsInt, -1);
if(root_mod!=null && training) {
clsInt = getInstanceClassInt(noT, root_mod);
for(int i=0; i<l; i++) {
cls[i] = "pos"+clsInt[i];
}
}
else
for(int i=0; i<l; i++)
// TODO Don't know if the unclassified instances can be
// labeled by the default label.
// should be ok.. it is only used for new instances
// that will directly go into the classifier.
//cls[i]="unclassified";
cls[i]="pos0";
int j,f,t; // feat vector num, from, to
// feature vector 1.. concerned letters (1-4)
j=0; f=0; t=3;
featVs[j] += noT.length()+",";
for(int i=f; i<=t; i++)
featVs[j] += (isLetterInGroup[i]+",");
for(int i=f; i<=t; i++)
featVs[j] += (WhatLetterHaraka[i]+",");
for(int i=f; i<=t; i++)
featVs[j] += (isLetterVowel[i]+",");
for(int i=f; i<=t; i++)
featVs[j] += (isLetterHamza[i]+",");
featVs[j] += cls[j];
featVs[j] += "\n";
// feature vector 2.. concerned letters (2-5)
j=1; f=1; t=4;
featVs[j] += noT.length()+",";
for(int i=f; i<=t; i++)
featVs[j] += (isLetterInGroup[i]+",");
for(int i=f; i<=t; i++)
featVs[j] += (WhatLetterHaraka[i]+",");
for(int i=f; i<=t; i++)
featVs[j] += (isLetterVowel[i]+",");
for(int i=f; i<=t; i++)
featVs[j] += (isLetterHamza[i]+",");
// TODO in case of classifying an unclassified instance (and testing as well),
// this should be updated with the value the previous classifier found..
// so.. how to do it ???
featVs[j] += (clsInt[j-1]+",");
featVs[j] += cls[j];
featVs[j] += "\n";
// feature vector 3.. concerned letters (3-7)
j=2; f=2; t=6;
featVs[j] += noT.length()+",";
for(int i=f; i<=t; i++)
featVs[j] += (isLetterInGroup[i]+",");
for(int i=f; i<=t; i++)
featVs[j] += (WhatLetterHaraka[i]+",");
for(int i=f; i<=t; i++)
featVs[j] += (isLetterVowel[i]+",");
for(int i=f; i<=t; i++)
featVs[j] += (isLetterHamza[i]+",");
featVs[j] += (clsInt[j-2]+",");
featVs[j] += (clsInt[j-1]+",");
featVs[j] += cls[j];
featVs[j] += "\n";
//System.out.println("Feats for "+deriv+" ("+noT+" + "+T+"), "+root+"\n"+featVs[0]+featVs[1]+featVs[2]);
return featVs;
}
// returns the class for this instance as an integer. used for setting up the training data
private static int[] getInstanceClassInt(String deriv, String root) {
//System.out.println("finding pos: \nroot:"+root+"\nderiv: "+deriv);
int[] classes = new int[l];
int pos = deriv.indexOf(root.charAt(0)) + 1; // +1 because 0 is reserved for 'not found'
if (pos>4) pos=0;
classes[0]= pos;
pos = (locateLetter(root.charAt(1), deriv, 1)+1);
if(pos>5) pos=0;
classes[1]= pos;
classes[2]= locateLetter(root.charAt(2), deriv, 2)+1;
return classes;
}
// returns the class for this instance as a string for weka demands so.
// used for setting up the training data
private static String[] getInstanceClass(String deriv, String root) {
String[] classes = new String[l];
int[] x = getInstanceClassInt(deriv, root);
if(x.length!=classes.length) {
System.err.println("What is happening here ?!!");
System.exit(0);
}
for(int i=0; i<x.length; i++)
classes[i] = "pos"+x[i];
return classes;
}
// normalizes hamza replacing all forms by one
private static String hamzaNorm(String string) {
return regex_hamza.matcher(string).replaceAll("ء");
}
// substitue a shadda by its equivalent
//unfold shadda: a letter with shadda is equivalnet to two instances of the same letter, where the first has a sukoon
private static String shaddaSub(String string) {
string = regex_letterShadda.matcher(string).replaceAll("$1ْ$1");
string = regex_shadda.matcher(string).replaceAll("");
return string;
}
// hamzmad (آ) replace by equivalent (hamza+mad) (ءا)
private static String hamzmadSub(String string) {
return regex_hamzmad.matcher(string).replaceAll("ءا");
}
// normalize vowel letters
private static String vowelNorm(String string) {
return regex_wawYa.matcher(string).replaceAll("ا");
}
// returns string of diacs with indexes matching with letters they modify
private static String getDiacs(String string) {
// if character is letter followed by diac, replace by diac
// else replace by kashida
// shadda should already be unfolded
// tanween is not accounted for
string = regex_allLetters.matcher(string).replaceAll("ـ");
string = regex_kashidaDiac.matcher(string).replaceAll("$1");
return string;
}
// returns the word unvocalized (undiacritized)
public static String removeDiacs(String string) {
return regex_allDiacs.matcher(string).replaceAll("");
}
// is the character a diacritical sign/ a vocalization mark
public static boolean isDiac(char c) {
return DIACS_str.indexOf(c) > -1;
}
private static int WhatLetterHaraka(String T, int i) {
// we want zero to mean don't care.. but how
try {
if( T.charAt(i) == 'َ' )
return 1;
else if(T.charAt(i) == 'ِ')
return 2;
else if(T.charAt(i) == 'ُ')
return 3;
else if(T.charAt(i) == 'ْ')
return 4;
}
catch(Exception e ) { return 0; }
return 0;
}
private static int locateLetter(char toLocate, String toSearch, int from, int to) {
try {
int loc = toSearch.substring(from,to).indexOf(toLocate)+from;
if (loc == -1) {
}
return loc;
} catch (StringIndexOutOfBoundsException e) {
return -1;
}
}
private static int locateLetter(char toLocate, String toSearch, int from) {
int loc = toSearch.indexOf(toLocate, from);
if(loc == -1) return loc;
else return loc;
}
private static int isLetterVowel(String noT, int i) {
try { if( noT.charAt(i) == 'ا'
|| noT.charAt(i) == 'و'
|| noT.charAt(i) == 'ي'
|| noT.charAt(i) == 'ى')
return 1; }
catch(Exception e ) { return -1; }
return 0;
}
private static int isLetterHamza(String noT, int i) {
try { if( noT.charAt(i) == 'ء'
|| noT.charAt(i) == 'ئ'
|| noT.charAt(i) == 'ؤ'
|| noT.charAt(i) == 'أ'
|| noT.charAt(i) == 'إ')
return 1; }
catch(Exception e ) { return -1; }
return 0;
}
private static int isLetterInGroup(String s, int i, int j) {
try { for (int x=0; x<group[j].length; x++) {
if(s.charAt(i)==group[j][x])
return 1;
} }
catch(Exception e ) { return -1; }
return 0;
}
// a guesser to return true if deriv is likely derived from root
public static boolean guessDerived(String deriv, String root) {
// deriv is the word to be tested whether it is derived from root
//Text Normalization
root = hamzaNorm(root);// normalize hamza
deriv = hamzaNorm(deriv);// normalize hamza
root = vowelNorm(root); // normalize vowels
deriv = vowelNorm(deriv); // normalize vowels
deriv = shaddaSub(deriv); //unfold shadda
deriv = removeDiacs(deriv); //remove tashkeel
deriv = hamzmadSub(deriv); // equiv آ
// The guesser
boolean success = false;
for(int i=0; i<root.length()-1; i++) {
try {
int ti1 = deriv.indexOf(root.charAt(i));
int ti2 = deriv.indexOf(root.charAt(i+1));
if(ti1 < ti2 && ti1 > -1)
success = true;
else
return false;
} catch (StringIndexOutOfBoundsException e) {return false;}
}
return success;
}
}