aboutsummaryrefslogtreecommitdiff
path: root/motus/bin/motus.ml
diff options
context:
space:
mode:
Diffstat (limited to 'motus/bin/motus.ml')
-rw-r--r--motus/bin/motus.ml45
1 files changed, 14 insertions, 31 deletions
diff --git a/motus/bin/motus.ml b/motus/bin/motus.ml
index 2ee058d..e32adca 100644
--- a/motus/bin/motus.ml
+++ b/motus/bin/motus.ml
@@ -25,12 +25,6 @@ let show_structure : Format.formatter -> Wordlist.t -> Criteria.t list -> unit =
Format.fprintf format "Got %d elements @\n" (Wordlist.list_size data);
- Format.fprintf
- format
- "Frequencies : @[<v>%a@]@\n"
- (Format.pp_print_list (fun f (k, v) -> Format.fprintf f "%c -> %d" k v))
- (Wordlist.extract_freq data);
-
if Wordlist.list_size data < 20
then
Format.fprintf
@@ -38,22 +32,20 @@ let show_structure : Format.formatter -> Wordlist.t -> Criteria.t list -> unit =
"Remaining words @[<v>@;%a@]@\n"
(Format.pp_print_list ~pp_sep:Format.pp_force_newline (fun f w ->
Format.fprintf f "%s" w ) )
- (Wordlist.words data);
+ (List.of_seq @@ Wordlist.words data);
Format.close_box ()
(** Get the initial list *)
-let rec get_list : in_channel -> Wordlist.t -> Criteria.t list -> Wordlist.t =
- fun channel data filters ->
- let word =
- try Some (String.lowercase_ascii (Stdlib.input_line channel)) with
+let get_list : in_channel -> Criteria.t list -> Wordlist.t =
+ fun channel filters ->
+ let read channel =
+ try Some (String.lowercase_ascii (Stdlib.input_line channel), channel) with
| End_of_file -> None
in
- match word with
- | None -> data
- | Some word ->
- let data = Wordlist.add_word filters data word in
- get_list channel data filters
+
+ let s = Seq.unfold read channel in
+ Wordlist.add_words filters s
(** Compare the proposed word and the result from the user in order to identify
@@ -79,15 +71,12 @@ let create_new_rules word result =
let rec run filters words =
let () = show_structure Format.std_formatter words filters in
- let freq = Wordlist.extract_freq words in
- let next, score = Wordlist.pick_next_word words freq in
+ let next =
+ Freq_analysis.analyse words |> Freq_analysis.pick_next_word words
+ in
let () =
- Format.fprintf
- Format.std_formatter
- "Next word will be : %s (%d)@\n"
- next
- score
+ Format.fprintf Format.std_formatter "Next word will be : %s@\n" next
in
let input = Stdlib.read_line () in
@@ -100,13 +89,7 @@ let rec run filters words =
Criteria.merge_lists ~init:filters (create_new_rules next input)
|> List.sort_uniq Stdlib.compare
in
-
- let words =
- List.fold_left
- (Wordlist.add_word new_rules)
- (Wordlist.empty_data ())
- (Wordlist.words words)
- in
+ let words = Wordlist.filter new_rules words in
run new_rules words
@@ -134,6 +117,6 @@ let () =
let initial_filter = Criteria.Lenght !length :: !rules in
let words_channel = open_in (List.hd !dict) in
- let words = get_list words_channel (Wordlist.empty_data ()) initial_filter in
+ let words = get_list words_channel initial_filter in
close_in words_channel;
run initial_filter words