diff --git a/src_ocaml/grewpy.ml b/src_ocaml/grewpy.ml index f4e4ab8db9b3400030ea41439eff85298e39d6d0..ebc4a8a65a28ebc0de872f40bba01181451e1317 100644 --- a/src_ocaml/grewpy.ml +++ b/src_ocaml/grewpy.ml @@ -74,14 +74,28 @@ let run_command request = (* ======================= load_grs ======================= *) | Some "load_grs" -> begin - match json |> member "filename" |> to_string_option with - | Some filename -> + let dict = json |> to_assoc in + match List.assoc_opt "filename" dict with + | Some `String filename -> let grs = Grs.load ~config filename in let index = Global.grs_add grs in - (* let json = Grs.to_json grs in *) let data = `Assoc [("index", `Int index)] in Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", data)]) - | None -> json_error "no 'filename' in 'load_grs' command" + | _ -> + match List.assoc_opt "str" dict with + | Some `String str -> + let grs = Grs.parse ~config str in + let index = Global.grs_add grs in + let data = `Assoc [("index", `Int index)] in + Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", data)]) + | _ -> + match List.assoc_opt "json" dict with + | Some data -> + let grs = json |> member "json" |> Grs.of_json ~config in + let index = Global.grs_add grs in + let data = `Assoc [("index", `Int index)] in + Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", data)]) + | _ -> json_error "fail in 'load_grs' command" end (* ======================= load_corpus ======================= *)