From 1333c7ad44fa95ba6f53ebbd6d7959dd00706c86 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Aur=C3=A9lien=20Lamercerie?=
 <aurelien.lamercerie@tetras-libre.fr>
Date: Mon, 11 Apr 2022 20:43:24 +0200
Subject: [PATCH] Dot Graph Generation (simple graph)

---
 .gitignore       |  3 +++
 asd/unl.py       | 22 +++++++++++++++++++++-
 parse.py         |  6 ++++++
 requirements.txt |  1 +
 4 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore
index 950f50f..8eb57be 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,6 @@
 devtemp*.py
 .project
 *.ttl.tbc
+output/*
+*env/*
+
diff --git a/asd/unl.py b/asd/unl.py
index ff21b26..ad0b48f 100644
--- a/asd/unl.py
+++ b/asd/unl.py
@@ -11,7 +11,7 @@
 # Importing required modules
 #==============================================================================
 
-# None
+import pydot
 
 
 #==============================================================================
@@ -42,6 +42,14 @@ class Graph:
         res += ")"
         return res
     
+    def to_dot_graph(self):
+        graph = pydot.Dot("My_Graph", graph_type="graph", bgcolor="yellow")
+        for link in self.link_list:
+            graph.add_node(link.uw1.to_dot_node())
+            graph.add_node(link.uw2.to_dot_node())
+            graph.add_edge(link.to_dot_edge())
+        return graph
+    
     
 class Link:
     """ link := Link(relation, uw, uw) """
@@ -57,6 +65,12 @@ class Link:
         res += self.uw2.to_string() + ")"
         return res
     
+    def to_dot_edge(self):
+        n1_name = self.uw1.to_dot_node().get_name()
+        n2_name = self.uw2.to_dot_node().get_name()
+        edge = pydot.Edge(n1_name, n2_name, color="blue")
+        return edge
+    
     
 class Relation:
     """ relation := Relation(id) """
@@ -89,6 +103,12 @@ class UniversalWord:
             res += ".@" + attr.to_string()
         return res
     
+    def to_dot_node(self):
+        node_name = self.headword.to_string() # TODO: à revoir pour id unique
+        node_label = self.headword.to_string()
+        node = pydot.Node(node_name, label=node_label)
+        return node
+    
     
 class Headword:
     """ headword := Headword(id) """
diff --git a/parse.py b/parse.py
index 0c23642..76559d3 100644
--- a/parse.py
+++ b/parse.py
@@ -145,6 +145,12 @@ def main(argv):
     print("-- UNL Parsing (UNL Representation) ")
     unl = parse_unl(InputStream(unl_part))
     print("----- UNL string:\n" + unl.to_string())
+    
+    # -- Generate dot file
+    unl_dot_graph = unl.to_dot_graph()
+    print("----- DOT string:\n" + unl_dot_graph.to_string())
+    unl_dot_graph.write_raw("output/output_raw.dot")
+    unl_dot_graph.write_png("output/output.png")
 
 
 if __name__ == '__main__':
diff --git a/requirements.txt b/requirements.txt
index 7545404..726e85c 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1 +1,2 @@
 antlr4-python3-runtime==4.9.3
+pydot
-- 
GitLab