evolve-keyboard-layout /

#!/usr/bin/env python3

u"""Get information about keyboard layouts (frontend)."""

from __future__ import division
from layout_info import *
from check_neo import short_number
sn = short_number

def p(*args, **kwds):
    u"""print without linebreak (saves typing :) )"""
    return print(end=u" ", *args, **kwds)

def pos_to_svg_coord(pos):
    u"""turn a position tuple into corresponding svg coordinates (xy)."""
    if pos[0] == 3 and pos[1] <= 1:
        pos = pos[0], 0.5*pos[1] + 1, pos[2]
    pos = (50*pos[1] - 2, 50 + 50*pos[0])
    return pos

def print_svg(bigrams, layout, svg_output=None, filepath=None, with_keys=True, lett=None, trigrams=None, repeats=None, number_of_letters=None):
    u"""print an svg from the bigrams.

    svg to png with inkscape (without gui): inkscape -D -z -e neo2.png -f neo2.svg

    TODO: Somehow mark the first letter in words (space-letter bigrams).
    TODO: Add some statistics below the layout image.
    TODO: Legende einfügen: Farben, Dicken, Hintergründe, Punkte im Hintergrundm Richtung.

    @param bigrams: [(number, cost, bigram), …]
    # only import here to avoid the import overhead for other actions.
    from svg_layouts import colorwheel, add_line, svg, defs, StyleBuilder, ShapeBuilder, g, text
    from layout_base import find_key, pos_is_left, get_key, get_all_positions_in_layout
    S = svg(u"Belegung")
    #oh = ShapeBuilder()
    #S.addElement(oh.createRect(0,0,750,250, strokewidth=0, fill='black'))

    d = defs()
    S.setAttribute(u"xmlns:inkscape", u"")
    #max_cost = max(cost for number, cost, bigram in bigrams)
    color_scale = 1
    max_linewidth = 25
    max_num = max(number for number, cost, bigram in bigrams)
    num_scale = max_linewidth/max_num
    letters = g()
    letter_dist = g()
    group_handswitch = g()
    group_shifts = g()
    group_inwards = g()
    group_outwards = g()
    group_fingerrepeat = g()
    group_info = g()
    for gr in (group_handswitch, group_shifts, group_inwards, group_outwards, group_fingerrepeat, letters, letter_dist, group_info): 
        gr.setAttribute(u"inkscape:groupmode", u"layer")
    group_handswitch.setAttribute(u"inkscape:label", u"Handwechsel")
    group_inwards.setAttribute(u"inkscape:label", u"Einwärts")
    group_outwards.setAttribute(u"inkscape:label", u"Auswärts")
    group_shifts.setAttribute(u"inkscape:label", u"Shift")
    group_fingerrepeat.setAttribute(u"inkscape:label", u"Fingerwiederholungen")
    letters.setAttribute(u"inkscape:label", u"Buchstaben")
    letter_dist.setAttribute(u"inkscape:label", u"Häufigkeit")
    group_info.setAttribute(u"inkscape:label", u"Info")


    ## letters and trigrams, yes, this is kinda not nice to get them here again…
    if lett is None or trigrams is None or repeats is None or number_of_letters is None: 
        lett, number_of_letters, repeats, number_of_bigrams, trigrams, number_of_trigrams = get_all_data(datapath=filepath)
    # shape builder for rectangles
    first_letters = {}
    if with_keys: 
        positions = get_all_positions_in_layout(layout)
        #: scale the color for the letters
        letter_scale = 128 / max(num for num, l in lett)
        # get first letters in words
        for num, cost, bi in [(num, cost, bi) for num, cost, bi in bigrams if bi[1:] and (bi[0] == u" " or bi[0] == u"\n")]:
            fl = bi[1] #: a first letter
            if fl in first_letters: first_letters[fl] += num
            else: first_letters[fl] = num
        #: scale the color for the first letters
        first_letter_scale = 254 / max(first_letters.values())

    else: positions = []
    oh = ShapeBuilder()
    for pos in positions:
        if pos[2] or pos[0]>3: continue # only base layer.
        # get the letter.
        l = get_key(pos, layout=layout)
        # shift the position to fit with the image.
        if pos[0] != 3:
            pos1 = (pos[0], pos[1] + 1, pos[2])
        else: pos1 = pos
        # get the coords in the image
        coord = pos_to_svg_coord(pos1)
        # get the color for the background
            num = lett[[le for n, le in lett].index(l)][0]
        except ValueError: num = 0
        color = colorwheel(num*letter_scale, palette=u"grey")
        # get the dimensions of the background
        x, y, dx, dy = coord[0]-25, coord[1]-25, 50, 50,
        if pos == (2, 13, 0):
            y -= 50
            dy += 50
            l = u"⏎"
        elif pos == (3, 12, 0):
            dx += 100
        # add the background
            oh.createRect(x, y,
                          dx, dy,
                          fill=u"rgb(" + u",".join([unicode(c) for c in color]) + u")",

        # add the letter itself.
        if l == u"<":
            ll = u"≤"
        elif l == u">":
            ll == u"≥"
        else: ll = l
        t = text(ll, coord[0]-5, coord[1]+4)

        # get the color for the first-letter circle
            num = first_letters[l]
        except KeyError: num = 0
        color = colorwheel(num*first_letter_scale, palette=u"grey")
        # get the dimensions of the first-letter circle
        x, y, dx, dy = coord[0]-15, coord[1]-15, 5, 5
        if pos == (2, 13, 0):
            y -= 50
        # add the background
            oh.createCircle(x, y,
                          dx, dy,
                          fill=u"rgb(" + u",".join([unicode(c) for c in color]) + u")",

    lay_strings = format_layer_1_string(layout).splitlines()
    for i in xrange(len(lay_strings)): 
        layout_string = text(lay_strings[i], 50, 250 + 20*i)
    # add statistics
    total, frep_num, cost, frep_top_bottom, disbalance, no_handswitches, line_change_same_hand, hand_load, no_switch_after_unbalancing, manual_penalty, neighboring_unbalance = total_cost(letters=lett, repeats=repeats, layout=layout, trigrams=trigrams)[:11]
    tppl = total/max(1, number_of_letters)
    cost_string = text("cost (tppl): " + str(tppl), 325, 250)
    # make sure the most used bigram is shown on top (drawn last)
    for number, cost, bigram in bigrams:

        # ignore spaces
        if u" " in bigram:

        pos0 = find_key(bigram[0], layout)
        pos1 = find_key(bigram[1], layout)
        if pos0 is None or pos1 is None:

        is_left0 = pos_is_left(pos0)
        is_left1 = pos_is_left(pos1)

        # handswitches have far lower opacity
        if is_left0 != is_left1:
            handswitch = True
            opacity = 0.03
            #continue # ignore them, they needlessly blow up the svg.
            handswitch = False
            opacity = 1.0

        # fix, upscale and reorder the positions
        if pos0[0] != 3:
            pos0 = pos0[0], pos0[1] + 1, pos0[2]
        if pos1[0] != 3:
            pos1 = pos1[0], pos1[1] + 1, pos1[2]
        # out- or inwards
        to_right = pos1[1] > pos0[1]
        inwards = is_left0 and to_right or not is_left0 and not to_right

        column_repeat = pos1[1] == pos0[1]
        # move the left shifts and m4 1/0.5 step(s) towards the center. They look awkward otherwise.
        pos0 = pos_to_svg_coord(pos0)
        pos1 = pos_to_svg_coord(pos1)

        # move the start and endpoints slightly away from the letters: looks better (thanks to Andreas Wettstein)
        dx0 = dx1 = dy0 = dy1 = 0
        if pos0[1] < pos1[1]: 
            dy0 += 7
            if pos0[0] == pos1[0]: dy1 -= 7
            elif inwards: dy1 -= 5
            else: dy1 += 5
        elif pos0[1] > pos1[1]:
            dy0 = - 7
            if pos0[0] == pos1[0]: dy1 += 7
            elif inwards: dy1 -= 5
            else: dy1 += 5
        if pos0[0] < pos1[0]:
            dx0 = +5
            dx1 = -5
        elif pos0[0] > pos1[0]:
            dx0 = -5
            dx1 = +5
        pos0 = pos0[0] + dx0, pos0[1] + dy0
        pos1 = pos1[0] + dx1, pos1[1] + dy1

        color = colorwheel(max(min(1020, cost*color_scale), 0))
        # invert the color
        color = tuple([255-c for c in color])
        width = num_scale * number

        shift = "⇗" in bigram or "⇧" in bigram

        if column_repeat:
            group_fingerrepeat.addElement(add_line(d, color=color, xy0=pos0, xy1=pos1, width=width, opacity=opacity, upstroke=inwards))
        elif handswitch:
            group_handswitch.addElement(add_line(d, color=color, xy0=pos0, xy1=pos1, width=width, opacity=opacity, upstroke=inwards))
        elif shift:
            group_shifts.addElement(add_line(d, color=color, xy0=pos0, xy1=pos1, width=width, opacity=opacity, upstroke=inwards))
        elif inwards: 
            group_inwards.addElement(add_line(d, color=color, xy0=pos0, xy1=pos1, width=width, opacity=opacity, upstroke=inwards))
            group_outwards.addElement(add_line(d, color=color, xy0=pos0, xy1=pos1, width=width, opacity=opacity, upstroke=inwards))

    if svg_output is None: 
        print S.getXML()
    else:, encoding=u"UTF-8")
        # and try to cleanup the svg with inkscape.
        from subprocess import call
        # this just fails when there’s no inkscape there.
        #call(["inkscape", "-z", "--vacuum-defs", svg_output])


def print_bigram_info(layout=NEO_LAYOUT, number=None, filepath=None, bars=False, secondary=True, svg=False, svg_output=None):
    """Print bigram-statistics of the layout."""
    letters, number_of_letters, repeats, number_of_bigrams, trigrams, number_of_trigrams = get_all_data(datapath=filepath) 
    if not svg: 
        print format_layer_1_string(layout)
        print u"Häufigkeit %, Bigram, Gesamt, Lage, Fingerwiederholung, Finger-oben-unten, Fingerübergang, rows², Kein Handwechsel nach Handverschiebung"
    # svg should have shifts and such.
    if svg: only_layer_0 = True
    else: only_layer_0 = False
    info = bigram_info(layout=layout, filepath=filepath, repeats=repeats, trigrams=trigrams, secondary=secondary, only_layer_0=only_layer_0)
    num_bigrams = sum([num for num, cost, rep in info])
    if number is None: number = len(info)
    numlen = len(unicode(float(info[0][0])))
    if svg: bigrams_with_cost = []
    for num, cost, rep in info[:number]:
        total, pos, finger_repeats, finger_repeats_top_bottom, movement_pattern, finger_disbalance, no_handswitch_despite_direction_change, shortcut_keys, rows, no_handswitch_after_unbalancing_key, hand_disbalance = cost[:11]
        #p(" "*(numlen-len(str(float(num)))))
        tot = total - finger_disbalance - hand_disbalance - no_handswitch_despite_direction_change  - shortcut_keys
        if svg:
            bigrams_with_cost.append((num, tot, rep))
        p(sn(100.0*num/num_bigrams, 5), rep, u"\t")
        if bars: 
            print u"*"*int(tot/60.4)
        p(sn(tot, 5), u"\t")
        p(pos, finger_repeats, finger_repeats_top_bottom, movement_pattern, rows, no_handswitch_after_unbalancing_key, sep=u"  ")
        if finger_repeats_top_bottom: p(u"Finger-oben-unten,")
        elif finger_repeats: p(u"Fingerwiederholung,")
        if movement_pattern: p(u"Unschöner Fingerübergang,")
        if rows > 0.5: p(u"Zeilensprung,")
        if no_handswitch_after_unbalancing_key: p(u"Kein Handwechsel nach Handverschiebung,")
    if svg:
        print_svg(bigrams_with_cost, layout, svg_output=svg_output, filepath=filepath, lett=letters, trigrams=trigrams, repeats=repeats, number_of_letters=number_of_letters)

def ask_for_layout_string_completion(l):
    u"""if the layout string only has one line (Windows, I look at you!),
    ask for the other lines."""
    if len(l.splitlines()) < 3:
        for i in xrange(3-len(l.splitlines())):
            l += u"\n" + raw_input(u"please enter the next line of the layout string: ")
    return l
if __name__ == u"__main__":
    from optparse import OptionParser

    parser = OptionParser(description=u"Check and evolve keyboard layouts. Actions can’t be combined, the other options and arguments can. Without action, it checks layouts.")
    # actions
    parser.add_option(u"-l", u"--layout", dest=u"layout_string", default=None, 
                      help=u"The layout to test the bigrams against.", metavar=u"layout_string")
    parser.add_option(u"-f", u"--file", dest=u"filepath", default=None, 
                      help=u"Use the given korpus file (file with text).", metavar=u"filepath")
    parser.add_option(u"-n", u"--number", dest=u"number", default=1000, type=u"int", 
                      help=u"The number of bigrams to show.", metavar=u"number")

    parser.add_option(u"--bars", dest=u"bars", default=False, action=u"store_true", 
                      help=u"Show cost bars instead of numbers.")
    parser.add_option(u"--svg", dest=u"svg", default=False, action=u"store_true", 
                      help=u"Print an svg instead of printing infos.")
    parser.add_option(u"--svg-output", dest=u"svg_output", default=None, 
                      help=u"Store the svg in the given file.", metavar=u"filepath")
    parser.add_option(u"--no-secondary", dest=u"secondary", action=u"store_false", default=True, 
                      help=u"Don’t calculate secondary/indirect bigrams.", metavar=u"number")

    (options, args) = parser.parse_args()

    if options.layout_string is not None:
        options.layout_string = ask_for_layout_string_completion(options.layout_string)
        options.layout = string_to_layout(options.layout_string, base_layout=NEO_LAYOUT)
    else: options.layout = NEO_LAYOUT

    print_bigram_info(layout=options.layout, number=options.number, filepath=options.filepath, bars=options.bars, secondary=options.secondary, svg=options.svg, svg_output=options.svg_output)