-
Notifications
You must be signed in to change notification settings - Fork 0
/
mls-import.py
941 lines (766 loc) · 34.8 KB
/
mls-import.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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
import argparse
from pprint import pprint
from knora import KnoraError, Knora, BulkImport, IrisLookup, ListsLookup
from xml.dom.minidom import parse
import xml.dom.minidom
import json
import datetime
import jdcal
import os
import sys
import inspect
import time
article_type_lut = {
'Person': 'person',
'Sache': 'thing',
'Ort': 'location',
'Institution': 'institution',
'Liste': 'List',
'Verweis': 'person' # quick fix for error in the data. Verweis is not a valid value for article type!
}
sex_lut = {
'männlich': 'male',
'weiblich': 'female',
'weiblich & männliche Gruppe': 'male+female'
}
# creates a lut allowing to lookup lemma1 to lemma2 links
def lemma2lemma_lut():
print("creating lemma2lemma lut ...")
xmlfile = './data/lemma1_x_lemma2.xml'
valpos = get_valpos(xmlfile)
rows = get_rows(xmlfile)
lut = {}
for row in rows:
datas = row.getElementsByTagName("DATA")
i = 0
for data in datas:
if data.firstChild is not None:
if valpos[i] == "PKF_Lemma1":
lemma1 = data.firstChild.nodeValue
if valpos[i] == "PKF_Lemma2":
lemma2 = data.firstChild.nodeValue
i += 1
# store the lemma1 ID as key and lemma2 ID as value
# if lemma1 ID exists, append lemma2 ID to existing array of values
try:
l1 = lut[lemma1]
l1.append("LM_" + lemma2)
except KeyError:
lut[lemma1] = ["LM_" + lemma2]
# pprint(lut)
print("creating lemma2lemma lut finished.")
return lut
# converts the deceased key into a unique value
def convert_deceased_key(key):
deceased_lut = {
'Ja': 'dec_ja',
'Nein': 'dec_nein',
'IMW': 'dec_imw',
'Umfeld': 'dec_umfeld',
'Irrelevant': 'dec_irrelevant',
'Verweis': 'dec_verweis',
'Sache / Ort': 'dec_sache_ort',
}
try:
res = deceased_lut[key]
except KeyError:
res = None
return res
def convert_relevance_key(key):
relevance_lut = {
'Ja': 'rel_ja',
'1': 'rel_1',
'2': 'rel_2',
'3': 'rel_3',
'4': 'rel_4',
'5': 'rel_5',
'Nein': 'rel_nein',
'Ausnahmefall': 'rel_ausnahmefall',
'Sache / Ort': 'rel_sache_ort',
'Irrelevant': 'rel_irrelevant'
}
try:
res = relevance_lut[key]
except KeyError:
res = None
return res
def convert_to_julian_day_count(datestring):
# data cleaning
if "." not in datestring:
fmt = "%Y"
else:
fmt = '%d.%m.%Y'
dt = datetime.datetime.strptime(datestring, fmt)
jdc = sum(jdcal.gcal2jd(dt.year, dt.month, dt.day))
print("convert_to_julian_day_count - datestring: " + datestring + ", JDC: " + str(jdc))
return jdc
# load the lists.json file
lists_json_string = open("lists.json").read()
ll = ListsLookup(json.loads(lists_json_string))
def get_valpos(xmlfile, debug: bool = False):
DOMTree = xml.dom.minidom.parse(xmlfile)
collection = DOMTree.documentElement
fields = collection.getElementsByTagName("FIELD")
valpos = []
for field in fields:
name = field.getAttribute('NAME')
valpos.append(name)
if debug is True:
pprint(valpos)
return valpos
def get_rows(xmlfile):
DOMTree = xml.dom.minidom.parse(xmlfile)
collection = DOMTree.documentElement
rows = collection.getElementsByTagName("ROW")
return rows
def create_library_resources(xmlfile, bulk: BulkImport,
debug: bool = False):
"""Creates mls:Library resources"""
print("==> {0} started ...".format(inspect.currentframe().f_code.co_name))
start_time = time.time()
valpos = get_valpos(xmlfile)
rows = get_rows(xmlfile)
for row in rows:
datas = row.getElementsByTagName("DATA")
i = 0
rec_id = 0
record = {}
for data in datas:
if data.firstChild is not None:
if valpos[i] == "PK_Bibl":
rec_id = data.firstChild.nodeValue
if valpos[i] == "Beschreibung":
record["hasLibrarydescription"] = data.firstChild.nodeValue
if valpos[i] == "Bibliothekssigle_ohne_Land":
record["hasSigle"] = data.firstChild.nodeValue
if valpos[i] == "Online_Katalog":
record["hasCatalogue"] = data.firstChild.nodeValue.strip("#")
if valpos[i] == "Web_site":
record["hasLibraryweblink"] = data.firstChild.nodeValue.strip("#")
i += 1
if record.get("hasSigle") is None:
record["hasSigle"] = "XXX"
if debug is True:
print("------------------------------------------")
print("ID=" + str(rec_id))
pprint(record)
print("------------------------------------------")
bulk.add_resource(
'Library',
'LIB_' + str(rec_id), record["hasSigle"], record)
print("==> ... {0} - {1} - finished in {2} seconds.".format(
inspect.currentframe().f_code.co_name,
rows.length,
time.time() - start_time)
)
def create_lemma_resources(xmlfile,
l2l_lut,
bulk: BulkImport,
debug: bool = False):
"""Creates mls:Lemma resources"""
print("==> {0} started ...".format(inspect.currentframe().f_code.co_name))
start_time = time.time()
valpos = get_valpos(xmlfile)
rows = get_rows(xmlfile)
for row in rows:
cols = row.getElementsByTagName("COL")
rec_id = 0
i = 0
record = {}
for col in cols:
datas = col.getElementsByTagName("DATA") # some columns can have multiple data tags
data = datas.item(0) # we are only interested in the first one
if data is not None:
if data.firstChild is not None: # and only look inside if there is some data inside the data tag
if valpos[i] == "PK_Lemma":
rec_id = data.firstChild.nodeValue
if valpos[i] == "Lemma":
record["hasLemmaText"] = data.firstChild.nodeValue
rec_label = data.firstChild.nodeValue
# if valpos[i] == "Beschreibung":
# record["hasLemmaDescription"] = data.firstChild.nodeValue
# rec_label = data.firstChild.nodeValue
if valpos[i] == "Artikeltyp":
record["hasLemmaType"] = ll.get_list_node_iri("articletype",
article_type_lut[data.firstChild.nodeValue])
if valpos[i] == "Geschlecht":
record["hasSex"] = ll.get_list_node_iri("sex", sex_lut[data.firstChild.nodeValue])
if valpos[i] == "Kommentar":
record["hasLemmaComment"] = data.firstChild.nodeValue
rec_label = data.firstChild.nodeValue
if valpos[i] == "Anfangasdatum":
record["hasStartDate"] = data.firstChild.nodeValue
if valpos[i] == "Information_Anfangsdatum":
record["hasStartDateInfo"] = data.firstChild.nodeValue
if valpos[i] == "Enddatum":
record["hasEndDate"] = data.firstChild.nodeValue
if valpos[i] == "Information_Enddatum":
record["hasEndDateInfo"] = data.firstChild.nodeValue
if valpos[i] == "GND":
record["hasGnd"] = data.firstChild.nodeValue
if valpos[i] == "Jahrhundertangabe":
record["hasCentury"] = data.firstChild.nodeValue
if valpos[i] == "VIAF":
record["hasViaf"] = data.firstChild.nodeValue
if valpos[i] == "Vorname":
record["hasGivenName"] = data.firstChild.nodeValue
if valpos[i] == "Familienname":
record["hasFamilyName"] = data.firstChild.nodeValue
if valpos[i] == "Varianten":
record["hasVariants"] = data.firstChild.nodeValue
if valpos[i] == "Verstorben":
record["hasDeceasedValue"] = ll.get_list_node_iri("deceased",
convert_deceased_key(
data.firstChild.nodeValue))
if valpos[i] == "Relevantes Lemma":
record["hasRelevanceValue"] = ll.get_list_node_iri("relevance",
convert_relevance_key(
data.firstChild.nodeValue))
if valpos[i] == "Pseudonym":
record["hasPseudonym"] = data.firstChild.nodeValue
# try to find sublemmata for current lemma
try:
l2s = l2l_lut[rec_id]
record["hasSubLemma"] = l2s
except KeyError:
pass
i += 1
if record.get("hasLemmaText") is None:
record["hasLemmaText"] = "XXX"
if debug is True:
print("------------------------------------------")
print("ID=" + str(rec_id))
pprint(record)
print("------------------------------------------")
bulk.add_resource(
'Lemma',
'LM_' + str(rec_id), record["hasLemmaText"], record)
print("==> ... {0} - {1} - finished in {2} seconds.".format(
inspect.currentframe().f_code.co_name,
rows.length,
time.time() - start_time)
)
def create_occupation_resources(xmlfile,
bulk: BulkImport,
debug: bool = False):
"""Creates mls:Occupation resources"""
print("==> {0} started ...".format(inspect.currentframe().f_code.co_name))
start_time = time.time()
valpos = get_valpos(xmlfile)
rows = get_rows(xmlfile)
for row in rows:
cols = row.getElementsByTagName("COL")
i = 0
rec_id = 0
record = {}
for col in cols:
datas = col.getElementsByTagName("DATA") # some columns can have multiple data tags
data = datas.item(0) # we are only interested in the first one
if data is not None:
if data.firstChild is not None: # and only look inside if there is some data inside the data tag
if valpos[i] == "PK_Wert":
rec_id = data.firstChild.nodeValue
if valpos[i] == "Wert_Personentätigkeit":
record["hasOccupation"] = data.firstChild.nodeValue
i += 1
# only create occupation resources if the hasOccupation field has a value
if record.get("hasOccupation") is not None:
if debug is True:
print("------------------------------------------")
print("ID=" + str(rec_id))
pprint(record)
print("------------------------------------------")
bulk.add_resource(
'Occupation',
'OCC_' + str(rec_id), rec_id, record)
print("==> ... {0} - {1} - finished in {2} seconds.".format(
inspect.currentframe().f_code.co_name,
rows.length,
time.time() - start_time)
)
def create_lemma_occupation_resources(xmlfile, bulk: BulkImport,
lemma_iris_lookup: IrisLookup,
occupation_iris_lookup: IrisLookup,
debug: bool = False):
"""Creates mls:LemmaOccupation resources"""
print("==> {0} started ...".format(inspect.currentframe().f_code.co_name))
start_time = time.time()
valpos = get_valpos(xmlfile)
rows = get_rows(xmlfile)
for row in rows:
datas = row.getElementsByTagName("DATA")
i = 0
rec_id = 0
record = {}
for data in datas:
if data.firstChild is not None:
if valpos[i] == "PK_Lemma_x_Wert":
rec_id = data.firstChild.nodeValue
if valpos[i] == "PKF_Lemma":
record["hasLOLinkToLemma"] = lemma_iris_lookup.get_resource_iri('LM_' + data.firstChild.nodeValue)
if valpos[i] == "PKF_Wert":
record["hasLOLinkToOccupation"] = occupation_iris_lookup.get_resource_iri(
'OCC_' + data.firstChild.nodeValue)
if valpos[i] == "Kommentar":
record["hasLOComment"] = data.firstChild.nodeValue
i += 1
# filter out links to dead-ends
if record.get("hasLOLinkToLemma") is None:
pass
elif record.get("hasLOLinkToOccupation") is None:
pass
else:
if debug is True:
print("------------------------------------------")
print("ID=" + str(rec_id))
pprint(record)
print("------------------------------------------")
bulk.add_resource(
'LemmaOccupation',
'LO_' + str(rec_id), rec_id, record)
print("==> ... {0} - {1} - finished in {2} seconds.".format(
inspect.currentframe().f_code.co_name,
rows.length,
time.time() - start_time)
)
def create_location_resources(xmlfile,
bulk: BulkImport,
debug: bool = False):
"""Creates mls:Location resources"""
print("==> {0} started ...".format(inspect.currentframe().f_code.co_name))
start_time = time.time()
valpos = get_valpos(xmlfile)
rows = get_rows(xmlfile)
for row in rows:
cols = row.getElementsByTagName("COL")
i = 0
rec_id = 0
record = {}
for col in cols:
datas = col.getElementsByTagName("DATA") # some columns can have multiple data tags
data = datas.item(0) # we are only interested in the first one
if data is not None:
if data.firstChild is not None:
if valpos[i] == "PK_Wert":
rec_id = data.firstChild.nodeValue
if valpos[i] == "Land":
record["hasCountry"] = data.firstChild.nodeValue
if valpos[i] == "Ortsname":
record["hasPlacename"] = data.firstChild.nodeValue
if valpos[i] == "Kanton 1":
record["hasCanton"] = data.firstChild.nodeValue
if valpos[i] == "Kommentar Ort":
record["hasLocationComment"] = data.firstChild.nodeValue
i += 1
# only create location resource if the hasCountry field has a value
if record.get("hasCountry") is not None:
if debug is True:
print("------------------------------------------")
print("ID=" + str(rec_id))
pprint(record)
print("------------------------------------------")
bulk.add_resource(
'Location',
'LOC_' + str(rec_id), rec_id, record)
print("==> ... {0} - {1} - finished in {2} seconds.".format(
inspect.currentframe().f_code.co_name,
rows.length,
time.time() - start_time)
)
def create_lemma_location_resources(xmlfile,
bulk: BulkImport,
lemma_iris_lookup: IrisLookup,
location_iris_lookup: IrisLookup,
debug: bool = False):
"""Creates mls:LemmaLocation resources"""
print("==> {0} started ...".format(inspect.currentframe().f_code.co_name))
start_time = time.time()
valpos = get_valpos(xmlfile)
rows = get_rows(xmlfile)
for row in rows:
datas = row.getElementsByTagName("DATA")
i = 0
rec_id = 0
record = {}
for data in datas:
if data.firstChild is not None:
if valpos[i] == "PK_Lemma_x_Ort":
rec_id = data.firstChild.nodeValue
if valpos[i] == "PKF_Lemma":
record["hasLLLinkToLemma"] = lemma_iris_lookup.get_resource_iri('LM_' + data.firstChild.nodeValue)
if valpos[i] == "PKF_Wert":
record["hasLLLinkToLocation"] = location_iris_lookup.get_resource_iri(
'LOC_' + data.firstChild.nodeValue)
if valpos[i] == "Bezug zum Ort":
record["hasLLRelation"] = data.firstChild.nodeValue
if valpos[i] == "Kommentar":
record["hasLLComment"] = data.firstChild.nodeValue
i += 1
# filter out links to dead-ends
if record.get("hasLLLinkToLemma") is None:
pass
elif record.get("hasLLLinkToLocation") is None:
pass
else:
if debug is True:
print("------------------------------------------")
print("ID=" + str(rec_id))
pprint(record)
print("------------------------------------------")
bulk.add_resource(
'LemmaLocation',
'LL_' + str(rec_id), rec_id, record)
print("==> ... {0} - {1} - finished in {2} seconds.".format(
inspect.currentframe().f_code.co_name,
rows.length,
time.time() - start_time)
)
def create_lexicon_resources(xmlfile, bulk: BulkImport, debug: bool = False):
"""Creates mls:Lexicon resources"""
print("==> {0} started ...".format(inspect.currentframe().f_code.co_name))
start_time = time.time()
valpos = get_valpos(xmlfile)
rows = get_rows(xmlfile)
for row in rows:
cols = row.getElementsByTagName("COL")
i = 0
rec_id = 0
record = {}
for col in cols:
datas = col.getElementsByTagName("DATA") # some columns can have multiple data tags
data = datas.item(0) # we are only interested in the first one
if data is not None:
if data.firstChild is not None:
if valpos[i] == "PK_Lexikon":
rec_id = data.firstChild.nodeValue
if valpos[i] == "Zitierform":
record["hasCitationForm"] = data.firstChild.nodeValue
if valpos[i] == "kürzel":
record["hasShortname"] = data.firstChild.nodeValue
if valpos[i] == "Zitierform":
record["hasCitationForm"] = data.firstChild.nodeValue
if valpos[i] == "Kommentar":
record["hasLexiconComment"] = data.firstChild.nodeValue
if valpos[i] == "Scan bearbeitet von":
record["hasScanVendor"] = data.firstChild.nodeValue
if valpos[i] == "Scan fertig":
record["hasScanFinished"] = data.firstChild.nodeValue
if valpos[i] == "OCR bearbeitet von":
record["hasOCRVendor"] = data.firstChild.nodeValue
if valpos[i] == "OCR fertig":
record["hasOCRFinished"] = data.firstChild.nodeValue
if valpos[i] == "Einträge bearbeitet von":
record["hasEditVendor"] = data.firstChild.nodeValue
if valpos[i] == "Einträge fertig":
record["hasEditFinished"] = data.firstChild.nodeValue
if valpos[i] == "Jahr":
record["hasYear"] = data.firstChild.nodeValue
i += 1
if debug is True:
print("------------------------------------------")
print("ID=" + str(rec_id))
pprint(record)
print("------------------------------------------")
bulk.add_resource(
'Lexicon',
'LX_' + str(rec_id), rec_id, record)
print("==> ... {0} - {1} - finished in {2} seconds.".format(
inspect.currentframe().f_code.co_name,
rows.length,
time.time() - start_time)
)
def create_article_resources(
xmlfile,
bulk: BulkImport,
lemma_iris_lookup: IrisLookup,
lexicon_iris_lookup: IrisLookup,
debug: bool = False):
"""Creates mls:Article resources"""
print("==> {0} started ...".format(inspect.currentframe().f_code.co_name))
start_time = time.time()
valpos = get_valpos(xmlfile)
rows = get_rows(xmlfile)
for row in rows:
cols = row.getElementsByTagName("COL")
i = 0
rec_id = 0
record = {}
for col in cols:
datas = col.getElementsByTagName("DATA") # some columns can have multiple data tags
data = datas.item(0) # we are only interested in the first one
if data is not None:
if data.firstChild is not None:
if valpos[i] == "PK_Artikel":
rec_id = data.firstChild.nodeValue
if valpos[i] == "PKF_Lemma":
record["hasALinkToLemma"] = lemma_iris_lookup.get_resource_iri(
'LM_' + data.firstChild.nodeValue)
if valpos[i] == "PKF_Lexikon":
record["hasALinkToLexicon"] = lexicon_iris_lookup.get_resource_iri(
'LX_' + data.firstChild.nodeValue)
if valpos[i] == "Seite":
record["hasPages"] = data.firstChild.nodeValue
if valpos[i] == "Artikel":
record["hasArticleText"] = data.firstChild.nodeValue
if valpos[i] == "Zeilen":
record["hasNumLines"] = data.firstChild.nodeValue
if valpos[i] == "Kommentar":
record["hasArticleComment"] = data.firstChild.nodeValue
if valpos[i] == "Zustand":
record["hasState"] = data.firstChild.nodeValue
if valpos[i] == "Interne Lexicographische Information":
record["hasInternalLex"] = data.firstChild.nodeValue
if valpos[i] == "Link":
record["hasWebLink"] = data.firstChild.nodeValue
if valpos[i] == "Kurzinformation":
record["hasShortInfo"] = data.firstChild.nodeValue
if valpos[i] == "Theaterlexikon Code":
record["hasTheaterLexCode"] = data.firstChild.nodeValue
if valpos[i] == "Dizionario Ticinese Code":
record["hasTicinoLexCode"] = data.firstChild.nodeValue
if valpos[i] == "Fonoteca Code":
record["hasFonotecacode"] = data.firstChild.nodeValue
if valpos[i] == "HLS Code":
record["hasHlsCcode"] = data.firstChild.nodeValue
if valpos[i] == "OEML Code":
record["hasOemlCode"] = data.firstChild.nodeValue
i += 1
if debug is True:
print("------------------------------------------")
print("ID=" + str(rec_id))
pprint(record)
print("------------------------------------------")
bulk.add_resource(
'Article',
'A_' + str(rec_id),
rec_id,
record
)
print("==> ... {0} - {1} - finished in {2} seconds.".format(
inspect.currentframe().f_code.co_name,
rows.length,
time.time() - start_time)
)
def create_exemplar_resources(xmlfile,
bulk: BulkImport,
lexicon_iris_lookup: IrisLookup,
library_iris_lookup: IrisLookup,
debug: bool = False):
"""Creates mls:Exemplar resources"""
print("==> {0} started ...".format(inspect.currentframe().f_code.co_name))
start_time = time.time()
valpos = get_valpos(xmlfile)
rows = get_rows(xmlfile)
for row in rows:
datas = row.getElementsByTagName("DATA")
i = 0
rec_id = 0
record = {}
for data in datas:
if data.firstChild is not None:
if valpos[i] == "PK_Exe":
rec_id = data.firstChild.nodeValue
if valpos[i] == "PKF_Bibl":
record["hasExamplarLinkToLibrary"] = library_iris_lookup.get_resource_iri(
'LIB_' + data.firstChild.nodeValue)
if valpos[i] == "PKF_Titel":
record["hasExemplarLinkToLexicon"] = lexicon_iris_lookup.get_resource_iri(
'LX_' + data.firstChild.nodeValue)
if valpos[i] == "Exemplarnr":
record["hasExamplarNumber"] = data.firstChild.nodeValue
if valpos[i] == "Signatur":
record["hasExemplarSignatur"] = data.firstChild.nodeValue
if valpos[i] == "Kommentar":
record["hasExamplarComment"] = data.firstChild.nodeValue
i += 1
# filter out links to dead-ends
if record.get("hasExamplarLinkToLibrary") is None:
pass
elif record.get("hasExemplarLinkToLexicon") is None:
pass
else:
if debug is True:
print("------------------------------------------")
print("ID=" + str(rec_id))
pprint(record)
print("------------------------------------------")
bulk.add_resource(
'Exemplar',
'EX_' + str(rec_id), rec_id, record)
print("==> ... {0} - {1} - finished in {2} seconds.".format(
inspect.currentframe().f_code.co_name,
rows.length,
time.time() - start_time)
)
def create_lexicon_lexicon_resources(xmlfile,
bulk: BulkImport,
lexicon_iris_lookup: IrisLookup,
debug: bool = False):
"""Creates mls:LexiconLexicon resources"""
print("==> {0} started ...".format(inspect.currentframe().f_code.co_name))
start_time = time.time()
valpos = get_valpos(xmlfile)
rows = get_rows(xmlfile)
for row in rows:
datas = row.getElementsByTagName("DATA")
i = 0
rec_id = ""
record = {}
for data in datas:
if data.firstChild is not None:
if valpos[i] == "PKF_TitelA":
record["hasLexiconLinkToParent"] = lexicon_iris_lookup.get_resource_iri(
'LX_' + data.firstChild.nodeValue)
rec_id += data.firstChild.nodeValue
if valpos[i] == "PKF_TitelB":
record["hasLexiconLinkToChild"] = lexicon_iris_lookup.get_resource_iri(
'LX_' + data.firstChild.nodeValue)
rec_id += '-' + data.firstChild.nodeValue
if valpos[i] == "Kommentar":
record["hasLexiconLexiconComment"] = data.firstChild.nodeValue
i += 1
# filter out links to dead-ends
if record.get("hasLexiconLinkToParent") is None:
pass
elif record.get("hasLexiconLinkToChild") is None:
pass
else:
if debug is True:
print("------------------------------------------")
print("ID=" + str(rec_id))
pprint(record)
print("------------------------------------------")
bulk.add_resource(
'LexiconLexicon',
'LXLX_' + str(rec_id), rec_id, record)
print("==> ... {0} - {1} - finished in {2} seconds.".format(
inspect.currentframe().f_code.co_name,
rows.length,
time.time() - start_time)
)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--server", type=str, default="http://0.0.0.0:3333", help="URL of the Knora server")
parser.add_argument("-u", "--user", type=str, default="[email protected]", help="Username for Knora")
# parser.add_argument("-u", "--user", type=str, default="[email protected]", help="Username for Knora")
parser.add_argument("-p", "--password", type=str, default="test", help="The password for login")
parser.add_argument("-P", "--projectcode", type=str, default="0807", help="Project short code")
parser.add_argument("-O", "--ontoname", type=str, default="mls", help="Shortname of ontology")
parser.add_argument("-x", "--xml", type=str, default="mls-bulk.xml", help="Name of bulk import XML-File")
parser.add_argument("--start", default="1", help="Start with given line")
parser.add_argument("--stop", default="all", help="End with given line ('all' reads all lines")
args = parser.parse_args()
con = Knora(args.server)
con.login(args.user, args.password)
schema = con.create_schema(args.projectcode, args.ontoname)
if not os.path.exists("./out"):
os.makedirs("./out")
# create Library resources
library_data_xml = './data/bibliotheken.xml'
library_bulk_object = BulkImport(schema)
create_library_resources(library_data_xml, library_bulk_object)
print("==> Library upload start ...")
start_time = time.time()
r = library_bulk_object.upload(args.user, args.password, "localhost", "3333")
library_iris_lookup = IrisLookup(r)
print("==> Library upload finished (%s seconds)." % (time.time() - start_time))
# create mls:Lemma resources
l2l_lut = lemma2lemma_lut()
lemma_data_xml = './data/lemma.xml'
lemma_bulk_object = BulkImport(schema)
create_lemma_resources(lemma_data_xml, l2l_lut, lemma_bulk_object)
print("==> Lemma upload start ...")
start_time = time.time()
r = lemma_bulk_object.upload(args.user, args.password, "localhost", "3333")
lemma_iris_lookup = IrisLookup(r)
print("==> Lemma upload finished (%s seconds)." % (time.time() - start_time))
# create Occupation resources
occupation_data_xml = './data/werte.xml'
occupation_bulk_object = BulkImport(schema)
create_occupation_resources(occupation_data_xml, occupation_bulk_object)
print("==> Occupation upload start ...")
start_time = time.time()
r = occupation_bulk_object.upload(args.user, args.password, "localhost", "3333")
occupation_iris_lookup = IrisLookup(r)
print("==> Occupation upload finished (%s seconds)." % (time.time() - start_time))
# create LemmaOccupation resources
lemma_occupation_data_xml = './data/lemma_x_wert.xml'
lemma_occupation_bulk_object = BulkImport(schema)
create_lemma_occupation_resources(lemma_occupation_data_xml,
lemma_occupation_bulk_object,
lemma_iris_lookup,
occupation_iris_lookup)
print("==> LemmaOccupation upload start ...")
start_time = time.time()
r = lemma_occupation_bulk_object.upload(args.user, args.password, "localhost", "3333")
lemma_occupation_iris_lookup = IrisLookup(r)
print("==> LemmaOccupation upload finished (%s seconds)." % (time.time() - start_time))
# create Location resources
location_data_xml = './data/werte.xml'
location_bulk_object = BulkImport(schema)
create_location_resources(location_data_xml, location_bulk_object)
print("==> Location upload start ...")
start_time = time.time()
r = location_bulk_object.upload(args.user, args.password, "localhost", "3333")
location_iris_lookup = IrisLookup(r)
print("==> Location upload finished (%s seconds)." % (time.time() - start_time))
# create LemmaLocation resources
lemma_location_data_xml = './data/lemma_x_ort.xml'
lemma_location_bulk_object = BulkImport(schema)
create_lemma_location_resources(lemma_location_data_xml,
lemma_location_bulk_object,
lemma_iris_lookup,
location_iris_lookup)
print("==> LemmaLocation upload start ...")
start_time = time.time()
r = lemma_location_bulk_object.upload(args.user, args.password, "localhost", "3333")
lemma_location_iris_lookup = IrisLookup(r)
print("==> LemmaLocation upload finished (%s seconds)." % (time.time() - start_time))
# create Lexicon resources
lexicon_data_xml = './data/lexikon.xml'
lexicon_bulk_object = BulkImport(schema)
create_lexicon_resources(lexicon_data_xml, lexicon_bulk_object)
print("==> Lexicon upload start ...")
start_time = time.time()
r = lexicon_bulk_object.upload(args.user, args.password, "localhost", "3333")
lexicon_iris_lookup = IrisLookup(r)
print("==> Lexicon upload finished (%s seconds)." % (time.time() - start_time))
# create Article resources
article_data_xml = './data/artikel.xml'
article_bulk_object = BulkImport(schema)
create_article_resources(article_data_xml,
article_bulk_object,
lemma_iris_lookup,
lexicon_iris_lookup)
print("==> Article upload start ...")
start_time = time.time()
r = article_bulk_object.upload(args.user, args.password, "localhost", "3333")
article_iris_lookup = IrisLookup(r)
print("==> Article upload finished (%s seconds)." % (time.time() - start_time))
# create Exemplar resources
exemplar_data_xml = './data/exemplar.xml'
exemplar_bulk_object = BulkImport(schema)
create_exemplar_resources(exemplar_data_xml,
exemplar_bulk_object,
lexicon_iris_lookup,
library_iris_lookup)
print("==> Exemplar upload start ...")
start_time = time.time()
r = exemplar_bulk_object.upload(args.user, args.password, "localhost", "3333")
exemplar_iris_lookup = IrisLookup(r)
print("==> Exemplar upload finished (%s seconds)." % (time.time() - start_time))
# create LexiconLexicon resources
lexlex_data_xml = './data/titelA_x_titelB.xml'
lexlex_bulk_object = BulkImport(schema)
create_lexicon_lexicon_resources(lexlex_data_xml,
lexlex_bulk_object,
lexicon_iris_lookup)
print("==> LexiconLexicon upload start ...")
start_time = time.time()
r = lexlex_bulk_object.upload(args.user, args.password, "localhost", "3333")
lexlex_iris_lookup = IrisLookup(r)
print("==> LexiconLexicon upload finished (%s seconds)." % (time.time() - start_time))
con = None
sys.exit()
if __name__ == "__main__":
main()