Skip to content
Snippets Groups Projects
Commit 508975d3 authored by Aurélien Lamercerie's avatar Aurélien Lamercerie
Browse files

ASD of UNL Graph

parent ac19a74e
No related branches found
No related tags found
No related merge requests found
...@@ -2,21 +2,16 @@ ...@@ -2,21 +2,16 @@
# -*-coding:Utf-8 -* # -*-coding:Utf-8 -*
#============================================================================== #==============================================================================
# TENET: inference # unlAnt: ASD of UNL document
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# Module to execute the extraction process, # --
# by applying the transduction schemes with the SHACL inference engine
#============================================================================== #==============================================================================
#============================================================================== #==============================================================================
# Importing required modules # Importing required modules
#============================================================================== #==============================================================================
import sys # None
from subprocess import Popen, PIPE
from antlr4 import FileStream, CommonTokenStream
# from antlr.unlLexer import unlLexer
# from antlr.unlParser import unlParser
#============================================================================== #==============================================================================
......
#!/usr/bin/python3.10
# -*-coding:Utf-8 -*
#==============================================================================
# unlAnt: ASD of UNL Graph
#------------------------------------------------------------------------------
# --
#==============================================================================
#==============================================================================
# Importing required modules
#==============================================================================
# None
#==============================================================================
# Parameters
#==============================================================================
# None
#==============================================================================
# Document Class
#==============================================================================
class Graph:
""" unl := Graph(link*) """
def __init__(self, link_list):
self.link_list = link_list
def to_string(self):
res = "Graph("
res_init_length = len(res)
for link in self.link_list:
res += link.to_string() + ", "
if len(res) > res_init_length:
res = res[:-2]
res += ")"
return res
class Link:
""" link := Link(relation, uw, uw) """
def __init__(self, relation, uw1, uw2):
self.relation = relation
self.uw1 = uw1
self.uw2 = uw2
def to_string(self):
res = self.relation.to_string() + "("
res += self.uw1.to_string() + ", "
res += self.uw2.to_string() + ")"
return res
class Relation:
""" relation := Relation(id) """
def __init__(self, id):
self.id = id
def to_string(self):
return self.id
class UniversalWord:
""" uw := UniversalWord(headword, restriction*, attr*) """
def __init__(self, headword, restriction_list, attr_list):
self.headword = headword
self.restriction_list = restriction_list
self.attr_list = attr_list
def to_string(self):
res = self.headword.to_string() + "("
res_init_length = len(res)
for restrictio in self.restriction_list:
res += restrictio.to_string() + ", "
res = res[:-1]
if len(res) > res_init_length:
res = res[:-1]
res += ")"
for attr in self.attr_list:
res += ".@" + attr.to_string()
return res
class Headword:
""" headword := Headword(id) """
def __init__(self, id):
self.id = id
def to_string(self):
return self.id.to_string()
class Restriction:
""" restriction := Restriction(id) """
def __init__(self, relation, id):
self.relation = relation
self.id = id
def to_string(self):
return self.relation.to_string() + ">" + self.id.to_string()
class Attribute:
""" attr := Attribute(id) """
def __init__(self, id):
self.id = id
def to_string(self):
return self.id.to_string()
class Ident:
""" id := Ident(string) """
def __init__(self, id):
self.id = id
def to_string(self):
return self.id
...@@ -4,6 +4,10 @@ ...@@ -4,6 +4,10 @@
grammar unl; grammar unl;
@header {
from asd import unl
}
//============================================================================= //=============================================================================
// Parser Grammar // Parser Grammar
...@@ -13,40 +17,50 @@ grammar unl; ...@@ -13,40 +17,50 @@ grammar unl;
// UNL representation // UNL representation
//--------------------------------------------------------- //---------------------------------------------------------
unlPart unlGraph returns [out]
: '{unl}' (relationOccurrence)+ '{/unl}' : '{unl}' {link_list = []}
(l=link {link_list.append($l.out)} )+
'{/unl}' {$out = unl.Graph(link_list)}
; ;
relationOccurrence link returns [out]
: universalRelation LP universalWord COMMA universalWord RP : r=universalRelation
LP uw1=universalWord COMMA uw2=universalWord RP
{$out = unl.Link($r.out, $uw1.out, $uw2.out)}
; ;
universalWord universalWord returns [out]
: headword : h=headword {headword = $h.out}
(LP restriction (COMMA restriction)* RP)? {restriction_list = []}
(attribute)* (LP
| value r1=restriction {restriction_list.append($r1.out)}
(COMMA r2=restriction {restriction_list.append($r2.out)} )*
RP)?
{attr_list = []}
(a=attribute {attr_list.append($a.out)} )*
{$out = unl.UniversalWord(headword, restriction_list, attr_list)}
| v=value {$out = unl.UniversalWord($v.out, [], [])}
; ;
headword headword returns [out]
: ident : i=ident {$out = unl.Headword($i.out)}
; ;
restriction restriction returns [out]
: universalRelation GREATER ident : r=universalRelation GREATER i=ident {$out = unl.Restriction($r.out, $i.out)}
; ;
attribute attribute returns [out]
: DOT AT ident : DOT AT i=ident {$out = unl.Attribute($i.out)}
; ;
value value returns [out]
: VALUE : v=VALUE {$out = unl.Ident($v.text)}
; ;
universalRelation universalRelation returns [out]
: ( AND | AOJ | BEN | CNT | : r=( AND | AOJ | BEN | CNT
EQU | ICL | OBJ | QUA ) | EQU | ICL | OBJ | QUA ) {$out = unl.Relation($r.text)}
; ;
...@@ -56,9 +70,16 @@ universalRelation ...@@ -56,9 +70,16 @@ universalRelation
sentence : (word | punctuation | bracket)* ; sentence : (word | punctuation | bracket)* ;
ident : word (UNDERSCORE word)* ; ident returns [out]
: w1=word {ident = $w1.out}
(UNDERSCORE w2=word {ident += "_" + $w2.out} )*
{$out = unl.Ident(ident)}
;
word : LETTER | WORD ; word returns [$out]
: l=LETTER {$out = $l.text}
| w=WORD {$out = $w.text}
;
punctuation : DOT | COMMA | SEMCOL | COLON | DASH ; punctuation : DOT | COMMA | SEMCOL | COLON | DASH ;
......
...@@ -59,8 +59,8 @@ WORD ...@@ -59,8 +59,8 @@ WORD
VALUE VALUE
rule names: rule names:
unlPart unlGraph
relationOccurrence link
universalWord universalWord
headword headword
restriction restriction
...@@ -75,4 +75,4 @@ bracket ...@@ -75,4 +75,4 @@ bracket
atn: atn:
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 29, 103, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 3, 2, 3, 2, 6, 2, 31, 10, 2, 13, 2, 14, 2, 32, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 7, 4, 49, 10, 4, 12, 4, 14, 4, 52, 11, 4, 3, 4, 3, 4, 5, 4, 56, 10, 4, 3, 4, 7, 4, 59, 10, 4, 12, 4, 14, 4, 62, 11, 4, 3, 4, 5, 4, 65, 10, 4, 3, 5, 3, 5, 3, 6, 3, 6, 3, 6, 3, 6, 3, 7, 3, 7, 3, 7, 3, 7, 3, 8, 3, 8, 3, 9, 3, 9, 3, 10, 3, 10, 3, 10, 7, 10, 84, 10, 10, 12, 10, 14, 10, 87, 11, 10, 3, 11, 3, 11, 3, 11, 7, 11, 92, 10, 11, 12, 11, 14, 11, 95, 11, 11, 3, 12, 3, 12, 3, 13, 3, 13, 3, 14, 3, 14, 3, 14, 2, 2, 15, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 2, 6, 3, 2, 19, 26, 3, 2, 27, 28, 3, 2, 6, 10, 3, 2, 11, 14, 2, 98, 2, 28, 3, 2, 2, 2, 4, 36, 3, 2, 2, 2, 6, 64, 3, 2, 2, 2, 8, 66, 3, 2, 2, 2, 10, 68, 3, 2, 2, 2, 12, 72, 3, 2, 2, 2, 14, 76, 3, 2, 2, 2, 16, 78, 3, 2, 2, 2, 18, 85, 3, 2, 2, 2, 20, 88, 3, 2, 2, 2, 22, 96, 3, 2, 2, 2, 24, 98, 3, 2, 2, 2, 26, 100, 3, 2, 2, 2, 28, 30, 7, 3, 2, 2, 29, 31, 5, 4, 3, 2, 30, 29, 3, 2, 2, 2, 31, 32, 3, 2, 2, 2, 32, 30, 3, 2, 2, 2, 32, 33, 3, 2, 2, 2, 33, 34, 3, 2, 2, 2, 34, 35, 7, 4, 2, 2, 35, 3, 3, 2, 2, 2, 36, 37, 5, 16, 9, 2, 37, 38, 7, 11, 2, 2, 38, 39, 5, 6, 4, 2, 39, 40, 7, 7, 2, 2, 40, 41, 5, 6, 4, 2, 41, 42, 7, 12, 2, 2, 42, 5, 3, 2, 2, 2, 43, 55, 5, 8, 5, 2, 44, 45, 7, 11, 2, 2, 45, 50, 5, 10, 6, 2, 46, 47, 7, 7, 2, 2, 47, 49, 5, 10, 6, 2, 48, 46, 3, 2, 2, 2, 49, 52, 3, 2, 2, 2, 50, 48, 3, 2, 2, 2, 50, 51, 3, 2, 2, 2, 51, 53, 3, 2, 2, 2, 52, 50, 3, 2, 2, 2, 53, 54, 7, 12, 2, 2, 54, 56, 3, 2, 2, 2, 55, 44, 3, 2, 2, 2, 55, 56, 3, 2, 2, 2, 56, 60, 3, 2, 2, 2, 57, 59, 5, 12, 7, 2, 58, 57, 3, 2, 2, 2, 59, 62, 3, 2, 2, 2, 60, 58, 3, 2, 2, 2, 60, 61, 3, 2, 2, 2, 61, 65, 3, 2, 2, 2, 62, 60, 3, 2, 2, 2, 63, 65, 5, 14, 8, 2, 64, 43, 3, 2, 2, 2, 64, 63, 3, 2, 2, 2, 65, 7, 3, 2, 2, 2, 66, 67, 5, 20, 11, 2, 67, 9, 3, 2, 2, 2, 68, 69, 5, 16, 9, 2, 69, 70, 7, 16, 2, 2, 70, 71, 5, 20, 11, 2, 71, 11, 3, 2, 2, 2, 72, 73, 7, 6, 2, 2, 73, 74, 7, 17, 2, 2, 74, 75, 5, 20, 11, 2, 75, 13, 3, 2, 2, 2, 76, 77, 7, 29, 2, 2, 77, 15, 3, 2, 2, 2, 78, 79, 9, 2, 2, 2, 79, 17, 3, 2, 2, 2, 80, 84, 5, 22, 12, 2, 81, 84, 5, 24, 13, 2, 82, 84, 5, 26, 14, 2, 83, 80, 3, 2, 2, 2, 83, 81, 3, 2, 2, 2, 83, 82, 3, 2, 2, 2, 84, 87, 3, 2, 2, 2, 85, 83, 3, 2, 2, 2, 85, 86, 3, 2, 2, 2, 86, 19, 3, 2, 2, 2, 87, 85, 3, 2, 2, 2, 88, 93, 5, 22, 12, 2, 89, 90, 7, 18, 2, 2, 90, 92, 5, 22, 12, 2, 91, 89, 3, 2, 2, 2, 92, 95, 3, 2, 2, 2, 93, 91, 3, 2, 2, 2, 93, 94, 3, 2, 2, 2, 94, 21, 3, 2, 2, 2, 95, 93, 3, 2, 2, 2, 96, 97, 9, 3, 2, 2, 97, 23, 3, 2, 2, 2, 98, 99, 9, 4, 2, 2, 99, 25, 3, 2, 2, 2, 100, 101, 9, 5, 2, 2, 101, 27, 3, 2, 2, 2, 10, 32, 50, 55, 60, 64, 83, 85, 93] [3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 29, 134, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 6, 2, 34, 10, 2, 13, 2, 14, 2, 35, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 7, 4, 59, 10, 4, 12, 4, 14, 4, 62, 11, 4, 3, 4, 3, 4, 5, 4, 66, 10, 4, 3, 4, 3, 4, 3, 4, 3, 4, 7, 4, 72, 10, 4, 12, 4, 14, 4, 75, 11, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 5, 4, 82, 10, 4, 3, 5, 3, 5, 3, 5, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 7, 3, 7, 3, 7, 3, 7, 3, 7, 3, 8, 3, 8, 3, 8, 3, 9, 3, 9, 3, 9, 3, 10, 3, 10, 3, 10, 7, 10, 106, 10, 10, 12, 10, 14, 10, 109, 11, 10, 3, 11, 3, 11, 3, 11, 3, 11, 3, 11, 3, 11, 7, 11, 117, 10, 11, 12, 11, 14, 11, 120, 11, 11, 3, 11, 3, 11, 3, 12, 3, 12, 3, 12, 3, 12, 5, 12, 128, 10, 12, 3, 13, 3, 13, 3, 14, 3, 14, 3, 14, 2, 2, 15, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 2, 5, 3, 2, 19, 26, 3, 2, 6, 10, 3, 2, 11, 14, 2, 130, 2, 28, 3, 2, 2, 2, 4, 40, 3, 2, 2, 2, 6, 81, 3, 2, 2, 2, 8, 83, 3, 2, 2, 2, 10, 86, 3, 2, 2, 2, 12, 91, 3, 2, 2, 2, 14, 96, 3, 2, 2, 2, 16, 99, 3, 2, 2, 2, 18, 107, 3, 2, 2, 2, 20, 110, 3, 2, 2, 2, 22, 127, 3, 2, 2, 2, 24, 129, 3, 2, 2, 2, 26, 131, 3, 2, 2, 2, 28, 29, 7, 3, 2, 2, 29, 33, 8, 2, 1, 2, 30, 31, 5, 4, 3, 2, 31, 32, 8, 2, 1, 2, 32, 34, 3, 2, 2, 2, 33, 30, 3, 2, 2, 2, 34, 35, 3, 2, 2, 2, 35, 33, 3, 2, 2, 2, 35, 36, 3, 2, 2, 2, 36, 37, 3, 2, 2, 2, 37, 38, 7, 4, 2, 2, 38, 39, 8, 2, 1, 2, 39, 3, 3, 2, 2, 2, 40, 41, 5, 16, 9, 2, 41, 42, 7, 11, 2, 2, 42, 43, 5, 6, 4, 2, 43, 44, 7, 7, 2, 2, 44, 45, 5, 6, 4, 2, 45, 46, 7, 12, 2, 2, 46, 47, 8, 3, 1, 2, 47, 5, 3, 2, 2, 2, 48, 49, 5, 8, 5, 2, 49, 50, 8, 4, 1, 2, 50, 65, 8, 4, 1, 2, 51, 52, 7, 11, 2, 2, 52, 53, 5, 10, 6, 2, 53, 60, 8, 4, 1, 2, 54, 55, 7, 7, 2, 2, 55, 56, 5, 10, 6, 2, 56, 57, 8, 4, 1, 2, 57, 59, 3, 2, 2, 2, 58, 54, 3, 2, 2, 2, 59, 62, 3, 2, 2, 2, 60, 58, 3, 2, 2, 2, 60, 61, 3, 2, 2, 2, 61, 63, 3, 2, 2, 2, 62, 60, 3, 2, 2, 2, 63, 64, 7, 12, 2, 2, 64, 66, 3, 2, 2, 2, 65, 51, 3, 2, 2, 2, 65, 66, 3, 2, 2, 2, 66, 67, 3, 2, 2, 2, 67, 73, 8, 4, 1, 2, 68, 69, 5, 12, 7, 2, 69, 70, 8, 4, 1, 2, 70, 72, 3, 2, 2, 2, 71, 68, 3, 2, 2, 2, 72, 75, 3, 2, 2, 2, 73, 71, 3, 2, 2, 2, 73, 74, 3, 2, 2, 2, 74, 76, 3, 2, 2, 2, 75, 73, 3, 2, 2, 2, 76, 77, 8, 4, 1, 2, 77, 82, 3, 2, 2, 2, 78, 79, 5, 14, 8, 2, 79, 80, 8, 4, 1, 2, 80, 82, 3, 2, 2, 2, 81, 48, 3, 2, 2, 2, 81, 78, 3, 2, 2, 2, 82, 7, 3, 2, 2, 2, 83, 84, 5, 20, 11, 2, 84, 85, 8, 5, 1, 2, 85, 9, 3, 2, 2, 2, 86, 87, 5, 16, 9, 2, 87, 88, 7, 16, 2, 2, 88, 89, 5, 20, 11, 2, 89, 90, 8, 6, 1, 2, 90, 11, 3, 2, 2, 2, 91, 92, 7, 6, 2, 2, 92, 93, 7, 17, 2, 2, 93, 94, 5, 20, 11, 2, 94, 95, 8, 7, 1, 2, 95, 13, 3, 2, 2, 2, 96, 97, 7, 29, 2, 2, 97, 98, 8, 8, 1, 2, 98, 15, 3, 2, 2, 2, 99, 100, 9, 2, 2, 2, 100, 101, 8, 9, 1, 2, 101, 17, 3, 2, 2, 2, 102, 106, 5, 22, 12, 2, 103, 106, 5, 24, 13, 2, 104, 106, 5, 26, 14, 2, 105, 102, 3, 2, 2, 2, 105, 103, 3, 2, 2, 2, 105, 104, 3, 2, 2, 2, 106, 109, 3, 2, 2, 2, 107, 105, 3, 2, 2, 2, 107, 108, 3, 2, 2, 2, 108, 19, 3, 2, 2, 2, 109, 107, 3, 2, 2, 2, 110, 111, 5, 22, 12, 2, 111, 118, 8, 11, 1, 2, 112, 113, 7, 18, 2, 2, 113, 114, 5, 22, 12, 2, 114, 115, 8, 11, 1, 2, 115, 117, 3, 2, 2, 2, 116, 112, 3, 2, 2, 2, 117, 120, 3, 2, 2, 2, 118, 116, 3, 2, 2, 2, 118, 119, 3, 2, 2, 2, 119, 121, 3, 2, 2, 2, 120, 118, 3, 2, 2, 2, 121, 122, 8, 11, 1, 2, 122, 21, 3, 2, 2, 2, 123, 124, 7, 27, 2, 2, 124, 128, 8, 12, 1, 2, 125, 126, 7, 28, 2, 2, 126, 128, 8, 12, 1, 2, 127, 123, 3, 2, 2, 2, 127, 125, 3, 2, 2, 2, 128, 23, 3, 2, 2, 2, 129, 130, 9, 3, 2, 2, 130, 25, 3, 2, 2, 2, 131, 132, 9, 4, 2, 2, 132, 27, 3, 2, 2, 2, 11, 35, 60, 65, 73, 81, 105, 107, 118, 127]
\ No newline at end of file \ No newline at end of file
...@@ -8,6 +8,9 @@ else: ...@@ -8,6 +8,9 @@ else:
from typing.io import TextIO from typing.io import TextIO
from asd import unl
def serializedATN(): def serializedATN():
with StringIO() as buf: with StringIO() as buf:
......
...@@ -5,24 +5,27 @@ if __name__ is not None and "." in __name__: ...@@ -5,24 +5,27 @@ if __name__ is not None and "." in __name__:
else: else:
from unlParser import unlParser from unlParser import unlParser
from asd import unl
# This class defines a complete listener for a parse tree produced by unlParser. # This class defines a complete listener for a parse tree produced by unlParser.
class unlListener(ParseTreeListener): class unlListener(ParseTreeListener):
# Enter a parse tree produced by unlParser#unlPart. # Enter a parse tree produced by unlParser#unlGraph.
def enterUnlPart(self, ctx:unlParser.UnlPartContext): def enterUnlGraph(self, ctx:unlParser.UnlGraphContext):
pass pass
# Exit a parse tree produced by unlParser#unlPart. # Exit a parse tree produced by unlParser#unlGraph.
def exitUnlPart(self, ctx:unlParser.UnlPartContext): def exitUnlGraph(self, ctx:unlParser.UnlGraphContext):
pass pass
# Enter a parse tree produced by unlParser#relationOccurrence. # Enter a parse tree produced by unlParser#link.
def enterRelationOccurrence(self, ctx:unlParser.RelationOccurrenceContext): def enterLink(self, ctx:unlParser.LinkContext):
pass pass
# Exit a parse tree produced by unlParser#relationOccurrence. # Exit a parse tree produced by unlParser#link.
def exitRelationOccurrence(self, ctx:unlParser.RelationOccurrenceContext): def exitLink(self, ctx:unlParser.LinkContext):
pass pass
......
This diff is collapsed.
...@@ -112,8 +112,11 @@ def parse_unl(input): ...@@ -112,8 +112,11 @@ def parse_unl(input):
# -- Parse UNL part # -- Parse UNL part
parser = instantiate_lexer_parser(input, unlLexer, unlParser) parser = instantiate_lexer_parser(input, unlLexer, unlParser)
print("--- Parse UNL representation") print("--- Parse UNL representation")
tree = parser.unlPart() tree = parser.unlGraph()
print("----- resulting tree:\n" + tree.toStringTree(recog=parser)) print("----- resulting tree:\n" + tree.toStringTree(recog=parser))
unl = tree.out
return unl
#============================================================================== #==============================================================================
...@@ -140,7 +143,8 @@ def main(argv): ...@@ -140,7 +143,8 @@ def main(argv):
# -- UNL Parsing (Sentence UNL Part) # -- UNL Parsing (Sentence UNL Part)
print("-- UNL Parsing (UNL Representation) ") print("-- UNL Parsing (UNL Representation) ")
parse_unl(InputStream(unl_part)) unl = parse_unl(InputStream(unl_part))
print("----- UNL string:\n" + unl.to_string())
if __name__ == '__main__': if __name__ == '__main__':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment