libgrew.ml 13.8 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
(* ==================================================================================================== *)
module Graph = struct
116
  type t = Grew_graph.G_graph.t
bguillaum's avatar
bguillaum committed
117

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

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

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

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

bguillaum's avatar
bguillaum committed
151
  let load ?domain file =
bguillaum's avatar
bguillaum committed
152 153 154
    handle ~name:"Graph.load_graph" ~file
      (fun () ->
        match Grew_base.File.get_suffix file with
bguillaum's avatar
bguillaum committed
155 156 157
        | Some ".gr" -> load_gr ?domain file
        | Some ".conll" -> load_conll ?domain file
        | Some ".br" | Some ".melt" -> load_brown ?domain file
158
        | Some ".cst" -> load_pst ?domain file
bguillaum's avatar
bguillaum committed
159 160 161 162
        | _ ->
            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
163
            | load_fct :: tail -> try load_fct ?domain file with _ -> loop tail in
164
            loop [load_gr; load_conll; load_brown; load_pst]
bguillaum's avatar
bguillaum committed
165
      ) ()
bguillaum's avatar
bguillaum committed
166

bguillaum's avatar
bguillaum committed
167 168
  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)) ()
169

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

173 174
  let of_pst ?domain pst_string =
    handle ~name:"of_pst"
175
      (fun () ->
176 177 178 179 180 181 182 183 184 185
        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)
186 187
      ) ()

bguillaum's avatar
bguillaum committed
188 189
  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
190

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

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

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

200 201
  let to_conll graph =
    handle ~name:"Graph.to_conll" (fun () -> Grew_graph.G_graph.to_conll graph) ()
202

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

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

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

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

224 225 226 227
  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
228
end
229

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

236
  let empty = Grew_grs.Old_grs.empty
237

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

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

252
  let get_domain grs = Grew_grs.Old_grs.get_domain grs
253 254

  let to_json t =
255
    let json = Grew_grs.Old_grs.to_json t in
256
    Yojson.Basic.pretty_to_string json
bguillaum's avatar
bguillaum committed
257 258
end

259 260 261
(* ==================================================================================================== *)
(** {2 New Graph Rewriting System} *)
(* ==================================================================================================== *)
262 263
module Grs = struct
  type t = Grew_grs.Grs.t
264 265

  let load file =
266
    handle ~name:"Grs.load" ~file
267
      (fun () ->
Bruno Guillaume's avatar
Bruno Guillaume committed
268 269 270 271 272 273 274
        Grew_grs.Grs.load file
      ) ()

  let load_old file =
    handle ~name:"Grs.load" ~file
      (fun () ->
        Grew_grs.Grs.load_old file
275
      ) ()
276 277

  let dump grs =
278
    handle ~name:"Grs.dump"
279
      (fun () ->
280
        Grew_grs.Grs.dump grs
281 282 283
      ) ()

  let domain grs =
284
    handle ~name:"Grs.domain"
285
      (fun () ->
286
        Grew_grs.Grs.domain grs
287
      ) ()
288

Bruno Guillaume's avatar
Bruno Guillaume committed
289 290 291
  let to_json t =
    let json = Grew_grs.Grs.to_json t in
      Yojson.Basic.pretty_to_string json
292 293

  let get_strat_list grs =
294
    handle ~name:"Grs.get_strat_list"
295
      (fun () ->
296
        Grew_grs.Grs.get_strat_list grs
297
        ) ()
298 299
end

bguillaum's avatar
bguillaum committed
300 301 302 303 304 305 306
(* ==================================================================================================== *)
(** {2 Rewrite} *)
(* ==================================================================================================== *)
module Rewrite = struct
  type display = Libgrew_types.rew_display
  type history = Grew_grs.Rewrite_history.t

307 308
  let size = Libgrew_types.rew_display_size

309 310 311
  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
312 313
  let set_debug_loop () = Grew_rule.Rule.set_debug_loop ()

314 315
  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
316

317 318
  let display ~gr ~grs ~strat =
    handle ~name:"Rewrite.display" (fun () -> Grew_grs.Grs.det_rew_display grs strat gr) ()
319

bguillaum's avatar
bguillaum committed
320 321 322
  let set_timeout t = Grew_base.Timeout.timeout := t

  let rewrite ~gr ~grs ~seq =
323 324 325 326
    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
327

328 329 330
  let simple_rewrite ~gr ~grs ~strat =
    handle ~name:"Rewrite.simple_rewrite" (fun () -> Grew_grs.Grs.simple_rewrite grs strat gr) ()

331
  let at_least_one ~grs ~strat =
332
    handle ~name:"Rewrite.at_least_one" (fun () -> Grew_grs.Grs.at_least_one grs strat) ()
333
  let at_most_one ~grs ~strat =
334
    handle ~name:"Rewrite.at_most_one" (fun () -> Grew_grs.Grs.at_most_one grs strat) ()
335

bguillaum's avatar
bguillaum committed
336 337 338 339 340 341 342 343 344
  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 save_index ~dirname ~base_names =
    handle ~name:"Rewrite.save_index" (fun () ->
      let out_ch = open_out (Filename.concat dirname "index") in
345
      Array.iter (fun f -> fprintf out_ch "%s\n" f) base_names;
bguillaum's avatar
bguillaum committed
346
      close_out out_ch
347 348
    ) ()

349 350
  let save_gr base rew_hist =
    handle ~name:"Rewrite.save_gr" (fun () -> Grew_grs.Rewrite_history.save_gr base rew_hist) ()
bguillaum's avatar
bguillaum committed
351

352 353
  let save_conll base rew_hist =
    handle ~name:"Rewrite.save_conll" (fun () -> Grew_grs.Rewrite_history.save_conll base rew_hist) ()
bguillaum's avatar
bguillaum committed
354

355 356
  let save_full_conll base rew_hist =
    handle ~name:"Rewrite.save_full_conll" (fun () -> Grew_grs.Rewrite_history.save_full_conll base rew_hist) ()
bguillaum's avatar
bguillaum committed
357

358 359
  let save_det_gr base rew_hist =
    handle ~name:"Rewrite.save_det_gr" (fun () -> Grew_grs.Rewrite_history.save_det_gr base rew_hist) ()
bguillaum's avatar
bguillaum committed
360

361 362
  let save_det_conll ?header base rew_hist =
    handle ~name:"Rewrite.save_det_conll" (fun () -> Grew_grs.Rewrite_history.save_det_conll ?header base rew_hist) ()
bguillaum's avatar
bguillaum committed
363

364 365
  let det_dep_string rew_hist =
    handle ~name:"Rewrite.det_dep_string" (fun () -> Grew_grs.Rewrite_history.det_dep_string rew_hist) ()
bguillaum's avatar
bguillaum committed
366

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