Commit bc40a6604e9ae2851a25c641d3ffab346f1de664

Authored by Wojciech Jaworski
1 parent 56fac264

wizualizacja parsowania drzew zależnościowych część I

parser/LCGchart.ml
... ... @@ -79,6 +79,7 @@ let lazify graph =
79 79 n+1)
80 80  
81 81 let rec dep_lazify_rec references next_reference (DepNode(id,left,l,right)) =
  82 + Printf.printf "dep_lazify_rec %s\n" id;
82 83 let l,references,next_reference = Xlist.fold l ([],references,next_reference) (fun (l,references,next_reference) (symbol,sem) ->
83 84 (symbol,Ref next_reference) :: l,
84 85 (next_reference,sem) :: references,
... ... @@ -86,7 +87,7 @@ let rec dep_lazify_rec references next_reference (DepNode(id,left,l,right)) =
86 87 let left,references,next_reference = Xlist.fold left ([],references,next_reference) (fun (left,references,next_reference) t ->
87 88 let t,references,next_reference = dep_lazify_rec references next_reference t in
88 89 t :: left,references,next_reference) in
89   - let right,references,next_reference = Xlist.fold left ([],references,next_reference) (fun (right,references,next_reference) t ->
  90 + let right,references,next_reference = Xlist.fold right ([],references,next_reference) (fun (right,references,next_reference) t ->
90 91 let t,references,next_reference = dep_lazify_rec references next_reference t in
91 92 t :: right,references,next_reference) in
92 93 DepNode(id,List.rev left,l,List.rev right),references,next_reference
... ...
parser/LCGlatexOf.ml
... ... @@ -16,16 +16,16 @@
16 16 * You should have received a copy of the GNU General Public License
17 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18 *)
19   -
  19 +
20 20 open LCGtypes
21 21 open Xstd
22 22 open Printf
23   -
  23 +
24 24 let type_term s = "\\text{" ^ s ^ "}"
25 25  
26 26 let rec first_digit_index v n i =
27 27 if i = n then n else
28   - if String.get v i >= '0' && String.get v i <= '9' then i
  28 + if String.get v i >= '0' && String.get v i <= '9' then i
29 29 else first_digit_index v n (i+1)
30 30  
31 31 let variable v =
... ... @@ -37,7 +37,7 @@ let variable v =
37 37 let pred_name = function
38 38 "name_token" -> "name\\_token"
39 39 | s -> s
40   -
  40 +
41 41 let rec linear_term c = function
42 42 Var v -> variable v
43 43 | Tuple l ->
... ... @@ -45,7 +45,7 @@ let rec linear_term c = function
45 45 let n = Xlist.fold l 0 (fun n s -> String.length s + n) in
46 46 let s =
47 47 if n < 100 then String.concat "\\otimes " l else
48   - let s = String.concat "\\otimes\\\\ {}" l in
  48 + let s = String.concat "\\otimes\\\\ {}" l in
49 49 "\\begin{array}{l}" ^ s ^ "\\end{array}" in
50 50 if c > 1 then "(" ^ s ^ ")" else s
51 51 (* | LetIn(l,s,t) -> "{\\bf let } " ^ String.concat "\\otimes " (Xlist.map l variable) ^ " = " ^ (linear_term 0 s) ^ " \\text{ {\\bf in }} " ^ (linear_term 0 t) *)
... ... @@ -56,7 +56,7 @@ let rec linear_term c = function
56 56 | SubstVar v -> variable v
57 57 | Subst(s,v,t) -> "{\\bf subst}(" ^ (linear_term 0 s) ^ "," ^ variable v ^ "," ^ (linear_term 0 t) ^ ")"
58 58 | Inj(n,t) -> "{\\bf inj}_{" ^ (string_of_int n) ^ "}" ^ (linear_term c t)
59   - | Case(t,l) -> "{\\bf case}\\; " ^ (linear_term 0 t) ^ " \\;{\\bf of}\\; " ^
  59 + | Case(t,l) -> "{\\bf case}\\; " ^ (linear_term 0 t) ^ " \\;{\\bf of}\\; " ^
60 60 (String.concat " | " (Xlist.map l (fun (v,t) -> variable v ^ " \\to " ^ (linear_term 0 t))))
61 61 | Lambda(v,t) -> "\\lambda " ^ variable v ^ "." ^ (linear_term c t)
62 62 | LambdaSet(l,t) -> "\\lambda " ^ (String.concat "," (Xlist.map l variable)) ^ "." ^ (linear_term c t)
... ... @@ -69,28 +69,28 @@ let rec linear_term c = function
69 69 | Empty t -> "{\\bf empty}(" ^ linear_term 0 t ^ ")"
70 70 | Apply t -> "{\\bf apply}(" ^ linear_term 0 t ^ ")"
71 71 | Insert(s,t) -> "{\\bf insert}(" ^ linear_term 0 s ^ "," ^ linear_term 0 t ^ ")"
72   - | Node t ->
73   - "{\\left[\\begin{array}{ll}" ^
74   - (String.concat "\\\\ " (Xlist.map (["PRED",Val t.pred;"CAT",Val t.cat;"ID",Val (string_of_int t.id);"WEIGHT",Val (string_of_float t.weight);"GS",t.gs;"ARGS",t.args] @ t.attrs) (fun (e,t) ->
  72 + | Node t ->
  73 + "{\\left[\\begin{array}{ll}" ^
  74 + (String.concat "\\\\ " (Xlist.map (["PRED",Val t.pred;"CAT",Val t.cat;"ID",Val (string_of_int t.id);"WEIGHT",Val (string_of_float t.weight);"GS",t.gs;"ARGS",t.args] @ t.attrs) (fun (e,t) ->
75 75 "\\text{" ^ (LatexMain.escape_string e) ^ "} & " ^ (linear_term 0 t)))) ^ "\\end{array}\\right]}"
76 76 | Morf m -> "\\text{" ^ LatexMain.escape_string (WalStringOf.morf m) ^ "}"
77 77 | Gf s -> "\\text{" ^ LatexMain.escape_string (WalStringOf.gf s) ^ "}"
78 78 | Ref i -> "{\\bf ref}\\; " ^ string_of_int i
79 79 | Cut t -> "{\\bf cut}(" ^ linear_term 0 t ^ ")"
80 80 | Choice choices -> "{\\bf choice}(" ^ String.concat ";" (StringMap.fold choices [] (fun l ei t -> (sprintf "%s: %s" ei (linear_term 0 t)) :: l)) ^ ")"
81   - | Concept c ->
82   - "{\\left[\\begin{array}{ll}" ^
  81 + | Concept c ->
  82 + "{\\left[\\begin{array}{ll}" ^
83 83 (String.concat "\\\\ " (Xlist.map ([
84 84 "SENSE",c.c_sense;"NAME",c.c_name;
85 85 "VARIABLE",Val (fst c.c_variable ^ "_" ^ snd c.c_variable);"POS",Val (string_of_int c.c_pos);
86   - "QUANT",c.c_quant;"LOCAL-QUANT",if c.c_local_quant then Val "+" else Val "-";"RELATIONS",c.c_relations]) (fun (e,t) ->
  86 + "QUANT",c.c_quant;"LOCAL-QUANT",if c.c_local_quant then Val "+" else Val "-";"RELATIONS",c.c_relations]) (fun (e,t) ->
87 87 "\\text{" ^ (LatexMain.escape_string e) ^ "} & " ^ (linear_term 0 t)))) ^ "\\end{array}\\right]}"
88   - | Context c ->
89   - "{\\left[\\begin{array}{ll}" ^
  88 + | Context c ->
  89 + "{\\left[\\begin{array}{ll}" ^
90 90 (String.concat "\\\\ " (Xlist.map ([
91 91 "SENSE",c.cx_sense;
92 92 "VARIABLE",Val (fst c.cx_variable ^ "_" ^ snd c.cx_variable);"POS",Val (string_of_int c.cx_pos);
93   - "RELATIONS",c.cx_relations;"CONTENTS",c.cx_contents]) (fun (e,t) ->
  93 + "RELATIONS",c.cx_relations;"CONTENTS",c.cx_contents]) (fun (e,t) ->
94 94 "\\text{" ^ (LatexMain.escape_string e) ^ "} & " ^ (linear_term 0 t)))) ^ "\\end{array}\\right]}"
95 95 | Relation(r,a,c) -> "{\\bf relation}(" ^ linear_term 0 r ^ "," ^ linear_term 0 a ^ "," ^ linear_term 0 c ^ ")"
96 96 | RevRelation(r,a,c) -> "{\\bf revrelation}(" ^ linear_term 0 r ^ "," ^ linear_term 0 a ^ "," ^ linear_term 0 c ^ ")"
... ... @@ -101,8 +101,8 @@ let rec linear_term c = function
101 101  
102 102 let rec linear_term_simple c = function
103 103 Var v -> variable v
104   - | Tuple l ->
105   - let s = String.concat "\\otimes " (Xlist.map l (linear_term_simple 2)) in
  104 + | Tuple l ->
  105 + let s = String.concat "\\otimes " (Xlist.map l (linear_term_simple 2)) in
106 106 if c > 1 then "(" ^ s ^ ")" else s
107 107 (* | LetIn(l,s,t) -> "\\;$\\\\\n${\\bf let } " ^ String.concat "\\otimes " (Xlist.map l variable) ^ " = " ^ (linear_term_simple 0 s) ^ " \\text{ {\\bf in }} " ^ (linear_term_simple 0 t) *)
108 108 (* | Triple(t1,t2,t3) -> "\\{" ^ linear_term_simple 0 t1 ^ "," ^ linear_term_simple 0 t2 ^ "," ^ linear_term_simple 0 t3 ^ "\\}" *)
... ... @@ -113,7 +113,7 @@ let rec linear_term_simple c = function
113 113 | SubstVar v -> variable v
114 114 | Subst(s,v,t) -> "{\\bf subst}(" ^ (linear_term_simple 0 s) ^ "," ^ variable v ^ "," ^ (linear_term_simple 0 t) ^ ")"
115 115 | Inj(n,t) -> "{\\bf inj}_" ^ (string_of_int n) ^ (linear_term_simple c t)
116   - | Case(t,l) -> "{\\bf case}\\; " ^ (linear_term_simple 0 t) ^ " \\;{\\bf of}\\; " ^
  116 + | Case(t,l) -> "{\\bf case}\\; " ^ (linear_term_simple 0 t) ^ " \\;{\\bf of}\\; " ^
117 117 (String.concat " | " (Xlist.map l (fun (v,t) -> variable v ^ " \\to " ^ (linear_term_simple 0 t))))
118 118 | Lambda(v,t) -> "\\lambda " ^ variable v ^ "." ^ (linear_term_simple c t)
119 119 | LambdaSet(l,t) -> "\\lambda " ^ (String.concat "," (Xlist.map l variable)) ^ "." ^ (linear_term_simple c t)
... ... @@ -132,19 +132,19 @@ let rec linear_term_simple c = function
132 132 | Ref i -> "{\\bf ref}\\; " ^ string_of_int i
133 133 | Cut t -> "{\\bf cut}(" ^ linear_term_simple 0 t ^ ")"
134 134 | Choice choices -> "{\\bf choice}(" ^ String.concat ";" (StringMap.fold choices [] (fun l ei t -> (sprintf "%s: %s" ei (linear_term_simple 0 t)) :: l)) ^ ")"
135   - | Concept c ->
136   - "{\\left[\\begin{array}{ll}" ^
  135 + | Concept c ->
  136 + "{\\left[\\begin{array}{ll}" ^
137 137 (String.concat "\\\\ " (Xlist.map ([
138 138 "SENSE",c.c_sense;"NAME",c.c_name;
139 139 "VARIABLE",Val (fst c.c_variable ^ "_" ^ snd c.c_variable);"POS",Val (string_of_int c.c_pos);
140   - "QUANT",c.c_quant;"LOCAL-QUANT",if c.c_local_quant then Val "+" else Val "-";"RELATIONS",c.c_relations]) (fun (e,t) ->
  140 + "QUANT",c.c_quant;"LOCAL-QUANT",if c.c_local_quant then Val "+" else Val "-";"RELATIONS",c.c_relations]) (fun (e,t) ->
141 141 "\\text{" ^ (LatexMain.escape_string e) ^ "} & " ^ (linear_term_simple 0 t)))) ^ "\\end{array}\\right]}"
142   - | Context c ->
143   - "{\\left[\\begin{array}{ll}" ^
  142 + | Context c ->
  143 + "{\\left[\\begin{array}{ll}" ^
144 144 (String.concat "\\\\ " (Xlist.map ([
145 145 "SENSE",c.cx_sense;
146 146 "VARIABLE",Val (fst c.cx_variable ^ "_" ^ snd c.cx_variable);"POS",Val (string_of_int c.cx_pos);
147   - "RELATIONS",c.cx_relations;"CONTENTS",c.cx_contents]) (fun (e,t) ->
  147 + "RELATIONS",c.cx_relations;"CONTENTS",c.cx_contents]) (fun (e,t) ->
148 148 "\\text{" ^ (LatexMain.escape_string e) ^ "} & " ^ (linear_term_simple 0 t)))) ^ "\\end{array}\\right]}"
149 149 | Relation(r,a,c) -> "{\\bf relation}(" ^ linear_term_simple 0 r ^ "," ^ linear_term_simple 0 a ^ "," ^ linear_term_simple 0 c ^ ")"
150 150 | RevRelation(r,a,c) -> "{\\bf revrelation}(" ^ linear_term_simple 0 r ^ "," ^ linear_term_simple 0 a ^ "," ^ linear_term_simple 0 c ^ ")"
... ... @@ -169,35 +169,35 @@ let atom = function
169 169 | "p2" -> "\\text{p}_2"
170 170 | "p3" -> "\\text{p}_3"
171 171 | s -> "\\text{" ^ LatexMain.escape_string s ^ "}"
172   -
  172 +
173 173 let rec internal_grammar_symbol c = function
174 174 Atom x -> atom x
175 175 | AVar x -> " " ^ x
176   - | With l ->
177   - let s = String.concat "\\with" (Xlist.map l (internal_grammar_symbol 2)) in
  176 + | With l ->
  177 + let s = String.concat "\\with" (Xlist.map l (internal_grammar_symbol 2)) in
178 178 if c > 1 then "(" ^ s ^ ")" else s
179 179 | Zero -> "0"
180 180 | Top -> "\\top"
181 181  
182 182 let rec grammar_symbol c = function
183   - Tensor l ->
184   - let s = String.concat "\\otimes" (Xlist.map l (internal_grammar_symbol 2)) in
  183 + Tensor l ->
  184 + let s = String.concat "\\otimes" (Xlist.map l (internal_grammar_symbol 2)) in
185 185 if c > 1 then "(" ^ s ^ ")" else s
186   - | Plus l ->
187   - let s = String.concat "\\oplus" (Xlist.map l (grammar_symbol 2)) in
  186 + | Plus l ->
  187 + let s = String.concat "\\oplus" (Xlist.map l (grammar_symbol 2)) in
188 188 if c > 1 then "(" ^ s ^ ")" else s
189 189 | Imp(s,d,t) -> "(" ^ (grammar_symbol 2 s) ^ "\\\\ \\hspace{1cm}" ^ direction d ^ (grammar_symbol 2 t) ^ ")"
190 190 | One -> "1"
191   - | ImpSet(s,l) ->
192   - let s = (grammar_symbol 1 s) ^ "\\{" ^ String.concat "\n," (Xlist.map l (fun (d,a) -> "\\\\ \\hspace{1cm}" ^ direction d ^ grammar_symbol 1 a)) ^ "\\}" in
  191 + | ImpSet(s,l) ->
  192 + let s = (grammar_symbol 1 s) ^ "\\{" ^ String.concat "\n," (Xlist.map l (fun (d,a) -> "\\\\ \\hspace{1cm}" ^ direction d ^ grammar_symbol 1 a)) ^ "\\}" in
193 193 if c > 0 then "(" ^ s ^ ")" else s
194 194 | WithVar(v,s,e,t) -> "\\bigwith_{" ^ e ^ ":" ^ v ^ ":=" ^ (internal_grammar_symbol 2 s) ^ "} " ^ (grammar_symbol 2 t)
195 195 | Star s -> grammar_symbol 2 s ^ "^\\star"
196   - | Bracket(lf,rf,s) -> "\\langle " ^ (if lf then "\\langle " else "") ^ (grammar_symbol 0 s) ^ "\\rangle" ^ (if rf then "\\rangle " else "")
  196 + | Bracket(lf,rf,s) -> "\\langle " ^ (if lf then "\\langle " else "") ^ (grammar_symbol 0 s) ^ "\\rangle" ^ (if rf then "\\rangle " else "")
197 197 | BracketSet d -> "{\\bf BracketSet}(" ^ direction d ^ ")"
198 198 | Maybe s -> "?" ^ grammar_symbol 2 s
199 199  
200   -let graph g =
  200 +let graph g =
201 201 let layers = LCGchart.fold g IntMap.empty (fun layers (symbol,node1,node2,sem,layer) ->
202 202 let nodes = try IntMap.find layers layer with Not_found -> IntMap.empty in
203 203 let content = node2, grammar_symbol 0 symbol, linear_term 0 sem in
... ... @@ -206,16 +206,36 @@ let graph g =
206 206 IntMap.add layers layer nodes) in
207 207 "\\begin{longtable}{|l|l|l|p{20cm}|}\n\\hline\n" ^
208 208 String.concat "" (List.rev (IntMap.fold layers [] (fun l layer nodes ->
209   - IntMap.fold nodes l (fun l node1 contents ->
210   - Xlist.fold contents l (fun l (node2,symbol,sem) ->
  209 + IntMap.fold nodes l (fun l node1 contents ->
  210 + Xlist.fold contents l (fun l (node2,symbol,sem) ->
211 211 (Printf.sprintf "%d & %d--%d & $\\begin{array}{l}%s\\end{array}$ & $%s$\\\\\n\\hline\n" layer node1 node2 symbol sem) :: l))))) ^
212 212 "\\end{longtable}"
213   -
  213 +
  214 +let dep_graph_parsed l =
  215 + if l = [] then "empty" else
  216 + "\\begin{longtable}{|l|p{20cm}|}\n\\hline\n" ^
  217 + String.concat "" (Xlist.rev_map l (fun (symbol,sem) ->
  218 + let symbol = grammar_symbol 0 symbol in
  219 + let sem = linear_term 0 sem in
  220 + Printf.sprintf "$\\begin{array}{l}%s\\end{array}$ & $%s$\\\\\n\\hline\n" symbol sem)) ^
  221 + "\\end{longtable}"
  222 +
  223 +let rec dep_graph_rec (DepNode(id,left,l,right)) =
  224 + printf "dep_graph_rec: %s\n" id;
  225 + String.concat "" (Xlist.map left dep_graph_rec) ^
  226 + String.concat "" (Xlist.rev_map l (fun (symbol,sem) ->
  227 + let symbol = grammar_symbol 0 symbol in
  228 + let sem = linear_term 0 sem in
  229 + Printf.sprintf "%s & $\\begin{array}{l}%s\\end{array}$ & $%s$\\\\\n\\hline\n" id symbol sem)) ^
  230 + String.concat "" (Xlist.map right dep_graph_rec)
  231 +
  232 +let dep_graph graph =
  233 + "\\begin{longtable}{|l|l|p{20cm}|}\n\\hline\n" ^
  234 + dep_graph_rec graph ^
  235 + "\\end{longtable}"
  236 +
214 237 let print_references name references =
215 238 LatexMain.latex_file_out "results/" name "a0" false (fun file ->
216 239 Int.iter 0 (Array.length references - 1) (fun i ->
217 240 if references.(i) <> Dot then Printf.fprintf file "{\\bf %d} $%s$\\\\\n" i (linear_term 0 references.(i))));
218   - LatexMain.latex_compile_and_clean "results/" name
219   -
220   -
221   -
222 241 \ No newline at end of file
  242 + LatexMain.latex_compile_and_clean "results/" name
... ...
parser/LCGlexicon.ml
... ... @@ -1274,6 +1274,7 @@ let rec dep_create_rec nodes sons id =
1274 1274 let node = if id = "0" then [] else StringMap.find nodes id in
1275 1275 let l = try StringMap.find sons id with Not_found -> [] in
1276 1276 let left,right = split_sons [] id [] l in
  1277 + Printf.printf "dep_create_rec [%s] %s [%s]\n" (String.concat ";" left) id (String.concat ";" right);
1277 1278 DepNode(id, Xlist.map left (dep_create_rec nodes sons), node, Xlist.map right (dep_create_rec nodes sons))
1278 1279  
1279 1280 let dep_create paths next_id =
... ...
parser/pipe.ml
... ... @@ -177,8 +177,8 @@ let process_conll_corpus filename =
177 177 ()
178 178  
179 179 let _ =
180   - (* process_conll_corpus "../../NLP resources/Skladnica-zaleznosciowa-mod_130121.conll"; *)
181   - process_conll_corpus "../testy/skladnica-proste-zdania.conll";
  180 + process_conll_corpus "../../NLP resources/Skladnica-zaleznosciowa-mod_130121.conll";
  181 + (* process_conll_corpus "../testy/skladnica-proste-zdania.conll"; *)
182 182 ()
183 183  
184 184 (* TO DO:
... ...
parser/visualization.ml
... ... @@ -704,6 +704,16 @@ let create_latex_graph path name graph =
704 704 Printf.fprintf file "%s\n" (LCGlatexOf.graph graph));
705 705 LatexMain.latex_compile_and_clean path name
706 706  
  707 +let create_latex_dep_graph_parsed path name graph =
  708 + LatexMain.latex_file_out path name "a1" false (fun file ->
  709 + Printf.fprintf file "%s\n" (LCGlatexOf.dep_graph_parsed graph));
  710 + LatexMain.latex_compile_and_clean path name
  711 +
  712 +let create_latex_dep_graph path name graph =
  713 + LatexMain.latex_file_out path name "a1" false (fun file ->
  714 + Printf.fprintf file "%s\n" (LCGlatexOf.dep_graph graph));
  715 + LatexMain.latex_compile_and_clean path name
  716 +
707 717 let html_of_eniam_sentence (result : eniam_parse_result) =
708 718 match result.status with
709 719 Idle -> "idle\n"
... ... @@ -742,7 +752,8 @@ let html_of_conll_sentence (result : conll_parse_result) =
742 752 (* create_latex_graph "results/" "chart" result.graph; *)
743 753 sprintf "timeout: %s\n" result.msg
744 754 | NotParsed ->
745   - (* create_latex_graph "results/" "chart" result.graph; *)
  755 + create_latex_dep_graph "results/" "dep_chart" result.dep_graph;
  756 + create_latex_dep_graph_parsed "results/" "dep_chart_parsed" result.dep_graph_parsed;
746 757 sprintf "not_parsed\n"
747 758 | ReductionError -> sprintf "error_reduction: %s\n" result.msg
748 759 | TooManyNodes -> sprintf "to_many_nodes\n"
... ... @@ -754,7 +765,7 @@ let html_of_conll_sentence (result : conll_parse_result) =
754 765 print_graph "results/" "term" result.term;
755 766 LCGlatexOf.print_references "references" result.term; *)
756 767 sprintf "parsed\n"
757   - | _ -> failwith "html_of_eniam_sentence"
  768 + | _ -> failwith "html_of_conll_sentence"
758 769  
759 770  
760 771 let rec html_of_sentence = function
... ...