(***********************************************************************) (* *) (* Corrector *) (* *) (* Damien Doligez and Francois Rouaix, INRIA Rocquencourt *) (* *) (* Copyright 1997 Institut National de Recherche en Informatique et *) (* Automatique. Distributed only by permission. *) (* *) (***********************************************************************) open Printf module Corrector = Correct.Make(struct type t = string type letter = char let length = String.length let get = String.get let implode l = String.concat "" (List.map (String.make 1) l) end) (Dict) open Corrector let costs = { tr = 80; del = 100; ins = 100; subst = (fun bogus good -> match bogus, good with | 'e', ('é'|'è'|'ê'|'ë') -> 50 | ('é'|'è'|'ê'|'ë'), ('e'|'é'|'è'|'ê'|'ë') -> 80 | 'c', 'ç' -> 50 | 'a', ('á'|'à'|'â') -> 50 | ('á'|'à'|'â'), ('a'|'á'|'à'|'â') -> 80 | 'i', ('î'|'ï') -> 50 | ('î'|'ï'), ('i'|'î'|'ï') -> 80 | lc,uc when uc = Char.uppercase lc -> 80 | uc,lc when uc = Char.uppercase lc -> 80 | _, _ -> 130); max = (fun w -> 18 * (String.length w - 1) + 100) } let pp = function | max_cost, [] -> printf "No suggestions under %d\n" max_cost | max_cost, l -> printf "Max cost %d\n" max_cost; List.iter (fun (s,n) -> printf "%s (cost %d)\n" s n) (Sort.list (fun (s,n) (s',n') -> n < n') l) let main () = let dict = ref None in Arg.parse [ "-dict", Arg.String (fun s -> dict := Some (Dict.load s)), "Dictionary" ] (fun s -> match !dict with None -> eprintf "No dictionary specified\n" | Some dict -> pp (Corrector.f dict costs s)) "Suggest correct spelling"; match !dict with None -> eprintf "No dictionary specified\n" | Some dict -> try while true do pp (Corrector.f dict costs (read_line())) done with End_of_file -> () let _ = Printexc.print main ()