let texte f = let c = open_in f in let minuscule c = if c<=`Z` && c >= `A` then char_of_int( int_of_char c - int_of_char `A` + int_of_char `a`) else c in let rec foo s i = if i == string_length s then ("",[]) else let (mot,suite) = foo s (i+1) in if s.[i] == ` ` && mot != "" then ("",mot::suite) else if (s.[i] <= `Z` && s.[i] >= `A`) || (s.[i] <= `z` && s.[i] >= `a`) then (string_of_char (minuscule s.[i])^mot,suite) else (mot,suite) in let decoupe s = if s<>"" && s.[0] = `*` then failwith "" else let (a,b) = foo s 0 in if a = "" then b else a::b in let texte = ref [] in try while (let lin = (input_line c) in lin="" || lin.[0]<>`*`) do done; while(true) do texte := (decoupe (input_line c))::(!texte) done; [] with _ -> (it_list (fun x y -> y@x) [] !texte) ;; let txt = map_vect texte (init_vect 8 (fun x -> string_of_int x ^ ".txt")) ;; let intchar c = if c >= `a` && c <= `z` then int_of_char c - int_of_char `a` else failwith "n'existe pas" ;; let freq y = let t = make_vect 26 0 in let rec do_freq s i = if i < string_length s then t.(intchar s.[i])<-t.(intchar s.[i])+1 in let rec foo = function | [] -> () | a::q -> do_freq a 0; foo q in foo y ; t ;; let sum2 t = let rec foo i = if vect_length t = i then 0. else (float_of_int t.(i))**2.+.foo (i+1) in (foo 0)**0.5;; let normalize t = let tot = (sum2 t) in let res = make_vect (vect_length t) 0. in let rec foo i = if i < vect_length t then (res.(i) <- float_of_int t.(i) /. tot ; foo (i+1)) in foo 0 ; res ;; let dist a b = let rec foo i = if i = vect_length a then 0. else (a.(i)-.b.(i))**2.+.foo (i+1) in foo 0 ;; let e = map_vect (fun x -> normalize (freq x)) txt ;; for i = 0 to vect_length e - 2 do for j = i+1 to vect_length e - 1 do if (dist e.(i) e.(j))<0.13 then (print_int i ; print_string " <-> "; print_int j ; print_string " " ; print_float (dist e.(i) e.(j)); print_newline ()) done; done;; let make_trigramme t = let res = make_matrix 27 27 [] in let rec do_mot p1 p2 m i = let c = if i = string_length m then ` ` else (do_mot p2 (intchar m.[i]) m (i+1);m.[i]) in res.(p1).(p2) <-c::res.(p1).(p2) in let rec do_text = function | [] -> () | a::q -> do_mot 26 26 a 0; do_text q in do_text t ; map_vect (map_vect vect_of_list) res ;; let rec write_mot t a b = let r = random__int (vect_length t.(a).(b)) in let c = t.(a).(b).(r) in print_char c ; if c <> ` ` then write_mot t b (intchar c) ;; let rec write t n = if n > 0 then ( write_mot t 26 26; write t (n-1) ) else print_newline() ;; do_vect (fun x -> write (make_trigramme x) 7) txt;;