Browse Source

clean code

master
zapashcanon 3 months ago
parent
commit
3333daea63
Signed by: zapashcanon GPG Key ID: 8981C3C62D1D28F1
  1. 13
      example/random_text.ml
  2. 33
      src/omg.ml

13
example/random_text.ml

@ -1,8 +1,7 @@
let _ =
if Array.length Sys.argv < 2 then
let () =
if Array.length Sys.argv <> 2 then
failwith (Format.sprintf "usage: %s <file>" Sys.argv.(0));
let file = Sys.argv.(1) in
let chan = open_in file in
let chan = open_in Sys.argv.(1) in
let lines = ref [] in
let keep_alpha s =
let to_keep = [ ' '; '\''; '-' ] in
@ -26,10 +25,8 @@ let _ =
with End_of_file -> close_in chan );
let lines = List.rev !lines in
let generator = Omg.init () in
let generator =
List.fold_left (fun generator el -> Omg.feed generator el) generator lines
in
let generator = List.fold_left Omg.feed generator lines in
for _ = 0 to 30 do
Format.fprintf Format.std_formatter "GEN: %s@."
Format.printf "GEN: %s@."
(Omg.generate_markov_text generator 50 (None, None) false)
done

33
src/omg.ml

@ -59,7 +59,7 @@ let feed generator msg =
add_key generator.forward_cache (w1, w2) w3;
add_key generator.backward_cache (w3, w2) w1 )
triples;
List.fold_left (fun generator el -> add_word generator el) generator splitted
List.fold_left add_word generator splitted
let select_seed generator seed_word backward =
let dir = if backward then -1 else 1 in
@ -67,10 +67,8 @@ let select_seed generator seed_word backward =
| None ->
let rec loop = function
| "\n", _ | _, "\n" ->
let seed =
if backward then 1 + Random.int (Hashtbl.length generator.words - 1)
else Random.int (Hashtbl.length generator.words - 1)
in
let seed = Random.int (Hashtbl.length generator.words - 1) in
let seed = if backward then 1 + seed else seed in
loop
( Hashtbl.find generator.words seed
, Hashtbl.find generator.words (seed + dir) )
@ -103,12 +101,11 @@ let generate_markov_text generator max_size seed backward =
let w1 = ref w1 in
let w2 = ref w2 in
let gen_words = ref [] in
let exception Stop in
( try
for _ = 0 to max_size do
gen_words := !w1 :: !gen_words;
let tbl =
try Hashtbl.find cache (!w1, !w2) with Not_found -> raise Stop
try Hashtbl.find cache (!w1, !w2) with Not_found -> raise Exit
in
let cache_n = Hashtbl.fold (fun _ v acc -> acc + v) tbl 0 in
let i = if cache_n = 0 then 0 else Random.int cache_n in
@ -122,18 +119,20 @@ let generate_markov_text generator max_size seed backward =
tbl 0
with
| exception Found s -> s
| _ -> raise Stop
| _ -> raise Exit
in
w1 := !w2;
w2 := new_word
done
with Stop -> () );
with Exit -> () );
if !w2 <> "\n" then gen_words := !w2 :: !gen_words;
let gen_words = List.filter (fun el -> el <> "\n") !gen_words in
let buff = Buffer.create 512 in
( match if backward then gen_words else List.rev gen_words with
| [] -> ()
| x :: s ->
Buffer.add_string buff x;
List.iter (fun el -> Buffer.add_string buff (" " ^ el)) s );
Buffer.contents buff
let gen_words =
(if backward then Fun.id else List.rev)
@@ List.filter (( <> ) "\n") !gen_words
in
let buf = Buffer.create 512 in
let fmt = Format.formatter_of_buffer buf in
Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt " ")
Format.pp_print_string fmt gen_words;
Buffer.contents buf

Loading…
Cancel
Save