wordlist_0.1.3_fad16afc/src/wordlist.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
with Ada.Numerics.Discrete_Random;
with Ada.Text_IO; use Ada.Text_IO;

with Resources;

with TOML.File_IO;

with Wordlist_Config;

package body Wordlist is

   package Intrnd is new Ada.Numerics.Discrete_Random (Integer);

   Generator : Intrnd.Generator;

   ---------
   -- Log --
   ---------

   procedure Log (Text : String) is
   begin
      if Wordlist_Config.Logging then
         Put_Line (Text);
      end if;
   end Log;

   package My_Resources is new Resources
     (Crate_Name => Wordlist_Config.Crate_Name);

   Words : Word_Set;

   ---------------
   -- All_Words --
   ---------------

   function All_Words return Word_Set is (Words);

   -----------------
   -- Random_Word --
   -----------------

   function Random_Word (This : Word_Vector) return String is
   begin
      return This
        (Intrnd.Random
           (Generator,
            This.First_Index,
            This.Last_Index));
   end Random_Word;

   ---------------
   -- To_Vector --
   ---------------

   function To_Vector (This : Word_Set'Class) return Word_Vector is
   begin
      return Result : Word_Vector do
         for Word of This loop
            Result.Append (Word);
         end loop;
      end return;
   end To_Vector;

   -----------------
   -- With_Length --
   -----------------

   function With_Length (Length : Positive) return Word_Set is
   begin
      return Result : Word_Set do
         for Word of Words loop
            if Word'Length = Length then
               Result.Include (Word);
            end if;
         end loop;
      end return;
   end With_Length;

begin
   Intrnd.Reset (Generator);

   Log ("Loading words from " & My_Resources.Resource_Path & "wordlist.json");

   declare
      Table : constant TOML.TOML_Value :=
                TOML.File_IO.Load_File
                  (My_Resources.Resource_Path
                   & AAA.Strings.To_Lower_Case (Wordlist_Config.Wordset'Image)
                   & ".toml").Value;

      List  : constant TOML.TOML_Value := Table.Get ("wordlist");
   begin
      for I in 1 .. List.Length loop
         Words.Include (List.Item (I).As_String);
      end loop;
   end;

   Log ("Loaded " & AAA.Strings.Trim (Words.Length'Image) & " words");
end Wordlist;