regression.ml 13.4 KB
Newer Older
huet's avatar
huet committed
1 2 3 4 5 6
(**************************************************************************)
(*                                                                        *)
(*                     The Sanskrit Heritage Platform                     *)
(*                                                                        *)
(*                        Pawan Goyal & Gérard Huet                       *)
(*                                                                        *)
7
(* ©2018 Institut National de Recherche en Informatique et en Automatique *)
huet's avatar
huet committed
8 9
(**************************************************************************)

10
(* Regression analysis - Legacy *)
huet's avatar
huet committed
11 12 13 14 15 16 17 18 19

(*i module Regression = struct i*)

(* Reads from stdin a previous regression file.
   For every line, reads in parsing parameters,
   parses with the current reader, prints a new trace file. *)

open Encode; (* [code_string] *)
open Constraints; (* [extract truncate_groups] *)
20
open Rank; (* Lex [morpho parse_solution parse_metadata segment_all complete] *)
huet's avatar
huet committed
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

module Prel = struct
 value prelude () = ()
;
 end (* Prel *)
;

(* TODO : Validate mode ought to store these parameters in metadata line *)
value topic = None
;

(* Adapted from Parser *)
(* Parsing projections stream in tagging mode *)
open Bank_lexer; 
module Gram = Camlp4.PreCast.MakeGram Bank_lexer 
;
open Bank_lexer.Token
;

value projs    = Gram.Entry.mk "projs"
and lproj      = Gram.Entry.mk "lproj"
and proj       = Gram.Entry.mk "proj"
and solution   = Gram.Entry.mk "solution"
and modec      = Gram.Entry.mk "modec"
and modes      = Gram.Entry.mk "modes"
and mode_sent  = Gram.Entry.mk "mode_sent"
and mode_trans = Gram.Entry.mk "mode_trans"
and quad       = Gram.Entry.mk "quad"
and max_sol    = Gram.Entry.mk "max_sol"
and sentence   = Gram.Entry.mk "sentence"
and out_phases = Gram.Entry.mk "out_phases"
and out_phase  = Gram.Entry.mk "out_phase"
and reg_metadata  = Gram.Entry.mk "reg_metadata"
;
(* A stream of projections is encoded under the form [1,2|2,3|...] *)
(* Extends the Parser grammar in specifying the validation format. *)
EXTEND Gram
  projs:
    [ [ l = lproj; `EOI -> l
      | lproj -> failwith "Wrong projections parsing\n"
    ] ];
  lproj:
    [ [ l = LIST0 proj SEP "|" -> l ] ];
  proj:
    [ [ n = INT; ","; m = INT -> (int_of_string n,int_of_string m) ] ];
  solution:
    [ [ mc = modec; ms = modes; mst = mode_sent; mt = mode_trans; s = sentence; sol = max_sol; o = out_phases -> 
        (mc,ms,mst,mt,s,sol,o) ] ];
  reg_metadata:
    [ [ v = modec; f = modes; n = sentence -> (v,f,n) ] ] ;
  modec:
    [ [ "["; t = TEXT; "]" -> t ] ];
  modes:
    [ [ "<"; t = TEXT; ">" -> t ] ];
  mode_sent:
    [ [ "|"; t = TEXT; "|" -> t ] ];
  mode_trans:
    [ [ "#"; t=TEXT; "#" -> t ] ];
  sentence:
    [ [ "("; t = TEXT; ")" -> t ] ];
  quad:
    [ [ "["; k = INT; ","; l = INT; ","; m = INT; "," ; n = INT; "]" -> 
        (int_of_string k,int_of_string l,int_of_string m,int_of_string n) ] ];
  max_sol:
    [ [ "["; k = INT; "]" -> (int_of_string k) ] ];
  out_phases:
    [ [ c = LIST0 out_phase SEP "&" -> c ] ];
  out_phase:
    [ [ "$"; t = TEXT; "$" -> t ] ];
END
;
value parse_fail s loc e = do
  { Format.eprintf "Wrong input: %s\n, at location %a:@." s Loc.print loc 
  ; raise e
  } 
;
value parse_phase s =
  try Gram.parse_string out_phases Loc.ghost s with
  [ Loc.Exc_located loc e -> parse_fail s loc e
  ]
;
value parse_metadata s =
  try Gram.parse_string reg_metadata Loc.ghost s with
  [ Loc.Exc_located loc e -> parse_fail s loc e
  ]
;
value parse_proj s =
  try Gram.parse_string projs Loc.ghost s with
  [ Loc.Exc_located loc e -> parse_fail s loc e
  ]
;
value parse_solution s =
  try Gram.parse_string solution Loc.ghost (String.sub s 0 ((String.length s)-1))
  with
  [ Loc.Exc_located loc e -> parse_fail s loc e
  ]
;
value check_tags current_sol_string tagging = 
  let pos = (String.length current_sol_string)-1 in
  let oc = parse_phase (String.sub current_sol_string 0 pos) in
  oc = tagging
;
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
(* OBS Used to be in Lexer - to be adapted 
value return_tagging output projs = 
  let get_tags phase rword projs = (* adapted from [print_proj] *)
     let form = mirror rword in  
     match tags_of phase form with
     [ Atomic polytag -> match projs with 
           [ [] -> failwith "Projection missing"
           | [ (n,m) :: rest ] -> 
              let gen = generative phase in
              let (delta,tags) = project n polytag in
              let tagging = [ project m tags ] in 
              let entry = get_morph gen phase form (delta,tagging) in
              (rest, lex_cat phase, entry)
           ]
     | _ -> failwith "Not implemented yet" (*i TODO for Regression 
         [ (projs, lex_cat Pv, (form, lex_cat Pv, Preverbs_list prevs, []))] i*)
     ] in 
  let rec taggings accu projs = fun
     [ [] -> match projs with 
             [ [] -> accu
             | _ -> failwith "Too many projections"
             ]
     | [ (phase,rword,_) :: rest ] -> (* sandhi ignored *)
          let (new_projs,phase,tags) = get_tags phase rword projs in
          taggings [ tags :: accu ] new_projs rest 
     ] in 
  return_morph (List.rev (taggings [] projs output))
;
value record_tagging unsandhied mode_sent mode_trans all sentence output proj = 
  let report = output_string Control.out_chan.val in
  let print_proj1 phase rword proj prevs = do 
  (* adapted from [print_proj] *)
  { report "${"
  ; let form = mirror rword in do 
    { report (decode form)
    ; let res = match proj with 
           [ [] -> failwith "Projection missing"
           | [ (n,m) :: rest ] -> 
              let gen = generative phase in
              let polytag = extract_lemma phase form in
              let (delta,tags) = project n polytag in 
              let tagging = [ project m tags ] in do 
                { report ":"
                ; report (string_of_phase phase ^ "")
                ; Morpho_out.report_morph gen form (delta,tagging) 
                ; (rest,[]) (* returns the rest of projections stream *)
                }
           ] in 
      do { report "}$&"; res }
    }
  } in do
  { report (if Control.full.val then "[{C}] " else "[{S}] ")
  ; report (if unsandhied then "<{F}> " else "<{T}> ")
  ; report (if mode_sent then "|{Sent}| " else "|{Word}| ")
  ; report ("#{" ^ mode_trans ^ "}# ")
  ; report ("({" ^ sentence ^ "})")
  ; report (" [" ^ (string_of_int all) ^ "] ")
  ; let rec pp (proj,prevs) = fun
    [ [] -> match proj with 
            [ [] -> () (* finished, projections exhausted *)
            | _ -> failwith "Too many projections"
            ]
    | [ (phase,rword,_) :: rest ] -> (* sandhi ignored *)
        let proj_prevs = print_proj1 phase rword proj prevs in
        pp proj_prevs rest 
    ] in pp (proj,[]) output
  ; report "\n"
  ; close_out Report_chan.chan.val
  }
;
OBS *)
huet's avatar
huet committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
value look_up_tags solution output tagging sol = 
  let proj = List.fold_left extract "" sol in
  let p = parse_proj proj in 
  let current_sol_string = 
     Lex.return_tagging (List.rev output) (List.rev p) in 
  if check_tags current_sol_string tagging then Some solution 
  else None
;
value search_bucket solution output tagging (p,b_p) = 
  let watch_verify max = watch_rec 0
    where rec watch_rec n =
      if n=max then None 
      else match (look_up_tags solution output tagging (List.nth b_p n))
           with [ None -> watch_rec (n+1)
                | s -> s
                ] in
  watch_verify (List.length b_p)
;
value find_proj solution output tagging sorted_groups = 
  let (top_groups, _) = truncate_groups sorted_groups in
  let watch_verify max = watch_rec 0
    where rec watch_rec n =
      if n=max then None
      else let gr = List.nth top_groups n in
           match search_bucket solution output tagging gr with
           [ None -> watch_rec (n+1)
           | s -> s 
           ] in
  watch_verify (List.length top_groups)
; 
value analyse tagging (solution,output) = 
  let tagger = Lex.extract_lemma in 
  let groups = make_groups tagger output in
  let sorted_groups = sort_flatten groups in 
  find_proj solution output tagging sorted_groups 
;

(* [analyse_results] will look for a solution consistent with taggings;
   If so will return Some(n,ind,kept,max) as given to [print_output] 
   in mode Validate, otherwise returns None. *)
value analyse_results limit taggings = fun
  [ [] -> None
  | best_sols -> 
    let kept = List.length best_sols
    and max = match limit with
              [ Some m -> m
              | None -> Web.truncation
              ] in 
    let watch_verify maxim = watch_rec 0
	where rec watch_rec n = 
          if n = maxim then None
          else match (analyse taggings (List.nth best_sols n)) with
               [ None -> watch_rec (n+1)
               | Some sol_number -> Some max
	       ] in
    watch_verify kept
  ]
;
value verify_sentence filter_mode us topic sentence encode taggings =
  let chunker = if us (* sandhi undone *) then Sanskrit.read_raw_sanskrit 
                else (* blanks non-significant *) Sanskrit.read_sanskrit in
  let chunks = chunker encode sentence (* normalisation here *) in 
  let all_chunks = match topic with
       [ Some topic -> chunks @ [ code_string topic ]
       | None -> chunks
       ] in
   try segment_all filter_mode all_chunks [] with
      [ Solutions limit revsols saved -> 
         let sols = List.rev revsols in 
         analyse_results limit taggings sols
      ]
;
value pdiff sol modec modes mode_sent mode_trans sentence psol tagging cho chd = 
  let report = output_string cho 
  and prdiff = output_string chd 
  and modes_report = "[{" ^ modec ^ "}] <{" ^ modes ^ "}> |{" 
                     ^ mode_sent ^ "}| #{" ^ mode_trans ^ "}# " in do
  { report (modes_report ^ "({" ^ sentence ^ "}) ")
  ; match sol with
    [ Some max -> do  
       { report ("[" ^ (string_of_int max) ^ "]")
       ; match psol with
	 [ 0 -> 
             prdiff (sentence ^ " " ^ modec ^ " " ^ modes ^ " [parses now]\n")
	 | max1 -> let diff = (max1-max) in
	           match diff with
                   [ 0 -> ()
	           | d -> prdiff (sentence ^ " " ^ modec ^ " " ^ modes ^ 
                                  " changes [" ^ (string_of_int d) ^ "]\n")
	           ]
	 ]
       }
    | None -> do
       { report ("[0]")
       ; match psol with
	 [ 0 -> () (* It didn't parse before, so no need to report *)
	 | _ -> 
           prdiff (sentence ^ " " ^ modec ^ " " ^ modes ^ " [does not parse]\n")
	 ]
       }
    ]
  ; report (" " ^ print_tag tagging ^ "\n")
      where rec print_tag = fun
      [ [ a :: rest ] -> "${" ^ a ^ "}$&" ^ (print_tag rest)
      | [] -> ""
      ]
  }
;
value regression s cho chd =  
  let (mc,ms,mst,mt,sc,solc,oc) = parse_solution s in
  let _ = complete.val:= (mc = "C") 
  and _ = iterate.val:= (mst = "Sent")
  and us = (ms = "F") in
  let solr = verify_sentence True us topic sc (switch_code mt) oc in 
  pdiff solr mc ms mst mt sc solc oc cho chd
;

value get_metadata input_info = 
 let (_, filename, _) = parse_metadata input_info 
 and version = Date.version_id in
 "[{" ^ version ^ "}] <{" ^ filename ^ "}> ({" ^ Version.version_date ^ "})"
;

value main_loop ic = 
  let use_metadata = input_line ic 
  and input_info = input_line ic 
  and version = Date.version_id 
  and date = Date.date_iso in
  let (old_version,filename,old_date) = parse_metadata input_info in
  let cho = open_out_gen [ Open_wronly; Open_trunc; Open_creat; Open_text ] 
            0o777 (Web.var_dir ^ "/" ^ filename ^ "-" ^ 
            version ^ "-" ^ date ^ ".txt") 
	and chd = open_out_gen [ Open_wronly; Open_trunc; Open_creat; Open_text ]
            0o777 (Web.var_dir ^ "/diff-" ^ filename ^ ".txt") in 
  let report_meta = output_string cho 
  and report_version = output_string chd in do
  { report_meta (use_metadata ^ "\n" ^ (get_metadata input_info) ^ "\n")
  ; let diff_meta = "diff: file = " ^ filename ^ " from Version " ^ old_version 
        ^ "." ^ old_date ^ " to " ^ version ^ "." ^ date ^ "\n" in
    report_version diff_meta
  ; try read_from_ic ic
        where rec read_from_ic ic = 
          let s = input_line ic in 
          do { regression s cho chd; read_from_ic ic }
    with [ End_of_file -> do { close_out cho; close_out chd } ] 
  }
;

(* Now regression reads on stdin - no need of unsafe file opening *)
try main_loop stdin with
  [ Sys_error m -> print_string ("Sys_error " ^ m) ]
;

(* Regression reads from stdin, first two metadata lines,
then for each line extracts parameters: mode (S or C),  sandhied or not (T or F),
sentence or word (Sent or Word), then the transliteration code (VH etc),
then the sentence as entered, then the quadruple (n,ind,kept,max), then the 
taggings previously computed in mode Validate.

It then calls: [verify_sentence] with appropriate parameters, 
which will either fail (returning None), or succeed with Some(n',ind',kept',max')
Finally it writes a new version of the input in local directory in file 
   [filename-Version.version-Version.version_date.txt]
where filename is a parameter read in the metadata second line.
It also prints a diff message in file [Web.var_dir/diff-filename.txt].
*)
(* Format of metada information:
1st line gives metadata about the corpus (unalysed)
2nd line gives global parameters [version, filename, version_date].

Example of current input format:
[
%%% Corpus from regression.txt %%% 
[{263}] <{regression}> ({2012-04-04})
[{S}] <{T}> |{Sent}| #{VH}# ({devaa~nch.r.noti}) [1] ${devaan:Noun2{ acc. pl. m. }[deva]}$&${z.r.noti:Root{ pr. [5] ac. sg. 3 }[zru]}$&
[{C}] <{T}> |{Sent}| #{VH}# ({devaa~nch.r.noti}) [1] ${devaan:Noun{ acc. pl. m. }[deva]}$&${z.r.noti:Root{ pr. [5] ac. sg. 3 }[zru]}$&
] *)

(*i end; i*)