-
Notifications
You must be signed in to change notification settings - Fork 0
/
wgraph.py
97 lines (78 loc) · 3.34 KB
/
wgraph.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
# hello.py - http://www.graphviz.org/content/hello
# -*- coding: utf-8 -*-
import re
import sys
import yaml
import format
import wlib
from graphviz import Digraph
## Graph creation
def wgraph(yaml_data, status_dict, phase, graph_file):
g = Digraph('G', filename=graph_file, format='svg')
#g = Digraph('G', filename=graph_file)
for record in yaml_data['statements']:
# m1 = re.match('\+', record['wave'])
# m2 = re.match('-', record['wave'])
if phase[record['id']]:
lbl = record['id'] + '\n' + format.format(record['text'])
if not (record['id'] in status_dict.keys()):
g.attr('node', color='red', href=record['wiki_link'], fontcolor = 'black')
g.node(record['id'], label=lbl)
else:
g.attr('node', color='gray90', href=record['wiki_link'], fontcolor = 'gray90')
g.node(record['id'], label=lbl)
else:
lbl = record['id'] + '\n' + format.format(record['text'])
if not (record['id'] in status_dict.keys()):
g.attr('node', color='blue', href=record['wiki_link'], fontcolor = 'black')
g.node(record['id'], label=lbl)
else:
g.attr('node', color='gray90', href=record['wiki_link'], fontcolor = 'gray90')
g.node(record['id'], label=lbl)
### Edge creation
for record in yaml_data['statements']:
for edge in record['direct_contradictions']:
if edge['id']:
if not (edge['id'] in status_dict.keys()):
g.edge(record['id'], edge['id'])
else:
g.edge(record['id'], edge['id'], color='gray90')
for edge in record['indirect_contradictions']:
if edge['id']:
if not (edge['id'] in status_dict.keys()):
g.edge(record['id'], edge['id'], style='dotted')
else:
g.edge(record['id'], edge['id'], color='gray90',style='dotted')
for edge in record['complement']:
if edge['id']:
if not (edge['id'] in status_dict.keys()):
g.edge(record['id'], edge['id'], color='red', dir="both")
else:
g.edge(record['id'], edge['id'], color='gray90', dir="both")
g.render(view=False)
# g.view()
if __name__ == "__main__":
######### get file's names from the command line ####################
if (len(sys.argv)==3):
yaml_file = sys.argv[1]
graph_file = sys.argv[2]
else:
print (" ######################################################\n")
print (" Syntax is:\n")
print (" python3 ../wikigraph/wgraph.py yaml/desdemona.yml desdemona\n")
print (" ######################################################\n")
quit()
######### take data from YAML file ####################
my_config=''
f = open( "%s" % yaml_file )
data1 = f.read()
f.close()
yaml_version = yaml.__version__
m = re.match('(\d(\.\d)?)', yaml_version)
yaml_ver = m.group(1)
if (float(yaml_ver) < 5.1):
yaml_data = yaml.load(data1)
else:
yaml_data = yaml.load(data1,Loader=yaml.FullLoader)
(root_id, rdict, reverse_rdict, phase, paths_dict ) = wlib.check_structure(yaml_data)
wgraph(yaml_data, {}, phase, graph_file)