From 92bb451ed0d67c756534af8d8b1902c897b1a75c Mon Sep 17 00:00:00 2001 From: Bruno Guillaume <Bruno.Guillaume@loria.fr> Date: Sun, 20 Nov 2022 16:55:46 +0100 Subject: [PATCH] new functions * corpus_form_dict * corpus_update * corpus_get_all --- src_ocaml/grewpy.ml | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src_ocaml/grewpy.ml b/src_ocaml/grewpy.ml index 7325b86..082f639 100644 --- a/src_ocaml/grewpy.ml +++ b/src_ocaml/grewpy.ml @@ -120,6 +120,32 @@ let run_command request = Yojson.Basic.to_string (`Assoc [("status", `String "ERROR"); ("message", js)]) end + (* ======================= corpus_from_dict ======================= *) + | Some "corpus_from_dict" -> + begin + let corpus = + json + |> member "graphs" + |> to_assoc + |> List.map (fun (sent_id, json_graph) -> (sent_id, Graph.of_json json_graph)) + |> Corpus.from_assoc_list in + let index = Global.corpus_add corpus in + let data = `Assoc [("index", `Int index)] in + Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", data)]) + end + + (* ======================= corpus_update ======================= *) + | Some "corpus_update" -> + begin + let corpus = json |> member "corpus_index" |> to_int |> Global.corpus_get in + let graphs = json |> member "graphs" |> to_assoc in + List.iter + (fun (sent_id, json_graph) -> + Corpus.update_graph sent_id (Graph.of_json json_graph) corpus + ) graphs; + Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", `Null)]) + end + (* ======================= corpus_get ======================= *) | Some "corpus_get" -> begin @@ -141,6 +167,17 @@ let run_command request = | Error msg -> json_error msg end + (* ======================= corpus_get_all ======================= *) + | Some "corpus_get_all" -> + begin + try + let corpus = json |> member "corpus_index" |> to_int |> Global.corpus_get in + let data = `Assoc (Corpus.fold_right (fun sent_id graph acc -> (sent_id, Graph.to_json graph) :: acc) corpus []) in + Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", data)]) + with + | Error msg -> json_error msg + end + (* ======================= corpus_size ======================= *) (* | Some "corpus_size" -> begin @@ -156,7 +193,7 @@ let run_command request = begin try let corpus = json |> member "corpus_index" |> to_int |> Global.corpus_get in - let sent_ids = Corpus.fold_left (fun acc sent_id _ -> (`String sent_id) :: acc) [] corpus in + let sent_ids = Corpus.fold_right (fun sent_id _ acc -> (`String sent_id) :: acc) corpus [] in Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", `List (sent_ids))]) with -- GitLab