diff --git a/src_ocaml/grewpy.ml b/src_ocaml/grewpy.ml index 93ae2e07ad5ab2bae5ffe3e41326c1ff3fff16e6..0e9e97021abb41f5b15515c62d1f53c0d41869f3 100644 --- a/src_ocaml/grewpy.ml +++ b/src_ocaml/grewpy.ml @@ -2,7 +2,7 @@ open Printf open Printf open Yojson.Basic.Util -open Conll +open Conllx open Libgrew open Grewpy_utils @@ -60,16 +60,15 @@ let run_command request = | Some "load_corpus" -> begin let directory_opt = json |> member "directory" |> to_string_option in - let domain_opt = json |> member "domain" |> to_string_option |> (CCOpt.map Domain.load) in let files = json |> member "files" |> to_list |> filter_string in let complete_files = match directory_opt with | None -> files | Some dir -> List.map (fun file -> Filename.concat dir file) files in - let conll_corpus = Conll_corpus.load_list complete_files in + let conll_corpus = Conllx_corpus.load_list ~config complete_files in - let index = Global.corpus_add (conll_corpus, domain_opt) in + let index = Global.corpus_add conll_corpus in let data = `Assoc [("index", `Int index)] in Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", data)]) end @@ -79,18 +78,17 @@ let run_command request = begin try let corpus = json |> member "corpus_index" |> to_int |> Global.corpus_get in - let graphs = corpus.Global.graphs in let position = match (json |> member "sent_id" |> to_string_option, json |> member "position" |> to_int_option) with | (Some sent_id, _) -> begin - match CCArray.find_idx (fun (id,_) -> id=sent_id) corpus.Global.graphs with + match CCArray.find_idx (fun (id,_) -> id=sent_id) corpus with | Some (i,_) -> i | None -> raise (Error (sprintf "sent_id '%s' not found in corpus" sent_id)) end | (_, Some pos) -> pos | (None, None) -> raise (Error "neither sent_id or pos in the request") in - let graph = snd graphs.(position) in + let graph = snd corpus.(position) in let data = Graph.to_json_python ~config graph in Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", data)]) with @@ -102,8 +100,7 @@ let run_command request = begin try let corpus = json |> member "corpus_index" |> to_int |> Global.corpus_get in - let graphs = corpus.Global.graphs in - Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", `Int (Array.length graphs))]) + Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", `Int (Array.length corpus))]) with | Error msg -> json_error msg end @@ -113,7 +110,7 @@ let run_command request = begin try let corpus = json |> member "corpus_index" |> to_int |> Global.corpus_get in - let sent_id = Array.to_list (Array.map (fun (id,_) -> `String id) corpus.Global.graphs) in + let sent_id = Array.to_list (Array.map (fun (id,_) -> `String id) corpus) in Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", `List (sent_id))]) with | Error msg -> json_error msg @@ -138,7 +135,7 @@ let run_command request = ] ) matching_list ) @ acc - ) [] corpus.Global.graphs in + ) [] corpus in Yojson.Basic.to_string (`Assoc [ ("status", `String "OK"); @@ -161,7 +158,7 @@ let run_command request = (fun acc (id,graph) -> let matching_list = Graph.search_pattern ~config pattern graph in (List.length matching_list) + acc - ) 0 corpus.Global.graphs in + ) 0 corpus in Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", `Int count)]) with diff --git a/src_ocaml/grewpy_utils.ml b/src_ocaml/grewpy_utils.ml index 68d8092194e3923fbeb4dd40a8a33c6b94c18d0a..d3b44875bd789d6fb72ed9f996513eb24de7539e 100644 --- a/src_ocaml/grewpy_utils.ml +++ b/src_ocaml/grewpy_utils.ml @@ -52,20 +52,19 @@ module Global = struct let grs_get index = Int_map.find index !grs_map (* the [corpus_map] stores corpus loaded by Python *) - type corpus = { - dom: Domain.t option; - graphs: (string * Graph.t) array - } + type corpus = (string * Graph.t) array let (corpus_map: corpus Int_map.t ref) = ref Int_map.empty let corpus_max = ref 0 - let corpus_add (conll_corpus, domain) = + + let corpus_add conll_corpus = incr corpus_max; - let corpus = { - dom=domain; - graphs = Array.map (fun (id,conll) -> (id, Graph.of_conll ~config ?domain conll)) conll_corpus - } in + let corpus = + Array.map + (fun (id,conll) -> (id, conll |> Conllx.to_json |> Graph.of_json)) + (Conllx_corpus.get_data conll_corpus) in corpus_map := Int_map.add !corpus_max corpus !corpus_map; !corpus_max + let corpus_get index = Int_map.find index !corpus_map end @@ -73,7 +72,7 @@ end module Debug = struct let get_time () = let time = Unix.localtime (Unix.time ()) in - sprintf "%02d/%02d/%04d %02d:%02d:%02d" time.Unix.tm_mday (time.Unix.tm_mon+1) (time.Unix.tm_year+1900) time.Unix.tm_hour time.Unix.tm_min time.Unix.tm_sec + sprintf "%02d/%02d/%04d %02d:%02d:%02d" time.Unix.tm_mday (time.Unix.tm_mon+1) (time.Unix.tm_year+1900) time.Unix.tm_hour time.Unix.tm_min time.Unix.tm_sec let log_ msg = if !Global.debug @@ -147,13 +146,13 @@ module Periodic = struct (** execute periodically the function [fct ()] in a new thread. [delay] is the number of seconds between two calls *) let rec start delay fct = - ignore ( - Thread.create - (fun () -> - Unix.sleep delay; (* wait for the delay*) - fct (); (* run the function *) - start delay fct (* start a new thread for next execution *) - ) () - ) + ignore ( + Thread.create + (fun () -> + Unix.sleep delay; (* wait for the delay*) + fct (); (* run the function *) + start delay fct (* start a new thread for next execution *) + ) () + ) end (* module Periodic *)