libgrew.ml 7.7 KB
Newer Older
pj2m's avatar
pj2m committed
1 2
include Grew_types

bguillaum's avatar
bguillaum committed
3
open Printf
pj2m's avatar
pj2m committed
4 5
open Log

6
open Grew_fs
bguillaum's avatar
bguillaum committed
7 8 9 10 11
open Grew_utils
open Grew_graph
open Grew_rule
open Grew_grs

pj2m's avatar
pj2m committed
12
open Grew_parser
bguillaum's avatar
bguillaum committed
13
open Grew_html
pj2m's avatar
pj2m committed
14 15


bguillaum's avatar
bguillaum committed
16 17 18 19 20 21
let css_file = Filename.concat DATA_DIR "style.css"

let empty_grs = Grs.empty

let set_timeout t = Timeout.timeout := t

bguillaum's avatar
bguillaum committed
22

pj2m's avatar
pj2m committed
23 24
exception File_dont_exists of string

bguillaum's avatar
bguillaum committed
25
exception Parsing_err of string
pj2m's avatar
pj2m committed
26 27
exception Build of string * (string * int) option
exception Run of string * (string * int) option
bguillaum's avatar
bguillaum committed
28
exception Bug of string * (string * int) option
pj2m's avatar
pj2m committed
29

bguillaum's avatar
bguillaum committed
30 31 32
let handle ?(name="") ?(file="No file defined") fct () =
  (* Printf.printf " ==========> %s ...%!" name; *)
  try fct () with
33 34 35 36 37 38 39
    (* Raise again already catched exceptions *)
    | Parsing_err msg -> raise (Parsing_err msg)
    | Build (msg,loc) -> raise (Build (msg,loc))
    | Bug (msg, loc) -> raise (Bug (msg,loc))
    | Run (msg, loc) -> raise (Run (msg,loc))

    (* Catch new exceptions *)
bguillaum's avatar
bguillaum committed
40 41 42 43 44 45 46
    | Grew_parser.Parse_error (msg,Some (sub_file,l)) ->
        raise (Parsing_err (sprintf "[file:%s, line:%d] %s" sub_file l msg))
    | Grew_parser.Parse_error (msg,None) ->
        raise (Parsing_err (sprintf "[file:%s] %s" file msg))
    | Error.Build (msg,loc) -> raise (Build (msg,loc))
    | Error.Bug (msg, loc) -> raise (Bug (msg,loc))
    | Error.Run (msg, loc) -> raise (Run (msg,loc))
47

bguillaum's avatar
bguillaum committed
48
    | exc -> raise (Bug (sprintf "[Libgrew.%s] UNCATCHED EXCEPTION: %s" name (Printexc.to_string exc), None))
pj2m's avatar
pj2m committed
49

bguillaum's avatar
bguillaum committed
50
let is_empty rh =
bguillaum's avatar
bguillaum committed
51
  handle ~name:"is_empty" (fun () -> Rewrite_history.is_empty rh) ()
bguillaum's avatar
bguillaum committed
52

bguillaum's avatar
bguillaum committed
53 54
let num_sol rh =
  handle ~name:"num_sol" (fun () -> Rewrite_history.num_sol rh) ()
pj2m's avatar
pj2m committed
55

bguillaum's avatar
bguillaum committed
56

57
IFDEF DEP2PICT THEN
58
let build_html_doc ?(corpus=false) dir grs =
bguillaum's avatar
bguillaum committed
59
  handle ~name:"build_doc [with Dep2pict]"
bguillaum's avatar
bguillaum committed
60
    (fun () ->
61
      Html_doc.build ~corpus ~dep:true dir grs;
bguillaum's avatar
bguillaum committed
62 63 64 65

      (* draw pattern graphs for all rules and all filters *)
      let fct module_ rule_ =
        let dep_code = Rule.to_dep rule_ in
bguillaum's avatar
bguillaum committed
66 67
        let dep_png_file = sprintf "%s/%s_%s-patt.png" dir module_ (Rule.get_name rule_) in
        ignore (Dep2pict.Dep2pict.fromDepStringToPng dep_code dep_png_file) in
bguillaum's avatar
bguillaum committed
68 69 70
      Grs.rule_iter fct grs;
      Grs.filter_iter fct grs
    ) ()
71
ELSE
72 73
let build_html_doc ?(corpus=false) dir grs =
  handle ~name:"build_doc [without Dep2pict]" (fun () -> Html_doc.build ~corpus ~dep:false grs_ dir) ()
74 75
END

bguillaum's avatar
bguillaum committed
76
let load_grs file =
bguillaum's avatar
bguillaum committed
77 78 79 80
  handle ~name:"load_grs" ~file
    (fun () ->
      if not (Sys.file_exists file)
      then raise (File_dont_exists file)
bguillaum's avatar
bguillaum committed
81
      else Grs.build file
bguillaum's avatar
bguillaum committed
82
    ) ()
bguillaum's avatar
bguillaum committed
83

bguillaum's avatar
bguillaum committed
84
let to_sentence ?main_feat gr =
bguillaum's avatar
bguillaum committed
85 86 87 88 89 90 91 92 93 94 95
  handle ~name:"to_sentence"
    (fun () ->
      let graph = gr.Instance.graph in
      G_graph.to_sentence ?main_feat graph
    ) ()

let get_sequence_names grs =
  handle ~name:"get_sequence_names"
    (fun () ->
      Grs.sequence_names grs
    ) ()
bguillaum's avatar
bguillaum committed
96 97

let load_gr file =
bguillaum's avatar
bguillaum committed
98 99 100 101 102
  if not (Sys.file_exists file)
  then raise (File_dont_exists file)
  else
    handle ~name:"load_gr" ~file
      (fun () ->
bguillaum's avatar
bguillaum committed
103 104
        let gr_ast = Grew_parser.gr_of_file file in
        Instance.from_graph (G_graph.build gr_ast)
bguillaum's avatar
bguillaum committed
105
      ) ()
pj2m's avatar
pj2m committed
106

bguillaum's avatar
bguillaum committed
107
let load_conll file =
bguillaum's avatar
bguillaum committed
108 109 110 111 112 113
  handle ~name:"load_conll" ~file
    (fun () ->
      let graph = G_graph.of_conll ~loc:(file,-1) (Conll.load file) in
      Instance.from_graph graph
    ) ()

bguillaum's avatar
bguillaum committed
114 115 116 117 118 119 120
let of_conll file_name line_list =
  handle ~name:"of_conll"
    (fun () ->
      let graph = G_graph.of_conll (Conll.parse file_name line_list) in
      Instance.from_graph graph
    ) ()

bguillaum's avatar
bguillaum committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
let load_graph file =
  handle ~name:"load_graph" ~file
    (fun () ->
      if Filename.check_suffix file ".gr"
      then load_gr file
      else if Filename.check_suffix file ".conll"
      then load_conll file
      else
        begin
          Log.fwarning "Unknown file format for input graph '%s', try to guess..." file;
          try load_gr file with
              Parsing_err _ ->
                try load_conll file with
                    Parsing_err _ ->
                      Log.fcritical "[Libgrew.load_graph] Cannot guess input file format of file '%s'. Use .gr or .conll file extension" file
        end
    ) ()
138

bguillaum's avatar
bguillaum committed
139
let xml_graph xml =
bguillaum's avatar
bguillaum committed
140
  handle ~name:"xml_graph" (fun () -> Instance.from_graph (G_graph.of_xml xml)) ()
bguillaum's avatar
bguillaum committed
141

bguillaum's avatar
bguillaum committed
142
let raw_graph instance =
bguillaum's avatar
bguillaum committed
143
  handle ~name:"raw_graph" (fun () -> G_graph.to_raw instance.Instance.graph) ()
bguillaum's avatar
bguillaum committed
144

bguillaum's avatar
bguillaum committed
145 146
let rewrite ~gr ~grs ~seq =
  handle ~name:"rewrite" (fun () -> Grs.rewrite grs seq gr) ()
147

bguillaum's avatar
bguillaum committed
148
let display ~gr ~grs ~seq =
bguillaum's avatar
bguillaum committed
149
  handle ~name:"display" (fun () -> Grs.build_rew_display grs seq gr) ()
bguillaum's avatar
bguillaum committed
150

bguillaum's avatar
bguillaum committed
151 152
let write_stat filename rew_hist =
  handle ~name:"write_stat" (fun () -> Gr_stat.save filename (Gr_stat.from_rew_history rew_hist)) ()
bguillaum's avatar
bguillaum committed
153

bguillaum's avatar
bguillaum committed
154 155 156
let write_annot static_dir annot_dir base_name_rew_hist_list =
  handle ~name:"write_annot" (fun () -> Html_annot.build static_dir annot_dir base_name_rew_hist_list) ()

bguillaum's avatar
bguillaum committed
157
let save_index ~dirname ~base_names =
bguillaum's avatar
bguillaum committed
158 159 160 161 162
  handle ~name:"save_index" (fun () ->
    let out_ch = open_out (Filename.concat dirname "index") in
    List.iter (fun f -> fprintf out_ch "%s\n" f) base_names;
    close_out out_ch
  ) ()
bguillaum's avatar
bguillaum committed
163

bguillaum's avatar
bguillaum committed
164
let save_graph_conll filename graph =
bguillaum's avatar
bguillaum committed
165 166 167 168 169
  handle ~name:"save_graph_conll" (fun () ->
    let out_ch = open_out filename in
    fprintf out_ch "%s" (Instance.to_conll graph);
    close_out out_ch
  ) ()
bguillaum's avatar
bguillaum committed
170

bguillaum's avatar
bguillaum committed
171 172
let save_gr base rew_hist =
  handle ~name:"save_gr" (fun () -> Rewrite_history.save_gr base rew_hist) ()
173

bguillaum's avatar
bguillaum committed
174 175
let save_conll base rew_hist =
  handle ~name:"save_conll" (fun () -> Rewrite_history.save_conll base rew_hist) ()
bguillaum's avatar
bguillaum committed
176

bguillaum's avatar
bguillaum committed
177 178
let save_det_gr base rew_hist =
  handle ~name:"save_det_gr" (fun () -> Rewrite_history.save_det_gr base rew_hist) ()
bguillaum's avatar
bguillaum committed
179

bguillaum's avatar
bguillaum committed
180
let save_det_conll ?header base rew_hist =
181
  handle ~name:"save_deeeet_conll" (fun () -> Rewrite_history.save_det_conll ?header base rew_hist) ()
182

bguillaum's avatar
bguillaum committed
183 184
let det_dep_string rew_hist =
  handle ~name:"det_dep_string" (fun () -> Rewrite_history.det_dep_string rew_hist) ()
bguillaum's avatar
bguillaum committed
185

bguillaum's avatar
bguillaum committed
186 187 188
let conll_dep_string ?keep_empty_rh rew_hist =
  handle ~name:"conll_dep_string" (fun () -> Rewrite_history.conll_dep_string ?keep_empty_rh rew_hist) ()

bguillaum's avatar
bguillaum committed
189
let write_html 
190 191
    ?(no_init=false)
    ?(out_gr=false)
192 193
    ?filter
    ?main_feat
194
    ?dot
bguillaum's avatar
bguillaum committed
195
    ~header
bguillaum's avatar
bguillaum committed
196
    ?graph_file
bguillaum's avatar
bguillaum committed
197 198
    rew_hist
    output_base =
bguillaum's avatar
bguillaum committed
199 200 201 202 203 204 205 206 207
  handle ~name:"write_html" (fun () ->
    ignore (
      Html_rh.build
        ?filter
        ?main_feat
        ?dot
        ~out_gr
        ~init_graph: (not no_init)
        ~header
bguillaum's avatar
bguillaum committed
208
        ?graph_file
bguillaum's avatar
bguillaum committed
209
        output_base rew_hist
bguillaum's avatar
bguillaum committed
210
    )
bguillaum's avatar
bguillaum committed
211
  ) ()
212 213

let error_html 
214 215 216
    ?(no_init=false) 
    ?main_feat 
    ?dot
217
    ~header
218 219
    msg 
    ?init
220
    output_base =
bguillaum's avatar
bguillaum committed
221 222 223 224 225 226 227 228
  handle ~name:"error_html" (fun () ->
    ignore (
      Html_rh.error
        ?main_feat
        ?dot
        ~init_graph: (not no_init)
        ~header
        output_base msg init
229
    )
bguillaum's avatar
bguillaum committed
230
  ) ()
231

232
let make_index ~title ~grs_file ~html ~grs ~seq ~input_dir ~output_dir ~base_names  =
bguillaum's avatar
bguillaum committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  handle ~name:"make_index" (fun () ->
    let init = Corpus_stat.empty grs seq in
    let corpus_stat =
      List.fold_left
        (fun acc base_name ->
          Corpus_stat.add_gr_stat base_name (Gr_stat.load (Filename.concat output_dir (base_name^".stat"))) acc
        ) init base_names in
    Corpus_stat.save_html title grs_file input_dir output_dir corpus_stat
  ) ()

let html_sentences ~title = handle ~name:"html_sentences" (fun () -> Html_sentences.build ~title) ()

let graph_of_instance instance = handle ~name:"graph_of_instance" (fun () -> instance.Instance.graph) ()

let feature_names () =  handle ~name:"feature_names" (fun () -> Domain.feature_names ()) ()
bguillaum's avatar
bguillaum committed
248

bguillaum's avatar
bguillaum committed
249 250
let to_dot_graph ?main_feat ?(deco=G_deco.empty) graph =
  handle ~name:"to_dot_graph" (fun () -> G_graph.to_dot ?main_feat graph ~deco) ()
bguillaum's avatar
bguillaum committed
251

bguillaum's avatar
bguillaum committed
252 253
let to_dep_graph ?filter ?main_feat ?(deco=G_deco.empty) graph =
  handle ~name:"to_dep_graph" (fun () -> G_graph.to_dep ?filter ?main_feat ~deco graph) ()
pj2m's avatar
pj2m committed
254

bguillaum's avatar
bguillaum committed
255 256
let to_gr_graph graph =
  handle ~name:"to_gr_graph" (fun () -> G_graph.to_gr graph) ()
257

bguillaum's avatar
bguillaum committed
258 259
let to_conll_graph graph =
  handle ~name:"to_conll_graph" (fun () -> G_graph.to_conll graph) ()