From c2bd6982e5ed845293a38ae600c239cd50924d76 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Thu, 24 Feb 2022 08:59:44 +0100 Subject: Update code, added tests --- motus/bin/motus.ml | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) (limited to 'motus/bin') diff --git a/motus/bin/motus.ml b/motus/bin/motus.ml index 0f2b17b..2ee058d 100644 --- a/motus/bin/motus.ml +++ b/motus/bin/motus.ml @@ -15,8 +15,7 @@ let format_filter : Format.formatter -> Criteria.t -> unit = (** Display the informations about the structure *) -let show_structure : - Format.formatter -> Persistence.t -> Criteria.t list -> unit = +let show_structure : Format.formatter -> Wordlist.t -> Criteria.t list -> unit = fun format data filters -> Format.fprintf format @@ -24,28 +23,27 @@ let show_structure : (Format.pp_print_list format_filter) filters; - Format.fprintf format "Got %d elements @\n" (Persistence.list_size data); + Format.fprintf format "Got %d elements @\n" (Wordlist.list_size data); Format.fprintf format "Frequencies : @[%a@]@\n" (Format.pp_print_list (fun f (k, v) -> Format.fprintf f "%c -> %d" k v)) - (Persistence.extract_freq data); + (Wordlist.extract_freq data); - if Persistence.list_size data < 20 + if Wordlist.list_size data < 20 then Format.fprintf format "Remaining words @[@;%a@]@\n" (Format.pp_print_list ~pp_sep:Format.pp_force_newline (fun f w -> Format.fprintf f "%s" w ) ) - (Persistence.words data); + (Wordlist.words data); Format.close_box () (** Get the initial list *) -let rec get_list : - in_channel -> Persistence.t -> Criteria.t list -> Persistence.t = +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 @@ -54,7 +52,7 @@ let rec get_list : match word with | None -> data | Some word -> - let data = Persistence.add_word filters data word in + let data = Wordlist.add_word filters data word in get_list channel data filters @@ -66,24 +64,23 @@ let create_new_rules word result = for i = 0 to max_length - 1 do match (String.get word i, String.get result i) with (* A space means that the letter is not present *) - | c, ' ' -> rules := Criteria.NotContain (c, None) :: !rules + | c, ' ' -> rules := Criteria.(add (NotContain (c, None))) !rules (* The same letter means that the we found the right caracter *) | c, c' when Char.equal c c' -> - rules := Criteria.Contain (c, Some i) :: !rules + rules := Criteria.(add (Contain (c, Some i)) !rules) (* Anything else, we got the letter, but at the wrong place *) | c, _ -> rules := - Criteria.Contain (c, None) - :: Criteria.NotContain (c, Some i) - :: !rules + Criteria.( + add (Contain (c, None)) (add (NotContain (c, Some i)) !rules)) done; !rules let rec run filters words = let () = show_structure Format.std_formatter words filters in - let freq = Persistence.extract_freq words in - let next, score = Persistence.pick_next_word words freq in + let freq = Wordlist.extract_freq words in + let next, score = Wordlist.pick_next_word words freq in let () = Format.fprintf @@ -97,7 +94,7 @@ let rec run filters words = (* if the input is empty, remove the word from the list and restart *) match String.equal String.empty input with - | true -> run filters (Persistence.remove_word words next) + | true -> run filters (Wordlist.remove_word words next) | false -> let new_rules = Criteria.merge_lists ~init:filters (create_new_rules next input) @@ -106,9 +103,9 @@ let rec run filters words = let words = List.fold_left - (Persistence.add_word new_rules) - (Persistence.empty_data ()) - (Persistence.words words) + (Wordlist.add_word new_rules) + (Wordlist.empty_data ()) + (Wordlist.words words) in run new_rules words @@ -137,8 +134,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 (Persistence.empty_data ()) initial_filter - in + let words = get_list words_channel (Wordlist.empty_data ()) initial_filter in close_in words_channel; run initial_filter words -- cgit v1.2.3