concraft_test.ml 3.43 KB

(* let concraft_in, concraft_out, concraft_err = Unix.open_process_full "../../../.local/bin/concraft-pl tag ../concraft/nkjp-model-0.2.gz" [| |] *)
(*let concraft_in, concraft_out, concraft_err =
  Unix.open_process_full "concraft-pl tag ../concraft/nkjp-model-0.2.gz"
    [|"PATH=" ^ Sys.getenv "PATH"|]

let _ =
  print_endline "out";
  Printf.fprintf concraft_out "Ala ma kota.\n\n%!";
  print_endline "in";
  print_endline ("concraft error message: " ^ input_line concraft_err);
  ()*)

(**********************)

(*
Aby korzytać z concrafta trzeba najpierw postawić serwer wpisując z linii poleceń:
concraft-pl server --inmodel ../concraft/nkjp-model-0.2.gz
*)

let read_whole_channel c =
  let r = ref [] in
  try
    while true do
      r := (input_line c) :: !r
    done;
    !r
  with End_of_file -> List.rev (!r)

(* Gdy serwer jest już włączony na concraft_err trafia komunikat:
concraft-pl: bind: resource busy (Address already in use)
w przeciwnym przypadku się program wiesza się na czytaniu concraft_in
*)

(*let _ =
  print_endline "Starting concraft server 1";
  let concraft_in, concraft_out, concraft_err =
    Unix.open_process_full "concraft-pl server --inmodel ../concraft/nkjp-model-0.2.gz"
      [|"PATH=" ^ Sys.getenv "PATH"; "LANG=en_GB.UTF-8"|] in
  (* let err_msg = String.concat "\n" (read_whole_channel concraft_err) in
  let result = read_whole_channel concraft_in in *)
  print_endline "Starting concraft server 2";
  print_endline (input_line concraft_err);
  print_endline "Starting concraft server 3";
  print_endline (input_line concraft_err);
  print_endline "Starting concraft server 3";
  (* print_endline err_msg;
  print_endline "Starting concraft server 3";
  print_endline (String.concat "\n" result);
  print_endline "Starting concraft server 4"; *)
  ()*)

let rec process_concraft_result orth lemma interp others rev = function
    [] -> List.rev ((orth,(lemma,interp) :: others) :: rev)
  | "" :: l -> process_concraft_result orth lemma interp others rev l
  | line :: l ->
      (match Xstring.split_delim "\t" line with
        [orth2;s] when s = "none" || s = "space" ->
           if orth = "" then process_concraft_result orth2 lemma interp others rev l
           else process_concraft_result orth2 "" "" [] ((orth,(lemma,interp) :: others) :: rev) l
      | ["";lemma2;interp2] -> process_concraft_result orth lemma interp ((lemma2,interp2) :: others) rev l
      | ["";lemma;interp;"disamb"] -> process_concraft_result orth lemma interp others rev l
      | _ -> failwith ("process_concraft_result: " ^ line))

let concraft_parse s =
  let concraft_in, concraft_out, concraft_err =
    Unix.open_process_full ("echo \"" ^ s ^ "\" | concraft-pl client")
      [|"PATH=" ^ Sys.getenv "PATH"; "LANG=en_GB.UTF-8"|] in
  let err_msg = String.concat "\n" (read_whole_channel concraft_err) in
  let result = read_whole_channel concraft_in in
  if err_msg <> "" then failwith err_msg else
  process_concraft_result "" "" "" [] [] result

let print_parsed_tokens l =
  Xlist.iter l (fun (orth,l) ->
    if l = [] then failwith "print_parsed_tokens" else
    let lemma,interp = List.hd l in
    print_endline (orth ^ "\t" ^ lemma ^ "\t" ^ interp))

let _ =
  print_parsed_tokens (concraft_parse "Ala ma kota.");
  print_parsed_tokens (concraft_parse "Szpak frunie.");
  print_parsed_tokens (concraft_parse "Miałem miał.");
  print_parsed_tokens (concraft_parse "Kiedyś miałem kota.");
  print_parsed_tokens (concraft_parse "Kiadyś kupiłem kota.");
  ()