libgrew.ml 16 KB
Newer Older
bguillaum's avatar
bguillaum committed
1 2 3 4 5 6 7 8 9 10
(**********************************************************************************)
(*    Libcaml-grew - a Graph Rewriting library dedicated to NLP applications      *)
(*                                                                                *)
(*    Copyright 2011-2013 Inria, Université de Lorraine                           *)
(*                                                                                *)
(*    Webpage: http://grew.loria.fr                                               *)
(*    License: CeCILL (see LICENSE folder or "http://www.cecill.info")            *)
(*    Authors: see AUTHORS file                                                   *)
(**********************************************************************************)

bguillaum's avatar
bguillaum committed
11
open Printf
pj2m's avatar
pj2m committed
12
open Log
13
open Conll
pj2m's avatar
pj2m committed
14

bguillaum's avatar
bguillaum committed
15
let libgrew_debug_mode () = Grew_base.Global.debug := true
bguillaum's avatar
bguillaum committed
16
let get_version () = VERSION
bguillaum's avatar
bguillaum committed
17

bguillaum's avatar
bguillaum committed
18
(* ==================================================================================================== *)
bguillaum's avatar
bguillaum committed
19
(** {2 Location} *)
bguillaum's avatar
bguillaum committed
20 21 22 23 24 25 26
(* ==================================================================================================== *)
module Loc = struct
  type t = Grew_base.Loc.t
  let to_string = Grew_base.Loc.to_string
end

(* ==================================================================================================== *)
bguillaum's avatar
bguillaum committed
27
(** {2 Exceptions} *)
bguillaum's avatar
bguillaum committed
28
(* ==================================================================================================== *)
29 30
exception Error of string
exception Bug of string
pj2m's avatar
pj2m committed
31

32 33
let handle ?(name="") ?(file="No file defined") fct () =
  try fct () with
bguillaum's avatar
typo  
bguillaum committed
34
    (* Raise again already caught exceptions *)
35 36
    | Error msg -> raise (Error msg)
    | Bug msg -> raise (Bug msg)
37 38

    (* Catch new exceptions *)
39 40 41 42 43 44
    | Grew_base.Error.Parse (msg, Some loc) -> raise (Error (sprintf "%s %s" (Grew_base.Loc.to_string loc) msg))
    | Grew_base.Error.Parse (msg, None) -> raise (Error (sprintf "%s" msg))
    | Grew_base.Error.Build (msg, Some loc) -> raise (Error (sprintf "%s %s" (Grew_base.Loc.to_string loc) msg))
    | Grew_base.Error.Build (msg, None) -> raise (Error (sprintf "%s" msg))
    | Grew_base.Error.Run (msg, Some loc) -> raise (Error (sprintf "%s %s" (Grew_base.Loc.to_string loc) msg))
    | Grew_base.Error.Run (msg, None) -> raise (Error (sprintf "%s" msg))
Bruno Guillaume's avatar
Bruno Guillaume committed
45
    | Conll_types.Error msg -> raise (Error (sprintf "Conll error: %s" (Yojson.Basic.to_string msg)))
46

47
    | Grew_base.Error.Bug (msg, Some loc) -> raise (Bug (sprintf "%s %s" (Grew_base.Loc.to_string loc) msg))
48 49
    | Grew_base.Error.Bug (msg, None) -> raise (Bug (sprintf "%s" msg))
    | exc -> raise (Bug (sprintf "[Libgrew.%s] UNCAUGHT EXCEPTION: %s" name (Printexc.to_string exc)))
pj2m's avatar
pj2m committed
50

bguillaum's avatar
bguillaum committed
51

bguillaum's avatar
bguillaum committed
52
(* ==================================================================================================== *)
53
(** {2 Domain} *)
bguillaum's avatar
bguillaum committed
54 55
(* ==================================================================================================== *)
module Domain = struct
56
  type t = Grew_domain.Domain.t
57

58
  let load filename =
59
    handle ~name:"Domain.load"
60 61
      (fun () ->
      let ast = Grew_loader.Loader.domain filename in
62
      Grew_grs.Old_grs.domain_build ast
63 64 65
      ) ()

  let feature_names domain =
66
    handle ~name:"Domain.feature_names"
67 68
      (fun () -> Grew_domain.Domain.feature_names domain)
      ()
69 70 71 72 73 74

  let dump domain =
    handle ~name:"Domain.dump"
      (fun () -> Grew_domain.Domain.dump domain)
      ()

bguillaum's avatar
bguillaum committed
75 76 77 78 79 80 81 82
end

(* ==================================================================================================== *)
(** {2 Patterns} *)
(* ==================================================================================================== *)
module Pattern = struct
  type t = Grew_rule.Rule.pattern

bguillaum's avatar
bguillaum committed
83 84
  let load ?domain file =
  handle ~name:"Pattern.load" (fun () -> Grew_rule.Rule.build_pattern ?domain (Grew_loader.Loader.pattern file)) ()
85 86 87

  let parse ?domain desc =
  handle ~name:"Pattern.load" (fun () -> Grew_rule.Rule.build_pattern ?domain (Grew_loader.Parser.pattern desc)) ()
88

89
  let pid_name_list pattern =
90
  handle ~name:"Pattern.pid_lits"
91
    (fun () -> List.map (fun x -> x) (Grew_rule.Rule.pid_name_list pattern)
92
    ) ()
bguillaum's avatar
bguillaum committed
93 94 95 96 97 98 99
end

(* ==================================================================================================== *)
(** {2 Matching} *)
(* ==================================================================================================== *)
module Matching = struct
  type t = Grew_rule.Rule.matching
100 101

  let to_python pattern graph t = Grew_rule.Rule.to_python pattern graph t
bguillaum's avatar
bguillaum committed
102 103 104 105 106 107 108 109 110 111 112
end

(* ==================================================================================================== *)
(** {2 Deco} *)
(* ==================================================================================================== *)
module Deco = struct
  type t = Grew_graph.G_deco.t
  let build pattern matching = Grew_rule.Rule.match_deco pattern matching
end

(* ==================================================================================================== *)
bguillaum's avatar
bguillaum committed
113
(** {2 Graph} *)
bguillaum's avatar
bguillaum committed
114 115 116 117 118 119
(* ==================================================================================================== *)
module Graph = struct


type t = Grew_graph.G_graph.t

bguillaum's avatar
bguillaum committed
120
  let load_gr ?domain file =
bguillaum's avatar
bguillaum committed
121
    if not (Sys.file_exists file)
122
    then raise (Error ("File_not_found: " ^ file))
bguillaum's avatar
bguillaum committed
123 124 125 126
    else
      handle ~name:"Graph.load_gr" ~file
        (fun () ->
          let gr_ast = Grew_loader.Loader.gr file in
bguillaum's avatar
bguillaum committed
127
          Grew_graph.G_graph.build ?domain gr_ast
bguillaum's avatar
bguillaum committed
128
        ) ()
bguillaum's avatar
bguillaum committed
129

bguillaum's avatar
bguillaum committed
130
  let load_conll ?domain file =
bguillaum's avatar
bguillaum committed
131
    handle ~name:"Graph.load_conll" ~file
132
      (fun () ->
bguillaum's avatar
bguillaum committed
133
        Grew_graph.G_graph.of_conll ?domain (Conll.load file)
134
      ) ()
pj2m's avatar
pj2m committed
135

bguillaum's avatar
bguillaum committed
136
  let load_brown ?domain file =
bguillaum's avatar
bguillaum committed
137 138 139
    handle ~name:"Graph.load_brown"
      (fun () ->
        let brown = Grew_base.File.load file in
bguillaum's avatar
bguillaum committed
140
        Grew_graph.G_graph.of_brown ?domain brown
bguillaum's avatar
bguillaum committed
141
      ) ()
142

143
  let load_pst ?domain file =
144
    if not (Sys.file_exists file)
145
    then raise (Error ("File_not_found: " ^ file))
146
    else
147
      handle ~name:"load_pst" ~file
148
        (fun () ->
149 150
          let const_ast = Grew_loader.Loader.phrase_structure_tree file in
          Grew_graph.G_graph.of_pst ?domain const_ast
151 152
        ) ()

bguillaum's avatar
bguillaum committed
153
  let load ?domain file =
bguillaum's avatar
bguillaum committed
154 155 156
    handle ~name:"Graph.load_graph" ~file
      (fun () ->
        match Grew_base.File.get_suffix file with
bguillaum's avatar
bguillaum committed
157 158 159
        | Some ".gr" -> load_gr ?domain file
        | Some ".conll" -> load_conll ?domain file
        | Some ".br" | Some ".melt" -> load_brown ?domain file
160
        | Some ".cst" -> load_pst ?domain file
bguillaum's avatar
bguillaum committed
161 162 163 164
        | _ ->
            Log.fwarning "Unknown file format for input graph '%s', try to guess..." file;
            let rec loop = function
            | [] -> Log.fcritical "[Libgrew.load_graph] Cannot guess input file format of file '%s'. Use .gr or .conll file extension" file
bguillaum's avatar
bguillaum committed
165
            | load_fct :: tail -> try load_fct ?domain file with _ -> loop tail in
166
            loop [load_gr; load_conll; load_brown; load_pst]
bguillaum's avatar
bguillaum committed
167
      ) ()
bguillaum's avatar
bguillaum committed
168

bguillaum's avatar
bguillaum committed
169 170
  let of_gr ?domain ?(grewpy=false) gr_string =
    handle ~name:"Graph.of_gr" (fun () -> Grew_graph.G_graph.build ?domain ~grewpy (Grew_loader.Parser.gr gr_string)) ()
171

bguillaum's avatar
bguillaum committed
172 173
  let of_conll ?domain conll =
    handle ~name:"Graph.of_conll" (fun () -> Grew_graph.G_graph.of_conll ?domain conll) ()
174

175 176
  let of_pst ?domain pst_string =
    handle ~name:"of_pst"
177
      (fun () ->
178 179 180 181 182 183 184 185 186 187
        let pst_ast = Grew_loader.Parser.phrase_structure_tree pst_string in
        (Grew_graph.G_graph.of_pst ?domain pst_ast)
      ) ()

  let sentence_of_pst ?domain pst_string =
    handle ~name:"of_pst"
      (fun () ->
        let pst_ast = Grew_loader.Parser.phrase_structure_tree pst_string in
        let word_list = Grew_ast.Ast.word_list pst_ast in
        Sentence.fr_clean_spaces (String.concat " " word_list)
188 189
      ) ()

bguillaum's avatar
bguillaum committed
190 191
  let of_brown ?domain ?sentid brown =
    handle ~name:"Graph.of_brown" (fun () -> Grew_graph.G_graph.of_brown ?domain ?sentid brown) ()
bguillaum's avatar
bguillaum committed
192

bguillaum's avatar
bguillaum committed
193 194
  let to_dot ?domain ?main_feat ?(deco=Grew_graph.G_deco.empty) graph =
    handle ~name:"Graph.to_dot" (fun () -> Grew_graph.G_graph.to_dot ?domain ?main_feat graph ~deco) ()
bguillaum's avatar
bguillaum committed
195

bguillaum's avatar
bguillaum committed
196 197
  let to_dep ?domain ?filter ?main_feat ?(deco=Grew_graph.G_deco.empty) graph =
    handle ~name:"Graph.to_dep" (fun () -> Grew_graph.G_graph.to_dep ?domain ?filter ?main_feat ~deco graph) ()
bguillaum's avatar
bguillaum committed
198

bguillaum's avatar
bguillaum committed
199 200
  let to_gr ?domain graph =
    handle ~name:"Graph.to_gr" (fun () -> Grew_graph.G_graph.to_gr ?domain graph) ()
bguillaum's avatar
bguillaum committed
201

bguillaum's avatar
bguillaum committed
202 203
  let to_conll_string ?domain graph =
    handle ~name:"Graph.to_conll_string" (fun () -> Grew_graph.G_graph.to_conll_string ?domain graph) ()
bguillaum's avatar
bguillaum committed
204

bguillaum's avatar
bguillaum committed
205 206 207 208 209
  let to_sentence ?main_feat gr =
    handle ~name:"Graph.to_sentence"
      (fun () ->
        Grew_graph.G_graph.to_sentence ?main_feat gr
      ) ()
bguillaum's avatar
bguillaum committed
210

bguillaum's avatar
bguillaum committed
211
  let save_conll ?domain filename graph =
bguillaum's avatar
bguillaum committed
212 213
    handle ~name:"Graph.save_conll" (fun () ->
      let out_ch = open_out filename in
bguillaum's avatar
bguillaum committed
214
      fprintf out_ch "%s" (Grew_graph.G_graph.to_conll_string ?domain graph);
bguillaum's avatar
bguillaum committed
215
      close_out out_ch
bguillaum's avatar
bguillaum committed
216 217
    ) ()

218 219 220 221
  let search_pattern ?domain pattern graph =
    handle ~name:"Graph.search_pattern" (fun () ->
      Grew_rule.Rule.match_in_graph ?domain pattern graph
    ) ()
222

223 224 225 226
  let node_matching pattern graph matching =
    handle ~name:"Graph.node_matching" (fun () ->
      Grew_rule.Rule.node_matching pattern graph matching
    ) ()
bguillaum's avatar
bguillaum committed
227
end
228

bguillaum's avatar
bguillaum committed
229
(* ==================================================================================================== *)
230
(** {2 Graph Rewriting System} *)
bguillaum's avatar
bguillaum committed
231
(* ==================================================================================================== *)
232 233
module Old_grs = struct
  type t = Grew_grs.Old_grs.t
234

235
  let empty = Grew_grs.Old_grs.empty
236

bguillaum's avatar
bguillaum committed
237
  let load file =
238
    handle ~name:"Old_grs.load" ~file
bguillaum's avatar
bguillaum committed
239 240
      (fun () ->
        if not (Sys.file_exists file)
241
        then raise (Error ("File_not_found: " ^ file))
242
        else Grew_grs.Old_grs.build file
bguillaum's avatar
bguillaum committed
243
      ) ()
244

bguillaum's avatar
bguillaum committed
245
  let get_sequence_names grs =
246
    handle ~name:"Old_grs.get_sequence_names"
bguillaum's avatar
bguillaum committed
247
      (fun () ->
248
        Grew_grs.Old_grs.sequence_names grs
bguillaum's avatar
bguillaum committed
249
      ) ()
250

251 252
  let build_html_doc ?(corpus=false) dir (grs : Grew_grs.Old_grs.t) =
    handle ~name:"Old_grs.build_doc [with Dep2pict]"
bguillaum's avatar
bguillaum committed
253 254 255
      (fun () ->
        Grew_html.Html_doc.build ~corpus ~dep:true dir grs;

Bruno Guillaume's avatar
Bruno Guillaume committed
256
        (* draw pattern graphs for all rules *)
bguillaum's avatar
bguillaum committed
257
        let fct module_ rule_ =
258
          let dep_code = Grew_rule.Rule.to_dep ?domain:(Grew_grs.Old_grs.get_domain grs) rule_ in
bguillaum's avatar
bguillaum committed
259 260 261
          let dep_png_file = sprintf "%s/%s_%s-patt.png" dir module_ (Grew_rule.Rule.get_name rule_) in
          let d2p = Dep2pict.Dep2pict.from_dep ~dep:dep_code in
          Dep2pict.Dep2pict.save_png ~filename:dep_png_file d2p in
262
        Grew_grs.Old_grs.rule_iter fct grs
bguillaum's avatar
bguillaum committed
263 264
      ) ()

265
  let get_domain grs = Grew_grs.Old_grs.get_domain grs
266 267

  let to_json t =
268
    let json = Grew_grs.Old_grs.to_json t in
269
    Yojson.Basic.pretty_to_string json
bguillaum's avatar
bguillaum committed
270 271
end

272 273 274
(* ==================================================================================================== *)
(** {2 New Graph Rewriting System} *)
(* ==================================================================================================== *)
275 276
module Grs = struct
  type t = Grew_grs.Grs.t
277 278

  let load file =
279
    handle ~name:"Grs.load" ~file
280
      (fun () ->
Bruno Guillaume's avatar
Bruno Guillaume committed
281 282 283 284 285 286 287
        Grew_grs.Grs.load file
      ) ()

  let load_old file =
    handle ~name:"Grs.load" ~file
      (fun () ->
        Grew_grs.Grs.load_old file
288
      ) ()
289 290

  let dump grs =
291
    handle ~name:"Grs.dump"
292
      (fun () ->
293
        Grew_grs.Grs.dump grs
294 295 296
      ) ()

  let domain grs =
297
    handle ~name:"Grs.domain"
298
      (fun () ->
299
        Grew_grs.Grs.domain grs
300
      ) ()
301

Bruno Guillaume's avatar
Bruno Guillaume committed
302 303 304
  let to_json t =
    let json = Grew_grs.Grs.to_json t in
      Yojson.Basic.pretty_to_string json
305 306

  let get_strat_list grs =
307
    handle ~name:"Grs.get_strat_list"
308
      (fun () ->
309
        Grew_grs.Grs.get_strat_list grs
310 311
        ) ()

312 313
end

bguillaum's avatar
bguillaum committed
314 315 316 317 318 319 320
(* ==================================================================================================== *)
(** {2 Rewrite} *)
(* ==================================================================================================== *)
module Rewrite = struct
  type display = Libgrew_types.rew_display
  type history = Grew_grs.Rewrite_history.t

321 322
  let size = Libgrew_types.rew_display_size

323 324 325
  let set_max_depth_det value = Grew_rule.Rule.set_max_depth_det value
  let set_max_depth_non_det value = Grew_rule.Rule.set_max_depth_non_det value

bguillaum's avatar
bguillaum committed
326 327
  let set_debug_loop () = Grew_rule.Rule.set_debug_loop ()

328 329
  let old_display ~gr ~grs ~seq =
    handle ~name:"Rewrite.old_display" (fun () -> Grew_grs.Old_grs.build_rew_display grs seq gr) ()
bguillaum's avatar
bguillaum committed
330

331 332
  let display ~gr ~grs ~strat =
    handle ~name:"Rewrite.display" (fun () -> Grew_grs.Grs.det_rew_display grs strat gr) ()
333

bguillaum's avatar
bguillaum committed
334 335 336
  let set_timeout t = Grew_base.Timeout.timeout := t

  let rewrite ~gr ~grs ~seq =
337 338 339 340
    handle ~name:"Rewrite.rewrite" (fun () -> Grew_grs.Old_grs.rewrite grs seq gr) ()

  let old_simple_rewrite ~gr ~grs ~strat =
    handle ~name:"Rewrite.old_simple_rewrite" (fun () -> Grew_grs.Old_grs.simple_rewrite grs strat gr) ()
bguillaum's avatar
bguillaum committed
341

342 343 344
  let simple_rewrite ~gr ~grs ~strat =
    handle ~name:"Rewrite.simple_rewrite" (fun () -> Grew_grs.Grs.simple_rewrite grs strat gr) ()

345
  let at_least_one ~grs ~strat =
346
    handle ~name:"Rewrite.at_least_one" (fun () -> Grew_grs.Grs.at_least_one grs strat) ()
347
  let at_most_one ~grs ~strat =
348
    handle ~name:"Rewrite.at_most_one" (fun () -> Grew_grs.Grs.at_most_one grs strat) ()
349

Bruno Guillaume's avatar
Bruno Guillaume committed
350

bguillaum's avatar
bguillaum committed
351 352 353 354 355 356 357 358 359 360 361 362
  let is_empty rh =
    handle ~name:"Rewrite.is_empty" (fun () -> Grew_grs.Rewrite_history.is_empty rh) ()

  let num_sol rh =
    handle ~name:"Rewrite.num_sol" (fun () -> Grew_grs.Rewrite_history.num_sol rh) ()

  let write_stat filename rew_hist =
    handle ~name:"Rewrite.write_stat" (fun () -> Grew_html.Gr_stat.save filename (Grew_html.Gr_stat.from_rew_history rew_hist)) ()

  let save_index ~dirname ~base_names =
    handle ~name:"Rewrite.save_index" (fun () ->
      let out_ch = open_out (Filename.concat dirname "index") in
363
      Array.iter (fun f -> fprintf out_ch "%s\n" f) base_names;
bguillaum's avatar
bguillaum committed
364
      close_out out_ch
365 366
    ) ()

bguillaum's avatar
bguillaum committed
367 368
  let save_gr ?domain base rew_hist =
    handle ~name:"Rewrite.save_gr" (fun () -> Grew_grs.Rewrite_history.save_gr ?domain base rew_hist) ()
bguillaum's avatar
bguillaum committed
369

bguillaum's avatar
bguillaum committed
370 371
  let save_conll ?domain base rew_hist =
    handle ~name:"Rewrite.save_conll" (fun () -> Grew_grs.Rewrite_history.save_conll ?domain base rew_hist) ()
bguillaum's avatar
bguillaum committed
372

bguillaum's avatar
bguillaum committed
373 374
  let save_full_conll ?domain base rew_hist =
    handle ~name:"Rewrite.save_full_conll" (fun () -> Grew_grs.Rewrite_history.save_full_conll ?domain base rew_hist) ()
bguillaum's avatar
bguillaum committed
375

bguillaum's avatar
bguillaum committed
376 377
  let save_det_gr ?domain base rew_hist =
    handle ~name:"Rewrite.save_det_gr" (fun () -> Grew_grs.Rewrite_history.save_det_gr ?domain base rew_hist) ()
bguillaum's avatar
bguillaum committed
378

bguillaum's avatar
bguillaum committed
379 380
  let save_det_conll ?domain ?header base rew_hist =
    handle ~name:"Rewrite.save_det_conll" (fun () -> Grew_grs.Rewrite_history.save_det_conll ?domain ?header base rew_hist) ()
bguillaum's avatar
bguillaum committed
381

bguillaum's avatar
bguillaum committed
382 383
  let det_dep_string ?domain rew_hist =
    handle ~name:"Rewrite.det_dep_string" (fun () -> Grew_grs.Rewrite_history.det_dep_string ?domain rew_hist) ()
bguillaum's avatar
bguillaum committed
384

bguillaum's avatar
bguillaum committed
385 386
  let conll_dep_string ?domain ?keep_empty_rh rew_hist =
    handle ~name:"Rewrite.conll_dep_string" (fun () -> Grew_grs.Rewrite_history.conll_dep_string ?domain ?keep_empty_rh rew_hist) ()
bguillaum's avatar
bguillaum committed
387

bguillaum's avatar
bguillaum committed
388
  let write_html ?domain ?(no_init=false) ?(out_gr=false) ?filter ?main_feat ?dot ~header ?graph_file rew_hist output_base =
bguillaum's avatar
bguillaum committed
389 390
    handle ~name:"Rewrite.write_html" (fun () ->
      ignore (
bguillaum's avatar
bguillaum committed
391
        Grew_html.Html_rh.build ?domain ?filter ?main_feat ?dot ~out_gr ~init_graph: (not no_init) ~header ?graph_file output_base rew_hist
bguillaum's avatar
bguillaum committed
392 393 394
      )
    ) ()

bguillaum's avatar
bguillaum committed
395
  let error_html ?domain ?(no_init=false) ?main_feat ?dot ~header msg ?init output_base =
bguillaum's avatar
bguillaum committed
396 397
    handle ~name:"Rewrite.error_html" (fun () ->
      ignore (
bguillaum's avatar
bguillaum committed
398
        Grew_html.Html_rh.error ?domain ?main_feat ?dot ~init_graph: (not no_init) ~header output_base msg init
bguillaum's avatar
bguillaum committed
399 400 401 402 403 404 405
      )
    ) ()

  let make_index ~title ~grs_file ~html ~grs ~seq ~input_dir ~output_dir ~base_names  =
    handle ~name:"Rewrite.make_index" (fun () ->
      let init = Grew_html.Corpus_stat.empty grs seq in
      let corpus_stat =
406
        Array.fold_left
bguillaum's avatar
bguillaum committed
407 408 409 410 411
          (fun acc base_name ->
            Grew_html.Corpus_stat.add_gr_stat base_name (Grew_html.Gr_stat.load (Filename.concat output_dir (base_name^".stat"))) acc
          ) init base_names in
      Grew_html.Corpus_stat.save_html title grs_file input_dir output_dir corpus_stat
    ) ()
bguillaum's avatar
bguillaum committed
412

bguillaum's avatar
bguillaum committed
413 414
  let html_sentences ~title = handle ~name:"Rewrite.html_sentences" (fun () -> Grew_html.Html_sentences.build ~title) ()
end