aboutsummaryrefslogtreecommitdiff
path: root/srchr/src/evaluation.rs
blob: b412d88c49877b46d5689f8bc683c571ecf72816 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use crate::config::*;
use crate::corpus::*;
use crate::layout::*;
use crate::output::*;

use std::fmt;

pub fn prelayout_fitness(corpus: &Corpus, layout: &Prelayout) -> f32 {
    let mut score: f32 = 0.0;
    let mut min1: f32 = std::f32::INFINITY;
    let mut min2: f32 = std::f32::INFINITY;

    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 += corpus.get_bigram_perc(k1, k2)
            + corpus.get_bigram_perc(k1, k3)
            + corpus.get_bigram_perc(k2, k3);

        let col_usage = corpus.get_character_perc(k1)
            + corpus.get_character_perc(k2)
            + corpus.get_character_perc(k3);

        if col_usage < min2 {
            if col_usage < min1 {
                min2 = min1;
                min1 = col_usage;
            } else {
                min2 = col_usage;
            }
        }

        score += (corpus.get_skipgram_perc(k1, k2)
            + corpus.get_skipgram_perc(k1, k3)
            + corpus.get_skipgram_perc(k2, k3))
            * STANDARD_DSFB_WEIGHT;
    }

    score += pinky_col_penalty(min1, min2);

    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];

        let index_percs = keys.iter().map(|&k| corpus.get_character_perc(k)).sum();
        score += index_usage_fitness(index_percs);

        score += corpus.get_bigram_perc(k1, k2)
            + corpus.get_bigram_perc(k1, k3)
            + corpus.get_bigram_perc(k2, k3)
            + corpus.get_bigram_perc(k4, k5)
            + corpus.get_bigram_perc(k4, k6)
            + corpus.get_bigram_perc(k5, k6)
            + corpus.get_bigram_perc(k1, k4)
            + corpus.get_bigram_perc(k1, k5)
            + corpus.get_bigram_perc(k1, k6)
            + corpus.get_bigram_perc(k2, k4)
            + corpus.get_bigram_perc(k2, k5)
            + corpus.get_bigram_perc(k2, k6)
            + corpus.get_bigram_perc(k3, k4)
            + corpus.get_bigram_perc(k3, k5)
            + corpus.get_bigram_perc(k3, k6);

        score += (corpus.get_skipgram_perc(k1, k2)
            + corpus.get_skipgram_perc(k1, k3)
            + corpus.get_skipgram_perc(k2, k3)
            + corpus.get_skipgram_perc(k4, k5)
            + corpus.get_skipgram_perc(k4, k6)
            + corpus.get_skipgram_perc(k5, k6)
            + corpus.get_skipgram_perc(k1, k4)
            + corpus.get_skipgram_perc(k1, k5)
            + corpus.get_skipgram_perc(k1, k6)
            + corpus.get_skipgram_perc(k2, k4)
            + corpus.get_skipgram_perc(k2, k5)
            + corpus.get_skipgram_perc(k2, k6)
            + corpus.get_skipgram_perc(k3, k4)
            + corpus.get_skipgram_perc(k3, k5)
            + corpus.get_skipgram_perc(k3, k6))
            * INDEX_DSFB_WEIGHT;
    }

    score
}

pub struct Evaluation {
    keypress_perc: [f32; NUM_KEYS],
    total_bigrams: u32,
    sfb: [f32; 8],
    dsfb: [f32; 8],
}

impl Evaluation {
    pub fn evaluate_layout(corpus: &Corpus, layout: &Layout) -> Evaluation {
        let mut keypress_perc: [f32; NUM_KEYS] = [0.0; NUM_KEYS];
        let mut sfb: [f32; 8] = [0.0; 8];
        let mut dsfb: [f32; 8] = [0.0; 8];

        for (i, &c) in layout.get_keys().iter().enumerate() {
            keypress_perc[i] = corpus.get_character_perc(c as u8);
        }

        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] = corpus.get_bigram_perc(k1, k2)
                + corpus.get_bigram_perc(k1, k3)
                + corpus.get_bigram_perc(k2, k3);

            dsfb[i] = corpus.get_skipgram_perc(k1, k2)
                + corpus.get_skipgram_perc(k1, k3)
                + corpus.get_skipgram_perc(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] += corpus.get_bigram_perc(k4, k5)
                    + corpus.get_bigram_perc(k4, k6)
                    + corpus.get_bigram_perc(k5, k6)
                    + corpus.get_bigram_perc(k1, k4)
                    + corpus.get_bigram_perc(k1, k5)
                    + corpus.get_bigram_perc(k1, k6)
                    + corpus.get_bigram_perc(k2, k4)
                    + corpus.get_bigram_perc(k2, k5)
                    + corpus.get_bigram_perc(k2, k6)
                    + corpus.get_bigram_perc(k3, k4)
                    + corpus.get_bigram_perc(k3, k5)
                    + corpus.get_bigram_perc(k3, k6);
                dsfb[i] += corpus.get_skipgram_perc(k4, k5)
                    + corpus.get_skipgram_perc(k4, k6)
                    + corpus.get_skipgram_perc(k5, k6)
                    + corpus.get_skipgram_perc(k1, k4)
                    + corpus.get_skipgram_perc(k1, k5)
                    + corpus.get_skipgram_perc(k1, k6)
                    + corpus.get_skipgram_perc(k2, k4)
                    + corpus.get_skipgram_perc(k2, k5)
                    + corpus.get_skipgram_perc(k2, k6)
                    + corpus.get_skipgram_perc(k3, k4)
                    + corpus.get_skipgram_perc(k3, k5)
                    + corpus.get_skipgram_perc(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] += corpus.get_bigram_perc(k4, k5)
                    + corpus.get_bigram_perc(k5, k6)
                    + corpus.get_bigram_perc(k4, k6)
                    + corpus.get_bigram_perc(k1, k4)
                    + corpus.get_bigram_perc(k1, k5)
                    + corpus.get_bigram_perc(k1, k6)
                    + corpus.get_bigram_perc(k2, k4)
                    + corpus.get_bigram_perc(k2, k5)
                    + corpus.get_bigram_perc(k2, k6)
                    + corpus.get_bigram_perc(k3, k4)
                    + corpus.get_bigram_perc(k3, k5)
                    + corpus.get_bigram_perc(k3, k6);
                dsfb[i] += corpus.get_skipgram_perc(k4, k5)
                    + corpus.get_skipgram_perc(k4, k6)
                    + corpus.get_skipgram_perc(k5, k6)
                    + corpus.get_skipgram_perc(k1, k4)
                    + corpus.get_skipgram_perc(k1, k5)
                    + corpus.get_skipgram_perc(k1, k6)
                    + corpus.get_skipgram_perc(k2, k4)
                    + corpus.get_skipgram_perc(k2, k5)
                    + corpus.get_skipgram_perc(k2, k6)
                    + corpus.get_skipgram_perc(k3, k4)
                    + corpus.get_skipgram_perc(k3, k5)
                    + corpus.get_skipgram_perc(k3, k6);
            }
        }

        return Evaluation {
            keypress_perc,
            total_bigrams: corpus.get_total_bigrams(),
            sfb,
            dsfb,
        };
    }

    fn output_eval(&self) -> String {
        let mut result = String::new();
        let big = self.total_bigrams as f32;
        let tig = self.total_bigrams as f32;

        result += &"Percent per key:\n";
        result += &format_block_output(self.keypress_perc.into_iter().map(|u| u * 100.0));

        let mut finger_percs = [0.0; 8];
        for (i, &c) in self.keypress_perc.iter().enumerate() {
            finger_percs[KEY_TO_FINGER[i]] += c;
        }

        result += "\nFinger usage: ";
        let mut lh: f32 = 0.0;
        let mut rh: f32 = 0.0;
        for (i, &u) in finger_percs.iter().enumerate() {
            result += &format!("{:>5.2}%{}", u * 100.0, if i < 7 { ", " } else { "" });
            if i % 10 < 4 {
                lh += u;
            } else {
                rh += u;
            }
        }

        result += &format!("\nHand usage: {:.2}% vs {:.2}%", lh, rh);

        result += "\nSFB distribution : ";
        for (i, &u) in self.sfb.iter().enumerate() {
            result += &format!("{:>6.3}%{}", u * 100.0, if i < 7 { ", " } else { "" });
        }

        result += "\nDSFB distribution: ";
        for (i, &u) in self.dsfb.iter().enumerate() {
            result += &format!("{:>6.3}%{}", u * 100.0, if i < 7 { ", " } else { "" });
        }

        let sfb = self.sfb.iter().sum::<f32>();
        let dsfb = self.dsfb.iter().sum::<f32>();
        result += &format!("\nTotal sfb: {:.3}% ({:.0})", sfb * 100.0, sfb * big);
        result += &format!("\nTotal dsfb: {:.3}% ({:.0})", dsfb * 100.0, dsfb * tig);

        return result;
    }
}

impl fmt::Display for Evaluation {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str(&self.output_eval())
    }
}