ENIAMsemGraphOf.ml
14.2 KB
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
(*
* ENIAMexec implements ENIAM processing stream
* Copyright (C) 2016-2017 Wojciech Jaworski <wjaworski atSPAMfree mimuw dot edu dot pl>
* Copyright (C) 2016-2017 Institute of Computer Science Polish Academy of Sciences
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*)
open ENIAMsemTypes
open Printf
let escape_string s =
Int.fold 0 (String.length s - 1) "" (fun t i ->
match String.sub s i 1 with
"<" -> t ^ "〈"
| ">" -> t ^ "〉"
| "\"" -> t ^ "\\\""
| c -> t ^ c)
let string_of_node t =
let l = [
"ORTH",Val t.orth;"LEMMA",Val t.lemma;"POS",Val t.pos;"ID",Val (string_of_int t.id);"WEIGHT",Val (string_of_float t.weight);
"SYMBOL",t.symbol;"ARG_SYMBOL",t.arg_symbol;"ARG_DIR",Val t.arg_dir] @ t.attrs in
"{ " ^ String.concat " | " (Xlist.map l (fun (e,t) -> "{ " ^ e ^ " | " ^ escape_string (ENIAMsemStringOf.linear_term 0 t) ^ " }")) ^ " }"
let rec string_of_quant_rec quant = function
Tuple l -> Xlist.fold l quant string_of_quant_rec
| Variant(e,l) -> (ENIAMsemStringOf.linear_term 0 (Variant(e,l))) :: quant
| Dot -> quant
| Val s -> s :: quant
| _ -> failwith "string_of_quant_rec"
let string_of_quant t =
let l = string_of_quant_rec [] t in
let s = String.concat " " l in
if s = "" then "" else "<I>" ^ s ^ "</I> "
(*
let single_rel_id_count = ref 0
let get_single_rel_id () =
let id = !single_rel_id_count in
incr single_rel_id_count;
"s" ^ string_of_int id
let print_edge file label upper id =
if upper <> "" then
if label = "" then fprintf file " %s -> %s\n" upper id
else fprintf file " %s -> %s [label=\"%s\"]\n" upper id label
let rec print_dependency_tree_rec file edge upper id = function
Node t ->
fprintf file " %s [label=\"%s\"]\n" id (string_of_node t);
print_edge file edge upper id;
print_dependency_tree_rec2 file "" id t.args
| Variant(e,l) ->
fprintf file " %s [shape=diamond,label=\"%s\"]\n" id e;
print_edge file edge upper id;
Xlist.iter l (fun (i,t) -> print_dependency_tree_rec2 file i id t)
| Val s ->
fprintf file " %s [shape=box,label=\"%s\"]\n" id s;
print_edge file edge upper id
(* | SetAttr(a,s,t) ->
fprintf file " %s [shape=box,label=\"SetAttr(%s,%s)\"]\n" id a (ENIAM_LCGstringOf.linear_term 0 s);
print_edge file edge upper id;
print_dependency_tree_rec2 file "" id t *)
| Dot -> ()
(* fprintf file " %s [shape=box,label=\"Dot\"]\n" id;
print_edge file edge upper id*)
| Ref i -> print_edge file edge upper ("x" ^ string_of_int i)
| t -> failwith ("print_dependency_tree_rec: " ^ ENIAMsemStringOf.linear_term 0 t)
and print_dependency_tree_rec2 file edge upper = function
Tuple l -> Xlist.iter l (print_dependency_tree_rec2 file edge upper)
| t -> print_dependency_tree_rec file edge upper (get_single_rel_id ()) t
let print_semantic_graph path name dependency_tree =
single_rel_id_count := 0;
File.file_out (path ^ name ^ ".gv") (fun file ->
fprintf file "digraph G {\n node [shape=record]\n";
Int.iter 0 (Array.length dependency_tree - 1) (fun i -> print_dependency_tree_rec file "" "" ("x" ^ string_of_int i) dependency_tree.(i));
fprintf file "}\n");
Sys.chdir path;
ignore (Sys.command ("dot -Tpng " ^ name ^ ".gv -o " ^ name ^ ".png"));
ignore (Sys.command ("rm " ^ name ^ ".gv"));
String.iter (function '/' -> Sys.chdir ".." | _ -> ()) path
*)
let id_counter = ref 0
let print_edge2 file edge_rev edge_label edge_style edge_head edge_tail upper id =
let edge_head,edge_tail,upper,id = if edge_rev then edge_tail,edge_head,id,upper else edge_head,edge_tail,upper,id in
let l =
(if edge_label = "" then [] else ["label=\"" ^ edge_label ^ "\""]) @
(if edge_style = "" then [] else ["style=\"" ^ edge_style ^ "\""]) @
(if edge_head = "" then [] else ["ltail=\"" ^ edge_head ^ "\""]) @
(if edge_tail = "" then [] else ["lhead=\"" ^ edge_tail ^ "\""]) in
if upper <> 0 then
if l = [] then fprintf file " %d -> %d\n" upper id
else fprintf file " %d -> %d [%s]\n" upper id (String.concat "," l)
(*let rec print_graph2_rec file edge_rev edge_label edge_head upper = function
Node t ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [label=\"%s\"]\n" id (string_of_node t);
print_edge2 file edge_rev edge_label edge_head "" upper id;
print_graph2_rec file false "" "" id t.args
| Concept t ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=box,label=<%s%s %s>]\n" id
(string_of_quant t.c_quant)
(escape_string (ENIAMsemStringOf.linear_term 0 t.c_sense))
(if t.c_name=Dot then "" else "„" ^ ENIAMsemStringOf.linear_term 0 t.c_name ^ "”"); (* FIXME *)
print_edge2 file edge_rev edge_label edge_head "" upper id;
print_graph2_rec file false "" "" id t.c_relations
| Context t ->
let id = !id_counter in
incr id_counter;
if t.cx_sense = Dot then fprintf file " subgraph cluster%d {\nlabel=\"\"\n" id
else fprintf file " subgraph cluster%d {\nlabel=\"%s\"\n" id (ENIAMsemStringOf.linear_term 0 t.cx_sense);
print_graph2_rec file false "" "" 0 t.cx_contents;
fprintf file " }\n";
print_edge2 file edge_rev edge_label edge_head ("cluster" ^ string_of_int id) upper (id+1);
print_graph2_rec file false "" ("cluster" ^ string_of_int id) (id+1) t.cx_relations;
| Relation(role,role_attr,t) ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=circle,label=\"%s\\n%s\"]\n" id role role_attr;
print_edge2 file false edge_label edge_head "" upper id;
print_graph2_rec file false "" "" id t
| RevRelation(role,role_attr,t) ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=circle,label=\"%s\\n%s\"]\n" id role role_attr;
print_edge2 file true edge_label edge_head "" upper id;
print_graph2_rec file true "" "" id t
| SingleRelation(role) ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=circle,label=\"%s\"]\n" id role;
print_edge2 file false edge_label edge_head "" upper id
| AddRelation(t,role,role_attr,s) ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=circle,label=\"AddRelation\\n%s\\n%s\"]\n" id role role_attr;
print_edge2 file edge_rev edge_label edge_head "" upper id;
print_graph2_rec file false "" "" id t;
print_graph2_rec file false "" "" id s
| RemoveRelation t ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=circle,label=\"RemoveRelation\"]\n" id;
print_edge2 file edge_rev edge_label edge_head "" upper id;
print_graph2_rec file false "" "" id t
| SetContextName(s,t) ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=circle,label=\"SetContextName\\n%s\"]\n" id s;
print_edge2 file edge_rev edge_label edge_head "" upper id;
print_graph2_rec file false "" "" id t;
| Tuple l -> Xlist.iter l (print_graph2_rec file edge_rev edge_label edge_head upper)
| Variant(e,l) ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=diamond,label=\"%s\"]\n" id e;
print_edge2 file edge_rev edge_label edge_head "" upper id;
Xlist.iter l (fun (i,t) -> print_graph2_rec file edge_rev i "" id t)
| Val s ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=box,label=\"%s\"]\n" id s;
print_edge2 file edge_rev edge_label edge_head "" upper id
| Ref i -> print_edge2 file edge_rev edge_label edge_head "" upper i
| Dot -> ()
(* | t -> failwith ("print_graph_rec: " ^ ENIAMsemStringOf.linear_term 0 t) *)
let print_semantic_graph path name query t =
(* print_endline *)
id_counter := 1;
File.file_out (path ^ name ^ ".gv") (fun file ->
fprintf file "digraph G {\n compound=true\n node [shape=record]\n";
Int.iter 0 (Array.length t - 1) (fun i -> print_graph2_rec file false "" "" i t.(i));
fprintf file "label=\"%s\"\n }\n" query);
Sys.chdir path;
ignore (Sys.command ("dot -Tpng " ^ name ^ ".gv -o " ^ name ^ ".png"));
ignore (Sys.command ("rm " ^ name ^ ".gv"));
String.iter (function '/' -> Sys.chdir ".." | _ -> ()) path*)
let rec print_graph2_rec file edge_rev edge_label edge_style edge_head upper = function
Node t ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [label=\"%s\"]\n" id (string_of_node t);
print_edge2 file edge_rev edge_label edge_style edge_head "" upper id;
print_graph2_rec file false "" "" "" id t.args
| Concept t ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=box,label=<%s%s %s>]\n" id
(string_of_quant t.c_quant)
(escape_string (ENIAMsemStringOf.linear_term 0 t.c_sense))
(if t.c_name=Dot then "" else "„" ^ ENIAMsemStringOf.linear_term 0 t.c_name ^ "”"); (* FIXME *)
print_edge2 file edge_rev edge_label edge_style edge_head "" upper id;
print_graph2_rec file false "" "" "" id t.c_relations
| Context t ->
let id = !id_counter in
incr id_counter;
if t.cx_sense = Dot then fprintf file " subgraph cluster%d {\nlabel=\"\"\n" id
else fprintf file " subgraph cluster%d {\nlabel=\"%s\"\n" id (ENIAMsemStringOf.linear_term 0 t.cx_sense);
print_graph2_rec file false "" "" "" 0 t.cx_contents;
fprintf file " }\n";
print_edge2 file edge_rev edge_label edge_style edge_head ("cluster" ^ string_of_int id) upper (id+1);
print_graph2_rec file false "" "" ("cluster" ^ string_of_int id) (id+1) t.cx_relations;
| Relation(role,role_attr,t) ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=circle,label=\"%s\\n%s\"]\n" id role role_attr;
print_edge2 file false edge_label edge_style edge_head "" upper id;
print_graph2_rec file false "" "" "" id t
| RevRelation(role,role_attr,t) ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=circle,label=\"%s\\n%s\"]\n" id role role_attr;
print_edge2 file true edge_label edge_style edge_head "" upper id;
print_graph2_rec file true "" "" "" id t
| SingleRelation(role) ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=circle,label=\"%s\"]\n" id (ENIAMsemStringOf.linear_term 0 role);
print_edge2 file false edge_label edge_style edge_head "" upper id
| TripleRelation(role,role_attr,s,t) ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=circle,label=\"%s\\n%s\"]\n" id role role_attr;
print_edge2 file false edge_label edge_style edge_head "" upper id;
print_graph2_rec file false "" "dotted" "" id s;
print_graph2_rec file false "" "" "" id t
| AddRelation(t,role,role_attr,s) ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=circle,label=\"AddRelation\\n%s\\n%s\"]\n" id role role_attr;
print_edge2 file edge_rev edge_label edge_style edge_head "" upper id;
print_graph2_rec file false "" "" "" id t;
print_graph2_rec file false "" "" "" id s
| RemoveRelation t ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=circle,label=\"RemoveRelation\"]\n" id;
print_edge2 file edge_rev edge_label edge_style edge_head "" upper id;
print_graph2_rec file false "" "" "" id t
| SetContextName(s,t) ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=circle,label=\"SetContextName\\n%s\"]\n" id (ENIAMsemStringOf.linear_term 0 s);
print_edge2 file edge_rev edge_label edge_style edge_head "" upper id;
print_graph2_rec file false "" "" "" id t;
| MakeTripleRelation(role,role_attr,t) ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=circle,label=\"MakeTripleRelation\\n%s\\n%s\"]\n" id role role_attr;
print_edge2 file false edge_label edge_style edge_head "" upper id;
print_graph2_rec file false "" "" "" id t
| Tuple l -> Xlist.iter l (print_graph2_rec file edge_rev edge_label edge_style edge_head upper)
| Variant(e,l) ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=diamond,label=\"%s\"]\n" id e;
print_edge2 file edge_rev edge_label edge_style edge_head "" upper id;
Xlist.iter l (fun (i,t) -> print_graph2_rec file edge_rev i "" "" id t)
| Val s ->
let id = !id_counter in
incr id_counter;
fprintf file " %d [shape=box,label=\"%s\"]\n" id s;
print_edge2 file edge_rev edge_label edge_style edge_head "" upper id
| Dot -> ()
| t -> failwith ("print_graph_rec: " ^ ENIAMsemStringOf.linear_term 0 t)
let print_semantic_graph2 path name query t =
(* print_endline *)
id_counter := 1;
File.file_out (path ^ name ^ ".gv") (fun file ->
fprintf file "digraph G {\n compound=true\n node [shape=record]\n";
print_graph2_rec file false "" "" "" 0 t;
fprintf file "label=\"%s\"\n }\n" query);
Sys.chdir path;
ignore (Sys.command ("dot -Tpng " ^ name ^ ".gv -o " ^ name ^ ".png"));
(* ignore (Sys.command ("rm " ^ name ^ ".gv")); *)
String.iter (function '/' -> Sys.chdir ".." | _ -> ()) path