-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmergeLOCandDBpedia.py
844 lines (509 loc) · 21.6 KB
/
mergeLOCandDBpedia.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
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
import sys, os, re, urllib, time
def main():
if not os.path.exists('data/loc_single'):
os.makedirs('data/loc_single')
dbData = open('data/jazzPeople.nt', 'r')
personNames = {}
personBirthDates = {}
personDeathDates = {}
nameCollisons = {}
matchesBothDate = []
matchesBothDateURIs = []
matchesSingleDate = []
foundCheckList = []
possibleLOC={}
allLOC = {}
for line in dbData:
quad = line.split()
if quad[1] == '<http://xmlns.com/foaf/0.1/name>':
name = ''
name = " ".join(quad[2:])
name = name[1:name[1:].find('@en')]
if len(name) < 5:
print name, line
name = name.replace('\\','')
if personNames.has_key(name) == False:
personNames[name] = quad[0]
else:
if personNames[name] != quad[0]:
if nameCollisons.has_key(name):
if quad[0] not in nameCollisons[name]:
nameCollisons[name].append(quad[0])
else:
nameCollisons[name]=[quad[0]]
print "1Name collision", name, line
print personNames[name], nameCollisons[name]
addNames = []
if name.find('"') != -1:
print name
name = name.split('"')[0].strip() + ' ' + name.split('"')[2].strip()
addNames.append(name)
#we also want to pull their name from the URL, because that is often the most common variant of their name
uri = quad[0]
name = formatName(quad[0].split('/resource/')[len(quad[0].split('/resource/'))-1])
name = name.replace('\\','')
addNames.append(name)
#print name
#remove any nick name and add that as well
if name.find('"') != -1:
print name
name = name.split('"')[0].strip() + ' ' + name.split('"')[2].strip()
addNames.append(name)
for aName in addNames:
#is this name already in the lookup:
print aName
if personNames.has_key(aName):
print "\t Name already in personNames"
#yes, is it the same uir as this one?
if personNames[aName] != quad[0]:
print "\t Name Has Diffrent URI Attached"
#no, it is a new UIR, is it aleady in the collision lookup?
if nameCollisons.has_key(aName):
print "\t Name already in collission"
#yes, is this URI already in it?
if quad[0] not in nameCollisons[aName]:
print "\t Diffrent Name, adding to it"
#no, add it
nameCollisons[aName].appen(quad[0])
else:
#no, add a new array to the collison with it
nameCollisons[aName] = [quad[0]]
print "\t Creating new collission record"
else:
print "\t not yet in personNames, adding it"
personNames[aName] = quad[0]
if quad[1] == '<http://dbpedia.org/ontology/deathDate>':
deathDate = ''
deathDate = " ".join(quad[2:])
deathDate = deathDate[1:deathDate[1:].find('-')+1]
if len(deathDate) != 4:
print "Error death date: ", line
else:
personDeathDates[quad[0]] = deathDate
#print deathDate
if quad[1] == '<http://dbpedia.org/ontology/birthDate>':
birthDate = ''
birthDate = " ".join(quad[2:])
birthDate = birthDate[1:birthDate[1:].find('-')+1]
if len(birthDate) != 4:
print "Error birth date: ", line
else:
personBirthDates[quad[0]] = birthDate
print len(personNames), len(personBirthDates), len(personDeathDates)
temp = open("db_tmp.txt","w")
for key, value in personNames.iteritems():
line = key + ' ' + value
if personBirthDates.has_key(value):
line = line + ' ' + personBirthDates[value]
if personDeathDates.has_key(value):
line = line + ' ' + personDeathDates[value]
temp.writelines(line + "\n")
for key, value in nameCollisons.iteritems():
for x in value:
line = key + ' ' + x
if personBirthDates.has_key(x):
line = line + ' ' + personBirthDates[x]
if personDeathDates.has_key(x):
line = line + ' ' + personDeathDates[x]
temp.writelines(line + "\n")
print line
locFile = open('data/personauthoritiesnames.nt.skos', 'r')
counter = 0
counterMatched = 0
print "building name list"
locDebug = open("loc_tmp.txt","w")
for line in locFile:
counter = counter+1
#if counter % 100000 == 0:
# print "procssed " + str(counter / 100000) + "00k names"
if counter % 1000000 == 0:
print "procssed ", counter / 1000000,"Million names!"
quad = line.split();
name = " ".join(quad[2:])
name = name[1:name[1:].find('@EN')]
name = name.replace('?','')
year = re.findall(r'\d{4}', name)
born = 0
died = 0
possibleNames = []
if len(year) != 0:
if len(year) == 1 and name[len(name)-1:] != '-':
if name.find(' b.') != -1:
born = year[0]
#print "Born : ",year[0]
elif name.find(' d.') != -1:
died = year[0]
#print "died : ",year[0]
elif name.find(' fl.') != -1:
born = year[0]
#print "born(flourished) : ",year[0]
elif name.find('jin shi') != -1:
born = year[0]
#print "born(third stage) : ",year[0]
elif name.find('ju ren') != -1:
born = year[0]
#print "born(second stage) : ",year[0]
elif len(re.findall(r'\d{3}\-', name)) != 0:
year = re.findall(r'\d{3}\-', name)
born = year[0][0:3]
#print "born : ", year[0][0:3]
#now get the death year
died = re.findall(r'\d{4}', name)[0]
elif len(re.findall(r'\-\d{4}', name)) != 0:
died = re.findall(r'\-\d{4}', name)[0][1:]
elif name.find(' ca. ') != -1 or name.find(' ca ') != -1:
born = year[0]
#print "born(ca) : ",year[0]
elif name.find(' b ') != -1:
born = year[0]
#print "Born : ",year[0]
elif name.find(' d ') != -1:
died = year[0]
#print "died : ",year[0]
elif name.find(' born ') != -1:
born = year[0]
#print "Born : ",year[0]
elif name.find(' died ') != -1:
died = year[0]
#print "died : ",year[0]
else:
#print name, "\n"
#print "error: cannot figure out this date, update the regex"
#we have hit like 90% of the cases here, now just stright up weird sutff, so just grab the date
born = year[0]
#print len(year)
elif len(year) == 1 and name[len(name)-1:] == '-':
born = year[0]
elif len(year) == 2:
born = year[0]
died = year[1]
elif len(year) == 3:
#they are doing "1999 or 2000 - blah blah blah" take first and last
born = year[0]
died = year[2]
elif len(year) == 4:
#they are doing "1999 or 2000 - blah blah blah" take first and last
born = year[0]
died = year[3]
else:
print name, "Coluld not process date \n"
sys.exit()
#print name, born, died
#else:
#these people would have lived < 0 bce - 999 AD, we currently do not care about them.
#if len(re.findall(r'\d{3}', name)) != 0:
#print name
#personDates[quad[0]] = [born,died]
#now process the name part
#chop off the rest where a number is detected to get rid of any date
if re.search(r'\d{1}',name) != None:
name = name[0:name.find(re.search(r'\d{1}',name).group())]
name=name.strip()
#now chop off anything past the second comma, it is not name stuff afterwards, also with 3 commas are a lot of "sir" and "duke of earl" etc, dont care about that stuff
if len(re.findall(',', name)) == 2 or len(re.findall(',', name)) == 3:
name = name.split(',')[0] + ', ' + name.split(',')[1]
#print name, '|', newname
if name.find('\"') != -1:
name = name.replace("\\",'')
if len(re.findall(',', name)) == 1:
if name.find('(') == -1:
#there is no pranthetical name
newname = name.split(',')
newname = newname[1] + ' ' + newname[0]
#print name, '|', newname
possibleNames.append(newname.strip())
#we want to add that name, but also add a version with out a middle intial, if that it is present
if len(newname.split()) == 3 and (newname.split()[1][len(newname.split()[1])-1] == '.' or len(newname.split()[1]) == 1):
newname = newname.split()[0] + ' ' + newname.split()[2]
#print "\t" + newname
possibleNames.append(newname.strip())
#we also want to add a name, that if they only have an inital for the first part and a full middle name drop the first intital
if len(newname.split()) == 3 and len(newname.split()[1]) > 2 and (newname.split()[0][len(newname.split()[0])-1] == '.' or len(newname.split()[1]) == 1):
newname = newname.split()[1] + ' ' + newname.split()[2]
#print "\t" + newname
possibleNames.append(newname.strip())
else:
#they have prenthasis in their name meaning that their long form of the name is contained in the pranthesis
newname = name.split(',')
newname = newname[1] + ' ' + newname[0]
#cut out the stuff before the pran
newname = newname[newname.find('(')+1:]
newname = newname.replace(')','')
#print name, '|', newname
possibleNames.append(newname.strip())
#now also cut out the middle inital if it is there and add that version
if len(newname.split()) == 3 and (newname.split()[1][len(newname.split()[1])-1] == '.' or len(newname.split()[1]) == 1):
newname = newname.split()[0] + ' ' + newname.split()[2]
#print "\t" + newname
possibleNames.append(newname.strip())
else:
#so here we are... the depths of the quirks
if name.find('(') != -1:
#if the very first thing is a inital, it is likely a abrrivated name and the full name is in the prans
if len(name.split()[0])==2:
if name.split()[0][1] == '.':
newname = name.split('(')[1]
newname = newname.replace(')','')
possibleNames.append(newname.strip())
#print name, '|', newname
#if len(name.split()[len(name.split())-1])==2:
# if name.split()[len(name.split())-1][1] == '.':
# print name, '|'
else:
#this will be stuff like P-King (Musician), or Shyne (Rapper), stuff we are intrested in, nicknames, so cut out the descriptor
newname = name.split('(')[0].strip()
#TODO: if we really care to take this further here is a spot where we will lose some names
#the quirks get very specific and would need a lot more rules
#print name, '|', newname
possibleNames.append(newname.strip())
else:
#print name, '|'
newname = name.strip()
#single names here, add them in
possibleNames.append(newname.strip())
#print possibleNames
#skip logic:
if int(born) != 0 and int(born) < 1875:
continue
for aPossible in possibleNames:
if personNames.has_key(aPossible):
#we have a match (!)
#add all the Ids we are going to check into a list
useURIs = []
#the main one
useURIs.append(personNames[aPossible])
#check for collision names, names that are the same but reflect diffrent URIs
if nameCollisons.has_key(aPossible):
for collison in nameCollisons[aPossible]:
useURIs.append(collison)
for useURI in useURIs:
locDebug.writelines(aPossible + ' ' + str(born) + ' ' + str(died) + "\n")
if allLOC.has_key(aPossible):
#it is in here already, see if it has this URI
if quad[0] not in allLOC[aPossible]:
allLOC[aPossible].append(quad[0])
else:
allLOC[aPossible] = [quad[0]]
didMatched = False
if personBirthDates.has_key(useURI) and personDeathDates.has_key(useURI):
if int(born) != 0 and int(died) != 0 and int(personBirthDates[useURI]) != 0 and int(personDeathDates[useURI]) != 0:
if (int(personBirthDates[useURI]) == int(born)) and (int(died) == int(personDeathDates[useURI])):
if [useURI, quad[0]] not in matchesBothDate:
didMatched=True
counterMatched = counterMatched + 1
matchesBothDate.append([useURI, quad[0]])
foundCheckList.append(useURI)
matchesBothDateURIs.append(useURI)
#print aPossible, quad[0], born, died
#print aPossible, useURI, personBirthDates[useURI], personDeathDates[useURI]
continue
#see if birth years match
if personBirthDates.has_key(useURI):
if int(personBirthDates[useURI]) == int(born) and int(personBirthDates[useURI]) != 0 and int(born) != 0:
if [useURI, quad[0]] not in matchesSingleDate:
#print personNames[aPossible], '=', quad[0]
didMatched=True
counterMatched = counterMatched + 1
matchesSingleDate.append([useURI, quad[0]])
foundCheckList.append(useURI)
#print aPossible, quad[0], born, "born match"
#print aPossible, useURI, personBirthDates[useURI]
continue
#does it have a death date match?
if personDeathDates.has_key(useURI):
if int(personDeathDates[useURI]) == int(died) and int(personDeathDates[useURI]) != 0 and int(died) != 0:
if [useURI, quad[0]] not in matchesSingleDate:
#print personNames[aPossible], '=', quad[0]
matchesSingleDate.append([useURI, quad[0]])
didMatched=True
counterMatched = counterMatched + 1
foundCheckList.append(useURI)
#print aPossible, quad[0], died, "death match"
#print aPossible, useURI, personDeathDates[useURI]
continue
#we are now going to remove any matches from matchesSingleDate where there is a perfect date match already
temp = []
for aSingleDateMatch in matchesSingleDate:
if aSingleDateMatch[0] not in matchesBothDateURIs:
temp.append(aSingleDateMatch)
else:
for x in matchesBothDate:
if x[0] == aSingleDateMatch[0]:
print "Attempted Dupe", aSingleDateMatch
print "With", x
matchesSingleDate = list(temp)
matchedSingle = []
matchedMany = []
matchedNone = []
for key, value in personNames.iteritems():
if value not in foundCheckList:
#print "Not matched " + value + ' ' + key
if allLOC.has_key(key):
if len(allLOC[key]) == 1:
#print "\tOnly one possible LOC match:" + allLOC[key][0]
matchedSingle.append([value,allLOC[key][0]])
else:
#print "\t 1+ possible LOC match:", allLOC[key]
matchedMany.append([value,allLOC[key]])
else:
matchedNone.append(value)
print " \n****Collision***\n"
for key, value in nameCollisons.iteritems():
for x in value:
if x not in foundCheckList:
#print "Not matched " + x + ' ' + key
if allLOC.has_key(key):
if len(allLOC[key]) == 1:
#print "\tOnly one possible LOC match:" + allLOC[key][0]
matchedSingle.append([x,allLOC[key][0]])
else:
#print "\t 1+ possible LOC match:", allLOC[key]
matchedMany.append([x,allLOC[key]])
else:
matchedNone.append(x)
#for key, value in possibleLOC.iteritems():
#if len(value) == 1:
#if value not in matches:
# matches.append(value)
#print key, '=', value
#make sure there are no duplicates, as in same DB to LOC records in the singles
tempCopy = []
for aSingle in matchedSingle:
add = True
for anotherSingle in tempCopy:
if aSingle[0] == anotherSingle[0] and aSingle[1] == anotherSingle[1]:
add = False
if add:
tempCopy.append(aSingle)
matchedSingle = list(tempCopy)
#now we are going to go through the singles and pull out anyone that has been added twice
#this can happen for common names born in the same year, move them to the 1->many list
matchedSingleCheck = []
matchedSingleDupes = []
for aSingle in matchedSingle:
if aSingle[0] not in matchedSingleCheck:
matchedSingleCheck.append(aSingle[0])
else:
print "Dupe in singles found:", aSingle
matchedSingleDupes.append(aSingle[0])
singleDupes = {}
tempCopy = []
print len(matchedSingle)
for aSingle in matchedSingle:
if aSingle[0] in matchedSingleDupes:
if singleDupes.has_key(aSingle[0]):
singleDupes[aSingle[0]].append(aSingle[1])
else:
singleDupes[aSingle[0]] = [aSingle[1]]
else:
tempCopy.append(aSingle)
matchedSingle = list(tempCopy)
print len(matchedSingle)
print singleDupes
#add them to the matchedmany list
for key, value in singleDupes.iteritems():
matchedMany.append([key,value])
#we now need to do the same for matchesSingleDate, they could have matched a single date true, but it could matched to other people
matchesSingleDateCheck = []
matchesSingleDateDupes = []
for aSingle in matchesSingleDate:
if aSingle[0] not in matchesSingleDateCheck:
matchesSingleDateCheck.append(aSingle[0])
else:
print "Dupe in single date found:", aSingle
matchesSingleDateDupes.append(aSingle[0])
singleDateDupes = {}
tempCopy = []
print len(matchesSingleDate)
for aSingle in matchesSingleDate:
if aSingle[0] in matchesSingleDateDupes:
if singleDateDupes.has_key(aSingle[0]):
singleDateDupes[aSingle[0]].append(aSingle[1])
else:
singleDateDupes[aSingle[0]] = [aSingle[1]]
else:
tempCopy.append(aSingle)
matchesSingleDate = list(tempCopy)
print len(matchesSingleDate)
#add them to the matchedmany list
for key, value in singleDateDupes.iteritems():
matchedMany.append([key,value])
print singleDateDupes
#TODO: This part needs to be fixed so the call to the LOC site is syncrounous, and wait for the file to be ready...
machtedSingleJazz = []
machtedSingleNoJazz = []
machtedSingleNoJazzLOC = []
for x in matchedSingle:
url = x[1]
id = formatName(url.split('/names/')[len(url.split('/names/'))-1])
foundJazz = False
if os.path.exists('data/loc_single/' + id + '.nt') == False:
os.system('wget --output-document="data/loc_single/' + id + '.nt" "http://id.loc.gov/authorities/names/' + id + '.nt"')
#sleep as a TODO fix,
time.sleep( 1.5 )
if os.path.exists('data/loc_single/' + id + '.nt'):
f = open('data/loc_single/' + id + '.nt', 'r')
for line in f:
line = line.lower()
if line.find('jazz') != -1 or line.find('music') != -1 or line.find('blues') != -1 or line.find('jazz') != -1 or line.find('vocal') != -1:
print line
foundJazz = True
f.close()
else:
print 'data/loc_single/' + id + '.nt does not exist'
if id in machtedSingleNoJazzLOC:
foundJazz = False
print "Dupe detected trying to assign" ,x
if foundJazz:
machtedSingleJazz.append(x)
machtedSingleNoJazzLOC.append(id)
else:
machtedSingleNoJazz.append(x)
print len(matchesBothDate), " BothDate Matches", len(matchesSingleDate), " Single Date Matches", len(matchedSingle), "Single LOC", len(matchedMany), "Multiple LOC matches", len(matchedNone), "No Matches"
#print len(matches)+ len(matchedSingle)+len(matchedMany) , " matched out of Total of about ", len(personNames)
print len(matchedSingle) , " = " , len(machtedSingleJazz) , " keyword found and ", len(machtedSingleNoJazz), " no keyword found"
#make the sameas files
allLines=[]
temp = open("data/sameAs_perfect.nt","w")
for value in matchesBothDate:
line = value[0] + ' <http://www.w3.org/2002/07/owl#sameAs> ' + value[1] + " . \n";
if line not in allLines:
temp.writelines(line)
allLines.append(line)
temp = open("data/sameAs_high.nt","w")
for value in matchesSingleDate:
line = value[0] + ' <http://www.w3.org/2002/07/owl#sameAs> ' + value[1] + " . \n";
if line not in allLines:
temp.writelines(line)
allLines.append(line)
temp = open("data/sameAs_medium.nt","w")
for value in machtedSingleJazz:
line = value[0] + ' <http://www.w3.org/2002/07/owl#sameAs> ' + value[1] + " . \n";
if line not in allLines:
temp.writelines(line)
allLines.append(line)
temp = open("data/sameAs_low.nt","w")
for value in machtedSingleNoJazz:
line = value[0] + ' <http://www.w3.org/2002/07/owl#sameAs> ' + value[1] + " . \n";
if line not in allLines:
temp.writelines(line)
allLines.append(line)
temp = open("data/sameAs_many.nt","w")
for value in matchedMany:
for x in value[1]:
temp.writelines(value[0] + ' <http://www.w3.org/2004/02/skos/core#closeMatch> ' + x + " . \n")
temp = open("data/sameAs_none.nt","w")
for value in matchedNone:
temp.writelines(value + ' <http://www.w3.org/2002/07/owl#sameAs> ' + '<none>' + " . \n")
def formatName(name):
#decode it to get rid of URL char codes
name = urllib.unquote(name)
#stop at the first pranathesis, so we dont get things like (jazz_player)
name = name[0:name.rfind('(')]
name = name.replace('_',' ').replace('>','').strip()
name = name.replace(",","")
name = name.replace("Jr.","Jr")
name = name.replace("Sr.","Sr")
return name
if __name__ == '__main__':
main()