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/test_writer.py | 11 +++++++++-- lulua/writer.py | 33 ++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 15 deletions(-) (limited to 'lulua') diff --git a/lulua/test_writer.py b/lulua/test_writer.py index bc02a7e..c8840d9 100644 --- a/lulua/test_writer.py +++ b/lulua/test_writer.py @@ -91,7 +91,7 @@ testCombs = [ ), ([ (tuple (), ('Fl_space', )), (tuple (), ('Fr_space', )), - ], 0, (('El_shift', ), ('Dr7', )) + ], 1, (('El_shift', ), ('Dr7', )) ), ([ (tuple (), ('Fl_space', )), (tuple (), ('Fr_space', )), @@ -102,6 +102,10 @@ testCombs = [ (('Er_shift', ), ('CD_ret', )), (('El_shift', ), ('CD_ret', )), ], 0, None), + ([ + (('El_shift', ), ('Cl4', )), + (('Er_shift', ), ('Cl4', )), + ], 1, (tuple (), ('Fr_space', ))), ] @pytest.mark.parametrize("combs, expect, prev", testCombs) @@ -114,5 +118,8 @@ def test_writer_chooseComb (combs, expect, prev): prev = toButtonComb (keyboard, prev) w.press (prev) combs = [toButtonComb (keyboard, x) for x in combs] - assert w.chooseCombination (combs) == combs[expect] + result = w.chooseCombination (combs) + assert result == combs[expect] + if len (result) == 2: + assert w.getHandFinger (first (result.modifier))[0] != w.getHandFinger (first (result.buttons))[0] 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