aboutsummaryrefslogtreecommitdiff
path: root/srchr/src/corpus.rs
blob: 8e4b437ba41a6d566a1d4dcd2f1f5100283c1774 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use std::fmt;
use std::fs;

use crate::layout::*;

const fn build_lookup_table() -> [usize; 128] {
    let mut result = [0; 128];
    let mut i = 0;

    while i < NUM_KEYS {
        result[KEY_CHARS[i] as usize] = i;
        i += 1;
    }
    return result;
}
const CHAR_TO_INDEX: [usize; 128] = build_lookup_table();

const NUM_BIGRAMS: usize = NUM_KEYS * NUM_KEYS;
pub struct Corpus {
    bigram_count: [u32; NUM_BIGRAMS],
    character_count: [u32; 256],
    total_count: u32,
    index_threshold: u32,
}

fn pair_to_index(x: char, y: char) -> usize {
    let xi = CHAR_TO_INDEX[x as usize];
    let yi = CHAR_TO_INDEX[y as usize];
    xi + NUM_KEYS * yi
}

impl Corpus {
    pub fn get_character_count(&self, c: char) -> u32 {
        self.character_count[c as usize]
    }

    pub fn load(path: &str) -> Result<Corpus, std::io::Error> {
        let contents = fs::read_to_string(path)?;

        let mut bigram_count = [0; NUM_BIGRAMS];
        let mut character_count = [0; 256];
        let mut total_count = 0;

        let mut last_char = None;
        for c in contents.chars() {
            if let Some(c) = canonicalise(c) {
                if let Some(lc) = last_char {
                    // We don't count these anyway
                    if lc != c {
                        bigram_count[pair_to_index(c, lc)] += 1;
                        bigram_count[pair_to_index(lc, c)] += 1;
                    }
                }
                last_char = Some(c);
                character_count[c as usize] += 1;
                total_count += 1;
            } else {
                last_char = None;
            }
        }

        let index_threshold = (total_count as f32 * 0.15) as u32;

        return Ok(Corpus {
            bigram_count,
            character_count,
            total_count,
            index_threshold,
        });
    }

    pub fn prelayout_fitness(&self, layout: &Prelayout) -> u32 {
        let mut score: u32 = 0;

        for i in 0..6 {
            let keys = layout.get_standard_column(i);
            let k1 = keys[0];
            let k2 = keys[1];
            let k3 = keys[2];

            score += self.bigram_count[pair_to_index(k1, k2)]
                + self.bigram_count[pair_to_index(k1, k3)]
                + self.bigram_count[pair_to_index(k2, k3)];
        }

        for i in 0..2 {
            let keys = layout.get_index_column(i);
            let k1 = keys[0];
            let k2 = keys[1];
            let k3 = keys[2];
            let k4 = keys[3];
            let k5 = keys[4];
            let k6 = keys[5];

            // We want index usage!
            let index_count: u32 = keys.iter().map(|&k| self.character_count[k as usize]).sum();
            score += (self.index_threshold as i64 - index_count as i64).abs() as u32 / 64;

            score += self.bigram_count[pair_to_index(k1, k2)]
                + self.bigram_count[pair_to_index(k1, k3)]
                + self.bigram_count[pair_to_index(k2, k3)]
                + self.bigram_count[pair_to_index(k4, k5)]
                + self.bigram_count[pair_to_index(k4, k6)]
                + self.bigram_count[pair_to_index(k5, k6)]
                + self.bigram_count[pair_to_index(k1, k4)]
                + self.bigram_count[pair_to_index(k1, k5)]
                + self.bigram_count[pair_to_index(k1, k6)]
                + self.bigram_count[pair_to_index(k2, k4)]
                + self.bigram_count[pair_to_index(k2, k5)]
                + self.bigram_count[pair_to_index(k2, k6)]
                + self.bigram_count[pair_to_index(k3, k4)]
                + self.bigram_count[pair_to_index(k3, k5)]
                + self.bigram_count[pair_to_index(k3, k6)];
        }

        score
    }

    pub fn evaluate_layout(&self, layout: &Layout) -> Evaluation {
        let mut keypress: [u32; NUM_KEYS] = [0; NUM_KEYS];
        let mut sfb: [u32; 8] = [0; 8];

        for (i, &c) in self.character_count.iter().enumerate() {
            if c > 0 {
                keypress[layout.get_index((i as u8) as char)] = c;
            }
        }

        // TODO: We make assumptions about NUM_KEYS here
        for i in 0..8 {
            let ind = if i < 4 { i } else { i + 2 };

            let k1 = layout.get_key(ind + 10 * 0);
            let k2 = layout.get_key(ind + 10 * 1);
            let k3 = layout.get_key(ind + 10 * 2);

            sfb[i] = self.bigram_count[pair_to_index(k1, k2)]
                + self.bigram_count[pair_to_index(k1, k3)]
                + self.bigram_count[pair_to_index(k2, k3)];

            if i == 3 {
                let k4 = layout.get_key(4 + 10 * 0);
                let k5 = layout.get_key(4 + 10 * 1);
                let k6 = layout.get_key(4 + 10 * 2);
                sfb[i] += self.bigram_count[pair_to_index(k4, k5)]
                    + self.bigram_count[pair_to_index(k4, k6)]
                    + self.bigram_count[pair_to_index(k5, k6)]
                    + self.bigram_count[pair_to_index(k1, k4)]
                    + self.bigram_count[pair_to_index(k1, k5)]
                    + self.bigram_count[pair_to_index(k1, k6)]
                    + self.bigram_count[pair_to_index(k2, k4)]
                    + self.bigram_count[pair_to_index(k2, k5)]
                    + self.bigram_count[pair_to_index(k2, k6)]
                    + self.bigram_count[pair_to_index(k3, k4)]
                    + self.bigram_count[pair_to_index(k3, k5)]
                    + self.bigram_count[pair_to_index(k3, k6)];
            } else if i == 4 {
                let k4 = layout.get_key(5 + 10 * 0);
                let k5 = layout.get_key(5 + 10 * 1);
                let k6 = layout.get_key(5 + 10 * 2);
                sfb[i] += self.bigram_count[pair_to_index(k4, k5)]
                    + self.bigram_count[pair_to_index(k5, k6)]
                    + self.bigram_count[pair_to_index(k4, k6)]
                    + self.bigram_count[pair_to_index(k1, k4)]
                    + self.bigram_count[pair_to_index(k1, k5)]
                    + self.bigram_count[pair_to_index(k1, k6)]
                    + self.bigram_count[pair_to_index(k2, k4)]
                    + self.bigram_count[pair_to_index(k2, k5)]
                    + self.bigram_count[pair_to_index(k2, k6)]
                    + self.bigram_count[pair_to_index(k3, k4)]
                    + self.bigram_count[pair_to_index(k3, k5)]
                    + self.bigram_count[pair_to_index(k3, k6)];
            }
        }

        return Evaluation::new(keypress, self.total_count, sfb);
    }
}

fn dump_bigrams(corpus: &Corpus) -> Vec<(String, u32)> {
    let mut result: Vec<(String, u32)> = Vec::new();

    for (i, &x) in KEY_CHARS.iter().enumerate() {
        for &y in &KEY_CHARS[i..] {
            let mut pair = String::from(x);
            pair.push(y);
            result.push((pair, corpus.bigram_count[pair_to_index(x, y)]));
        }
    }

    result.sort_by(|(_, c1), (_, c2)| c1.cmp(c2).reverse());

    return result;
}

impl fmt::Display for Corpus {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str(&format!(
            "Counted {} characters, top 5 bigrams {:?}",
            self.total_count,
            &dump_bigrams(self)[0..5]
        ))
    }
}