diff options
Diffstat (limited to 'motus/lib/entropy.ml')
-rw-r--r-- | motus/lib/entropy.ml | 48 |
1 files changed, 38 insertions, 10 deletions
diff --git a/motus/lib/entropy.ml b/motus/lib/entropy.ml index 5b86a9d..043d0c8 100644 --- a/motus/lib/entropy.ml +++ b/motus/lib/entropy.ml @@ -1,5 +1,34 @@ +open StdLabels + type t = float * string +module CharSet = Set.Make (Char) + +let compare_words : string -> ref:string * CharSet.t -> Validity.t array option + = + fun w1 ~ref -> + let wordRef = fst ref in + let l1 = String.length w1 in + if l1 <> String.length wordRef + then None + else + let result = + Array.init l1 ~f:(fun i -> + let c1 = String.get w1 i + and c2 = String.get wordRef i in + + let state = + if Char.equal c1 c2 + then Validity.Wellplaced + else if CharSet.mem c1 (snd ref) + then Validity.Misplaced + else Validity.Missing + in + state ) + in + Some result + + let get_entropy max_element words_number arr = let entropy = ref 0. in for idx = 0 to max_element - 1 do @@ -12,11 +41,12 @@ let get_entropy max_element words_number arr = entropy -let analyse : int -> Wordlist.t -> t = - fun base words -> +let analyse : int -> catalog:Wordlist.t -> Wordlist.t -> t = + fun base ~catalog words -> let max_element = Float.to_int @@ (Validity.elements ** Float.of_int base) in - let words_number = Float.of_int (Wordlist.list_size words) in + let words_number = Float.of_int (Wordlist.list_size catalog) in + let arr = Bigarray.Array1.create Bigarray.Int Bigarray.C_layout max_element in match Wordlist.pick words with | None -> (0., "") | Some v -> @@ -25,15 +55,13 @@ let analyse : int -> Wordlist.t -> t = (fun (score, word) word_ref -> (* Reinitialize the array (we use the same in the successive iterations *) - let set_ref = String.to_seq word_ref |> Validity.CharSet.of_seq in + let set_ref = String.to_seq word_ref |> CharSet.of_seq in - let arr = - Bigarray.Array1.create Bigarray.Int Bigarray.C_layout max_element - in + Bigarray.Array1.fill arr 0; Seq.iter (fun w2 -> - let result = Validity.compare_words ~ref:(word_ref, set_ref) w2 in + let result = compare_words ~ref:(word_ref, set_ref) w2 in match result with | None -> () | Some r -> @@ -51,5 +79,5 @@ let analyse : int -> Wordlist.t -> t = Printf.printf "Entropy for selecting %s : %.2f\n" word_ref !entropy; (!entropy, word_ref) ) else (score, word) ) - (0., v) - (Wordlist.words words) + (-0., v) + (Wordlist.words catalog) |