-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdf_utils.py
1095 lines (852 loc) · 35.1 KB
/
rdf_utils.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
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from collections import defaultdict
from datetime import datetime
from rdflib.namespace import RDF, RDFS, XSD
from rdflib.namespace import Namespace
from rdflib import URIRef
from rdflib import Literal, BNode
from rdflib import ConjunctiveGraph, Graph
from graphviz import Digraph
SUPPORTED_LANGUAGES = {
'eng',
'nld'
}
LANGUAGE_TO_ADJECTIVE = {
'eng' : 'English',
'nld' : 'Dutch'
}
LUTYPE_TO_LU_TYPE_URL = {
'idiom' : 'http://www.lexinfo.net/ontology/3.0/lexinfo#idiom',
}
LU_TYPE_URL_TO_INFO = {
'phrasal' : {
"type" : "http://www.w3.org/2002/07/owl#Thing",
"label" : "phrasal verb",
"comment" : "most often the combination of a verb and a verb particle.",
"seeAlso" : "https://en.wikipedia.org/wiki/Phrasal_verb"
},
'endocentric compound' : {
"type" : "http://www.w3.org/2002/07/owl#Thing",
"label" : "endocentric compound",
"comment" : "most often a compound with a head and a modfier.",
"seeAlso" : "http://www.glottopedia.org/index.php/Endocentric_compound",
"subtype" : "http://www.lexinfo.net/ontology/3.0/lexinfo#compound"
},
'exocentric compound' : {
"type": "http://www.w3.org/2002/07/owl#Thing",
"label": "exocentric compound",
"comment": "a compound that lacks a head",
"seeAlso": "http://www.glottopedia.org/index.php/Exocentric_compound",
"subtype": "http://www.lexinfo.net/ontology/3.0/lexinfo#compound"
}
}
COMP_ATTR_TO_URL = {}
COMP_ATTR_TO_INFO = {
"order" : {
"type" : "http://www.w3.org/2002/07/owl#Thing",
"label" : "order",
"comment": "the order of the lexeme in the lemma (starting from 1)",
"seeAlso": "http://purl.org/olia/ubyCat.owl#position"
},
"headword" : {
"type": "http://www.w3.org/2002/07/owl#Thing",
"label" : "headword",
"comment" : "the lexeme is the head (true or false)",
"seeAlso": "http://purl.org/olia/ubyCat.owl#isHead"
},
"breakBefore" : {
"type": "http://www.w3.org/2002/07/owl#Thing",
"label" : "breakBefore",
"comment": "Can this lexeme be separated from the previous lexeme by another token?",
"seeAlso": "http://purl.org/olia/ubyCat.owl#isBreakBefore"
},
"name" : {
"type": "http://www.w3.org/2002/07/owl#Thing",
"label": "name",
"comment": "the lexeme itself",
"seeAlso" : "http://www.w3.org/ns/lemon/ontolex#writtenRep"
},
"incorporatedFE" : {
"type": "http://www.w3.org/2002/07/owl#Thing",
"label" : "incorporatedFE",
"comment" : "indicates the incorporated Frame Element of an LU, which can be indicated at the level of the lexeme and of the Lexical Unit"
}
}
def initialize_graph(g, namespace, SKOS):
"""
initialize graph with our own relationships
:return:
"""
# lu types
for lu_type, lu_type_info in LU_TYPE_URL_TO_INFO.items():
lu_type_url = f'{namespace}{lu_type.replace(" ","_")}'
LUTYPE_TO_LU_TYPE_URL[lu_type] = lu_type_url
lu_type_obj = URIRef(lu_type_url)
g.add((lu_type_obj, RDF.type, URIRef(lu_type_info['type'])))
g.add((lu_type_obj, RDFS.label, Literal(lu_type_info['label'])))
g.add((lu_type_obj, RDFS.comment, Literal(lu_type_info['comment'])))
g.add((lu_type_obj, RDFS.seeAlso, URIRef(lu_type_info['seeAlso'])))
if 'subtype' in lu_type_info:
g.add((lu_type_obj,
SKOS.broader,
URIRef(lu_type_info['subtype'])))
# component attributes
for comp_attr, comp_info in COMP_ATTR_TO_INFO.items():
comp_attr_url = f'{namespace}{comp_attr}'
COMP_ATTR_TO_URL[comp_attr] = comp_attr_url
comp_attr_obj = URIRef(comp_attr_url)
g.add((comp_attr_obj, RDF.type, URIRef(comp_info['type'])))
g.add((comp_attr_obj, RDFS.label, Literal(comp_info['label'])))
g.add((comp_attr_obj, RDFS.comment, Literal(comp_info['comment'])))
if 'seeAlso' in comp_info:
g.add((comp_attr_obj, RDFS.seeAlso, URIRef(comp_info['seeAlso'])))
def get_lexeme_info(lexeme,
premon,
frame_uri,
fn_pos_to_lexinfo,
g,
DCT,
LEMON,
LEXINFO):
"""
generate dictionary of information used per lexem
:param nltk.corpus.reader.framenet.PrettyDict lexeme: a lexeme
:rtype: dict
"""
bn_node = BNode()
lu_id = lexeme.get('lu_id', None)
if lu_id is not None:
le_obj_of_component = get_le_uri(g=g,
DCT=DCT,
LEMON=LEMON,
lu_identifier=lu_id)
else:
le_obj_of_component = None
info = {
'bn_node' : bn_node,
'le_obj_of_component' : le_obj_of_component,
'attr_to_value' : {
}
}
for attr, string_value in lexeme.items():
if attr == 'POS':
attr_obj = LEXINFO.partOfSpeech
value_obj = URIRef(fn_pos_to_lexinfo[string_value])
elif attr in {'breakBefore', 'headword', 'name'}:
attr_obj = URIRef(COMP_ATTR_TO_URL[attr])
value_obj = Literal(string_value)
elif attr == 'order':
attr_obj = URIRef(COMP_ATTR_TO_URL[attr])
value_obj = Literal(string_value, datatype=XSD.integer)
elif attr == 'incorporatedFE':
fe_uri = get_fe_uri(graph=premon, frame_uri=frame_uri, fe_label=string_value)
attr_obj = URIRef(COMP_ATTR_TO_URL[attr])
value_obj = URIRef(fe_uri)
elif attr == 'lu_id':
pass # this is covered outside of this function
else:
raise Exception(f'attr ({attr}) not known. Please inspect.')
info['attr_to_value'][attr_obj] = value_obj
return info
def add_complement_attributes(g, lexeme_info, comp_obj):
"""
:param g:
:param lexeme_info:
:return:
"""
for attr_obj, value_obj in lexeme_info['attr_to_value'].items():
g.add((comp_obj, attr_obj, value_obj))
def add_decomposition(g,
fn_pos_to_lexinfo,
frame_uri,
lu,
LEMON,
LEXINFO,
DCT,
lemon,
premon,
le_obj):
"""
add lemon representation of decomposition of terms
:param rdflib.graph.Graph g: the graph to which we are added information
:param nltk.corpus.reader.framenet.AttrDict lu: FrameNet NLTK LU object
:param rdflib.namespace.Namespace LEMON: Lemon namespace
:param rdflib.graph.Graph lemon: lemon graph
:param rdflib.URIRef le_obj: uriref of LexicalEntry
"""
# LE -> : blank node representing first :ComponentList
assert LEMON.decomposition in lemon.subjects()
lexeme_order_to_info = {
lexeme['order'] : get_lexeme_info(lexeme=lexeme,
premon=premon,
frame_uri=frame_uri,
fn_pos_to_lexinfo=fn_pos_to_lexinfo,
g=g,
DCT=DCT,
LEMON=LEMON,
LEXINFO=LEXINFO)
for lexeme in lu.lexemes
}
for lexeme_order, lexeme_info in sorted(lexeme_order_to_info.items()):
comp_uri = le_obj + f'#Component{lexeme_order}'
comp_obj = URIRef(comp_uri)
assert LEMON.Component in lemon.subjects()
g.add((comp_obj, RDF.type, LEMON.Component))
g.add((lexeme_info['bn_node'], RDF.first, comp_obj))
if lexeme_info['le_obj_of_component'] is not None:
assert LEMON.element in lemon.subjects()
g.add((comp_obj, LEMON.element, lexeme_info['le_obj_of_component']))
assert LEMON.Component in lemon.subjects()
g.add((comp_obj, RDF.type, LEMON.Component))
add_complement_attributes(g=g, lexeme_info=lexeme_info, comp_obj=comp_obj)
# add relationships between :LexicalEntry and :ComponentList(s)
order_plus_one = lexeme_order + 1
# first :ComponentList is linked to LexicalEntry
if lexeme_order == 1:
g.add((le_obj, LEMON.decomposition, lexeme_info['bn_node']))
# second: :ComponentList is linked to :ComponentList
if order_plus_one in lexeme_order_to_info:
g.add((lexeme_info['bn_node'],
RDF.rest,
lexeme_order_to_info[order_plus_one]['bn_node']))
# last: last item in the list is linked to RDF.nil
else:
g.add((lexeme_info['bn_node'],
RDF.rest,
RDF.nil))
def get_le_uri(g,
DCT,
LEMON,
lu_identifier):
"""
query to obtain LexicalEntry URI
"""
query = f"""SELECT ?le_obj WHERE {{
?lu_obj <{RDF.type}> <{LEMON.LexicalSense}> .
?lu_obj <{DCT.identifier}> {lu_identifier} .
?lu_obj <{LEMON.isSenseOf}> ?le_obj . }}
"""
result = g.query(query)
urirefs = []
for info in result.bindings:
for uriref in info.values():
urirefs.append(uriref)
assert len(urirefs) == 1, f'expected to find one uri , found {urirefs}'
le_obj = urirefs[0]
return le_obj
def get_lu_type(lu, language):
"""
the attribute lu is only defined for Dutch FrameNet.
this function determines what the lu type is
:param lu:
:return:
"""
if language == 'nld':
lu_type = lu.lu_type
elif language == 'eng':
return None, None
if lu_type == 'singleton':
return lu_type, None
lu_type_uri = LUTYPE_TO_LU_TYPE_URL[lu_type]
lu_type_obj = URIRef(lu_type_uri)
return lu_type, lu_type_obj
def get_word_or_phrase(lu_type, lexemes, language, LEMON, lemon):
"""
:param lu_type:
:param lexemes:
:return:
"""
if language == 'eng':
if len(lexemes) == 1:
word_or_phrase = 'word'
elif len(lexemes) >= 2:
word_or_phrase = 'phrase'
else:
raise Exception(f'there should be 1 or more lexemes: {lexemes}')
if language == 'nld':
if lu_type is not None:
if lu_type in {'endocentric compound',
'exocentric compound',
'singleton'}:
word_or_phrase = 'word'
elif lu_type in {'phrasal',
'idiom'}:
word_or_phrase = 'phrase'
else:
raise Exception(f'unknown lu type {lu_type}')
else:
num_lexemes = len(lexemes)
if num_lexemes == 1:
word_or_phrase = 'word'
elif num_lexemes >= 2:
word_or_phrase = 'phrase'
lemon_obj = None
if word_or_phrase == 'word':
assert LEMON.Word in lemon.subjects()
lemon_obj = LEMON.Word
elif word_or_phrase == 'phrase':
assert LEMON.Phrase in lemon.subjects()
lemon_obj = LEMON.Phrase
return lemon_obj
def get_provenance_uriref(lexicon_uri, cBy, lu):
provenance_uri = f'{lexicon_uri}#Provenance{cBy}'
return URIRef(provenance_uri)
def get_date(cDate):
template = '%m/%d/%Y %H:%M:%S'
date = datetime.strptime(cDate[:-8], template)
rdf_string = date.strftime('%Y-%m-%dT%I:%M:%s')
return rdf_string
def add_agents_and_provenances(your_fn,
g,
lexicon_uri,
PROV,
language,
verbose=0):
"""
:param your_fn: your framenet in nltk format
:param g: the lemon graph of your framenet
:param verbose:
:return:
"""
cby_prov_to_cdates = defaultdict(list)
for lu in your_fn.lus():
cby = lu.cBy
prov = lu.get('provenance')
if prov is not None:
key = (cby, prov)
else:
key = (cby, None)
date = datetime.strptime(lu.cDate[:-8], '%m/%d/%Y %H:%M:%S')
cby_prov_to_cdates[key].append(date)
cby_prov_to_prov_obj = {}
for (cby, provenance), dates in cby_prov_to_cdates.items():
# add software agent
software_agent_uri = f'{lexicon_uri}#Agent#{cby}'
software_agent_obj = URIRef(software_agent_uri)
g.add((software_agent_obj, RDF.type, PROV.Agent))
g.add((software_agent_obj, RDFS.label, Literal(cby, lang=language)))
# add provenance
if provenance is not None:
provenance_uri = f'{lexicon_uri}#Provenance#{cby}-{provenance}'
else:
provenance_uri = f'{lexicon_uri}#Provenance#{cby}'
provenance_obj = URIRef(provenance_uri)
g.add((provenance_obj, RDF.type, PROV.Activity))
start = min(dates)
start_rdf_string = start.strftime('%Y-%m-%dT%I:%M:%s')
end = max(dates)
end_rdf_string = end.strftime('%Y-%m-%dT%I:%M:%s')
g.add((provenance_obj, PROV.startedAtTime, Literal(start_rdf_string,
datatype=XSD.dateTime)))
g.add((provenance_obj, PROV.endedAtTime, Literal(end_rdf_string,
datatype=XSD.dateTime)))
g.add((provenance_obj, PROV.wasAssociatedWith, software_agent_obj))
cby_prov_to_prov_obj[(cby, provenance)] = provenance_obj
return cby_prov_to_prov_obj
def convert_to_lemon(lemon,
premon_nt_path,
ontolex,
fn_pos_to_lexinfo,
your_fn,
namespace,
namespace_prefix,
language,
major_version,
minor_version,
output_path=None,
verbose=0):
"""
Convert the FrameNet in NLTK format to Lemon
https://lemon-model.net/lemon#
:param rdflib.graph.Graph lemon: use FrameNetNLTK.lemon
:param rdflib.graph.Graph premon: use FrameNetNLTK.premon_nt
:param rdflib.graph.Graph ontolex: use FrameNetNLTK.ontolex
:param dict fn_pos_to_lexinfo: use FrameNetNLTK.fn_pos_to_lexinfo
:param nltk.corpus.reader.framenet.FramenetCorpusReader your_fn: a FrameNet in the NLTK format
:param str namespace: a namespace.
for Dutch, we use http://rdf.cltl.nl/dfn/
for English, we use http://rdf.cltl.nl/efn/
:param str namespace_prefix: e.g., dfn for Dutch FrameNet
:param str language: supported: nld | eng
:param int major_version: the major version
:param int minor_version: the minor version
"""
from .LexicalDataD2TAnnotationTool import lemmas_from_lu_name
# loading premon
premon = load_nt_graph(nt_path=premon_nt_path)
# validate parameters
error_message = f'{language} not part of supported languages: {SUPPORTED_LANGUAGES}'
assert language in SUPPORTED_LANGUAGES, error_message
# query for identifiers
lexicon_uri = generate_lexicon_rdf_uri(namespace=namespace,
language=language,
major_version=major_version,
minor_version=minor_version)
if verbose >= 2:
print(f'lexicon uri: {lexicon_uri}')
g = Graph()
LEMON = Namespace('http://lemon-model.net/lemon#')
DCT = Namespace('http://purl.org/dc/terms/')
LEXINFO = Namespace('http://www.lexinfo.net/ontology/3.0/lexinfo#')
ONTOLEX = Namespace('http://www.w3.org/ns/lemon/ontolex#')
PROV = Namespace('http://www.w3.org/ns/prov#')
skos_namespace = 'http://www.w3.org/2004/02/skos/core#'
SKOS = Namespace(skos_namespace)
FN = Namespace(namespace)
g.bind('lemon', LEMON)
g.bind('dct', DCT)
g.bind('lexinfo', LEXINFO)
g.bind('prov', PROV)
g.bind('ontolex', ONTOLEX)
g.bind('skos', SKOS)
g.bind('prov', PROV)
g.bind(namespace_prefix, FN)
# initialize graph
initialize_graph(g=g, namespace=namespace, SKOS=SKOS)
# update lexicon information
lexicon_uri_obj = URIRef(lexicon_uri)
assert LEMON.Lexicon in lemon.subjects()
g.add((lexicon_uri_obj, RDF.type, LEMON.Lexicon))
assert LEMON.language in lemon.subjects()
g.add((lexicon_uri_obj, LEMON.language, Literal(language)))
lexicon_label = f'{LANGUAGE_TO_ADJECTIVE[language]} FrameNet v{major_version}.{minor_version}'
g.add((lexicon_uri_obj, RDFS.label, Literal(lexicon_label)))
lexicon_version = float(f'{major_version}.{minor_version}')
g.add((lexicon_uri_obj, DCT.identifier, Literal(lexicon_version,
datatype=XSD.decimal)))
the_lu_iterable = list(your_fn.lus())
cby_prov_to_prov_obj = add_agents_and_provenances(your_fn=your_fn,
g=g,
lexicon_uri=lexicon_uri,
PROV=PROV,
language=language,
verbose=verbose)
# update for each LE and LU
for index, lu in enumerate(the_lu_iterable):
provenance_obj = cby_prov_to_prov_obj[(lu.cBy, lu.get('provenance'))]
if verbose >= 3:
print(f'convert LU {lu.ID} ({lu.name}) to Lemon')
if all([verbose >= 5,
index == 5]):
print('QUITTING AFTER FIRST FIVE ITERATIONS TOP')
break
# generate LE and LU rdf uri
le_uri, leform_uri, lu_uri = generate_le_and_lu_rdf_uri(your_fn=your_fn,
namespace=namespace,
language=language,
major_version=major_version,
minor_version=minor_version,
lu_id=lu.ID)
# update LE information
le_obj = URIRef(le_uri)
assert LEMON.LexicalEntry in lemon.subjects()
g.add((le_obj, RDF.type, LEMON.LexicalEntry))
# provenance LE
date = get_date(cDate=lu.cDate)
g.add((le_obj, PROV.generatedAtTime, Literal(date,
datatype=XSD.dateTime)))
g.add((le_obj, PROV.wasGeneratedBy, provenance_obj))
g.add((le_obj,
LEXINFO.partOfSpeech,
URIRef(fn_pos_to_lexinfo[lu.POS]))
)
# update LE form
lemma, pos = lu.name.rsplit('.', 1)
le_form_obj = URIRef(leform_uri)
g.add((le_form_obj, RDFS.isDefinedBy, le_obj))
assert LEMON.Form in lemon.subjects()
g.add((le_form_obj, RDF.type, LEMON.Form))
assert LEMON.writtenRep in lemon.subjects()
for lemma_variant in lemmas_from_lu_name(lemma):
g.add((le_form_obj, LEMON.writtenRep, Literal(lemma_variant, lang=language)))
assert LEMON.canonicalForm in lemon.subjects()
g.add((le_obj, LEMON.canonicalForm, le_form_obj))
# update LU information
lu_obj = URIRef(lu_uri)
# provenance LU
date = get_date(cDate=lu.cDate)
g.add((lu_obj, PROV.generatedAtTime, Literal(date,
datatype=XSD.dateTime)))
g.add((lu_obj, PROV.wasGeneratedBy, provenance_obj))
assert LEMON.sense in lemon.subjects()
g.add((le_obj, LEMON.sense, lu_obj))
assert LEMON.LexicalSense in lemon.subjects()
g.add((lu_obj, RDF.type, LEMON.LexicalSense))
g.add((lu_obj, DCT.identifier, Literal(lu.ID,
datatype=XSD.integer)))
assert LEMON.isSenseOf in lemon.subjects()
g.add((lu_obj, LEMON.isSenseOf, le_obj))
assert LEMON.definition in lemon.subjects()
g.add((lu_obj, LEMON.definition, Literal(lu.definition,
lang=language)))
# update with SKOS relationships to external references
table = {ord('{'): '', ord('}'): ''}
for attr_name, attr_value in lu.items():
if attr_name.startswith('{%s}' % skos_namespace):
skos_pred_uri = attr_name.translate(table)
skos_pred_uriref = URIRef(skos_pred_uri)
value_uriref = URIRef(attr_value)
g.add((lu_obj, skos_pred_uriref, value_uriref))
# evokes relationship
frame_uri = get_rdf_uri(premon_nt=premon,
frame_label=lu.frame.name)
frame_obj = URIRef(frame_uri)
# add incorporatedFE if it is there
incorporated_fe_label = lu.get('incorporatedFE', None)
# mistakes in English FrameNet
if all([frame_uri == 'http://premon.fbk.eu/resource/fn17-measurable_attributes',
incorporated_fe_label == 'Dimension']):
incorporated_fe_label = None
if incorporated_fe_label is not None:
fe_uri = get_fe_uri(graph=premon, frame_uri=frame_uri, fe_label=incorporated_fe_label)
attr_obj = URIRef(COMP_ATTR_TO_URL['incorporatedFE'])
g.add((lu_obj, attr_obj, URIRef(fe_uri)))
assert frame_obj in premon.subjects()
assert ONTOLEX.evokes in ontolex.subjects()
g.add((le_obj, ONTOLEX.evokes, frame_obj))
assert LEMON.entry in lemon.subjects()
assert frame_obj
g.add((lexicon_uri_obj, LEMON.entry, le_obj))
for index, lu in enumerate(the_lu_iterable):
if verbose >= 3:
print(f'convert LU {lu.ID} ({lu.name}) to Lemon')
if all([verbose >= 5,
index >= 5]):
print('QUITTING AFTER FIRST FIVE ITERATIONS BOTTOM')
break
# obtain LE obj
le_obj = get_le_uri(g=g,
DCT=DCT,
LEMON=LEMON,
lu_identifier=lu.ID)
# LU type
lu_type,\
lu_type_obj = get_lu_type(lu=lu, language=language)
if lu_type_obj is not None:
g.add((le_obj, RDF.type, lu_type_obj))
word_or_phrase = get_word_or_phrase(lu_type=lu_type,
lexemes=lu.lexemes,
language=language,
LEMON=LEMON,
lemon=lemon)
g.add((le_obj, RDF.type, word_or_phrase))
# evokes relationship
frame_uri = get_rdf_uri(premon_nt=premon,
frame_label=lu.frame.name)
if language == 'nld':
if lu_type == 'singleton':
continue
elif lu_type in {'endocentric compound',
'exocentric compound',
'idiom',
'phrasal'}:
add_decomposition(g=g,
fn_pos_to_lexinfo=fn_pos_to_lexinfo,
frame_uri=frame_uri,
lu=lu,
DCT=DCT,
LEMON=LEMON,
LEXINFO=LEXINFO,
lemon=lemon,
premon=premon,
le_obj=le_obj)
else:
raise Exception(f'lu type ({lu_type}) not known')
elif language == 'eng':
if word_or_phrase == LEMON.Phrase:
add_decomposition(g=g,
fn_pos_to_lexinfo=fn_pos_to_lexinfo,
frame_uri=frame_uri,
lu=lu,
DCT=DCT,
LEMON=LEMON,
LEXINFO=LEXINFO,
lemon=lemon,
premon=premon,
le_obj=le_obj)
if output_path is not None:
g.serialize(format='turtle', destination=output_path)
if verbose >= 1:
print(f'written Lemon representation of FrameNet ({major_version}.{minor_version} in language {language}) to {output_path}')
def generate_lexicon_rdf_uri(namespace,
language,
major_version,
minor_version):
error_message = f'the provided language ({language}) is not supported: {SUPPORTED_LANGUAGES}'
assert language in SUPPORTED_LANGUAGES, error_message
assert namespace.endswith('/'), 'namespace should end with a forward slash'
assert namespace.startswith('http'), 'namespace should start with http'
for version in [major_version, minor_version]:
error_message = f'expecting an integer for the minor and major version, you provided: {type(version)}'
assert type(version) == int, error_message
return f'{namespace}fn_{language}-lexicon-{major_version}.{minor_version}'
def generate_le_and_lu_rdf_uri(your_fn,
namespace,
language,
major_version,
minor_version,
lu_id):
"""
:param str namespace: the RDF namespace, e.g., http://rdf.cltl.nl/
:param str language: supported: nl | en
:param int major_version: the major version
:param int minor_version: the minor version
:return: (le_uri, leform_uri, lu_uri)
"""
error_message = f'there is no lu for the provided lu_id ({lu_id}) in the provided FrameNet'
assert lu_id in your_fn.lu_ids_and_names(), error_message
error_message = f'the provided language ({language}) is not supported: {SUPPORTED_LANGUAGES}'
assert language in SUPPORTED_LANGUAGES, error_message
assert namespace.endswith('/'), 'namespace should end with a forward slash'
assert namespace.startswith('http'), 'namespace should start with http'
for version in [major_version, minor_version]:
error_message = f'expecting an integer for the minor and major version, you provided: {type(version)}'
assert type(version) == int, error_message
lexicon_uri = generate_lexicon_rdf_uri(namespace=namespace,
language=language,
major_version=major_version,
minor_version=minor_version)
le_rdf_uri = f'{lexicon_uri}-le-{lu_id}'
leform_rdf_uri = f'{lexicon_uri}-leform-{lu_id}'
lu_rdf_uri = f'{lexicon_uri}-lu-{lu_id}'
return le_rdf_uri, leform_rdf_uri, lu_rdf_uri
def load_nquads_file(path_to_nquad_file):
"""
load rdf file in nquads format
:param str path_to_nquad_file: path to rdf file in nquad format
:rtype: rdflib.graph.ConjunctiveGraph
:return: nquad
"""
g = ConjunctiveGraph()
with open(path_to_nquad_file, "rb") as infile:
g.parse(infile, format="nquads")
return g
def convert_nquads_to_nt(g, output_path):
"""
:param rdflib.graph.ConjunctiveGraph g: a nquad graph
:rtype:
:return:
"""
g.serialize(destination=output_path, format='nt')
def load_graph(path, format='nt'):
g = Graph()
with open(path, 'rb') as infile:
g.parse(file=infile, format=format)
return g
def get_rdf_uri(premon_nt, frame_label):
frame_query = """SELECT ?s WHERE {
?s rdf:type <http://premon.fbk.eu/ontology/fn#Frame> .
?s rdfs:label "%s" .
}"""
the_query = frame_query % frame_label
results = [result
for result in premon_nt.query(the_query)]
assert len(results) == 1, f'query should only have one result: {the_query}\n{results}'
for result in results:
frame_rdf_uri = str(result.asdict()['s'])
return frame_rdf_uri
def get_rdf_label(graph, uri):
query = """SELECT ?o WHERE {
<%s> rdfs:label ?o
}"""
the_query = query % uri
results = graph.query(the_query)
labels = set()
for result in results:
label = str(result.asdict()['o'])
labels.add(label)
assert len(labels) == 1, f'expected one label for {uri}, got {labels}'
return labels.pop()
def get_lu_identifier(graph, lu_uri):
query = """SELECT ?o WHERE {
<%s> dct:identifier ?o
}"""
the_query = query % lu_uri
results = graph.query(the_query)
identifiers = set()
for result in results:
identifier = str(result.asdict()['o'])
identifiers.add(identifier)
if not identifiers:
lu_id = 'NOT-PART-OF-LEXICON'
else:
lu_id = int(identifiers.pop())
return lu_id
def get_fe_uri(graph, frame_uri, fe_label):
"""
:param graph:
:param frame_uri:
:return:
"""
query = """SELECT ?o WHERE {
?o <http://www.w3.org/2000/01/rdf-schema#label> "%s" .
<%s> <http://premon.fbk.eu/ontology/core#semRole> ?o
}"""
the_query = query % (fe_label, frame_uri)
results = graph.query(the_query)
labels = set()
for result in results:
label = str(result.asdict()['o'])
labels.add(label)
assert len(labels) == 1, f'expected one label for frame ({frame_uri}) with FE label ({fe_label}), got {labels}'
return labels.pop()
def get_attributes(fn_in_lemon, the_lemon_type):
"""
:param graph:
:param the_type:
:return:
"""
query = """SELECT ?s ?p ?o WHERE {
?s ?p ?o .
?s <%s> <%s>
}"""
the_query = query % (RDF.type, the_lemon_type)
results = fn_in_lemon.query(the_query)
s_to_attrs = defaultdict(set)
for result in results:
as_dict = result.asdict()
s = as_dict['s']
attribute = as_dict['p']
s_to_attrs[s].add(attribute)
num_s = len(s_to_attrs)
attr_to_freq = defaultdict(int)
for subject, s_attrs in s_to_attrs.items():
for s_attr in s_attrs:
attr_to_freq[s_attr] += 1
attr_to_info = {}
for attr, freq in attr_to_freq.items():
ratio = (freq / num_s)
info = {
'freq' : freq,
'ratio' : ratio
}
attr_to_info[attr] = info
return attr_to_info
def get_attributes_between_two(fn_in_lemon, type_one, type_two):
"""
:param graph:
:param the_type:
:return:
"""
query = """SELECT ?s WHERE {
?s ?p ?o .
?s <%s> <%s> .
}"""
the_query = query % (RDF.type, type_one)
results = fn_in_lemon.query(the_query)
all_s = set()
for result in results:
as_dict = result.asdict()
s = as_dict['s']
all_s.add(s)
query = """SELECT ?o WHERE {
?s ?p ?o .
?o <%s> <%s> .
}"""
the_query = query % (RDF.type, type_two)
results = fn_in_lemon.query(the_query)
all_o = set()
for result in results:
as_dict = result.asdict()
o = as_dict['o']
all_o.add(o)
s_p = set()
for subject, predicate in fn_in_lemon.subject_predicates():
if subject in all_s:
s_p.add(predicate)
p_o = set()
for predicate, object in fn_in_lemon.predicate_objects():
if object in all_o:
p_o.add(predicate)
result = s_p & p_o
return result
def shorten_namespaces(fn_in_lemon,
uriref):
uri = uriref.toPython()
long_to_short = {
uriref.toPython() : short
for short, uriref in fn_in_lemon.namespaces()
}
for long, short in long_to_short.items():
uri = uri.replace(long, f'{short}:')
return uri
def get_id_and_label(fn_in_lemon,
uriref,
uriref_to_attr_to_info={}):
"""
:param fn_in_lemon:
:param uriref: