libgrew.ml 4.47 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

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

pj2m's avatar
pj2m committed
11 12 13 14
open Grew_parser
open HTMLer


bguillaum's avatar
bguillaum committed
15

pj2m's avatar
pj2m committed
16 17 18 19 20
exception Parsing_err of string
exception File_dont_exists of string

exception Build of string * (string * int) option
exception Run of string * (string * int) option
bguillaum's avatar
bguillaum committed
21
exception Bug of string * (string * int) option
pj2m's avatar
pj2m committed
22 23 24

type grs = Grs.t
type gr = Instance.t
bguillaum's avatar
bguillaum committed
25
type rew_history = Rewrite_history.t
pj2m's avatar
pj2m committed
26

bguillaum's avatar
bguillaum committed
27 28
let is_empty = Rewrite_history.is_empty

pj2m's avatar
pj2m committed
29 30
let empty_grs = Grs.empty

bguillaum's avatar
bguillaum committed
31 32 33 34
let load_grs ?doc_output_dir file =
  if not (Sys.file_exists file)
  then raise (File_dont_exists file)
  else
35
    try
bguillaum's avatar
bguillaum committed
36
      let grs_ast = Grew_parser.grs_of_file file in
bguillaum's avatar
bguillaum committed
37 38
      (match doc_output_dir with
      | None -> ()
bguillaum's avatar
bguillaum committed
39 40
      | Some dir -> HTMLer.proceed dir grs_ast);
      Grs.build grs_ast
bguillaum's avatar
bguillaum committed
41
    with
42
    | Grew_parser.Parse_error msg -> raise (Parsing_err msg)
bguillaum's avatar
bguillaum committed
43 44
    | Error.Build (msg,loc) -> raise (Build (msg,loc))
    | Error.Bug (msg, loc) -> raise (Bug (msg,loc))
bguillaum's avatar
bguillaum committed
45
    | exc -> raise (Bug (sprintf "[Libgrew.load_grs] UNCATCHED EXCEPTION: %s" (Printexc.to_string exc), None))
bguillaum's avatar
bguillaum committed
46 47


48
let get_sequence_names grs = Grs.sequence_names grs
bguillaum's avatar
bguillaum committed
49

pj2m's avatar
pj2m committed
50 51
let empty_gr = Instance.empty

bguillaum's avatar
bguillaum committed
52
let load_gr file =
53 54
  if (Sys.file_exists file) then (
    try
bguillaum's avatar
bguillaum committed
55 56
      let gr_ast = Grew_parser.gr_of_file file in
      Instance.build gr_ast
57 58
    with
    | Grew_parser.Parse_error msg -> raise (Parsing_err msg)
bguillaum's avatar
bguillaum committed
59 60
    | Error.Build (msg,loc) -> raise (Build (msg,loc))
    | Error.Bug (msg, loc) -> raise (Bug (msg,loc))
bguillaum's avatar
bguillaum committed
61
    | exc -> raise (Bug (sprintf "[Libgrew.load_gr] UNCATCHED EXCEPTION: %s" (Printexc.to_string exc), None))
62 63 64 65

   ) else (
    raise (File_dont_exists file)
   )
pj2m's avatar
pj2m committed
66

bguillaum's avatar
bguillaum committed
67
let load_conll file =
68 69 70 71 72 73 74
  try
    let lines = File.read file in
    Instance.of_conll (List.map Conll.parse lines)
  with
  | Grew_parser.Parse_error msg -> raise (Parsing_err msg)
  | Error.Build (msg,loc) -> raise (Build (msg,loc))
  | Error.Bug (msg, loc) -> raise (Bug (msg,loc))
bguillaum's avatar
bguillaum committed
75
  | exc -> raise (Bug (sprintf "[Libgrew.load_conll] UNCATCHED EXCEPTION: %s" (Printexc.to_string exc), None))
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

let load_graph file = 
  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

bguillaum's avatar
bguillaum committed
92

93
let rewrite ~gr ~grs ~seq = 
94
  try Grs.rewrite grs seq gr
95
  with
bguillaum's avatar
bguillaum committed
96 97
  | Error.Run (msg,loc) -> raise (Run (msg,loc))
  | Error.Bug (msg, loc) -> raise (Bug (msg,loc))
bguillaum's avatar
bguillaum committed
98
  | exc -> raise (Bug (sprintf "[Libgrew.rewrite] UNCATCHED EXCEPTION: %s" (Printexc.to_string exc), None))
99

bguillaum's avatar
bguillaum committed
100
let display ~gr ~grs ~seq =
101
  try Grs.build_rew_display grs seq gr
pj2m's avatar
pj2m committed
102
  with
bguillaum's avatar
bguillaum committed
103 104
  | Error.Run (msg,loc) -> raise (Run (msg,loc))
  | Error.Bug (msg, loc) -> raise (Bug (msg,loc))
105
  | Error.Build (msg, loc) -> raise (Build (msg,loc))
bguillaum's avatar
bguillaum committed
106
  | exc -> raise (Bug (sprintf "[Libgrew.display] UNCATCHED EXCEPTION: %s" (Printexc.to_string exc), None))
bguillaum's avatar
bguillaum committed
107 108 109

let write_stat filename rew_hist = Gr_stat.save filename (Gr_stat.from_rew_history rew_hist) 

bguillaum's avatar
bguillaum committed
110 111 112 113 114
let save_index ~dirname ~base_names =
  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
115 116 117 118 119
let write_html 
    ?(no_init=false) ?main_feat 
    ~header
    rew_hist
    output_base =
120
IFDEF DEP2PICT THEN
121
  ignore (
bguillaum's avatar
bguillaum committed
122 123 124 125 126 127
  Rewrite_history.save_html 
    ?main_feat 
    ~init_graph: (not no_init)
    ~header
    output_base rew_hist
    )
128 129 130
ELSE
    Log.critical "[write_html] The \"libcaml-grew\" library is compiled without Dep2pict"
ENDIF
131 132 133 134 135 136

let error_html 
    ?(no_init=false) ?main_feat 
    ~header
    msg ?init
    output_base =
137
IFDEF DEP2PICT THEN
138 139 140 141 142 143 144
  ignore (
  Rewrite_history.error_html 
    ?main_feat 
    ~init_graph: (not no_init)
    ~header
    output_base msg init
    )
145
ELSE
bguillaum's avatar
bguillaum committed
146
    Log.critical "[error_html] The \"libcaml-grew\" library is compiled without Dep2pict"
147
ENDIF
148

bguillaum's avatar
bguillaum committed
149 150
let make_index ~title ~grs_file ~html ~grs ~seq ~output_dir ~base_names  =
  let init = Corpus_stat.empty grs seq in
bguillaum's avatar
bguillaum committed
151 152 153 154 155 156 157
  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 html output_dir corpus_stat

pj2m's avatar
pj2m committed
158 159
let get_css_file = Filename.concat DATA_DIR "style.css"