summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--README.md3
-rw-r--r--config.py90
-rw-r--r--gtkui.py113
-rw-r--r--hasher.py37
-rwxr-xr-xkaylee.py206
-rw-r--r--kayleevc/__init__.py0
-rw-r--r--kayleevc/gui.py (renamed from gtktrayui.py)104
-rw-r--r--kayleevc/kaylee.py207
-rw-r--r--kayleevc/numbers.py (renamed from numberparser.py)3
-rw-r--r--kayleevc/recognizer.py (renamed from recognizer.py)0
-rw-r--r--kayleevc/util.py202
-rw-r--r--languageupdater.py80
12 files changed, 517 insertions, 528 deletions
diff --git a/README.md b/README.md
index 7d59f01..5715733 100644
--- a/README.md
+++ b/README.md
@@ -7,7 +7,8 @@ but adds a lot of features that go beyond the original purpose of Blather.
 
 ## Requirements
 
-1. pocketsphinx
+1. Python 3 (tested with 3.5, may work with older versions)
+1. pocketsphinx 5prealpha
 2. gstreamer-1.0 (and what ever plugin has pocketsphinx support)
 3. gstreamer-1.0 base plugins (required for ALSA)
 4. python-gobject (required for GStreamer and the GTK-based UI)
diff --git a/config.py b/config.py
deleted file mode 100644
index 30f8df0..0000000
--- a/config.py
+++ /dev/null
@@ -1,90 +0,0 @@
-# This is part of Kaylee
-# -- this code is licensed GPLv3
-# Copyright 2015-2016 Clayton G. Hobbs
-# Portions Copyright 2013 Jezra
-
-import json
-import os
-from argparse import ArgumentParser, Namespace
-
-from gi.repository import GLib
-
-class Config:
-    """Keep track of the configuration of Kaylee"""
-    # Name of the program, for later use
-    program_name = "kaylee"
-
-    # Directories
-    conf_dir = os.path.join(GLib.get_user_config_dir(), program_name)
-    cache_dir = os.path.join(GLib.get_user_cache_dir(), program_name)
-    data_dir = os.path.join(GLib.get_user_data_dir(), program_name)
-
-    # Configuration files
-    command_file = os.path.join(conf_dir, "commands.conf")
-    opt_file = os.path.join(conf_dir, "options.json")
-
-    # Cache files
-    history_file = os.path.join(cache_dir, program_name + "history")
-    hash_file = os.path.join(cache_dir, "hash.json")
-
-    # Data files
-    strings_file = os.path.join(data_dir, "sentences.corpus")
-    lang_file = os.path.join(data_dir, 'lm')
-    dic_file = os.path.join(data_dir, 'dic')
-
-    def __init__(self):
-        # Ensure necessary directories exist
-        self._make_dir(self.conf_dir)
-        self._make_dir(self.cache_dir)
-        self._make_dir(self.data_dir)
-
-        # Set up the argument parser
-        self.parser = ArgumentParser()
-        self.parser.add_argument("-i", "--interface", type=str,
-                dest="interface", action='store',
-                help="Interface to use (if any). 'g' for GTK or 'gt' for GTK" +
-                " system tray icon")
-
-        self.parser.add_argument("-c", "--continuous",
-                action="store_true", dest="continuous", default=False,
-                help="Start interface with 'continuous' listen enabled")
-
-        self.parser.add_argument("-p", "--pass-words",
-                action="store_true", dest="pass_words", default=False,
-                help="Pass the recognized words as arguments to the shell" +
-                " command")
-
-        self.parser.add_argument("-H", "--history", type=int,
-                action="store", dest="history",
-                help="Number of commands to store in history file")
-
-        self.parser.add_argument("-m", "--microphone", type=int,
-                action="store", dest="microphone", default=None,
-                help="Audio input card to use (if other than system default)")
-
-        self.parser.add_argument("--valid-sentence-command", type=str,
-                dest="valid_sentence_command", action='store',
-                help="Command to run when a valid sentence is detected")
-
-        self.parser.add_argument("--invalid-sentence-command", type=str,
-                dest="invalid_sentence_command", action='store',
-                help="Command to run when an invalid sentence is detected")
-
-        # Read the configuration file
-        self._read_options_file()
-
-        # Parse command-line arguments, overriding config file as appropriate
-        self.parser.parse_args(namespace=self.options)
-
-    def _make_dir(self, directory):
-        if not os.path.exists(directory):
-            os.makedirs(directory)
-
-    def _read_options_file(self):
-        try:
-            with open(self.opt_file, 'r') as f:
-                self.options = json.load(f)
-                self.options = Namespace(**self.options)
-        except FileNotFoundError:
-            # Make an empty options namespace
-            self.options = Namespace()
diff --git a/gtkui.py b/gtkui.py
deleted file mode 100644
index 1268ccf..0000000
--- a/gtkui.py
+++ /dev/null
@@ -1,113 +0,0 @@
-# This is part of Kaylee
-# -- this code is licensed GPLv3
-# Copyright 2015-2016 Clayton G. Hobbs
-# Portions Copyright 2013 Jezra
-
-import sys
-import gi
-from gi.repository import GObject
-# Gtk
-gi.require_version('Gtk', '3.0')
-from gi.repository import Gtk, Gdk
-
-class UI(GObject.GObject):
-    __gsignals__ = {
-        'command' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,))
-    }
-
-    def __init__(self, args, continuous):
-        GObject.GObject.__init__(self)
-        self.continuous = continuous
-        # Make a window
-        self.window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
-        self.window.connect("delete_event", self.delete_event)
-        # Give the window a name
-        self.window.set_title("Kaylee")
-        self.window.set_resizable(False)
-
-        layout = Gtk.VBox()
-        self.window.add(layout)
-        # Make a listen/stop button
-        self.lsbutton = Gtk.Button("Listen")
-        layout.add(self.lsbutton)
-        # Make a continuous button
-        self.ccheckbox = Gtk.CheckButton("Continuous Listen")
-        layout.add(self.ccheckbox)
-
-        # Connect the buttons
-        self.lsbutton.connect("clicked", self.lsbutton_clicked)
-        self.ccheckbox.connect("clicked", self.ccheckbox_clicked)
-
-        # Add a label to the UI to display the last command
-        self.label = Gtk.Label()
-        layout.add(self.label)
-
-        # Create an accellerator group for this window
-        accel = Gtk.AccelGroup()
-        # Add the ctrl+q to quit
-        accel.connect(Gdk.keyval_from_name('q'), Gdk.ModifierType.CONTROL_MASK,
-                Gtk.AccelFlags.VISIBLE, self.accel_quit)
-        # Lock the group
-        accel.lock()
-        # Add the group to the window
-        self.window.add_accel_group(accel)
-
-    def ccheckbox_clicked(self, widget):
-        checked = self.ccheckbox.get_active()
-        self.lsbutton.set_sensitive(not checked)
-        if checked:
-            self.lsbutton_stopped()
-            self.emit('command', "continuous_listen")
-            self.set_icon_active()
-        else:
-            self.emit('command', "continuous_stop")
-            self.set_icon_inactive()
-
-    def lsbutton_stopped(self):
-        self.lsbutton.set_label("Listen")
-
-    def lsbutton_clicked(self, button):
-        val = self.lsbutton.get_label()
-        if val == "Listen":
-            self.emit("command", "listen")
-            self.lsbutton.set_label("Stop")
-            # Clear the label
-            self.label.set_text("")
-            self.set_icon_active()
-        else:
-            self.lsbutton_stopped()
-            self.emit("command", "stop")
-            self.set_icon_inactive()
-
-    def run(self):
-        # Set the default icon
-        self.set_icon_inactive()
-        self.window.show_all()
-        if self.continuous:
-            self.set_icon_active()
-            self.ccheckbox.set_active(True)
-
-    def accel_quit(self, accel_group, acceleratable, keyval, modifier):
-        self.emit("command", "quit")
-
-    def delete_event(self, x, y):
-        self.emit("command", "quit")
-
-    def finished(self, text):
-        # If the continuous isn't pressed
-        if not self.ccheckbox.get_active():
-            self.lsbutton_stopped()
-            self.set_icon_inactive()
-        self.label.set_text(text)
-
-    def set_icon_active_asset(self, i):
-        self.icon_active = i
-
-    def set_icon_inactive_asset(self, i):
-        self.icon_inactive = i
-
-    def set_icon_active(self):
-        Gtk.Window.set_default_icon_from_file(self.icon_active)
-
-    def set_icon_inactive(self):
-        Gtk.Window.set_default_icon_from_file(self.icon_inactive)
diff --git a/hasher.py b/hasher.py
deleted file mode 100644
index 4aebd51..0000000
--- a/hasher.py
+++ /dev/null
@@ -1,37 +0,0 @@
-# This is part of Kaylee
-# -- this code is licensed GPLv3
-# Copyright 2015-2016 Clayton G. Hobbs
-# Portions Copyright 2013 Jezra
-
-import json
-import hashlib
-
-class Hasher:
-    """Keep track of hashes for Kaylee"""
-
-    def __init__(self, config):
-        self.config = config
-        try:
-            with open(self.config.hash_file, 'r') as f:
-                self.hashes = json.load(f)
-        except IOError:
-            # No stored hash
-            self.hashes = {}
-
-    def __getitem__(self, hashname):
-        try:
-            return self.hashes[hashname]
-        except (KeyError, TypeError):
-            return None
-
-    def __setitem__(self, hashname, value):
-        self.hashes[hashname] = value
-
-    def get_hash_object(self):
-        """Returns an object to compute a new hash"""
-        return hashlib.sha256()
-
-    def store(self):
-        """Store the current hashes into a the hash file"""
-        with open(self.config.hash_file, 'w') as f:
-            json.dump(self.hashes, f)
diff --git a/kaylee.py b/kaylee.py
index 298a2c6..082d6b6 100755
--- a/kaylee.py
+++ b/kaylee.py
@@ -1,212 +1,10 @@
 #!/usr/bin/env python3
-
 # This is part of Kaylee
 # -- this code is licensed GPLv3
 # Copyright 2015-2016 Clayton G. Hobbs
 # Portions Copyright 2013 Jezra
 
-import sys
-import signal
-import os.path
-import subprocess
-from gi.repository import GObject, GLib
-
-from recognizer import Recognizer
-from config import Config
-from languageupdater import LanguageUpdater
-from numberparser import NumberParser
-from hasher import Hasher
-
-
-class Kaylee:
-
-    def __init__(self):
-        self.ui = None
-        self.options = {}
-        ui_continuous_listen = False
-        self.continuous_listen = False
-
-        # Load configuration
-        self.config = Config()
-        self.options = vars(self.config.options)
-        self.commands = self.options['commands']
-
-        # Create number parser for later use
-        self.number_parser = NumberParser()
-
-        # Create a hasher
-        self.hasher = Hasher(self.config)
-
-        # Create the strings file
-        self.update_strings_file_if_changed()
-
-        if self.options['interface']:
-            if self.options['interface'] == "g":
-                from gtkui import UI
-            elif self.options['interface'] == "gt":
-                from gtktrayui import UI
-            else:
-                print("no GUI defined")
-                sys.exit()
-
-            self.ui = UI(self.options, self.options['continuous'])
-            self.ui.connect("command", self.process_command)
-            # Can we load the icon resource?
-            icon = self.load_resource("icon.png")
-            if icon:
-                self.ui.set_icon_active_asset(icon)
-            # Can we load the icon_inactive resource?
-            icon_inactive = self.load_resource("icon_inactive.png")
-            if icon_inactive:
-                self.ui.set_icon_inactive_asset(icon_inactive)
-
-        if self.options['history']:
-            self.history = []
-
-        # Update the language if necessary
-        self.language_updater = LanguageUpdater(self.config)
-        self.language_updater.update_language_if_changed()
-
-        # Create the recognizer
-        self.recognizer = Recognizer(self.config)
-        self.recognizer.connect('finished', self.recognizer_finished)
+from kayleevc import kaylee
 
-    def update_voice_commands_if_changed(self):
-        """Use hashes to test if the voice commands have changed"""
-        stored_hash = self.hasher['voice_commands']
-
-        # Calculate the hash the voice commands have right now
-        hasher = self.hasher.get_hash_object()
-        for voice_cmd in self.commands.keys():
-            hasher.update(voice_cmd.encode('utf-8'))
-            # Add a separator to avoid odd behavior
-            hasher.update('\n'.encode('utf-8'))
-        new_hash = hasher.hexdigest()
-
-        if new_hash != stored_hash:
-            self.create_strings_file()
-            self.hasher['voice_commands'] = new_hash
-            self.hasher.store()
-
-    def create_strings_file(self):
-        # Open the strings file
-        with open(self.config.strings_file, 'w') as strings:
-            # Add command words to the corpus
-            for voice_cmd in sorted(self.commands.keys()):
-                strings.write(voice_cmd.strip().replace('%d', '') + "\n")
-            # Add number words to the corpus
-            for word in self.number_parser.number_words:
-                strings.write(word + "\n")
-
-    def log_history(self, text):
-        if self.options['history']:
-            self.history.append(text)
-            if len(self.history) > self.options['history']:
-                # Pop off the first item
-                self.history.pop(0)
-
-            # Open and truncate the history file
-            with open(self.config.history_file, 'w') as hfile:
-                for line in self.history:
-                    hfile.write(line + '\n')
-
-    def run_command(self, cmd):
-        """Print the command, then run it"""
-        print(cmd)
-        subprocess.call(cmd, shell=True)
-
-    def recognizer_finished(self, recognizer, text):
-        t = text.lower()
-        numt, nums = self.number_parser.parse_all_numbers(t)
-        # Is there a matching command?
-        if t in self.commands:
-            # Run the valid_sentence_command if it's set
-            if self.options['valid_sentence_command']:
-                subprocess.call(self.options['valid_sentence_command'],
-                                shell=True)
-            cmd = self.commands[t]
-            # Should we be passing words?
-            if self.options['pass_words']:
-                cmd += " " + t
-            self.run_command(cmd)
-            self.log_history(text)
-        elif numt in self.commands:
-            # Run the valid_sentence_command if it's set
-            if self.options['valid_sentence_command']:
-                subprocess.call(self.options['valid_sentence_command'],
-                                shell=True)
-            cmd = self.commands[numt]
-            cmd = cmd.format(*nums)
-            # Should we be passing words?
-            if self.options['pass_words']:
-                cmd += " " + t
-            self.run_command(cmd)
-            self.log_history(text)
-        else:
-            # Run the invalid_sentence_command if it's set
-            if self.options['invalid_sentence_command']:
-                subprocess.call(self.options['invalid_sentence_command'],
-                                shell=True)
-            print("no matching command {0}".format(t))
-        # If there is a UI and we are not continuous listen
-        if self.ui:
-            if not self.continuous_listen:
-                # Stop listening
-                self.recognizer.pause()
-            # Let the UI know that there is a finish
-            self.ui.finished(t)
-
-    def run(self):
-        if self.ui:
-            self.ui.run()
-        else:
-            self.recognizer.listen()
-
-    def quit(self):
-        sys.exit()
-
-    def process_command(self, UI, command):
-        print(command)
-        if command == "listen":
-            self.recognizer.listen()
-        elif command == "stop":
-            self.recognizer.pause()
-        elif command == "continuous_listen":
-            self.continuous_listen = True
-            self.recognizer.listen()
-        elif command == "continuous_stop":
-            self.continuous_listen = False
-            self.recognizer.pause()
-        elif command == "quit":
-            self.quit()
-
-    def load_resource(self, string):
-        local_data = os.path.join(os.path.dirname(__file__), 'data')
-        paths = ["/usr/share/kaylee/", "/usr/local/share/kaylee", local_data]
-        for path in paths:
-            resource = os.path.join(path, string)
-            if os.path.exists(resource):
-                return resource
-        # If we get this far, no resource was found
-        return False
-
-
-if __name__ == "__main__":
-    # Make our kaylee object
-    kaylee = Kaylee()
-    # Init gobject threads
-    GObject.threads_init()
-    # We want a main loop
-    main_loop = GObject.MainLoop()
-    # Handle sigint
-    signal.signal(signal.SIGINT, signal.SIG_DFL)
-    # Run the kaylee
+if __name__ == '__main__':
     kaylee.run()
-    # Start the main loop
-    try:
-        main_loop.run()
-    except:
-        print("time to quit")
-        main_loop.quit()
-        sys.exit()
-
diff --git a/kayleevc/__init__.py b/kayleevc/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/kayleevc/__init__.py
diff --git a/gtktrayui.py b/kayleevc/gui.py
index f595f26..3369e33 100644
--- a/gtktrayui.py
+++ b/kayleevc/gui.py
@@ -10,7 +10,7 @@ from gi.repository import GObject
 gi.require_version('Gtk', '3.0')
 from gi.repository import Gtk, Gdk
 
-class UI(GObject.GObject):
+class GTKTrayInterface(GObject.GObject):
     __gsignals__ = {
         'command' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,))
     }
@@ -105,3 +105,105 @@ class UI(GObject.GObject):
 
     def set_icon_inactive(self):
         self.statusicon.set_from_file(self.icon_inactive)
+
+class GTKInterface(GObject.GObject):
+    __gsignals__ = {
+        'command' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,))
+    }
+
+    def __init__(self, args, continuous):
+        GObject.GObject.__init__(self)
+        self.continuous = continuous
+        # Make a window
+        self.window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
+        self.window.connect("delete_event", self.delete_event)
+        # Give the window a name
+        self.window.set_title("Kaylee")
+        self.window.set_resizable(False)
+
+        layout = Gtk.VBox()
+        self.window.add(layout)
+        # Make a listen/stop button
+        self.lsbutton = Gtk.Button("Listen")
+        layout.add(self.lsbutton)
+        # Make a continuous button
+        self.ccheckbox = Gtk.CheckButton("Continuous Listen")
+        layout.add(self.ccheckbox)
+
+        # Connect the buttons
+        self.lsbutton.connect("clicked", self.lsbutton_clicked)
+        self.ccheckbox.connect("clicked", self.ccheckbox_clicked)
+
+        # Add a label to the UI to display the last command
+        self.label = Gtk.Label()
+        layout.add(self.label)
+
+        # Create an accellerator group for this window
+        accel = Gtk.AccelGroup()
+        # Add the ctrl+q to quit
+        accel.connect(Gdk.keyval_from_name('q'), Gdk.ModifierType.CONTROL_MASK,
+                Gtk.AccelFlags.VISIBLE, self.accel_quit)
+        # Lock the group
+        accel.lock()
+        # Add the group to the window
+        self.window.add_accel_group(accel)
+
+    def ccheckbox_clicked(self, widget):
+        checked = self.ccheckbox.get_active()
+        self.lsbutton.set_sensitive(not checked)
+        if checked:
+            self.lsbutton_stopped()
+            self.emit('command', "continuous_listen")
+            self.set_icon_active()
+        else:
+            self.emit('command', "continuous_stop")
+            self.set_icon_inactive()
+
+    def lsbutton_stopped(self):
+        self.lsbutton.set_label("Listen")
+
+    def lsbutton_clicked(self, button):
+        val = self.lsbutton.get_label()
+        if val == "Listen":
+            self.emit("command", "listen")
+            self.lsbutton.set_label("Stop")
+            # Clear the label
+            self.label.set_text("")
+            self.set_icon_active()
+        else:
+            self.lsbutton_stopped()
+            self.emit("command", "stop")
+            self.set_icon_inactive()
+
+    def run(self):
+        # Set the default icon
+        self.set_icon_inactive()
+        self.window.show_all()
+        if self.continuous:
+            self.set_icon_active()
+            self.ccheckbox.set_active(True)
+
+    def accel_quit(self, accel_group, acceleratable, keyval, modifier):
+        self.emit("command", "quit")
+
+    def delete_event(self, x, y):
+        self.emit("command", "quit")
+
+    def finished(self, text):
+        # If the continuous isn't pressed
+        if not self.ccheckbox.get_active():
+            self.lsbutton_stopped()
+            self.set_icon_inactive()
+        self.label.set_text(text)
+
+    def set_icon_active_asset(self, i):
+        self.icon_active = i
+
+    def set_icon_inactive_asset(self, i):
+        self.icon_inactive = i
+
+    def set_icon_active(self):
+        Gtk.Window.set_default_icon_from_file(self.icon_active)
+
+    def set_icon_inactive(self):
+        Gtk.Window.set_default_icon_from_file(self.icon_inactive)
diff --git a/kayleevc/kaylee.py b/kayleevc/kaylee.py
new file mode 100644
index 0000000..a99f9d4
--- /dev/null
+++ b/kayleevc/kaylee.py
@@ -0,0 +1,207 @@
+# This is part of Kaylee
+# -- this code is licensed GPLv3
+# Copyright 2015-2016 Clayton G. Hobbs
+# Portions Copyright 2013 Jezra
+
+import sys
+import signal
+import os.path
+import subprocess
+from gi.repository import GObject, GLib
+
+from kayleevc.recognizer import Recognizer
+from kayleevc.util import *
+from kayleevc.numbers import NumberParser
+
+
+class Kaylee:
+
+    def __init__(self):
+        self.ui = None
+        self.options = {}
+        ui_continuous_listen = False
+        self.continuous_listen = False
+
+        # Load configuration
+        self.config = Config()
+        self.options = vars(self.config.options)
+        self.commands = self.options['commands']
+
+        # Create number parser for later use
+        self.number_parser = NumberParser()
+
+        # Create a hasher
+        self.hasher = Hasher(self.config)
+
+        # Create the strings file
+        self.update_voice_commands_if_changed()
+
+        if self.options['interface']:
+            if self.options['interface'] == "g":
+                from kayleevc.gui import GTKInterface as UI
+            elif self.options['interface'] == "gt":
+                from kayleevc.gui import GTKTrayInterface as UI
+            else:
+                print("no GUI defined")
+                sys.exit()
+
+            self.ui = UI(self.options, self.options['continuous'])
+            self.ui.connect("command", self.process_command)
+            # Can we load the icon resource?
+            icon = self.load_resource("icon.png")
+            if icon:
+                self.ui.set_icon_active_asset(icon)
+            # Can we load the icon_inactive resource?
+            icon_inactive = self.load_resource("icon_inactive.png")
+            if icon_inactive:
+                self.ui.set_icon_inactive_asset(icon_inactive)
+
+        if self.options['history']:
+            self.history = []
+
+        # Update the language if necessary
+        self.language_updater = LanguageUpdater(self.config)
+        self.language_updater.update_language_if_changed()
+
+        # Create the recognizer
+        self.recognizer = Recognizer(self.config)
+        self.recognizer.connect('finished', self.recognizer_finished)
+
+    def update_voice_commands_if_changed(self):
+        """Use hashes to test if the voice commands have changed"""
+        stored_hash = self.hasher['voice_commands']
+
+        # Calculate the hash the voice commands have right now
+        hasher = self.hasher.get_hash_object()
+        for voice_cmd in self.commands.keys():
+            hasher.update(voice_cmd.encode('utf-8'))
+            # Add a separator to avoid odd behavior
+            hasher.update('\n'.encode('utf-8'))
+        new_hash = hasher.hexdigest()
+
+        if new_hash != stored_hash:
+            self.create_strings_file()
+            self.hasher['voice_commands'] = new_hash
+            self.hasher.store()
+
+    def create_strings_file(self):
+        # Open the strings file
+        with open(self.config.strings_file, 'w') as strings:
+            # Add command words to the corpus
+            for voice_cmd in sorted(self.commands.keys()):
+                strings.write(voice_cmd.strip().replace('%d', '') + "\n")
+            # Add number words to the corpus
+            for word in self.number_parser.number_words:
+                strings.write(word + "\n")
+
+    def log_history(self, text):
+        if self.options['history']:
+            self.history.append(text)
+            if len(self.history) > self.options['history']:
+                # Pop off the first item
+                self.history.pop(0)
+
+            # Open and truncate the history file
+            with open(self.config.history_file, 'w') as hfile:
+                for line in self.history:
+                    hfile.write(line + '\n')
+
+    def run_command(self, cmd):
+        """Print the command, then run it"""
+        print(cmd)
+        subprocess.call(cmd, shell=True)
+
+    def recognizer_finished(self, recognizer, text):
+        t = text.lower()
+        numt, nums = self.number_parser.parse_all_numbers(t)
+        # Is there a matching command?
+        if t in self.commands:
+            # Run the valid_sentence_command if it's set
+            if self.options['valid_sentence_command']:
+                subprocess.call(self.options['valid_sentence_command'],
+                                shell=True)
+            cmd = self.commands[t]
+            # Should we be passing words?
+            if self.options['pass_words']:
+                cmd += " " + t
+            self.run_command(cmd)
+            self.log_history(text)
+        elif numt in self.commands:
+            # Run the valid_sentence_command if it's set
+            if self.options['valid_sentence_command']:
+                subprocess.call(self.options['valid_sentence_command'],
+                                shell=True)
+            cmd = self.commands[numt]
+            cmd = cmd.format(*nums)
+            # Should we be passing words?
+            if self.options['pass_words']:
+                cmd += " " + t
+            self.run_command(cmd)
+            self.log_history(text)
+        else:
+            # Run the invalid_sentence_command if it's set
+            if self.options['invalid_sentence_command']:
+                subprocess.call(self.options['invalid_sentence_command'],
+                                shell=True)
+            print("no matching command {0}".format(t))
+        # If there is a UI and we are not continuous listen
+        if self.ui:
+            if not self.continuous_listen:
+                # Stop listening
+                self.recognizer.pause()
+            # Let the UI know that there is a finish
+            self.ui.finished(t)
+
+    def run(self):
+        if self.ui:
+            self.ui.run()
+        else:
+            self.recognizer.listen()
+
+    def quit(self):
+        sys.exit()
+
+    def process_command(self, UI, command):
+        print(command)
+        if command == "listen":
+            self.recognizer.listen()
+        elif command == "stop":
+            self.recognizer.pause()
+        elif command == "continuous_listen":
+            self.continuous_listen = True
+            self.recognizer.listen()
+        elif command == "continuous_stop":
+            self.continuous_listen = False
+            self.recognizer.pause()
+        elif command == "quit":
+            self.quit()
+
+    def load_resource(self, string):
+        local_data = os.path.join(os.path.dirname(__file__), '..', 'data')
+        paths = ["/usr/share/kaylee/", "/usr/local/share/kaylee", local_data]
+        for path in paths:
+            resource = os.path.join(path, string)
+            if os.path.exists(resource):
+                return resource
+        # If we get this far, no resource was found
+        return False
+
+
+def run():
+    # Make our kaylee object
+    kaylee = Kaylee()
+    # Init gobject threads
+    GObject.threads_init()
+    # We want a main loop
+    main_loop = GObject.MainLoop()
+    # Handle sigint
+    signal.signal(signal.SIGINT, signal.SIG_DFL)
+    # Run the kaylee
+    kaylee.run()
+    # Start the main loop
+    try:
+        main_loop.run()
+    except:
+        print("time to quit")
+        main_loop.quit()
+        sys.exit()
diff --git a/numberparser.py b/kayleevc/numbers.py
index fec07f2..6d41b63 100644
--- a/numberparser.py
+++ b/kayleevc/numbers.py
@@ -1,10 +1,9 @@
 #!/usr/bin/env python
-# numberparser.py - Translate words to decimal
-
 # This is part of Kaylee
 # -- this code is licensed GPLv3
 # Copyright 2015-2016 Clayton G. Hobbs
 # Portions Copyright 2013 Jezra
+
 import re
 
 # Define the mappings from words to numbers
diff --git a/recognizer.py b/kayleevc/recognizer.py
index b54c055..b54c055 100644
--- a/recognizer.py
+++ b/kayleevc/recognizer.py
diff --git a/kayleevc/util.py b/kayleevc/util.py
new file mode 100644
index 0000000..5c93b7f
--- /dev/null
+++ b/kayleevc/util.py
@@ -0,0 +1,202 @@
+# This is part of Kaylee
+# -- this code is licensed GPLv3
+# Copyright 2015-2016 Clayton G. Hobbs
+# Portions Copyright 2013 Jezra
+
+import re
+import json
+import hashlib
+import os
+from argparse import ArgumentParser, Namespace
+
+import requests
+
+from gi.repository import GLib
+
+class Config:
+    """Keep track of the configuration of Kaylee"""
+    # Name of the program, for later use
+    program_name = "kaylee"
+
+    # Directories
+    conf_dir = os.path.join(GLib.get_user_config_dir(), program_name)
+    cache_dir = os.path.join(GLib.get_user_cache_dir(), program_name)
+    data_dir = os.path.join(GLib.get_user_data_dir(), program_name)
+
+    # Configuration files
+    command_file = os.path.join(conf_dir, "commands.conf")
+    opt_file = os.path.join(conf_dir, "options.json")
+
+    # Cache files
+    history_file = os.path.join(cache_dir, program_name + "history")
+    hash_file = os.path.join(cache_dir, "hash.json")
+
+    # Data files
+    strings_file = os.path.join(data_dir, "sentences.corpus")
+    lang_file = os.path.join(data_dir, 'lm')
+    dic_file = os.path.join(data_dir, 'dic')
+
+    def __init__(self):
+        # Ensure necessary directories exist
+        self._make_dir(self.conf_dir)
+        self._make_dir(self.cache_dir)
+        self._make_dir(self.data_dir)
+
+        # Set up the argument parser
+        self.parser = ArgumentParser()
+        self.parser.add_argument("-i", "--interface", type=str,
+                dest="interface", action='store',
+                help="Interface to use (if any). 'g' for GTK or 'gt' for GTK" +
+                " system tray icon")
+
+        self.parser.add_argument("-c", "--continuous",
+                action="store_true", dest="continuous", default=False,
+                help="Start interface with 'continuous' listen enabled")
+
+        self.parser.add_argument("-p", "--pass-words",
+                action="store_true", dest="pass_words", default=False,
+                help="Pass the recognized words as arguments to the shell" +
+                " command")
+
+        self.parser.add_argument("-H", "--history", type=int,
+                action="store", dest="history",
+                help="Number of commands to store in history file")
+
+        self.parser.add_argument("-m", "--microphone", type=int,
+                action="store", dest="microphone", default=None,
+                help="Audio input card to use (if other than system default)")
+
+        self.parser.add_argument("--valid-sentence-command", type=str,
+                dest="valid_sentence_command", action='store',
+                help="Command to run when a valid sentence is detected")
+
+        self.parser.add_argument("--invalid-sentence-command", type=str,
+                dest="invalid_sentence_command", action='store',
+                help="Command to run when an invalid sentence is detected")
+
+        # Read the configuration file
+        self._read_options_file()
+
+        # Parse command-line arguments, overriding config file as appropriate
+        self.parser.parse_args(namespace=self.options)
+
+    def _make_dir(self, directory):
+        if not os.path.exists(directory):
+            os.makedirs(directory)
+
+    def _read_options_file(self):
+        try:
+            with open(self.opt_file, 'r') as f:
+                self.options = json.load(f)
+                self.options = Namespace(**self.options)
+        except FileNotFoundError:
+            # Make an empty options namespace
+            self.options = Namespace()
+
+class Hasher:
+    """Keep track of hashes for Kaylee"""
+
+    def __init__(self, config):
+        self.config = config
+        try:
+            with open(self.config.hash_file, 'r') as f:
+                self.hashes = json.load(f)
+        except IOError:
+            # No stored hash
+            self.hashes = {}
+
+    def __getitem__(self, hashname):
+        try:
+            return self.hashes[hashname]
+        except (KeyError, TypeError):
+            return None
+
+    def __setitem__(self, hashname, value):
+        self.hashes[hashname] = value
+
+    def get_hash_object(self):
+        """Returns an object to compute a new hash"""
+        return hashlib.sha256()
+
+    def store(self):
+        """Store the current hashes into a the hash file"""
+        with open(self.config.hash_file, 'w') as f:
+            json.dump(self.hashes, f)
+
+class LanguageUpdater:
+    """
+    Handles updating the language using the online lmtool.
+
+    This class provides methods to check if the corpus has changed, and to
+    update the language to match the new corpus using the lmtool.  This allows
+    us to automatically update the language if the corpus has changed, saving
+    the user from having to do this manually.
+    """
+
+    def __init__(self, config):
+        self.config = config
+        self.hasher = Hasher(config)
+
+    def update_language_if_changed(self):
+        """Test if the language has changed, and if it has, update it"""
+        if self.language_has_changed():
+            self.update_language()
+            self.save_language_hash()
+
+    def language_has_changed(self):
+        """Use hashes to test if the language has changed"""
+        self.stored_hash = self.hasher['language']
+
+        # Calculate the hash the language file has right now
+        hasher = self.hasher.get_hash_object()
+        with open(self.config.strings_file, 'rb') as sfile:
+            buf = sfile.read()
+            hasher.update(buf)
+        self.new_hash = hasher.hexdigest()
+
+        return self.new_hash != self.stored_hash
+
+    def update_language(self):
+        """Update the language using the online lmtool"""
+        print('Updating language using online lmtool')
+
+        host = 'http://www.speech.cs.cmu.edu'
+        url = host + '/cgi-bin/tools/lmtool/run'
+
+        # Submit the corpus to the lmtool
+        response_text = ""
+        with open(self.config.strings_file, 'rb') as corpus:
+            files = {'corpus': corpus}
+            values = {'formtype': 'simple'}
+
+            r = requests.post(url, files=files, data=values)
+            response_text = r.text
+
+        # Parse response to get URLs of the files we need
+        path_re = r'.*<title>Index of (.*?)</title>.*'
+        number_re = r'.*TAR([0-9]*?)\.tgz.*'
+        for line in response_text.split('\n'):
+            # If we found the directory, keep it and don't break
+            if re.search(path_re, line):
+                path = host + re.sub(path_re, r'\1', line)
+            # If we found the number, keep it and break
+            elif re.search(number_re, line):
+                number = re.sub(number_re, r'\1', line)
+                break
+
+        lm_url = path + '/' + number + '.lm'
+        dic_url = path + '/' + number + '.dic'
+
+        self._download_file(lm_url, self.config.lang_file)
+        self._download_file(dic_url, self.config.dic_file)
+
+    def save_language_hash(self):
+        self.hasher['language'] = self.new_hash
+        self.hasher.store()
+
+    def _download_file(self, url, path):
+        r = requests.get(url, stream=True)
+        if r.status_code == 200:
+            with open(path, 'wb') as f:
+                for chunk in r:
+                    f.write(chunk)
diff --git a/languageupdater.py b/languageupdater.py
deleted file mode 100644
index 3f36d06..0000000
--- a/languageupdater.py
+++ /dev/null
@@ -1,80 +0,0 @@
-# This is part of Kaylee
-# -- this code is licensed GPLv3
-# Copyright 2015-2016 Clayton G. Hobbs
-# Portions Copyright 2013 Jezra
-
-import re
-
-import requests
-
-from hasher import Hasher
-
-class LanguageUpdater:
-
-    def __init__(self, config):
-        self.config = config
-        self.hasher = Hasher(config)
-
-    def update_language_if_changed(self):
-        """Test if the language has changed, and if it has, update it"""
-        if self.language_has_changed():
-            self.update_language()
-            self.save_language_hash()
-
-    def language_has_changed(self):
-        """Use hashes to test if the language has changed"""
-        self.stored_hash = self.hasher['language']
-
-        # Calculate the hash the language file has right now
-        hasher = self.hasher.get_hash_object()
-        with open(self.config.strings_file, 'rb') as sfile:
-            buf = sfile.read()
-            hasher.update(buf)
-        self.new_hash = hasher.hexdigest()
-
-        return self.new_hash != self.stored_hash
-
-    def update_language(self):
-        """Update the language using the online lmtool"""
-        print('Updating language using online lmtool')
-
-        host = 'http://www.speech.cs.cmu.edu'
-        url = host + '/cgi-bin/tools/lmtool/run'
-
-        # Submit the corpus to the lmtool
-        response_text = ""
-        with open(self.config.strings_file, 'rb') as corpus:
-            files = {'corpus': corpus}
-            values = {'formtype': 'simple'}
-
-            r = requests.post(url, files=files, data=values)
-            response_text = r.text
-
-        # Parse response to get URLs of the files we need
-        path_re = r'.*<title>Index of (.*?)</title>.*'
-        number_re = r'.*TAR([0-9]*?)\.tgz.*'
-        for line in response_text.split('\n'):
-            # If we found the directory, keep it and don't break
-            if re.search(path_re, line):
-                path = host + re.sub(path_re, r'\1', line)
-            # If we found the number, keep it and break
-            elif re.search(number_re, line):
-                number = re.sub(number_re, r'\1', line)
-                break
-
-        lm_url = path + '/' + number + '.lm'
-        dic_url = path + '/' + number + '.dic'
-
-        self._download_file(lm_url, self.config.lang_file)
-        self._download_file(dic_url, self.config.dic_file)
-
-    def save_language_hash(self):
-        self.hasher['language'] = self.new_hash
-        self.hasher.store()
-
-    def _download_file(self, url, path):
-        r = requests.get(url, stream=True)
-        if r.status_code == 200:
-            with open(path, 'wb') as f:
-                for chunk in r:
-                    f.write(chunk)