Commit 6fd47fb5 authored by Bruno Guillaume's avatar Bruno Guillaume
Browse files

add -columns argument, fix invalid json output if there is more than one output graph

parent 90d7881c
......@@ -23,8 +23,8 @@ module Grew_args = struct
let grs = ref None
let dep_dir = ref None
type output = Conllx | Cupt | Semcor | Gr | Dot | Json
let output = ref Conllx
type output = Conllx of Conllx_columns.t | Gr | Dot | Json
let output = ref (Conllx Conllx_columns.default)
let (input_data : string list ref) = ref []
let (output_data : string option ref) = ref None
......@@ -102,8 +102,10 @@ module Grew_args = struct
| "-max_rules" :: i :: args -> Rewrite.set_max_rules (int_of_string i); loop args
| "-quiet" :: args -> quiet := true; loop args
| "-cupt" :: args -> output := Cupt; loop args
| "-semcor" :: args -> output := Semcor; 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
......
......@@ -3,7 +3,7 @@
(* *)
(* Copyright 2011-2021 Inria, Université de Lorraine *)
(* *)
(* Webpage: https://grew.fr *)
(* Webpage: https://grew.fr *)
(* License: CeCILL (see LICENSE folder or "http://www.cecill.info") *)
(* Authors: see AUTHORS file *)
(***********************************************************************)
......@@ -56,53 +56,46 @@ let transform () =
| Some output_file -> open_out output_file
| None -> stdout in
let out_graph ?new_sent_id graph = match (!Grew_args.output, new_sent_id) with
| (Grew_args.Conllx,None) ->
fprintf out_ch "%s\n" (graph |> Graph.to_json |> Conllx.of_json |> Conllx.to_string ~config)
| (Grew_args.Conllx,Some nsi) ->
fprintf out_ch "%s\n" (graph |> Graph.to_json |> Conllx.of_json |> Conllx.set_sent_id nsi |> Conllx.to_string ~config)
| (Grew_args.Cupt, None) ->
fprintf out_ch "%s\n" (graph |> Graph.to_json |> Conllx.of_json |> Conllx.to_string ~config ~columns:Conllx_columns.cupt)
| (Grew_args.Cupt,Some nsi) ->
fprintf out_ch "%s\n" (graph |> Graph.to_json |> Conllx.of_json |> Conllx.set_sent_id nsi |> Conllx.to_string ~config ~columns:Conllx_columns.cupt)
| (Grew_args.Semcor, None) ->
fprintf out_ch "%s\n" (graph |> Graph.to_json |> Conllx.of_json |> Conllx.to_string ~config ~columns:Conllx_columns.frsemcor)
| (Grew_args.Semcor,Some nsi) ->
fprintf out_ch "%s\n" (graph |> Graph.to_json |> Conllx.of_json |> Conllx.set_sent_id nsi |> Conllx.to_string ~config ~columns:Conllx_columns.frsemcor)
| (Grew_args.Gr, None) -> fprintf out_ch "%s\n" (Graph.to_gr ~config graph)
| (Grew_args.Gr, Some nsi) -> fprintf out_ch "%s\n" (Graph.to_gr ~config (Graph.set_meta "sent_id" nsi graph))
| (Grew_args.Dot, None) -> fprintf out_ch "%s\n" (Graph.to_dot ~config graph)
| (Grew_args.Dot, Some nsi) -> fprintf out_ch "# sent_id = %s\n%s\n" nsi (Graph.to_dot ~config graph)
| (Grew_args.Json, None) -> fprintf out_ch "%s\n" (graph |> Graph.to_json |> Yojson.Basic.pretty_to_string)
| (Grew_args.Json, Some nsi) -> fprintf out_ch "%s\n" (graph |> Graph.set_meta "sent_id" nsi |> Graph.to_json |> Yojson.Basic.pretty_to_string) in
begin
match !Grew_args.output with
| Conllx -> fprintf out_ch "%s\n" (Conllx_columns.to_string Conllx_columns.default)
| Cupt -> fprintf out_ch "%s\n" (Conllx_columns.to_string Conllx_columns.cupt)
| Semcor -> fprintf out_ch "%s\n" (Conllx_columns.to_string Conllx_columns.frsemcor)
| _ -> ()
end;
let (next_graph, final) = match !Grew_args.output with
| Grew_args.Conllx columns ->
fprintf out_ch "%s\n" (Conllx_columns.to_string columns);
(fun graph -> fprintf out_ch "%s\n" (graph |> Graph.to_json |> Conllx.of_json |> Conllx.to_string ~config ~columns)),
(fun () -> ())
| Grew_args.Gr ->
(fun graph -> fprintf out_ch "%s\n" (Graph.to_gr ~config graph)),
(fun () -> ())
| Grew_args.Dot ->
let flag = ref false in
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)
else (flag := true; bprintf buff "%s" (Graph.to_dot ~config graph))
),
(fun () -> fprintf out_ch "%s\n" (Buffer.contents buff))
| Grew_args.Json ->
let data = ref [] in
(fun graph -> data := (graph |> Graph.to_json) :: !data),
(fun () ->
let json = match !data with
| [one] -> one
| _ -> `List (List.rev !data) in
fprintf out_ch "%s\n" (Yojson.Basic.pretty_to_string json)
)
in
Corpus.iteri
(fun index id gr ->
Counter.print index len id;
(fun index sent_id gr ->
Counter.print index len sent_id;
match Rewrite.simple_rewrite ~config gr grs !Grew_args.strat with
| [one] -> out_graph one
| [one] -> next_graph one
| l ->
List.iteri
(fun i graph ->
let new_sent_id = sprintf "%s_%d" id i in
out_graph ~new_sent_id graph
(* graph
|> Graph.to_json
|> Conllx.of_json
|> Conllx.set_sent_id (sprintf "%s_%d" id i)
|> Conllx.to_string ~config
|> fprintf out_ch "%s\n" *)
(fun i graph -> next_graph (Graph.set_meta "sent_id" (sprintf "%s_%d" sent_id i) graph)
) l
) corpus;
Counter.finish ();
final ();
match !Grew_args.output_data with
| Some output_file -> close_out out_ch
| None -> ()
......
# global.columns = ID FORM LEMMA UPOS XPOS FEATS HEAD DEPREL DEPS MISC
1 A _ _ _ _ 0 root _ _
2 B _ _ _ _ 1 case:obj@x _ _
2 B _ _ _ _ 1|1 case:obj@x|zzz:obj@x _ _
3 C _ _ _ _ 2 case:obj@x _ _
4 D _ _ _ _ 3 case:obj@x _ _
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