aboutsummaryrefslogtreecommitdiff
path: root/srchr/src/evaluation.rs
blob: bbbe364dcebb376e6d86364dadc149224499507e (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
use crate::config::*;
use crate::corpus::*;
use crate::layout::*;
use crate::output::*;

use std::fmt;

pub fn prelayout_fitness(corpus: &Corpus, 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 += corpus.get_bigram_count(k1, k2)
            + corpus.get_bigram_count(k1, k3)
            + corpus.get_bigram_count(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| corpus.get_character_count(k)).sum();
        score += index_usage_fitness(corpus.get_index_threshold(), index_count);

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

    score
}

#[derive(Copy, Clone)]
pub struct Evaluation {
    keypress: [u32; NUM_KEYS],
    total_keypress: u32,
    sfb: [u32; 8],
}

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

        for (i, &c) in layout.get_keys().iter().enumerate() {
            keypress[i] = corpus.get_character_count(c);
        }

        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_count(k1, k2)
                + corpus.get_bigram_count(k1, k3)
                + corpus.get_bigram_count(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_count(k4, k5)
                    + corpus.get_bigram_count(k4, k6)
                    + corpus.get_bigram_count(k5, k6)
                    + corpus.get_bigram_count(k1, k4)
                    + corpus.get_bigram_count(k1, k5)
                    + corpus.get_bigram_count(k1, k6)
                    + corpus.get_bigram_count(k2, k4)
                    + corpus.get_bigram_count(k2, k5)
                    + corpus.get_bigram_count(k2, k6)
                    + corpus.get_bigram_count(k3, k4)
                    + corpus.get_bigram_count(k3, k5)
                    + corpus.get_bigram_count(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_count(k4, k5)
                    + corpus.get_bigram_count(k5, k6)
                    + corpus.get_bigram_count(k4, k6)
                    + corpus.get_bigram_count(k1, k4)
                    + corpus.get_bigram_count(k1, k5)
                    + corpus.get_bigram_count(k1, k6)
                    + corpus.get_bigram_count(k2, k4)
                    + corpus.get_bigram_count(k2, k5)
                    + corpus.get_bigram_count(k2, k6)
                    + corpus.get_bigram_count(k3, k4)
                    + corpus.get_bigram_count(k3, k5)
                    + corpus.get_bigram_count(k3, k6);
            }
        }

        return Evaluation {
            keypress,
            total_keypress: corpus.get_total_count(),
            sfb,
        };
    }

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

        result += &"Percent per key:\n";
        result += &format_block_output(self.keypress.into_iter().map(|k| 100.0 * k as f32 / tot));

        let mut finger_usages = [0; 8];
        for (i, &c) in self.keypress.iter().enumerate() {
            finger_usages[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_usages.iter().enumerate() {
            let f = u as f32 / tot * 100.0;
            result += &format!("{:>5.2}%{}", f, if i < 7 { ", " } else { "" });
            if i % 10 < 4 {
                lh += f;
            } else {
                rh += f;
            }
        }

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

        result += "\nSame finger bigrams: ";
        for (i, &u) in self.sfb.iter().enumerate() {
            result += &format!(
                "{:>6.3}%{}",
                u as f32 / tot * 100.0,
                if i < 7 { ", " } else { "" }
            );
        }

        let sfb = self.sfb.iter().sum::<u32>();
        result += &format!("\nTotal sfb: {:.2}% ({})", sfb as f32 / tot * 100.0, sfb);

        return result;
    }
}

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