Commit 3d4411bd authored by Bruno Guillaume's avatar Bruno Guillaume

add json mode for annot_tool

parent a128b3d2
......@@ -50,13 +50,13 @@ let critical msg = ksprintf
| Found i -> Some (String.sub file_name i (len-i))
module Format = struct
type format = Dep | Conll | Png | Svg | Pdf | No_suff | Unk of string
type format = Dep | Conll | Png | Svg | Pdf | Json | No_suff | Unk of string
let get file =
match get_suffix file with
| None -> Log.fwarning "Cannot guess format (no suffix) for file \"%s\"" file; No_suff
| Some suff ->
try List.assoc suff [ (".dep",Dep); (".conll",Conll); (".conllu",Conll); (".png",Png); (".svg",Svg); (".pdf",Pdf)]
try List.assoc suff [ (".dep",Dep); (".conll",Conll); (".conllu",Conll); (".png",Png); (".svg",Svg); (".pdf",Pdf); (".json",Json)]
with Not_found -> Log.fwarning "Unkwnow file extension \"%s\" for file \"%s\"" suff file; Unk suff
let to_string = function
......@@ -65,6 +65,7 @@ module Format = struct
| Png -> "png"
| Svg -> "svg"
| Pdf -> "pdf"
| Json -> "json"
| No_suff -> "no_suff"
| Unk suff -> sprintf "unknown suffix '%s'" suff
end
......
......@@ -77,6 +77,35 @@ let rec parse_arg = function
end;
parse_arg tail
let json_apply json_in json_out =
match json_in with
| `List l ->
let (new_json : Yojson.Basic.json) = `List (List.map (
function
| `Assoc item ->
begin
match List.assoc_opt "dep_file" item with
| Some (`String dep_file) ->
let out_file = (Filename.chop_extension dep_file) ^ ".svg" in
let dep = Dep2pict.from_dep (File.read dep_file) in
Dep2pict.save_svg ~filename:out_file dep;
let new_fields =
match Dep2pict.highlight_shift () with
| Some f -> [("svg_file", `String out_file); ("shift", `Float f)]
| None -> [("svg_file", `String out_file)] in
`Assoc (item @ new_fields)
| _ -> Log.warning "Json items should contain a \"del_file\" field"; exit 0
end
| _ -> Log.warning "Json input file should be a list of Assoc list"; exit 0
) l) in
let out_ch = open_out json_out in
Printf.fprintf out_ch "%s\n" (Yojson.Basic.pretty_to_string (new_json));
close_out out_ch
| _ -> Log.warning "Json input file should be a list"; exit 0
(* (Yojson.Basic.pretty_to_string json) *)
(* -------------------------------------------------------------------------------- *)
let _ =
let () = parse_arg (List.tl (Array.to_list Sys.argv)) in
......@@ -98,6 +127,9 @@ 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
load !input_file;
set_position ();
......
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