Commit 59dfd7e2 authored by Bruno Guillaume's avatar Bruno Guillaume
Browse files

remove log dependency (TBC)

parent 98031590
S src
B _build/src
PKG yojson log amr conll dep2pict libgrew_dev containers
PKG yojson amr conll dep2pict libgrew_dev containers ANSITerminal
true: package(yojson, containers, str, ANSITerminal, log, amr, conll)
true: package(yojson, containers, str, ANSITerminal, amr, conll)
true: bin_annot
......@@ -12,7 +12,6 @@ remove: [
depends: [
"cppo"
"ANSITerminal"
"libcaml-log"
"libcaml-grew"
]
synopsis: "A graph rewriting tool for NLP applications (see https://grew.fr)"
......
......@@ -9,7 +9,6 @@
(***********************************************************************)
open Arg
open Log
open Conllx
open Libgrew
......@@ -39,93 +38,93 @@ module Grew_args = struct
let grew_match_server = ref None
let force = ref false
let help () = List.iter (fun x -> Printf.printf "%s\n" x) [
"----------------------------------------------------------";
"usage: grew <subcommand> [<args>]";
"";
"subcommands are:";
" transform Apply a GRS on a corpus";
" grep Search for a pattern in a corpus";
" version Print current version number";
" help <sub> Print help for the given subcommand";
"";
"see subcommands help for args";
"For additional information, see https://grew.fr";
"----------------------------------------------------------";
]
let help () = List.iter (fun x -> Printf.printf "%s\n%!" x) [
"----------------------------------------------------------";
"usage: grew <subcommand> [<args>]";
"";
"subcommands are:";
" transform Apply a GRS on a corpus";
" grep Search for a pattern in a corpus";
" version Print current version number";
" help <sub> Print help for the given subcommand";
"";
"see subcommands help for args";
"For additional information, see https://grew.fr";
"----------------------------------------------------------";
]
let help_grep () = List.iter (fun x -> Printf.printf "%s\n" x) [
"----------------------------------------------------------";
"usage: grew grep [<args>]";
"";
"This subcommand searches for a pattern in a corpus.";
"";
"args are optionnal and can be change in the GUI:";
" -pattern <pat> The pattern to search for";
" -i <corp> The input data";
"";
"For additional information, see https://grew.fr";
"----------------------------------------------------------";
]
"----------------------------------------------------------";
"usage: grew grep [<args>]";
"";
"This subcommand searches for a pattern in a corpus.";
"";
"args are optionnal and can be change in the GUI:";
" -pattern <pat> The pattern to search for";
" -i <corp> The input data";
"";
"For additional information, see https://grew.fr";
"----------------------------------------------------------";
]
let help_transform () = List.iter (fun x -> Printf.printf "%s\n" x) [
"----------------------------------------------------------";
"usage: grew transform [<args>]";
"";
"This subcommand applies a grs to a graph or a corpus.";
"";
"Required arguments:";
" -grs <file> The Graph Rewriting System to load";
" -i <file> The input data (a graph or a corpus)";
" -o <file> The output file";
"";
"Optionnal arguments:";
" -strat <name> The stategy used in transformation (default=\"main\")";
" -cupt If the option is present, a 11-column CoNLL format is produced";
" -gr If the option is present, the gr output format is produced";
" -track_rules If the option is present, data about the rules applied are given in output";
"";
"For additional information, see https://grew.fr";
"----------------------------------------------------------";
]
"----------------------------------------------------------";
"usage: grew transform [<args>]";
"";
"This subcommand applies a grs to a graph or a corpus.";
"";
"Required arguments:";
" -grs <file> The Graph Rewriting System to load";
" -i <file> The input data (a graph or a corpus)";
" -o <file> The output file";
"";
"Optionnal arguments:";
" -strat <name> The stategy used in transformation (default=\"main\")";
" -cupt If the option is present, a 11-column CoNLL format is produced";
" -gr If the option is present, the gr output format is produced";
" -track_rules If the option is present, data about the rules applied are given in output";
"";
"For additional information, see https://grew.fr";
"----------------------------------------------------------";
]
let rec loop = function
| [] -> ()
| "-grs" :: file :: args -> grs := Some file; loop args
| "-i" :: files :: args -> input_data := !input_data @ (Str.split (Str.regexp " ") files); loop args
| "-o" :: file :: args -> output_data := Some file; loop args
| "-strat" :: s :: args -> strat := s; loop args
| "-pattern" :: files :: args
| "-patterns" :: files :: args -> patterns := !patterns @ (Str.split (Str.regexp " ") files); loop args
| "-key" :: s :: args -> key := Some s; loop args
| "-html" :: args -> html := true; loop args
| [] -> ()
| "-grs" :: file :: args -> grs := Some file; loop args
| "-i" :: files :: args -> input_data := !input_data @ (Str.split (Str.regexp " ") files); loop args
| "-o" :: file :: args -> output_data := Some file; loop args
| "-strat" :: s :: args -> strat := s; loop args
| "-pattern" :: files :: args
| "-patterns" :: files :: args -> patterns := !patterns @ (Str.split (Str.regexp " ") files); loop args
| "-key" :: s :: args -> key := Some s; loop args
| "-html" :: args -> html := true; loop args
| "-timeout" :: f :: args -> timeout := Some (float_of_string f); Rewrite.set_timeout (Some (float_of_string f)); loop args
| "-max_rules" :: i :: args -> Rewrite.set_max_rules (int_of_string i); loop args
| "-timeout" :: f :: args -> timeout := Some (float_of_string f); Rewrite.set_timeout (Some (float_of_string f)); loop args
| "-max_rules" :: i :: args -> Rewrite.set_max_rules (int_of_string i); loop args
| "-quiet" :: args -> quiet := true; loop args
| "-quiet" :: args -> quiet := true; loop args
| "-cupt" :: args -> output := Conllx (Conllx_columns.cupt); loop args
| "-semcor" :: args -> output := Conllx (Conllx_columns.frsemcor); loop args
| "-columns" :: desc :: args -> output := Conllx (Conllx_columns.build desc); loop args
| "-gr" :: args -> output := Gr; loop args
| "-dot" :: args -> output := Dot; loop args
| "-json" :: args -> output := Json; loop args
| "-multi_json" :: args -> output := Multi_json; loop args
| "-cupt" :: args -> output := Conllx (Conllx_columns.cupt); loop args
| "-semcor" :: args -> output := Conllx (Conllx_columns.frsemcor); loop args
| "-columns" :: desc :: args -> output := Conllx (Conllx_columns.build desc); loop args
| "-gr" :: args -> output := Gr; loop args
| "-dot" :: args -> output := Dot; loop args
| "-json" :: args -> output := Json; loop args
| "-multi_json" :: args -> output := Multi_json; loop args
| "-grew_match_server" :: dir :: args -> grew_match_server := Some dir; loop args
| "-force" :: args -> force := true; loop args
| "-grew_match_server" :: dir :: args -> grew_match_server := Some dir; loop args
| "-force" :: args -> force := true; loop args
| "-safe_commands" :: args -> Libgrew.set_safe_commands true; loop args
| "-track_rules" :: args -> Libgrew.set_track_rules true; loop args
| "-debug" :: args -> Libgrew.set_debug_mode true; loop args
| "-dep_dir" :: dir :: args -> dep_dir := Some dir; loop args
| "-safe_commands" :: args -> Libgrew.set_safe_commands true; loop args
| "-track_rules" :: args -> Libgrew.set_track_rules true; loop args
| "-debug" :: args -> Libgrew.set_debug_mode true; loop args
| "-dep_dir" :: dir :: args -> dep_dir := Some dir; loop args
| "-config" :: value :: args -> config := handle (fun () -> Conllx_config.build value) (); loop args
| "-config" :: value :: args -> config := handle (fun () -> Conllx_config.build value) (); loop args
| "-rff" :: value :: args -> config := Conllx_config.remove_from_feats value !config; loop args
| "-rff" :: value :: args -> config := Conllx_config.remove_from_feats value !config; loop args
| x :: args -> Log.fwarning "Invalid argument: %s, it is ignored!" x; loop args
| x :: args -> Log.warning "Invalid argument: %s, it is ignored!" x; loop args
let parse () =
match Array.to_list Sys.argv with
......@@ -147,5 +146,5 @@ module Grew_args = struct
| _ :: "help" :: "help" :: _ -> Printf.printf "Such a complex feature is still in development!\n"
| [_] -> help ()
| _ :: "help" :: _ -> help ()
| _ :: cmd :: _ -> Log.fwarning "Unknown command \"%s\"" cmd; help()
| _ :: cmd :: _ -> Log.fail "Unknown command \"%s\"" cmd
end
......@@ -9,7 +9,6 @@
(***********************************************************************)
open Printf
open Log
open Conllx
open Libgrew
......@@ -29,7 +28,7 @@ let load_corpus () =
match Unix.stat one with
| { Unix.st_kind = Unix.S_DIR } -> Corpus.from_dir ~config one
| _ -> Corpus.from_file ~config one
with Unix.Unix_error _ -> fail (sprintf "File not found `%s`" one)
with Unix.Unix_error _ -> Log.fail "%s" (sprintf "File not found `%s`" one)
end
| files ->
let sub_corpora =
......@@ -38,7 +37,7 @@ let load_corpus () =
try
let subcorpus = Corpus.from_file ~config file in
subcorpus :: acc
with Unix.Unix_error _ -> fail (sprintf "File not found `%s`" file)
with Unix.Unix_error _ -> Log.fail "%s" (sprintf "File not found `%s`" file)
) [] files in
Corpus.merge sub_corpora
......@@ -71,7 +70,7 @@ let transform () =
let buff = Buffer.create 32 in
(fun graph ->
if !flag
then (Log.fmessage "`dot` output cannot be used when there is more than one graph in the output"; exit 1)
then (Log.fail "`dot` output cannot be used when there is more than one graph in the output")
else (flag := true; bprintf buff "%s" (Graph.to_dot ~config graph))
),
(fun () -> fprintf out_ch "%s\n" (Buffer.contents buff))
......@@ -89,7 +88,7 @@ let transform () =
(fun graph -> data := (graph |> Graph.to_json) :: !data),
(fun () ->
match (!Grew_args.output_data, !data) with
| (None,_) -> fail "-multi_json implies -o"
| (None,_) -> Log.fail "%s" "-multi_json implies -o"
| (Some out_file, l) ->
let base =
match Filename.chop_suffix_opt ~suffix:".json" out_file with
......@@ -154,7 +153,7 @@ let grep_with_key key =
(key_value, `List l) :: acc
) final_map [] in
Printf.printf "%s\n" (Yojson.Basic.pretty_to_string (`Assoc json_list))
| l -> Log.fmessage "1 pattern expected for grep mode (%d given)" (List.length l); exit 1
| l -> Log.fail "1 pattern expected for grep mode (%d given)" (List.length l)
(* -------------------------------------------------------------------------------- *)
let grep_without_key () =
......@@ -220,7 +219,7 @@ let grep_without_key () =
) acc matchings
) [] corpus in
Printf.printf "%s\n" (Yojson.Basic.pretty_to_string (`List final_json))
| l -> Log.fmessage "1 pattern expected for grep mode (%d given)" (List.length l); exit 1
| l -> Log.fail "1 pattern expected for grep mode (%d given)" (List.length l)
(* -------------------------------------------------------------------------------- *)
......@@ -360,7 +359,7 @@ let count () =
printf "\n%!"
) maps
| (l,_) -> Log.fwarning "When the 'key' parameter is used, exactly one pattern is expected (%d given)" (List.length l)
| (l,_) -> Log.warning "When the 'key' parameter is used, exactly one pattern is expected (%d given)" (List.length l)
) ()
(* -------------------------------------------------------------------------------- *)
......@@ -372,8 +371,8 @@ let stat () =
handle
(fun () ->
match !Grew_args.patterns with
| [] -> Log.fwarning "No pattern given (expected one json file with patterns)"
| _::_::_ -> Log.fwarning "Too much patterns given (expected one json file with patterns)"
| [] -> Log.warning "No pattern given (expected one json file with patterns)"
| _::_::_ -> Log.warning "Too much patterns given (expected one json file with patterns)"
| [one] ->
let (pat_descs, json) = Stat.load_json one in
......@@ -453,8 +452,6 @@ let valid () =
(* -------------------------------------------------------------------------------- *)
let _ =
Printexc.record_backtrace true;
Log.set_active_levels ~levels:[`INFO; `MESSAGE; `WARNING];
Log.set_write_to_log_file false;
(* parsing command line args *)
Grew_args.parse ();
......
......@@ -9,7 +9,6 @@
(***********************************************************************)
open Printf
open Log
open Conllx
open Libgrew
......@@ -19,6 +18,23 @@ module Int_map = Map.Make (Int)
let quiet = ref false
(* ==================================================================================================== *)
module Log = struct
let warning_ message =
ANSITerminal.eprintf [ANSITerminal.blue] "WARNING: %s\n" message;
Printf.eprintf "%!" (* force synchronous printing *)
let warning message = Printf.ksprintf warning_ message
let fail_ message =
ANSITerminal.eprintf [ANSITerminal.red] "FAIL: %s\n" message;
Printf.eprintf "%!" (* force synchronous printing *);
exit 1
let fail message = Printf.ksprintf fail_ message
end
(* ==================================================================================================== *)
exception Error of Yojson.Basic.t
......@@ -37,18 +53,16 @@ let error ?file ?line ?fct ?data = Printf.ksprintf (error_ ?file ?line ?fct ?dat
(* -------------------------------------------------------------------------------- *)
let fail msg = Log.fmessage "%s" msg; exit 2
let handle fct () =
try fct ()
with
| Error json -> fail (Yojson.Basic.pretty_to_string json)
| Conllx_error json -> fail (Yojson.Basic.pretty_to_string json)
| Libgrew.Error msg -> fail msg
| Sys_error msg -> fail (sprintf "System error: %s" msg)
| Yojson.Json_error msg -> fail (sprintf "Json error: %s" msg)
| Libgrew.Bug msg -> fail (sprintf "Libgrew.bug, please report: %s" msg)
| exc -> fail (sprintf "Uncaught exception, please report: %s" (Printexc.to_string exc))
| Error json -> Log.fail "%s" (Yojson.Basic.pretty_to_string json)
| Conllx_error json -> Log.fail "%s" (Yojson.Basic.pretty_to_string json)
| Libgrew.Error msg -> Log.fail "%s" msg
| Sys_error msg -> Log.fail "%s" (sprintf "System error: %s" msg)
| Yojson.Json_error msg -> Log.fail "%s" (sprintf "Json error: %s" msg)
| Libgrew.Bug msg -> Log.fail "%s" (sprintf "Libgrew.bug, please report: %s" msg)
| exc -> Log.fail "%s" (sprintf "Uncaught exception, please report: %s" (Printexc.to_string exc))
(* ---------------------------------------------------------------------------------------------------- *)
let ensure_dir dir =
......@@ -331,4 +345,4 @@ module Validation = struct
let out_file = Filename.concat dir ((Corpus_desc.get_id corpus_desc) ^ ".json") in
CCIO.with_out out_file (fun out_ch -> fprintf out_ch "%s\n" (Yojson.Basic.pretty_to_string json))
end
\ No newline at end of file
end
Supports Markdown
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