Commit e0c65072 authored by Bruno Guillaume's avatar Bruno Guillaume
Browse files

stat mode

parent 5e1f8dc5
......@@ -17,7 +17,7 @@ open Grew_utils
module Grew_args = struct
type mode = Undefined | Gui of string | Transform | Grep | Count | Valid | Compile | Clean | Test
type mode = Undefined | Gui of string | Transform | Grep | Count | Valid | Stat | Compile | Clean | Test
let mode = ref Undefined
let grs = ref None
......@@ -145,6 +145,7 @@ module Grew_args = struct
| _ :: "grep" :: args -> mode := Grep; loop args
| _ :: "count" :: args -> mode := Count; loop args
| _ :: "valid" :: args -> mode := Valid; loop args
| _ :: "stat" :: args -> mode := Stat; loop args
| _ :: "compile" :: args -> mode := Compile; loop args
| _ :: "clean" :: args -> mode := Clean; loop args
| _ :: "version" :: _ ->
......
......@@ -256,12 +256,62 @@ let count () =
) (!Grew_args.input_data)
) ()
(* -------------------------------------------------------------------------------- *)
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)"
| [one] ->
let (pat_descs, json) = Stat.load_json one in
let corpora =
List.fold_left
(fun acc conf_file ->
Corpus_desc.load_json conf_file @ acc
) [] !Grew_args.input_data in
let lines =
List.map
(fun corpus_desc ->
let corpus = Corpus_desc.build_corpus corpus_desc in
let config = Corpus_desc.get_config corpus_desc in
`List (
`String (Corpus_desc.get_id corpus_desc) ::
List.map (
fun pat_desc ->
let pattern =
try Pattern.parse ~config (String.concat " " pat_desc.Stat.code)
with Libgrew.Error msg ->
error
~fct:"Grew.stat"
~data:(`String (String.concat " " pat_desc.Stat.code))
"cannot parse pattern associated with desc: %s" pat_desc.Stat.desc in
let count =
Corpus.fold_left (fun acc _ graph ->
acc + (List.length (Graph.search_pattern ~config pattern graph))
) 0 corpus in
`Int count
) pat_descs
)
) corpora in
let final_json = `Assoc [
("patterns", json);
("stats", `List lines)
] in
match !Grew_args.output_data with
| None -> printf "%s\n" (Yojson.Basic.pretty_to_string final_json)
| Some f -> Yojson.Basic.to_file f final_json
) ()
(* -------------------------------------------------------------------------------- *)
let valid () =
handle
(fun () ->
match !Grew_args.output_data with
| None -> error ~fct:"valid" "an output directory is required (use -i option)"
| None -> error ~fct:"valid" "an output directory is required (use -o option)"
| Some dir ->
match ensure_dir dir with
| Some pble -> error ~fct:"valid" "%s" pble
......@@ -293,6 +343,7 @@ let _ =
| Grew_args.Transform -> transform ()
| Grew_args.Count-> count ()
| Grew_args.Valid-> valid ()
| Grew_args.Stat-> stat ()
| Grew_args.Compile -> compile ()
| Grew_args.Clean -> clean ()
| Grew_args.Grep -> grep ()
......
......@@ -207,6 +207,34 @@ module Timer = struct
diff
end
(* ==================================================================================================== *)
module Stat = struct
type pat_desc = {
id: string;
desc: string;
code: string list;
}
type t = pat_desc list
let load_json json_file =
let open Yojson.Basic.Util in
let json =
try Yojson.Basic.from_file json_file
with Yojson.Json_error msg -> error ~fct:"Stat.load_json" ~file:json_file "%s" msg in
let parse_pattern (id, json) =
let assoc = json |> to_assoc in
{ id;
desc = List.assoc "desc" assoc |> to_string;
code = List.assoc "code" assoc |> to_list |> List.map to_string;
} in
(json |> to_assoc |> List.map parse_pattern, json)
end
(* ==================================================================================================== *)
module Validation = struct
type item = {
......
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