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
use crate::en::grammar::WordUsage;

#[derive(Copy,Clone)]
pub struct WordUsageTensor {
   pub tensor: [f64; 32]
}

impl From<WordUsage> for WordUsageTensor {
   fn from(wu: WordUsage) -> Self {
      let bits = wu.bits();
      WordUsageTensor {
         tensor: [
            if (bits & 0b00000000_00000000_00000000_00000001)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00000000_00000000_00000010)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00000000_00000000_00000100)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00000000_00000000_00001000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00000000_00000000_00010000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00000000_00000000_00100000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00000000_00000000_01000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00000000_00000000_10000000)>0 { 1.0 } else { 0.0 },

            if (bits & 0b00000000_00000000_00000001_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00000000_00000010_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00000000_00000100_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00000000_00001000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00000000_00010000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00000000_00100000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00000000_01000000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00000000_10000000_00000000)>0 { 1.0 } else { 0.0 },

            if (bits & 0b00000000_00000001_00000000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00000010_00000000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00000100_00000000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00001000_00000000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00010000_00000000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_00100000_00000000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_01000000_00000000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000000_10000000_00000000_00000000)>0 { 1.0 } else { 0.0 },

            if (bits & 0b00000001_00000000_00000000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000010_00000000_00000000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00000100_00000000_00000000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00001000_00000000_00000000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00010000_00000000_00000000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b00100000_00000000_00000000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b01000000_00000000_00000000_00000000)>0 { 1.0 } else { 0.0 },
            if (bits & 0b10000000_00000000_00000000_00000000)>0 { 1.0 } else { 0.0 },
         ]
      }
   }
}

impl std::ops::Add for WordUsageTensor {
   type Output = Self;

   fn add(self, other: Self) -> Self {
      let mut tensor: [f64; 32] = [0.0; 32];
      for i in 0..32 {
         tensor[i] += self.tensor[i];
         tensor[i] += other.tensor[i];
      }
      WordUsageTensor {
         tensor: tensor
      }
   }
}

impl WordUsageTensor {
   pub fn multiply(&mut self, c: f64) {
      for i in 0..32 {
         self.tensor[i] *= c;
      }
   }
}

use radix_trie::Trie;

pub struct DictionLayer {
   diction: Trie<String,WordUsageTensor>
}
impl DictionLayer {
   pub fn new() -> DictionLayer {
      DictionLayer {
         diction: Trie::new()
      }
   }
   pub fn add(&mut self, key: String, usage: WordUsageTensor) {
      if let Some(record) = self.diction.get_mut(&key) {
         *record = *record + usage;
      } else {
         self.diction.insert(key, usage);
      }
   }
}