webInterface.ml
5.16 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
(*
* ENIAMmorphology, a morphological analyser and a guesser for Polish
* Copyright (C) 2016-2018 Wojciech Jaworski <wjaworski atSPAMfree mimuw dot edu dot pl>
* Copyright (C) 2016-2018 Institute of Computer Science Polish Academy of Sciences
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*)
open Printf
let logfile = open_out_gen [Open_wronly; Open_append; Open_creat] ((6*8+4)*8+4) "results/logi/morphology_analyze.log"
let log_query query =
(* let addr = try Sys.getenv "REMOTE_ADDR" with Not_found -> "" in *)
let tm = Unix.gmtime (Unix.time ()) in
(* Printf.fprintf logfile "host=%s time=%d.%02d.%02d %02d:%02d:%02d query=%s\n" addr (tm.Unix.tm_year+1900) (tm.Unix.tm_mon+1) tm.Unix.tm_mday tm.Unix.tm_hour tm.Unix.tm_min tm.Unix.tm_sec query *)
Printf.fprintf logfile "time=%d.%02d.%02d %02d:%02d:%02d query=%s\n" (tm.Unix.tm_year+1900) (tm.Unix.tm_mon+1) tm.Unix.tm_mday tm.Unix.tm_hour tm.Unix.tm_min tm.Unix.tm_sec query
let get_sock_addr host_name port =
let he =
try Unix.gethostbyname host_name
with Not_found -> failwith ("get_sock_addr: host " ^ host_name ^ " not found") in
let addr = he.Unix.h_addr_list in
Unix.ADDR_INET(addr.(0),port)
let process_query query =
let sock = get_sock_addr (*"wloczykij"*)"localhost" 5736 in
let ic,oc =
try Unix.open_connection sock
with e -> failwith ("server connection error: " ^ Printexc.to_string e) in
Printf.fprintf oc "%s\n\n%!" query;
let result,msg = (Marshal.from_channel ic : ENIAMinflexion.t list * string) in
Printf.fprintf oc "\n%!";
let _ = Unix.shutdown_connection ic in
ENIAMinflexion.html_of_interpretations (ENIAMinflexion.sort_results result) msg
let get_input () =
let r = ref [] in
(try
while true do
r := (input_line stdin) :: (!r)
done;
!r
with End_of_file -> !r)
let rec translate_input_rec buf i size query =
if i >= size then Buffer.contents buf else (
let c,i =
if String.get query i = '%' then
Scanf.sscanf (String.sub query (i+1) 2) "%x" (fun a -> Char.chr a), i+3 else
if String.get query i = '+' then ' ', i+1 else
String.get query i, i+1 in
Buffer.add_char buf c;
translate_input_rec buf i size query)
let translate_input query =
match query with
[query] ->
if String.sub query 0 6 = "text0=" then
let buf = Buffer.create (String.length query) in
translate_input_rec buf 6 (String.length query) query
else failwith "translate_input 1"
| _ -> failwith "translate_input 2"
(* let get_query_id () =
let filename = Filename.temp_file ~temp_dir:"results/web/" "page_" "" in
(* print_endline filename; *)
let n = String.length "results/web/" + String.length "page_" in
let id = String.sub filename n (String.length filename - n) in
(* print_endline id; *)
id *)
let generate_header () =
Printf.printf "Content-type: text/html\n";
Printf.printf "\n"
let generate_trailer () =
(*Printf.printf "</BODY>\n</HTML>\n"*)()
let page_header path =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<META HTTP-EQUIV=\"CONTENT-TYPE\" CONTENT=\"text/html; charset=utf8\">
<TITLE>ENIAMmorphology</TITLE>
<META HTTP-EQUIV=\"Content-Language\" CONTENT=\"pl\">
</head>
<body>
<center>
<h1>ENIAMmorphology: analizator morfologiczny i guessser dla języka polskiego</h1>
<h3>Podaj słowo:</h3>
<form method=POST action=\"" ^ path ^ "morphology.cgi\">
<p><input type=\"text\" name=\"text0\" value=\"\" size=\"100\"></p>
<p><input type=\"submit\" value=\"Analizuj\" size=\"60\"></p>
</form>"
let generate_webpage query content =
print_endline (page_header "");
printf " <h3>%s</h3>
%s
</center>
</body>
</html>" query content
let generate_error_message e =
Printf.printf
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<META HTTP-EQUIV=\"CONTENT-TYPE\" CONTENT=\"text/html; charset=utf8\">
<TITLE>ENIAMmorphology</TITLE>
<META HTTP-EQUIV=\"Content-Language\" CONTENT=\"pl\">
</head>
<body>
<center>
<h1>ENIAMmorphology</h1>
<h3>%s</h3>
</center>
</body>
</html>" e
let _ =
generate_header ();
(try
let query = get_input () in
let query = translate_input query in
log_query query;
(* let id = get_query_id () in *)
let content = process_query query in
generate_webpage query content
with
Failure e -> generate_error_message e
| e -> generate_error_message (Printexc.to_string e));
generate_trailer ()
(* uruchamianie serwera:
./morphology -m -p 5736
*)
(* testowanie z linii poleceń:
echo "text0=profesory" | ./morphology.cgi
echo "text0=miauczy" | ./morphology.cgi
*)