Commit d08a9b3c authored by Bruno Guillaume's avatar Bruno Guillaume

version 2.26.0: new libcaml_dep2pict error handling

parent 3d4411bd
......@@ -29,12 +29,27 @@ let write file string =
end (* module File *)
exception Error of Yojson.Basic.json
let error ?file ?line ?fct ?data msg =
let opt_list = [
Some ("message", `String msg);
(CCOpt.map (fun x -> ("file", `String x)) file);
(CCOpt.map (fun x -> ("line", `Int x)) line);
Some ("program", `String "dep2pict");
(CCOpt.map (fun x -> ("function", `String x)) fct);
(CCOpt.map (fun x -> ("data", `String x)) data);
] in
let json = `Assoc (CCList.filter_map (fun x->x) opt_list) in
raise (Error json)
let batch = ref false
let rtl = ref false
let critical msg = ksprintf
(fun m -> match !batch with
| true -> printf "%s\n" m; exit 1
| true -> eprintf "%s\n" m; exit 1
| false -> Log.fcritical "%s" m
) msg
......@@ -109,7 +124,7 @@ let search_sentid sentid =
begin
match array_assoc sentid arr with
| Some p -> current_position := p
| None -> Log.fwarning "No conll struct with name \"%s\"" sentid
| None -> Log.fcritical "No conll struct with name \"%s\"" sentid
end
| _ -> Log.critical "[search_sentid] can be use only with CONLL data"
......@@ -176,18 +191,12 @@ let view_label () =
(* -------------------------------------------------------------------------------- *)
let load file =
try
match Format.get file with
| Format.Dep -> let dep = File.read file in current_data := Dep (Dep2pict.from_dep dep)
| Format.Conll -> current_data := Conll (Conll_corpus.load file)
| _ ->
Log.fwarning "No valid input format detected for file \"%s\", try to guess...\n%!" file;
let text = File.read file in
if String.length text > 0 && (text.[0] = '1' || text.[0] = '#')
then current_data := Conll (Conll_corpus.load file)
else current_data := Dep (Dep2pict.from_dep text)
with
| Dep2pict.Parse_error msgs -> List.iter (fun (l,m) -> printf "Line %d: %s\n" l m) msgs; critical "Parse error !!"
| Dep2pict.Id_already_in_use_ id -> critical "Id already in use : %s" id
| Dep2pict.Unknown_index id -> critical "Can't find index: %s" id
| Dep2pict.Loop_in_dep msg -> critical "Loop in dependency : %s" msg
match Format.get file with
| Format.Dep -> let dep = File.read file in current_data := Dep (Dep2pict.from_dep dep)
| Format.Conll -> current_data := Conll (Conll_corpus.load file)
| _ ->
Log.fwarning "No valid input format detected for file \"%s\", try to guess...\n%!" file;
let text = File.read file in
if String.length text > 0 && (text.[0] = '1' || text.[0] = '#')
then current_data := Conll (Conll_corpus.load file)
else current_data := Dep (Dep2pict.from_dep text)
......@@ -76,14 +76,8 @@ let main () =
user_hpos := ui#scroll#hadjustment#value; (* Hack (cf above) *)
refresh_view ()
with
| Dep2pict.Parse_error msgs ->
ui#error_view#buffer#set_text
(String.concat "\n" (List.map (fun (l,m) -> sprintf "Line %d: %s" l m) msgs));
| Dep2pict.Id_already_in_use_ id -> ui#error_view#buffer#set_text ("Id already in use: "^id)
| Dep2pict.Unknown_index id -> ui#error_view#buffer#set_text ("Can't find index: "^id)
| Dep2pict.Loop_in_dep msg -> ui#error_view#buffer#set_text ("Loop in dependency: "^msg)
| Dep2pict.Conll_format msg -> ui#error_view#buffer#set_text ("Conll format: "^msg)
| Conll_types.Error json -> ui#error_view#buffer#set_text (Yojson.Basic.pretty_to_string json) in
| Conll_types.Error json -> ui#error_view#buffer#set_text (Yojson.Basic.pretty_to_string json)
| Dep2pict.Error json -> ui#error_view#buffer#set_text (Yojson.Basic.pretty_to_string json) in
(* check if file has changed *)
......
......@@ -61,7 +61,7 @@ let rec parse_arg = function
| "-d"::tail | "--debug"::tail -> debug := true; parse_arg tail
| "-b"::tail | "--batch"::tail -> batch := true; parse_arg tail
| "-b"::tail | "--batch"::tail -> Log.set_active_levels []; batch := true; parse_arg tail
| "-rtl":: tail | "--right_to_left":: tail -> rtl := true; parse_arg tail
| s::_ when s.[0] = '-' -> Log.fcritical "Unknwon option \"%s\"" s
......@@ -104,10 +104,8 @@ let json_apply json_in json_out =
| _ -> Log.warning "Json input file should be a list"; exit 0
(* (Yojson.Basic.pretty_to_string json) *)
(* -------------------------------------------------------------------------------- *)
let _ =
let main () =
let () = parse_arg (List.tl (Array.to_list Sys.argv)) in
if !debug then Dep2pict.set_verbose ();
......@@ -127,15 +125,17 @@ let _ =
Log.warning "Dep2pict was compiled without lablwebkit, the GUI in not available"; exit 0
#endif
| Some out_file ->
if (Format.get !input_file) = Format.Json
then json_apply (Yojson.Basic.from_file !input_file) out_file
else
try
if (Format.get !input_file) = Format.Json
then json_apply (Yojson.Basic.from_file !input_file) out_file
else
try
load !input_file;
set_position ();
let graph = match (!current_data, !current_position) with
| (Dep g,_) -> g
| (Conll arr, pos) -> Dep2pict.from_conll ~rtl:!rtl ~conll:(snd arr.(pos)) in
| (Conll [||],_) -> error ~file: !input_file "Empty Conll file"
| (Conll arr, pos) ->
set_position ();
Dep2pict.from_conll ~rtl:!rtl ~conll:(snd arr.(pos)) in
begin
match Format.get out_file with
| Format.Svg -> Dep2pict.save_svg ~filename:out_file graph
......@@ -149,11 +149,13 @@ let _ =
| f -> critical "<%s> is not a valid output format" (Format.to_string f)
end;
Log.finfo "File %s generated." out_file
with
| Dep2pict.Parse_error msgs -> List.iter (fun (l,m) -> printf "Line %d: %s\n" l m) msgs; critical "Parse error !!"
| Dep2pict.Id_already_in_use_ id -> critical "Id already used: %s" id
| Dep2pict.Conll_format msg -> critical "Invalid CONLL line: %s" msg
| Dep2pict.Unknown_index id -> critical "Can't find index: %s" id
| Dep2pict.Loop_in_dep msg -> critical "Loop in dependency: %s" msg
| Conll_types.Error json -> critical "%s" (Yojson.Basic.pretty_to_string json)
| exc -> critical "Unexpected exception <%s>, please report" (Printexc.to_string exc)
with
| Error json -> raise (Error json)
| Dep2pict.Error json -> raise (Error json)
| Conll_types.Error json -> raise (Error json)
| Sys_error data -> error ~file: !input_file ~data "Sys_error"
| exc -> error ~file: !input_file ~data:(Printexc.to_string exc) "Unexpected exception, please report"
let _ =
try main ()
with Error json -> critical "%s" (Yojson.Basic.pretty_to_string json)
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment