server.ml 3.58 KB
(*
 *  ENIAM: Categorial Syntactic-Semantic Parser for Polish
 *  Copyright (C) 2016 Wojciech Jaworski <wjaworski atSPAMfree mimuw dot edu dot pl>
 *  Copyright (C) 2016 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 ExecTypes

let logfile = open_out_gen [Open_wronly; Open_append; Open_creat] ((6*8+4)*8+4) "results/queries.log"

let get_sock_addr host_name port =
  let he = Unix.gethostbyname host_name in
  let addr = he.Unix.h_addr_list in
  Unix.ADDR_INET(addr.(0),port)

let rec clean_result_sentence = function
    SemSentence result -> SemSentence {result with disamb=[| |]; sem=[| |]; sem2=[| |]; sem3=LCGtypes.Dot}
  | AltSentence l ->
      let l = Xlist.rev_map l (fun (mode,sentence) ->
        mode, clean_result_sentence sentence) in
      AltSentence(List.rev l)
  | t -> t

let rec clean_result_paragraph = function
    RawParagraph s -> RawParagraph s
  | StructParagraph sentences ->
      let sentences = Xlist.rev_map sentences (fun p ->
        let sentence = clean_result_sentence p.psentence in
        {p with psentence=sentence}) in
      StructParagraph(List.rev sentences)
  | AltParagraph l ->
      let l = Xlist.rev_map l (fun (mode,paragraph) ->
        mode, clean_result_paragraph paragraph) in
      AltParagraph(List.rev l)

let rec clean_result_text = function
    RawText s -> RawText s
  | StructText(paragraphs,tokens) ->
      let paragraphs = Xlist.rev_map paragraphs (fun paragraph ->
        clean_result_paragraph paragraph) in
      StructText(List.rev paragraphs, tokens)
  | AltText l -> AltText(Xlist.map l (fun (mode,text) ->
       mode, clean_result_text text))

let parse query =
  let max_n = 10 in
  let ic,oc = Unix.open_connection (get_sock_addr Paths.pre_host Paths.pre_port) in
  let result = Exec.process_query ic oc 30. false "x" (PreTypes.RawText query) max_n in
  Printf.fprintf oc "\n%!";
  let _ = Unix.shutdown_connection ic in
  {result with
     pre_text = RawText "";
     parsed_text = RawText "";
     selected_sent_text = RawText "";
     semantic_text = RawText "";
     selected_semantic_text = clean_result_text result.selected_semantic_text}

let rec main_loop in_chan out_chan =
  (* let query = input_line in_chan in *)
  let query = (Marshal.from_channel in_chan : PreTypes.text) in
  let query = match query with PreTypes.RawText q -> q | _ -> failwith "bad query format" in
  (* Printf.fprintf logfile "raw query: '%s'\n" query; *)
  if query = "" then Marshal.to_channel out_chan {Exec.empty_result with msg="Empty query"} [] else
  (try
    let result : ExecTypes.result = parse query in
    Exec.print_result logfile result;
    Marshal.to_channel out_chan result []
  with e ->
    Printf.fprintf logfile "query: %s\nerror_other: %s\n%!" query (Printexc.to_string e);
    Marshal.to_channel out_chan {Exec.empty_result with msg=Printexc.to_string e} []);
  (* flush out_chan *)
  ()


let sockaddr = Unix.ADDR_INET(Unix.inet_addr_any,Paths.server_port)

let _ =
  Unix.establish_server main_loop sockaddr