|
|
@ -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 |
|
|
|