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

Minimal UNL-RDF generation

parent 1333c7ad
No related branches found
No related tags found
No related merge requests found
...@@ -11,14 +11,17 @@ ...@@ -11,14 +11,17 @@
# Importing required modules # Importing required modules
#============================================================================== #==============================================================================
import pydot import pydot as dot
import asd.urdf as urdf
#============================================================================== #==============================================================================
# Parameters # Parameters
#============================================================================== #==============================================================================
# None UNL_RDF_SCHEMA = "https://unl.tetras-libre.fr/rdf/schema/"
UNL_RDF_BASE_URI = "https://unl.tetras-libre.fr/rdf/dev/"
#============================================================================== #==============================================================================
...@@ -43,13 +46,19 @@ class Graph: ...@@ -43,13 +46,19 @@ class Graph:
return res return res
def to_dot_graph(self): def to_dot_graph(self):
graph = pydot.Dot("My_Graph", graph_type="graph", bgcolor="yellow") graph = dot.Dot("My_Graph", graph_type="graph", bgcolor="yellow")
for link in self.link_list: for link in self.link_list:
graph.add_node(link.uw1.to_dot_node()) graph.add_node(link.uw1.to_dot_node())
graph.add_node(link.uw2.to_dot_node()) graph.add_node(link.uw2.to_dot_node())
graph.add_edge(link.to_dot_edge()) graph.add_edge(link.to_dot_edge())
return graph return graph
def to_rdf_graph(self):
graph = urdf.init_unl_rdf_graph()
for link in self.link_list:
graph = link.to_rdf_graph(graph)
return graph
class Link: class Link:
""" link := Link(relation, uw, uw) """ """ link := Link(relation, uw, uw) """
...@@ -68,9 +77,15 @@ class Link: ...@@ -68,9 +77,15 @@ class Link:
def to_dot_edge(self): def to_dot_edge(self):
n1_name = self.uw1.to_dot_node().get_name() n1_name = self.uw1.to_dot_node().get_name()
n2_name = self.uw2.to_dot_node().get_name() n2_name = self.uw2.to_dot_node().get_name()
edge = pydot.Edge(n1_name, n2_name, color="blue") edge = dot.Edge(n1_name, n2_name, color="blue")
return edge return edge
def to_rdf_graph(self, graph):
graph, rel_node = self.relation.to_rdf_graph(graph)
graph, uw1_node = self.uw1.to_rdf_graph(graph)
graph, uw2_node = self.uw2.to_rdf_graph(graph)
graph = urdf.add_relation_triple(graph, rel_node, uw1_node, uw2_node)
return graph
class Relation: class Relation:
""" relation := Relation(id) """ """ relation := Relation(id) """
...@@ -81,6 +96,9 @@ class Relation: ...@@ -81,6 +96,9 @@ class Relation:
def to_string(self): def to_string(self):
return self.id return self.id
def to_rdf_graph(self, graph):
return urdf.add_relation_node(graph, self.id)
class UniversalWord: class UniversalWord:
""" uw := UniversalWord(headword, restriction*, attr*) """ """ uw := UniversalWord(headword, restriction*, attr*) """
...@@ -93,8 +111,8 @@ class UniversalWord: ...@@ -93,8 +111,8 @@ class UniversalWord:
def to_string(self): def to_string(self):
res = self.headword.to_string() + "(" res = self.headword.to_string() + "("
res_init_length = len(res) res_init_length = len(res)
for restrictio in self.restriction_list: for restriction in self.restriction_list:
res += restrictio.to_string() + ", " res += restriction.to_string() + ", "
res = res[:-1] res = res[:-1]
if len(res) > res_init_length: if len(res) > res_init_length:
res = res[:-1] res = res[:-1]
...@@ -103,12 +121,21 @@ class UniversalWord: ...@@ -103,12 +121,21 @@ class UniversalWord:
res += ".@" + attr.to_string() res += ".@" + attr.to_string()
return res return res
def to_id(self):
res = self.headword.to_string()
for restriction in self.restriction_list:
res += "-" + restriction.to_id()
return res
def to_dot_node(self): def to_dot_node(self):
node_name = self.headword.to_string() # TODO: à revoir pour id unique node_name = self.headword.to_string() # TODO: à revoir pour id unique
node_label = self.headword.to_string() node_label = self.headword.to_string()
node = pydot.Node(node_name, label=node_label) node = dot.Node(node_name, label=node_label)
return node return node
def to_rdf_graph(self, graph):
return urdf.add_uw_node(graph, self.to_id(), self.to_string())
class Headword: class Headword:
""" headword := Headword(id) """ """ headword := Headword(id) """
...@@ -130,6 +157,9 @@ class Restriction: ...@@ -130,6 +157,9 @@ class Restriction:
def to_string(self): def to_string(self):
return self.relation.to_string() + ">" + self.id.to_string() return self.relation.to_string() + ">" + self.id.to_string()
def to_id(self):
return self.relation.to_string() + "-" + self.id.to_string()
class Attribute: class Attribute:
""" attr := Attribute(id) """ """ attr := Attribute(id) """
......
#!/usr/bin/python3.10
# -*-coding:Utf-8 -*
#==============================================================================
# unlAnt: uRDF
#------------------------------------------------------------------------------
# This module contains methods to generate an UNL-RDF graph
#==============================================================================
#==============================================================================
# Importing required modules
#==============================================================================
import rdflib as rdf
from rdflib import Namespace, URIRef, Literal #, BNode
from rdflib import XSD, RDFS, RDF, OWL
#==============================================================================
# Parameters
#==============================================================================
# -- Schémas, URI
UNL_RDF_SCHEMA = "https://unl.tetras-libre.fr/rdf/schema/"
UNL_RDF_BASE_URI = "https://unl.tetras-libre.fr/rdf/dev/"
# -- Namespace(s)
URDF = Namespace(UNL_RDF_SCHEMA)
#==============================================================================
# Main Functions
#==============================================================================
# ----------------------------------------------
# Methods to initialize the UNL-RDF graph
# ----------------------------------------------
def init_unl_rdf_graph():
graph = rdf.Graph()
graph.bind("urdf", UNL_RDF_SCHEMA) # prefix for UNL-RDF schema
return graph
def add_useful_prefix(graph):
""" Add useful prefix, as RDFS, OWL... """
graph.bind("xsd", XSD)
graph.bind("rdfs", RDFS)
graph.bind("owl", OWL)
graph.bind("rdf", RDF)
return graph
# ----------------------------------------------
# Methods to add a node in the UNL-RDF graph
# ----------------------------------------------
def add_relation_node(graph, relation_id):
""" Add a node corresponding to a relation in UNL.
Attrs:
- relation_id (str): relation identifier
Returns:
- a RDF node corresponding to the UNL relation
"""
relation_node = URIRef(UNL_RDF_BASE_URI + relation_id)
relation_label = Literal(relation_id)
graph.add((relation_node, RDF.type, URDF.UW_Occurrence))
graph.add((relation_node, RDFS.label, relation_label))
return graph, relation_node
def add_uw_node(graph, uw_id, uw_label):
""" Add a node corresponding to an universal word (UW) in UNL.
Attrs:
- uw_id (str): UW identifier
- uw_label (str): label for the UW node
Returns:
- a RDF node corresponding to the UW
"""
uw_node = URIRef(UNL_RDF_BASE_URI + uw_id)
uw_label = Literal(uw_label)
graph.add((uw_node, RDF.type, URDF.UW_Occurrence))
graph.add((uw_node, RDFS.label, uw_label))
return graph, uw_node
# ----------------------------------------------
# Methods to add a triple in the UNL-RDF graph
# ----------------------------------------------
def add_relation_triple(graph, relation_node, uw1_node, uw2_node):
""" Add a triple corresponding to a relation between two nodes in UNL.
Attrs:
- relation_node (RDF node): relation node
- uw1_node (RDF node): UW1 node
- uw2_node (RDF node): UW2 node
"""
graph.add((uw1_node, relation_node, uw2_node))
return graph
...@@ -21,11 +21,13 @@ from antlr4 import FileStream, CommonTokenStream, InputStream ...@@ -21,11 +21,13 @@ from antlr4 import FileStream, CommonTokenStream, InputStream
#============================================================================== #==============================================================================
# ANTLR Grammar # ANTLR Grammar
doc_grammar = 'grammar/doc/doc.g4' doc_grammar = 'grammar/doc/doc.g4'
org_grammar = 'grammar/org/org.g4' org_grammar = 'grammar/org/org.g4'
unl_grammar = 'grammar/unl/unl.g4' unl_grammar = 'grammar/unl/unl.g4'
# RDF URIs
UNL_RDF_BASE_URI = "https://unl.tetras-libre.fr/rdf/dev/"
#============================================================================== #==============================================================================
# Utilities # Utilities
...@@ -152,6 +154,13 @@ def main(argv): ...@@ -152,6 +154,13 @@ def main(argv):
unl_dot_graph.write_raw("output/output_raw.dot") unl_dot_graph.write_raw("output/output_raw.dot")
unl_dot_graph.write_png("output/output.png") unl_dot_graph.write_png("output/output.png")
# -- Generate rdf file
unl_rdf_graph = unl.to_rdf_graph()
print("----- RDF file generated: " + "output/output.ttl")
unl_rdf_graph.serialize(destination ="output/output.ttl",
base = UNL_RDF_BASE_URI + "output",
format = 'turtle')
if __name__ == '__main__': if __name__ == '__main__':
main(sys.argv) main(sys.argv)
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment