-
Notifications
You must be signed in to change notification settings - Fork 7
/
example.py
596 lines (498 loc) · 17 KB
/
example.py
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
"""
attr_id,class
cooling@residential_listing_-_feature_details@house_listing@homeseekers.csv,cooling
cooling@house_listing@nky.csv,cooling
ac@house_listing@windermere.csv,cooling
house_location@house_listing@nky.csv,address
location@residential_listing_-_basic_features@house_listing@homeseekers.csv,address
address@house_listing@windermere.csv,address
house_location@house_listing@yahoo.csv,address
house_location@house_listing@texas.csv,address
company@file.csv,Organization.name
ceo@file.csv,Person.name
file.csv,Organization-ceo-Person
file.csv,Organization-location-City
file.csv,Person-livesIn-City
business_info_ssd = (sn
.SSD(business_info, ontology, name='business-info')
.map(Column("company"), DataNode(ClassNode("Organization"), "name"))
.map(Column("ceo"), DataNode(ClassNode("Person"), "name"))
.map(Column("city"), DataNode(ClassNode("City"), "name"))
.map(Column("state"), DataNode(ClassNode("State"), "name"))
.link("Organization", "operatesIn", "City")
.link("Organization", "ceo", "Person")
.link("City", "state", "State"))
business_info_ssd.show()
print()
print("Displaying businessInfo.csv Semantic Source Description (SSD)")
input("press any key to continue...")
#
# We also have just a string shorthand...
#
ontology_file = 'tests/resources/owl/dataintegration_report_ontology.ttl'
datasets = [
'tests/resources/data/businessInfo.csv',
'tests/resources/data/EmployeeAddresses.csv',
'tests/resources/data/getCities.csv',
'tests/resources/data/getEmployees.csv',
'tests/resources/data/postalCodeLookup.csv',
]
map_file = tests/resources/data/example-map-file.csv
> cat example-map-file.csv
attr_id, class
name@EmployeeAdddress.csv, Person.name
address@EmployeeAdddress.csv, Place.name
postcode@EmployeeAdddress.csv, Place.postalCode
getCities.csv@city, City.name
getCities.csv@state, State.name
employer@getEmployees.csv, Organization.name
employee@getEmployees.csv, Person.name
zipcode@postalCodeLookup.csv, Place.postalCode
city@postalCodeLookup.csv, City.name
state@postalCodeLookup.csv, State.name
link_file = tests/resources/data/example-link-file.csv
> cat example-link-file.csv
file, src, type, dst
EmployeeAdddress.csv, Person, livesIn, Place
getCities.csv, City, state, State
getEmployees.csv, Person, worksFor, Organization
postalCodeLookup.csv, City, state, State
postalCodeLookup.csv, City, isPartOf, Place
input
Some knowledge about the ontology or where you want to end up...
/some/junk/test1.csv - contains columns: name, birth, city, state, workplace
/some/junk/test2.csv - contains columns: state, city
/some/junk/test3.csv - contains columns: company, ceo, city, state
/some/junk/test4.csv - contains columns: employer, employee
/some/junk/test5.csv - contains columns: zipcode, city, state
..
..
..
...
output
SSD: semantic source description files
/some/junk/test1.ssd
/some/junk/test2.ssd
/some/junk/test3.ssd
/some/junk/test4.ssd
/some/junk/test5.ssd
...
...
...
"""
# ===========
# SETUP
# ===========
try:
import serene
except ImportError as e:
import sys
sys.path.insert(0, '.')
import serene
import datetime
import pandas as pd
import os
from serene import SSD, Status, DataProperty, Mapping, ObjectProperty, Column, Class, DataNode, ClassNode
# We have these datasets:
#
# businessInfo.csv company,ceo,city,state
# EmployeeAddresses.csv: FirstName,LastName,address,postcode
# getCities: state,city
# getEmployees: employer,employee
# postalCodeLookup: zipcode,city,state
#
# and we want to predict these...
#
# personalInfo: name,birthDate,city,state,workplace
#
# =======================
#
# Step 1: Start with a connection to the server...
#
# =======================
sn = serene.Serene(
host='127.0.0.1',
port=8080,
)
print(sn)
#
# >>> Serene(127.0.0.1:9000)
#
# =======================
#
# Step 2: Upload some training datasets...
#
# =======================
#
# assigning the DataSet to a variable will help us keep track...
#
print()
print("First we upload some datasets...")
data_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "tests", "resources", "data")
owl_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "tests", "resources", "owl")
data_path = os.path.join(os.curdir, "tests", "resources", "data")
owl_path = os.path.join(os.curdir, "tests", "resources", "owl")
business_info = sn.datasets.upload(os.path.join(data_path, 'businessInfo.csv'))
employee_address = sn.datasets.upload(os.path.join(data_path, 'EmployeeAddresses.csv'))
get_cities = sn.datasets.upload(os.path.join(data_path, 'getCities.csv'))
get_employees = sn.datasets.upload(os.path.join(data_path, 'getEmployees.csv'))
postal_code = sn.datasets.upload(os.path.join(data_path, 'postalCodeLookup.csv'))
datasets = [business_info, employee_address, get_cities, get_employees, postal_code]
print("Done.")
#
# lets have a look...
#
for d in datasets:
print("DataSet:", d.id)
print(" ", d.description)
print(" ", d.columns)
print()
#
# employee_address is not so great, we need to combine first and last name. We
# can use pandas for this
#
print()
print("Here we edit a dataset and re-upload...")
sn.datasets.remove(employee_address)
df = pd.read_csv(os.path.join(data_path, 'EmployeeAddresses.csv'))
# move to first name/last name
df['name'] = df[['FirstName', 'LastName']].apply(
lambda row: row[0] + ' ' + row[1], axis=1
)
del df['FirstName']
del df['LastName']
#
# now we can re-upload
#
employee_address = sn.datasets.upload(df)
print("Done")
# =======================
#
# Step 3: Upload some ontologies
#
# =======================
print()
print("Now we handle the ontologies...")
#
# first let's see the ontologies that exist
#
ontologies = sn.ontologies.items
if len(ontologies):
print()
print("Displaying first ontology on the server...")
ontologies[0].show()
# input("Press enter to continue")
# #
# # The most straightforward way is to upload an OWL file directly...
# #
# ontology = sn.ontologies.upload('tests/resources/owl/dataintegration_report_ontology.ttl')
#
# print("Showing the uploaded ontology...")
#
# ontology.show()
#
# input("Press enter to continue...")
#
# #
# # Or if you want to check it first you can do...
# #
#local_ontology = serene.Ontology('tests/resources/owl/dataintegration_report_ontology.ttl')
ontology_local = serene.Ontology(os.path.join(owl_path, 'paper.ttl'))
#
# local_ontology.show()
#
# print("Showing the local ontology...")
#
# input("Press enter to continue...")
#
# #
# # Now we can upload
# #
# ontology2 = sn.ontologies.upload(local_ontology)
#
# Or you can build one programmatically
#
# ontology_local = (
# serene.Ontology()
# .uri("http://www.semanticweb.org/serene/example_ontology")
# .owl_class("Place", ["name", "postalCode"])
# .owl_class("City", is_a="Place")
# .owl_class("Person", {"name": str, "phone": int, "birthDate": datetime.datetime})
# .owl_class("Organization", {"name": str, "phone": int, "email": str})
# .owl_class("Event", {"startDate": datetime.datetime, "endDate": datetime.datetime})
# .owl_class("State", is_a="Place")
# .link("Person", "bornIn", "Place")
# .link("Organization", "ceo", "Person")
# .link("Place", "isPartOf", "Place")
# .link("Person", "livesIn", "Place")
# .link("Event", "location", "Place")
# .link("Organization", "location", "Place")
# .link("Organization", "operatesIn", "City")
# .link("Place", "nearby", "Place")
# .link("Event", "organizer", "Person")
# .link("City", "state", "State")
# .link("Person", "worksFor", "Organization"))
# Have a quick look...
#
ontology_local.show()
input("press any key to continue...")
#
# or output to an owl file if you want...
ontology_local.to_turtle(os.path.join(owl_path, 'test.ttl'))
#
print()
print("Displaying local ontology...")
# input("Press enter to continue...")
#
# If ok, we can upload this to the server as well...
#
ontology = sn.ontologies.upload(ontology_local)
# ===============
#
# Step 4: Now start labelling some datasets with SSDs
#
# ===============
# First lets have a look at the datasets...
for d in datasets:
print("DataSet:", d.id)
print(" ", d.description)
print(d.sample)
print()
# And at the datanodes we'll need to map
print("DataNodes:")
print(ontology.data_nodes)
# businessInfo.csv company,ceo,city,state
# EmployeeAddresses.csv: name,address,postcode
# getCities: state,city
# getEmployees: employer,employee
# postalCodeLookup: zipcode,city,state
business_info_ssd = (sn
.SSD(business_info, ontology, name='business-info')
.map(Column("company"), DataNode(ClassNode("Organization"), "name"))
.map(Column("ceo"), DataNode(ClassNode("Person"), "name"))
.map(Column("city"), DataNode(ClassNode("City"), "name"))
.map(Column("state"), DataNode(ClassNode("State"), "name"))
.link("Organization", "operatesIn", "City")
.link("Organization", "ceo", "Person")
.link("City", "state", "State"))
business_info_ssd.show(outfile="businessInfo.dot")
print()
print("Displaying businessInfo.csv Semantic Source Description (SSD)")
# input("press any key to continue...")
#
# We also have just a string shorthand...
#
employee_address_ssd = (sn
.SSD(employee_address, ontology, name='employee-addr')
.map("name", "Person.name")
.map("address", "Place.name")
.map("postcode", "Place.postalCode")
.link("Person", "livesIn", "Place"))
employee_address_ssd.show()
print()
print("Displaying EmployeeAdddress.csv Semantic Source Description (SSD)")
# input("press any key to continue...")
get_cities_ssd = (sn
.SSD(get_cities, ontology, name='cities')
.map(Column("city"), DataNode(ClassNode("City"), "name"))
.map(Column("state"), DataNode(ClassNode("State"), "name"))
.link("City", "state", "State"))
get_cities_ssd.show()
print()
print("Displaying getCities.csv Semantic Source Description (SSD)")
# input("press any key to continue...")
get_employees_ssd = (sn
.SSD(get_employees, ontology, name='employees')
.map(Column("employer"), DataNode(ClassNode("Organization"), "name"))
.map(Column("employee"), DataNode(ClassNode("Person"), "name"))
.link("Person", "worksFor", "Organization"))
get_employees_ssd.show()
print()
print("Displaying getEmployees.csv Semantic Source Description (SSD)")
# input("press any key to continue...")
postal_code_ssd = (sn
.SSD(postal_code, ontology, name='postal-code')
.map(Column("zipcode"), "Place.postalCode")
.map(Column("city"), "City.name")
.map(Column("state"), "State.name")
.link("City", "state", "State")
.link("City", "isPartOf", "Place")
)
postal_code_ssd.show()
print()
print("Displaying postalCodeLookup.csv Semantic Source Description (SSD)")
# input("press any key to continue...")
postal_code_ssd2 = (sn
.SSD(postal_code, ontology, name='postal-code')
.map(Column("zipcode"), "Place.postalCode")
.map(Column("city"), "City.name")
.map(Column("state"), "State.name")
.link("City", "state", "State")
.link("City", "isPartOf", "Place")
)
postal_code_ssd2.show()
# upload all these to the server. Here we ignore the return value (the SSD object will be updated)
for ssd in [business_info_ssd, employee_address_ssd, get_cities_ssd, get_employees_ssd, postal_code_ssd]:
sn.ssds.upload(ssd)
# ==========
#
# Step 5. Create an Octopus data integration object...
#
# ==========
octo_local = sn.Octopus(
ssds=[
business_info_ssd,
employee_address_ssd,
get_cities_ssd,
get_employees_ssd,
postal_code_ssd
],
ontologies=[ontology],
name='octopus-test',
description='Testing example for places and companies',
resampling_strategy="NoResampling", # optional
num_bags=100, # optional
bag_size=10, # optional
model_type="randomForest",
modeling_props={
"compatibleProperties": True,
"ontologyAlignment": False,
"addOntologyPaths": False,
"mappingBranchingFactor": 50,
"numCandidateMappings": 10,
"topkSteinerTrees": 50,
"multipleSameProperty": False,
"confidenceWeight": 1.0,
"coherenceWeight": 1.0,
"sizeWeight": 0.5,
"numSemanticTypes": 10,
"thingNode": False,
"nodeClosure": True,
"propertiesDirect": True,
"propertiesIndirect": True,
"propertiesSubclass": True,
"propertiesWithOnlyDomain": True,
"propertiesWithOnlyRange": True,
"propertiesWithoutDomainRange": False,
"unknownThreshold": 0.05
},
feature_config={
"activeFeatures": [
"num-unique-vals",
"prop-unique-vals",
"prop-missing-vals",
"ratio-alpha-chars",
"prop-numerical-chars",
"prop-whitespace-chars",
"prop-entries-with-at-sign",
"prop-entries-with-hyphen",
"prop-range-format",
"is-discrete",
"entropy-for-discrete-values",
"shannon-entropy"
],
"activeFeatureGroups": [
"inferred-data-type",
"char-dist-features",
"stats-of-text-length",
"stats-of-numeric-type",
# "prop-instances-per-class-in-knearestneighbours",
# "mean-character-cosine-similarity-from-class-examples",
# "min-editdistance-from-class-examples",
# "min-wordnet-jcn-distance-from-class-examples",
# "min-wordnet-lin-distance-from-class-examples"
],
"featureExtractorParams": [
{
"name": "prop-instances-per-class-in-knearestneighbours",
"num-neighbours": 3
}, {
"name": "min-editdistance-from-class-examples",
"max-comparisons-per-class": 3
}, {
"name": "min-wordnet-jcn-distance-from-class-examples",
"max-comparisons-per-class": 3
}, {
"name": "min-wordnet-lin-distance-from-class-examples",
"max-comparisons-per-class": 3
}
]
}
)
# add this to the endpoint...
print("Now we upload to the server")
octo = sn.octopii.upload(octo_local)
# =======================
#
# Step 6. Train
#
# =======================
print()
print("Next we can train the Octopus")
print("The initial state for {} is {}".format(octo.id, octo.state))
print("Training...")
octo.train()
print("Done.")
print("The final state for {} is {}".format(octo.id, octo.state))
if octo.state.status in {Status.ERROR}:
print("Something went wrong. Failed to train the Octopus.")
# exit()
# =======================
#
# Step 7. Run predictions
#
# =======================
personal_info = sn.datasets.upload(os.path.join(data_path,'Employees.csv'))
predicted = octo.predict(personal_info)
print()
print("Predicted schema matcher results::")
print()
schema_results = octo.matcher_predict(personal_info, features=True)
print(schema_results)
schema_results.to_csv("employees_features.csv", index=False)
input("Press any key to continue...")
print()
print("Predicted results::")
print()
for pred in predicted:
print(pred.score)
pred.ssd.show()
#input("Press enter to continue...")
# =======================
#
# Step 7. Correct the predicted result...
#
# =======================
print("the best is number 0!")
print("Fixing birthdate column")
predicted_ssd = predicted[0].ssd
predicted_ssd.show()
print("Showing first guess...")
input("Press any key to continue...")
predicted_ssd.remove(Column("birthDate"))
predicted_ssd.show()
print("Removing birth date assignment")
input("Press any key to continue...")
predicted_ssd.map(Column("birthDate"), DataNode(ClassNode("Person"), "birthDate"))
predicted_ssd.link("Person", "worksFor", "Organization")
predicted_ssd.show(outfile="personInfo.dot")
print("Adding correct birthdate node")
input("Press any key to continue...")
# =======================
#
# Step 8. Confirm the result and add to the training data on the server...
#
# =======================
new_ssd = sn.ssds.upload(predicted_ssd)
octo = sn.octopii.update(octo.add(new_ssd))
# =======================
#
# Step 9. Extract the mappings
#
# =======================
maps = octo.mappings(DataNode(ClassNode("Person"), "name"))
print()
print("These are the columns mapped to Person.name:")
for dn in maps:
print(dn, "which is in", dn.dataset)
print()