From 59ca079dba6c95948ebbf6442066e9313d330302 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Sun, 8 Mar 2020 08:34:13 +0100 Subject: writer: Fix bug in chooseCombination MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some combinations had the same score and the “wrong” one was chosen as a result. This affects how key combinations are chosen and thus it significantly affects results (lowering ar-lulua’s asymmetry and increasing it for almost every other layout) and probably optimization of levels > 1. --- lulua/writer.py | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'lulua/writer.py') diff --git a/lulua/writer.py b/lulua/writer.py index 94ad1b4..a186315 100644 --- a/lulua/writer.py +++ b/lulua/writer.py @@ -152,29 +152,36 @@ class Writer: - A key on the right is usually combined with the shift button on the left and vice versa. - The spacebar is usually hit by the thumb of the previously unused - hand or the one on the left if two buttons were pressed at the same - time. + hand. If two hands were used the one pressing the key (not the + modifier) is chosen, since it’ll usually be closer. - The combination with the minimum amount of fingers required is chosen if multiple options are available """ + if len (combinations) == 1: + return combinations[0] + dirToScore = {LEFT: 1, RIGHT: -1} def calcEffort (comb): prev = self.lastCombination - if prev is None: - e = 0 - elif len (prev) > 1: - # prefer left side - e = dirToScore[RIGHT] + + if prev is not None: + prevBalance = 0 + for b in chain (prev.modifier or prev.buttons, comb.buttons): + pos = self.getHandFinger (b)[0] + prevBalance += dirToScore[pos] else: - assert len (prev.buttons) == 1 - e = dirToScore[self.getHandFinger (first (prev.buttons))[0]] + # prefer the left side (arbitrary decision) + prevBalance = dirToScore[RIGHT] + + balance = 0 for b in comb: pos = self.getHandFinger (b)[0] - e += dirToScore[pos] - #print ('score for', buttons, abs (e)) - return abs (e) + len (comb) + balance += dirToScore[pos] + + return (len (comb) << 16) | (abs (balance) << 8) | (abs (prevBalance) << 0) - return min (zip (map (calcEffort, combinations), combinations), key=itemgetter (0))[1] + m = min (zip (map (calcEffort, combinations), combinations), key=itemgetter (0)) + return m[1] def press (self, comb): self.lastCombination = comb -- cgit v1.2.3