-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFieldMatcher.java
531 lines (461 loc) · 16.4 KB
/
FieldMatcher.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
package eu.slipo.triplegeo.ml.mappings;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.yaml.snakeyaml.Yaml;
import com.opencsv.CSVIterator;
import com.opencsv.CSVParser;
import com.opencsv.CSVReader;
/**
* Class that produces predicate matchings for each column of a CSV file.
*/
public class FieldMatcher implements Serializable {
private static final long serialVersionUID = 2L;
private char delimiter = '|';
private char quote = '"';
private ArrayList<Predicate> predicates;
private ArrayList<Field> fields;
public FieldMatcher() {
predicates = new ArrayList<Predicate>();
fields = new ArrayList<Field>();
}
public static FieldMatcher create(String modelFileName) throws IOException, ClassNotFoundException {
try (
FileInputStream fileInputStream = new FileInputStream(modelFileName);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
ObjectInputStream objectInputStream = new ObjectInputStream(bufferedInputStream);
) {
return (FieldMatcher) objectInputStream.readObject();
}
}
public static FieldMatcher create(File file) throws IOException, ClassNotFoundException {
try (
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
ObjectInputStream objectInputStream = new ObjectInputStream(bufferedInputStream);
) {
return (FieldMatcher) objectInputStream.readObject();
}
}
public char getDelimiter() {
return delimiter;
}
public void setDelimiter(char delimiter) {
this.delimiter = delimiter;
}
public char getQuote() {
return quote;
}
public void setQuote(String quote) {
if (quote == null) {
this.quote = CSVParser.NULL_CHARACTER;
} else {
this.quote = quote.charAt(0);
}
}
/**
* Reads a dataset and adds observed predicates and fields
*
* @param yamlPath The path to YAML mapping file
* @param csvPath The path to CSV data file
*
* @throws IOException If an I/O error occurs
*/
private void readSingleDataset(String yamlPath, String csvPath) throws IOException {
// reads everything
Yaml yaml = new Yaml();
InputStream inputStream = new FileInputStream(yamlPath);
FileReader filereader = new FileReader(csvPath);
Map<String, Object> data = yaml.load(inputStream);
Set<String> keys = data.keySet();
CSVReader csvReader = new CSVReader(filereader, delimiter, quote);
String[] fieldNames = csvReader.readNext();
// produces features from field content
boolean[][] feats = scanCSV(csvReader, fieldNames);
// for each csv field add to fields and predicates
for (String key : keys) {
Map<String, String> attrs = (Map<String, String>) data.get(key);
if (!predicateExists(attrs)) {
addToPredicates(attrs);
}
addToFields(key, attrs, feats[whichEquals(fieldNames, key)]);
}
}
/**
* Creates features from the contents of each field
*
* @param csvReader The reader for reading a CSV data file
* @param fieldNames A list of field names
*
* @return
* @throws IOException
*/
private boolean[][] scanCSV(CSVReader csvReader, String[] fieldNames) throws IOException {
int fieldNum = fieldNames.length;
double[][] stats = new double[fieldNum][7];
CSVIterator csvIter = new CSVIterator(csvReader);
String[] nxt = null;
HashSet<String> hs = new HashSet<String>();
double[] count = new double[fieldNum];
for(int i=0;i<1000 && csvIter.hasNext();i++){
nxt = csvIter.next();
for(int j=0;j<fieldNum;j++) {
if(!nxt[j].equals("")) {
count[j]++;
stats[j][0] += isInteger(nxt[j]);
stats[j][1] += isDouble(nxt[j]);
stats[j][2] += isChar(nxt[j]);
stats[j][3] += isMixed(nxt[j]);
stats[j][4] += hasAt(nxt[j]);
stats[j][5] += hasInternet(nxt[j]);
stats[j][6] += isDistinct(nxt[j], hs);
}
}
}
boolean[][] feats = new boolean[fieldNum+1][7];
for(int i=0;i<fieldNum;i++) {
for(int j=0;j<7;j++) {
if(j!=6 && stats[i][j]>count[i]/2 || j==6 && stats[i][j]<count[i]/10) {
feats[i][j] = true;
}
}
}
return feats;
}
private double isMixed(String s) {
s = s.replaceAll("[()\\s-]+", "");
if(!StringUtils.isNumeric(s) && !StringUtils.isAlpha(s) && StringUtils.isAlphanumeric(s) ) {
return 1;
} else {
return 0;
}
}
private int isInteger(String s) {
s = s.replaceAll("[()\\s-]+", "");
try {
double num = Double.parseDouble(s);
if (Math.floor(num) == num && !Double.isInfinite(num)) {
return 1;
} else {
return 0;
}
}
catch(Exception e){
return 0;
}
}
private int isDouble(String s) {
s = s.replaceAll("[()\\s-]+", "");
try {
double num = Double.parseDouble(s);
if (Math.floor(num) != num && !Double.isInfinite(num)) {
return 1;
} else {
return 0;
}
}
catch (Exception e){
return 0;
}
}
private int isChar(String s) {
s = s.replaceAll("[()\\s-]+", "");
if(StringUtils.isAlpha(s)) {
return 1;
} else {
return 0;
}
}
private int hasAt(String s) {
if(s.contains("@")) {
return 1;
} else {
return 0;
}
}
private int hasInternet(String s) {
if(s.contains("www") || s.contains("http")) {
return 1;
} else {
return 0;
}
}
private int isDistinct(String s, HashSet<String> hs) {
if(hs.contains(s)) {
return 0;
} else {
hs.add(s);
return 1;
}
}
private int whichEquals(String[] fieldNames, String key) {
for(int i=0;i<fieldNames.length;i++) {
if(fieldNames[i].toUpperCase().equals(key.toUpperCase())) {
return i;
}
}
return fieldNames.length;
}
private void addToFields(String key, Map<String,String> attrs, boolean[] feats) {
Field fld = new Field(key,attrs,feats);
fields.add(fld);
updateKeywords(fld);
}
private void updateKeywords(Field fld) {
for(Predicate pred:predicates) {
if(pred.getName().equals(fld.getMatchingPred())) {
pred.addKeyword(fld.getName());
}
}
}
private void addToPredicates(Map<String,String> attrs) {
Predicate pred = new Predicate(attrs.get("predicate"));
predicates.add(pred);
}
private boolean predicateExists(Map<String,String> attrs) {
String predName = attrs.get("predicate");
for(Predicate pred:predicates) {
if(pred.getName().equals(predName.toUpperCase())) {
return true;
}
}
return false;
}
//trains one classifier for each predicate
private void trainClassifiers() throws Exception {
for(Predicate pred:predicates) {
pred.trainClassifier(fields);
}
}
//for a given input field, runs the classifiers for each predicate, returns predicate:probability map
private LinkedHashMap<String,Double> predict(Field fld) throws Exception {
LinkedHashMap<String,Double> res = new LinkedHashMap<String, Double>();
for(Predicate pred:predicates) {
res.put(pred.getName(),pred.predict(fld));
}
return sortPreds(res);
}
private LinkedHashMap<String,Double> sortPreds(LinkedHashMap<String,Double> res) {
List<Map.Entry<String, Double>> entries = new ArrayList<Map.Entry<String, Double>>(res.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<String, Double>>() {
@Override
public int compare(Map.Entry<String, Double> a, Map.Entry<String, Double> b) {
return b.getValue().compareTo(a.getValue());
}
});
LinkedHashMap<String, Double> sortedMap = new LinkedHashMap<String, Double>();
for (Map.Entry<String, Double> entry : entries) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
//evaluates the model through leave-one-out cross validation
/*public double evaluate(String outPath) throws Exception {
Field test = null;
ArrayList<String> preds = new ArrayList<String>();
String pred;
double acc = 0;
for (int i = 0; i < fields.size(); i++) {
test = removeField(fields,i);
trainClassifiers();
pred = predict(test);
if (pred.equals(test.getMatchingPred()))
acc++;
preds.add(predict(test));
reAddField(fields,test,i);
}
acc /= fields.size();
System.out.println(acc);
PrintWriter pw = null;
try {
pw = new PrintWriter(outPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
for(int i=0;i<fields.size();i++)
pw.println(fields.get(i).getName()+" -> "+preds.get(i));
pw.close();
return (acc);
}
private void printConfusionMatrix(ArrayList<Field> fields, ArrayList<String> preds,double acc) {
double[][] confMat = new double[predicates.size()][predicates.size()];
for (int i = 0; i < fields.size(); i++) {
confMat[findInPredicates(predicates, fields.get(i).getMatchingPred())][findInPredicates(predicates, preds.get(i))]++;
}
PrintWriter pw = null;
try {
pw = new PrintWriter("/home/pant/Desktop/slipo/out");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
for (int i = 0; i < confMat.length; i++) {
for (int j = 0; j < confMat.length; j++){
pw.print(confMat[i][j]);
pw.print(" ");
}
pw.println();
}
pw.close();
try {
pw = new PrintWriter("/home/pant/Desktop/slipo/preds");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
for(Predicate pred:predicates)
pw.println(pred.getName());
pw.close();
}
*/
private int findInPredicates(ArrayList<Predicate> predicates, String matchingPred) {
for(int i=0;i<predicates.size();i++) {
if(predicates.get(i).getName().equals(matchingPred)) {
return i;
}
}
return -1;
}
private void reAddField(ArrayList<Field> fields, Field fld,int i) {
fields.add(i,fld);
for(Predicate pred:predicates) {
if(fld.getMatchingPred().equals(pred.getName())) {
pred.addKeyword(fld.getName());
}
}
}
private Field removeField(ArrayList<Field> fields, int i) {
Field fld = fields.remove(i);
for(Predicate pred:predicates) {
pred.removeField(fld);
}
return fld;
}
private void readAll(String folderPath, HashMap<String, String> nameMap) throws IOException {
for(Map.Entry<String,String> name:nameMap.entrySet()) {
readSingleDataset(folderPath+name.getKey(), folderPath+name.getValue());
}
}
//Reads all .yml .csv from a file. It matches each .yml to a .csv . If not matching .csv exists it does not use the .yml.
private void readAllFromFolder(String folderPath) throws IOException {
File f = new File(folderPath);
String[] namesAr = f.list();
ArrayList<String> names = new ArrayList<String>(Arrays.asList(namesAr));
ArrayList<String> yamls = getYamls(names);
ArrayList<String> csvs = getCsvs(names);
HashMap<String,String> nameMap = alignLists(yamls,csvs);
readAll(folderPath,nameMap);
}
private HashMap alignLists(ArrayList<String> yamls, ArrayList<String> csvs) {
HashMap<String,String> nameMap = new HashMap<String, String>();
int ind;
for(String y:yamls) {
ind = matchCsv(y,csvs);
if ( ind > -1 ) {
nameMap.put(y,csvs.get(ind));
}
}
return nameMap;
}
private int matchCsv(String y, ArrayList<String> csvs) {
for(int i=0;i<csvs.size();i++) {
if(stripEnd(y).equals(stripEnd(csvs.get(i)))) {
return i;
}
}
return -1;
}
private String stripEnd(String s) {
return s.substring(0,s.length()-4);
}
private ArrayList<String> getCsvs(ArrayList<String> names) {
ArrayList<String> csvs = new ArrayList<String>();
for(String n:names) {
if(n.contains(".csv")) {
csvs.add(n);
}
}
return csvs;
}
private ArrayList<String> getYamls(ArrayList<String> names) {
ArrayList<String> yamls = new ArrayList<String>();
for(String n:names) {
if(n.contains(".yml")) {
yamls.add(n);
}
}
return yamls;
}
/**
* Constructs features for new fields and calls predict
*
* @param csvPath The path to a CSV data file
* @return An instance of {@link Mappings} with the file fields mappings
*
* @throws Exception
*/
public Mappings giveMatchings(String csvPath) throws Exception {
// Read field names
FileReader filereader = null;
filereader = new FileReader(csvPath);
CSVReader csvReader = new CSVReader(filereader, delimiter, quote);
String[] fieldNames = csvReader.readNext();
// Scan data file
boolean[][] feats = scanCSV(csvReader, fieldNames);
ArrayList<Field> fields = new ArrayList<Field>();
for (int i = 0; i < fieldNames.length; i++) {
fields.add(new Field(fieldNames[i], feats[i]));
}
// Create mappings
Mappings maps = new Mappings();
for (Field fld : fields) {
// Field names have been converted to upper case. Reset letter case.
String name = Arrays.stream(fieldNames)
.filter(value -> value.equalsIgnoreCase(fld.getName()))
.findFirst()
.get();
maps.addField(name, predict(fld));
}
return maps;
}
private void writeFMToFile(String path) throws IOException {
FileOutputStream fileOut = new FileOutputStream(path);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(this);
out.close();
fileOut.close();
}
/**
* Reads all YAML configuration files and CSV data files from the input folder. For
* each configuration file, a matching data file is expected with the same name as the
* former one.
*
* The current instance of the {@link FieldMatcher} object containing the trained
* classifiers is serialized and stored in the output file.
*
* @param inFolderPath The folder with input files
* @param outModelsPath The mode output file name
* @throws Exception
*/
public void makeModels(String inFolderPath,String outModelsPath) throws Exception {
readAllFromFolder(inFolderPath);
trainClassifiers();
writeFMToFile(outModelsPath);
}
}