From 84b8439aa90f1465d05dcba936a25eaf96f914a0 Mon Sep 17 00:00:00 2001
From: Sébastien Dailly <sebastien@dailly.me>
Date: Sat, 26 Feb 2022 13:42:32 +0100
Subject: Use the whole dictionnary when searching for a word for a faster
 exploration

---
 motus/lib/entropy.ml   | 48 ++++++++++++++++++++++++++++++++++++++----------
 motus/lib/validity.ml  | 26 --------------------------
 motus/lib/validity.mli |  4 ----
 3 files changed, 38 insertions(+), 40 deletions(-)

(limited to 'motus/lib')

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)
diff --git a/motus/lib/validity.ml b/motus/lib/validity.ml
index 0fdc40c..3964e0b 100644
--- a/motus/lib/validity.ml
+++ b/motus/lib/validity.ml
@@ -57,32 +57,6 @@ let sequence : int -> t array Seq.t =
     0
 
 
-module CharSet = Set.Make (Char)
-
-let compare_words : string -> ref:string * CharSet.t -> 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 Wellplaced
-            else if CharSet.mem c2 (snd ref)
-            then Misplaced
-            else Missing
-          in
-          state )
-    in
-    Some result
-
-
 let to_criteria : char -> int -> t -> Criteria.t list -> Criteria.t list =
  fun c i t acc ->
   match t with
diff --git a/motus/lib/validity.mli b/motus/lib/validity.mli
index a3d8ae3..3a59775 100644
--- a/motus/lib/validity.mli
+++ b/motus/lib/validity.mli
@@ -1,5 +1,3 @@
-module CharSet : Set.S with type elt = char
-
 type t =
   | Wellplaced
   | Misplaced
@@ -16,8 +14,6 @@ val index_of_result : t array -> int
 
 val index_to_result : base:int -> int -> t array
 
-val compare_words : string -> ref:string * CharSet.t -> t array option
-
 val to_criteria : char -> int -> t -> Criteria.t list -> Criteria.t list
 
 val to_criterias : string -> t array -> Criteria.t list
-- 
cgit v1.2.3