-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser.py
686 lines (564 loc) · 20.2 KB
/
parser.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
"""KGCL parser."""
import logging
import sys
from functools import lru_cache
from pathlib import Path
from typing import List
import click
from prefixmaps import load_converter
from curies import Converter
from kgcl_schema.datamodel.kgcl import (
AddNodeToSubset,
Change,
ClassCreation,
EdgeCreation,
EdgeDeletion,
NewSynonym,
NewTextDefinition,
NodeAnnotationChange,
NodeCreation,
NodeDeepening,
NodeDeletion,
NodeMove,
NodeObsoletion,
NodeObsoletionWithDirectReplacement,
NodeRename,
NodeShallowing,
NodeTextDefinitionChange,
NodeUnobsoletion,
PlaceUnder,
PredicateChange,
RemoveSynonym,
RemoveNodeFromSubset,
RemoveUnder,
Session,
RemoveTextDefinition,
)
from kgcl_schema.datamodel.ontology_model import Edge
from kgcl_schema.utils import to_json, to_rdf, to_yaml
from lark import Lark, Token
@lru_cache()
def get_curie_converter() -> Converter:
return load_converter(["obo", "bioregistry.upper", "linked_data"])
def id_generator():
"""Return a new ID for KGCL change operations."""
id = 0
while True:
yield id
id += 1
# initialise ID generator
id_gen = id_generator()
# initialise parser
path = Path(__file__).parent
kgcl_parser = Lark.open(str(path) + "/kgcl.lark", start="expression")
def parse(input: str) -> List[Change]:
"""
Parse a set of KGCL command separated by next-line operator.
Returns instantiated dataclass objects from model.kgcl_schema.
"""
statements = input.splitlines()
parsed = []
for s in statements:
parsed.append(parse_statement(s))
return parsed
def parse_statement(input: str) -> Change:
"""
Parse a KGCL command.
Return an instantiated dataclass object from model.kgcl_schema.
"""
# regex = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
# uri_list = re.findall(regex, input)
# if uri_list:
# # curie = curie_from_iri(uri[0].replace("<", "").replace(">",""))
# for _, uri in enumerate(uri_list):
# pref, i = parse_iri(uri)
# pref = get_preferred_prefix(pref)
# curie = curie_to_str(pref, i)
# input = input.replace(uri, curie)
tree = kgcl_parser.parse(input)
id = "kgcl_change_id_" + str(next(id_gen))
command = tree.data
if command == "rename":
return parse_rename(tree, id)
elif command == "obsolete":
return parse_obsolete(tree, id)
elif command == "unobsolete":
return parse_unobsolete(tree, id)
elif command == "delete":
return parse_delete(tree, id)
elif command == "move":
return parse_move(tree, id)
elif command == "deepen":
return parse_deepen(tree, id)
elif command == "shallow":
return parse_shallow(tree, id)
elif command == "create_edge":
return parse_create_edge(tree, id)
elif command == "delete_edge":
return parse_delete_edge(tree, id)
elif command == "change_relationship":
return parse_change_relationship(tree, id)
elif command == "change_annotation":
return parse_change_annotation(tree, id)
elif command == "create":
return parse_create(tree, id)
elif command == "create_class":
return parse_create_class(tree, id)
elif command == "create_synonym":
return parse_create_synonym(tree, id)
elif command == "add_to_subset":
return parse_add_to_subset(tree, id)
elif command == "remove_from_subset":
return parse_remove_from_subset(tree, id)
elif command == "remove_synonym":
return parse_remove_synonym(tree, id)
elif command == "add_definition":
return parse_add_definition(tree, id)
elif command == "change_definition":
return parse_change_definition(tree, id)
elif command == "remove_definition":
return parse_remove_definition(tree, id)
else:
raise NotImplementedError("No implementation for KGCL command: " + command)
# TODO: more commands
# if(command == "merge"):
# if(command == "add"):
# if(command == "add_parent"):
# if(command == "add_annotation"):
# if(command == "add_axiom"): (in Manchester Syntax?)
# if(command == "add_class"):
# if(command == "add_text_definition"):
# if(command == "merge"):
def parse_remove_from_subset(tree, id):
"""Remove node from subset."""
term_id_token = extract(tree, "id")
term, representation = get_entity_representation(term_id_token)
subset_id_token = extract(tree, "subset")
subset, _ = get_entity_representation(subset_id_token)
return RemoveNodeFromSubset(
id=id,
in_subset=subset,
about_node=term,
about_node_representation=representation,
)
def parse_add_to_subset(tree, id):
"""Add node to subset."""
term_id_token = extract(tree, "id")
term, representation = get_entity_representation(term_id_token)
subset_id_token = extract(tree, "subset")
subset, _ = get_entity_representation(subset_id_token)
return AddNodeToSubset(
id=id,
in_subset=subset,
about_node=term,
about_node_representation=representation,
)
def parse_create_synonym(tree, id):
"""Create new synonym."""
entity_token = extract(tree, "entity")
entity, representation = get_entity_representation(entity_token)
synonym_string_token = extract(tree, "synonym")
synonym, representation2 = get_entity_representation(synonym_string_token)
language_token = extract(tree, "language")
qualifier_token = extract(tree, "synonym_qualifier")
return NewSynonym(
id=id,
about_node=entity,
about_node_representation=representation,
new_value=synonym,
qualifier=qualifier_token,
language=language_token,
)
def parse_remove_synonym(tree, id):
"""Remove a synonym."""
entity_token = extract(tree, "entity")
entity, representation = get_entity_representation(entity_token)
synonym_string_token = extract(tree, "synonym")
synonym, _ = get_entity_representation(synonym_string_token)
language_token = extract(tree, "language")
# qualifier_token = extract(tree, "synonym_qualifier")
return RemoveSynonym(
id=id,
old_value=synonym,
about_node=entity,
about_node_representation=representation,
# qualifier=qualifier_token,
language=language_token,
)
def parse_create_class(tree, id):
"""Create new class."""
term_id_token = extract(tree, "id")
entity, representation = get_entity_representation(term_id_token)
return ClassCreation(
id=id, node_id=entity, about_node_representation=representation
)
def parse_add_definition(tree, id):
"""Add definition to class."""
entity_token = extract(tree, "entity")
new_value = extract(tree, "new_definition")
entity, representation = get_entity_representation(entity_token)
return NewTextDefinition(
id=id,
about_node=entity,
about_node_representation=representation,
new_value=new_value,
)
def parse_change_definition(tree, id):
"""Change the definition of a class."""
entity_token = extract(tree, "entity")
old_value = extract(tree, "old_definition")
new_value = extract(tree, "new_definition")
entity, representation = get_entity_representation(entity_token)
return NodeTextDefinitionChange(
id=id,
about_node=entity,
about_node_representation=representation,
new_value=new_value,
old_value=old_value,
)
def parse_remove_definition(tree, id):
"""Remove definition for a given entity."""
entity_token = extract(tree, "entity")
entity, representation = get_entity_representation(entity_token)
return RemoveTextDefinition(
id=id,
about_node=entity,
about_node_representation=representation,
)
# the KGCL model suggests the command
# 'create node {id} {label} with {annotation set}'
# TODO: handling of {annotation set}
def parse_create(tree, id):
"""Create a node."""
term_id_token = extract(tree, "id")
label_token = extract(tree, "label")
language_token = extract(tree, "language")
entity, representation = get_entity_representation(term_id_token)
return NodeCreation(
id=id,
about_node=entity,
about_node_representation=representation,
node_id=entity, # was term_id_token
name=label_token,
language=language_token,
)
def parse_change_annotation(tree, id):
"""Change node annotation."""
subject_token = extract(tree, "entity_subject")
predicate_token = extract(tree, "entity_predicate")
subject, s_representation = get_entity_representation(subject_token)
predicate, p_representation = get_entity_representation(predicate_token)
old_token = extract(tree, "old_entity_object")
new_token = extract(tree, "new_entity_object")
old, old_representation = get_entity_representation(old_token)
new, new_representation = get_entity_representation(new_token)
old_language_token = extract(tree, "old_language")
new_language_token = extract(tree, "new_language")
old_datatype_token = extract(tree, "old_datatype")
new_datatype_token = extract(tree, "new_datatype")
return NodeAnnotationChange(
id=id,
about_node=subject,
about_node_representation=s_representation,
annotation_property=predicate,
annotation_property_type=p_representation,
old_value=old,
new_value=new,
old_value_type=old_representation,
new_value_type=new_representation,
old_language=old_language_token,
new_language=new_language_token,
old_datatype=old_datatype_token,
new_datatype=new_datatype_token,
)
def parse_change_relationship(tree, id):
"""Change predicate."""
subject_token = extract(tree, "entity_subject")
object_token = extract(tree, "entity_object")
subject, s_representation = get_entity_representation(subject_token)
object, o_representation = get_entity_representation(object_token)
old_token = extract(tree, "old_entity")
new_token = extract(tree, "new_entity")
old, old_representation = get_entity_representation(old_token)
new, new_representation = get_entity_representation(new_token)
edge = Edge(
subject=subject,
predicate=old,
object=object,
subject_representation=s_representation,
predicate_representation=old_representation,
object_representation=o_representation,
)
language_token = extract(tree, "language")
datatype_token = extract(tree, "datatype")
return PredicateChange(
id=id,
about_edge=edge,
old_value=old,
new_value=new,
old_value_type=old_representation,
new_value_type=new_representation,
language=language_token,
datatype=datatype_token,
)
def parse_delete_edge(tree, id):
"""Delete edge."""
subject_token = extract(tree, "entity_subject")
predicate_token = extract(tree, "entity_predicate")
object_token = extract(tree, "entity_object_id")
subject, s_representation = get_entity_representation(subject_token)
predicate, p_representation = get_entity_representation(predicate_token)
object, o_representation = get_entity_representation(object_token)
if (
predicate == "rdfs:subClassOf"
or predicate == "<http://www.w3.org/2000/01/rdf-schema#subClassOf>"
):
return RemoveUnder(
id=id,
subject=subject,
predicate=predicate,
object=object,
subject_type=s_representation,
predicate_type=p_representation,
object_type=o_representation,
)
else:
return EdgeDeletion(
id=id,
subject=subject,
predicate=predicate,
object=object,
subject_type=s_representation,
predicate_type=p_representation,
object_type=o_representation,
)
def parse_create_edge(tree, id):
"""Create new edge."""
subject_token = extract(tree, "entity_subject")
predicate_token = extract(tree, "entity_predicate")
object_token = extract(tree, "entity_object_id")
subject, s_representation = get_entity_representation(subject_token)
predicate, p_representation = get_entity_representation(predicate_token)
object, o_representation = get_entity_representation(object_token)
if (
predicate == "rdfs:subClassOf"
or predicate == "<http://www.w3.org/2000/01/rdf-schema#subClassOf>"
):
return PlaceUnder(
id=id,
subject=subject,
predicate=predicate,
object=object,
subject_type=s_representation,
predicate_type=p_representation,
object_type=o_representation,
)
else:
return EdgeCreation(
id=id,
subject=subject,
predicate=predicate,
object=object,
subject_type=s_representation,
predicate_type=p_representation,
object_type=o_representation,
)
def parse_shallow(tree, id):
"""Perform node shallowing."""
entity_token = extract(tree, "entity")
old_token = extract(tree, "old_entity")
new_token = extract(tree, "new_entity")
entity, e_representation = get_entity_representation(entity_token)
old_entity, o_representation = get_entity_representation(old_token)
new_entity, n_representation = get_entity_representation(new_token)
edge = Edge(
subject=entity,
object=old_entity,
subject_representation=e_representation,
object_representation=o_representation,
)
return NodeShallowing(
id=id,
about_edge=edge,
old_value=old_entity,
new_value=new_entity,
old_object_type=o_representation,
new_object_type=n_representation,
)
def parse_deepen(tree, id):
"""Perform node deepening."""
entity_token = extract(tree, "entity")
old_token = extract(tree, "old_entity")
new_token = extract(tree, "new_entity")
entity, e_representation = get_entity_representation(entity_token)
old_entity, o_representation = get_entity_representation(old_token)
new_entity, n_representation = get_entity_representation(new_token)
edge = Edge(
subject=entity,
object=old_entity,
subject_representation=e_representation,
object_representation=o_representation,
)
return NodeDeepening(
id=id,
about_edge=edge,
old_value=old_entity,
new_value=new_entity,
old_object_type=o_representation,
new_object_type=n_representation,
)
def parse_move(tree, id):
"""Move a node."""
subject_token = extract(tree, "entity_subject")
predicate_token = extract(tree, "entity_predicate")
object_token = extract(tree, "entity_object")
subject, s_representation = get_entity_representation(subject_token)
predicate, p_representation = get_entity_representation(predicate_token)
object, o_representation = get_entity_representation(object_token)
old_token = extract(tree, "old_entity")
new_token = extract(tree, "new_entity")
old, old_representation = get_entity_representation(old_token)
new, new_representation = get_entity_representation(new_token)
edge = Edge(
subject=subject,
predicate=predicate,
object=object,
subject_representation=s_representation,
predicate_representation=p_representation,
object_representation=o_representation,
)
return NodeMove(
id=id,
about_edge=edge,
old_value=old,
new_value=new,
old_object_type=old_representation,
new_object_type=new_representation,
)
def parse_delete(tree, id):
"""Delete a node."""
entity_token = extract(tree, "entity")
entity, representation = get_entity_representation(entity_token)
return NodeDeletion(
id=id, about_node=entity, about_node_representation=representation
)
def parse_unobsolete(tree, id):
"""Unobsolete a node."""
entity_token = extract(tree, "entity")
entity, representation = get_entity_representation(entity_token)
return NodeUnobsoletion(
id=id, about_node=entity, about_node_representation=representation
)
def parse_obsolete(tree, id):
"""Obsolete a node."""
entity_token = extract(tree, "entity")
entity, representation = get_entity_representation(entity_token)
replacement_token = extract(tree, "replacement")
if replacement_token:
return NodeObsoletionWithDirectReplacement(
id=id,
about_node=entity,
about_node_representation=representation,
has_direct_replacement=replacement_token,
)
else:
return NodeObsoletion(
id=id, about_node=entity, about_node_representation=representation
)
def parse_rename(tree, id):
"""Rename a node."""
old_token = extract(tree, "old_label")
new_token = extract(tree, "new_label")
old_language = extract(tree, "old_language")
new_language = extract(tree, "new_language")
# TODO old_token and new_token are enclosed in ''
term_id_token = extract(tree, "id")
if term_id_token is not None:
entity, representation = get_entity_representation(term_id_token)
else:
entity = None
representation = None
return NodeRename(
id=id,
about_node=entity,
about_node_representation=representation,
old_value=old_token,
new_value=new_token,
old_language=old_language,
new_language=new_language,
)
def extract(tree, data):
"""Extract node."""
node = get_next(tree.find_data(data))
if node is not None:
node_token = next(get_tokens(node))
return node_token
else:
return None
def get_tokens(tree):
"""Get tokens."""
return tree.scan_values(lambda v: isinstance(v, Token))
def get_next(generator):
"""Next generator."""
try:
res = next(generator)
return res
except StopIteration:
return None
def get_entity_representation(entity):
"""Get entity representation."""
first_character = entity[0]
last_character = entity[-1:]
if first_character == "<" and last_character == ">":
return (
contract_uri(entity.replace("<", "").replace(">", "")),
"curie",
) # removing brackets
if first_character == "'" and last_character == "'" and entity[1] != "'":
return entity[1:-1], "label"
if first_character == '"' and last_character == '"':
return entity[1:-1], "literal"
if entity[0:3] == '"""' and entity[-3:] == '"""':
return entity[3:-3], "literal"
if entity[0:3] == "'''" and entity[-3:] == "'''":
return entity[3:-3], "literal"
# TODO: use predefined set of prefixes to identify CURIEs
return contract_uri(str(entity)), "curie"
# return entity, "error"
def contract_uri(uri_or_curie: str):
if uri_or_curie.startswith("http://") or uri_or_curie.startswith("https://"):
converter = get_curie_converter()
return converter.compress(uri_or_curie)
else:
return uri_or_curie
@click.command()
@click.option("--output", "-o", type=click.File(mode="w"), default=sys.stdout)
@click.option("--output-type", "-O", help="format for output")
@click.option("-v", "--verbose", count=True)
@click.argument("patches", nargs=-1)
def cli(patches, verbose: int, output, output_type):
"""
Parse a patch or patches specified in KGCL DSL
"""
if verbose >= 2:
logging.basicConfig(level=logging.DEBUG)
elif verbose == 1:
logging.basicConfig(level=logging.INFO)
else:
logging.basicConfig(level=logging.WARNING)
# parser kgcl_schema commands
changes = []
for patch in patches:
logging.info(f"Patch: {patch}")
changes += parse(patch)
session = Session(change_set=changes)
if output_type is None or output_type == "yaml":
output.write(to_yaml(session))
elif output_type == "json":
output.write(to_json(session))
elif output_type == "rdf":
output.write(to_rdf(session))
else:
raise NotImplementedError(output_type)
if __name__ == "__main__":
cli()