From 5d41f8a91dc47e41f45ecaeec4ad1640196be37e Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Mon, 9 Sep 2013 00:12:34 -0500 Subject: move vba_keyboard.py -> keyboard.py --- pokemontools/vba/keyboard.py | 63 ++++++++++++++++++++++++++++++++++++++++ pokemontools/vba/vba.py | 5 +--- pokemontools/vba/vba_keyboard.py | 63 ---------------------------------------- 3 files changed, 64 insertions(+), 67 deletions(-) create mode 100644 pokemontools/vba/keyboard.py delete mode 100644 pokemontools/vba/vba_keyboard.py (limited to 'pokemontools/vba') diff --git a/pokemontools/vba/keyboard.py b/pokemontools/vba/keyboard.py new file mode 100644 index 0000000..f25fd06 --- /dev/null +++ b/pokemontools/vba/keyboard.py @@ -0,0 +1,63 @@ +# -*- coding: utf-8 -*- +""" +This file constructs a networkx.DiGraph object called graph, which can be used +to find the shortest path of keypresses on the keyboard to type a word. +""" + +import itertools +import networkx + +graph = networkx.DiGraph() + +# load graph data from file +data_path = os.path.join(os.path.abspath(__file__), "keyboard.data") +graph_data = open(data_path, "r").read() + +for line in graph_data.split("\n"): + if line == "": + continue + elif line[0] == "#": + continue + + (node1, node2, edge_name) = line.split(" ") + graph.add_edge(node1, node2, key=edge_name) + + #print "Adding edge ("+edge_name+") "+node1+" -> "+node2 + +def shortest_path(node1, node2): + """ + Figures out the shortest list of button presses to move from one letter to + another. + """ + buttons = [] + last = None + path = networkx.shortest_path(graph, node1, node2) + for each in path: + if last != None: + buttons.append(convert_nodes_to_button_press(last, each)) + last = each + return buttons + #return [convert_nodes_to_button_press(node3, node4) for (node3, node4) in zip(*(iter(networkx.shortest_path(graph, node1, node2)),) * 2)] + +def convert_nodes_to_button_press(node1, node2): + """ + Determines the button necessary to switch from node1 to node2. + """ + print "getting button press for state transition: " + node1 + " -> " + node2 + return graph.get_edge_data(node1, node2)["key"] + +def plan_typing(text, current="A"): + """ + Plans a sequence of button presses to spell out the given text. + """ + buttons = [] + for target in text: + if target == current: + buttons.append("a") + else: + print "Finding the shortest path between " + current + " and " + target + more_buttons = shortest_path(current, target) + buttons.extend(more_buttons) + buttons.append("a") + current = target + return buttons diff --git a/pokemontools/vba/vba.py b/pokemontools/vba/vba.py index 3e0b7dd..b7c3768 100644 --- a/pokemontools/vba/vba.py +++ b/pokemontools/vba/vba.py @@ -109,10 +109,7 @@ Gb.loadVBA() from vba_config import * -try: - import vba_keyboard as keyboard -except ImportError: - print "Not loading the keyboard module (which uses networkx)." +import keyboard if not os.path.exists(rom_path): raise Exception("rom_path is not configured properly; edit vba_config.py? " + str(rom_path)) diff --git a/pokemontools/vba/vba_keyboard.py b/pokemontools/vba/vba_keyboard.py deleted file mode 100644 index f25fd06..0000000 --- a/pokemontools/vba/vba_keyboard.py +++ /dev/null @@ -1,63 +0,0 @@ -# -*- coding: utf-8 -*- -""" -This file constructs a networkx.DiGraph object called graph, which can be used -to find the shortest path of keypresses on the keyboard to type a word. -""" - -import itertools -import networkx - -graph = networkx.DiGraph() - -# load graph data from file -data_path = os.path.join(os.path.abspath(__file__), "keyboard.data") -graph_data = open(data_path, "r").read() - -for line in graph_data.split("\n"): - if line == "": - continue - elif line[0] == "#": - continue - - (node1, node2, edge_name) = line.split(" ") - graph.add_edge(node1, node2, key=edge_name) - - #print "Adding edge ("+edge_name+") "+node1+" -> "+node2 - -def shortest_path(node1, node2): - """ - Figures out the shortest list of button presses to move from one letter to - another. - """ - buttons = [] - last = None - path = networkx.shortest_path(graph, node1, node2) - for each in path: - if last != None: - buttons.append(convert_nodes_to_button_press(last, each)) - last = each - return buttons - #return [convert_nodes_to_button_press(node3, node4) for (node3, node4) in zip(*(iter(networkx.shortest_path(graph, node1, node2)),) * 2)] - -def convert_nodes_to_button_press(node1, node2): - """ - Determines the button necessary to switch from node1 to node2. - """ - print "getting button press for state transition: " + node1 + " -> " + node2 - return graph.get_edge_data(node1, node2)["key"] - -def plan_typing(text, current="A"): - """ - Plans a sequence of button presses to spell out the given text. - """ - buttons = [] - for target in text: - if target == current: - buttons.append("a") - else: - print "Finding the shortest path between " + current + " and " + target - more_buttons = shortest_path(current, target) - buttons.extend(more_buttons) - buttons.append("a") - current = target - return buttons -- cgit v1.2.3