concraft_test.ml
3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(* 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.");
()