From a40bbe25e236c85d3a7e3e86576395c2e4b65b0b Mon Sep 17 00:00:00 2001 From: "Clayton G. Hobbs" Date: Sun, 27 Dec 2015 17:19:38 -0500 Subject: Rename blather.py to kaylee.py --- README.md | 14 ++--- blather.py | 186 ------------------------------------------------------------- kaylee.py | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 193 insertions(+), 193 deletions(-) delete mode 100755 blather.py create mode 100755 kaylee.py diff --git a/README.md b/README.md index 1900197..0b6c2d0 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,11 @@ but adds a lot of features that go beyond the original purpose of Blather. 1. Move commands.tmp to ~/.config/kaylee/commands.conf and fill the file with sentences and command to run -2. Run blather.py. This will generate ~/.local/share/kaylee/sentences.corpus +2. Run kaylee.py. This will generate ~/.local/share/kaylee/sentences.corpus based on sentences in the 'commands' file, then use to create and save a new language model and dictionary. - * For GTK UI, run blather.py -i g + * For GTK UI, run kaylee.py -i g * To start a UI in 'continuous' listen mode, use the -c flag * To use a microphone other than the system default, use the -m flag 3. Start talking @@ -36,13 +36,13 @@ accordingly. ### Examples * To run Kaylee with the GTK UI and start in continuous listen mode: -`./blather.py -i g -c` +`./kaylee.py -i g -c` * To run Kaylee with no UI and using a USB microphone recognized and device 2: -`./blather.py -m 2` +`./kaylee.py -m 2` * To have Kaylee pass the matched sentence to the executed command: -`./blather.py -p` +`./kaylee.py -p` **explanation:** if the commands.conf contains: `good morning world: example_command.sh` @@ -50,10 +50,10 @@ then 3 arguments, 'good', 'morning', and 'world' would get passed to example_command.sh as `example_command.sh good morning world` * To run a command when a valid sentence has been detected: - `./blather.py --valid-sentence-command=/path/to/command` + `./kaylee.py --valid-sentence-command=/path/to/command` **note:** this can be set in the options.yml file * To run a command when a invalid sentence has been detected: - `./blather.py --invalid-sentence-command=/path/to/command` + `./kaylee.py --invalid-sentence-command=/path/to/command` **note:** this can be set in the options.yml file ### Finding the Device Number of a USB microphone diff --git a/blather.py b/blather.py deleted file mode 100755 index 23802e8..0000000 --- a/blather.py +++ /dev/null @@ -1,186 +0,0 @@ -#!/usr/bin/env python3 - -# This is part of Kaylee -# -- this code is licensed GPLv3 -# Copyright 2013 Jezra -# Copyright 2015 Clayton G. Hobbs - -from __future__ import print_function -import sys -import signal -import hashlib -import os.path -import subprocess -from gi.repository import GObject, GLib -import json - -from recognizer import Recognizer -from config import Config -from languageupdater import LanguageUpdater - - -class Blather: - - def __init__(self): - self.ui = None - self.options = {} - ui_continuous_listen = False - self.continuous_listen = False - - self.commands = {} - - # Load configuration - self.config = Config() - self.options = vars(self.config.options) - - # Read the commands - self.read_commands() - - 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) - - def read_commands(self): - # Read the commands file - file_lines = open(self.config.command_file) - strings = open(self.config.strings_file, "w") - for line in file_lines: - # Trim the white spaces - line = line.strip() - # If the line has length and the first char isn't a hash - if len(line) and line[0] != "#": - # This is a parsible line - (key, value) = line.split(":", 1) - self.commands[key.strip().lower()] = value.strip() - strings.write(key.strip() + "\n") - # Close the strings file - strings.close() - - 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 blather history file - hfile = open(self.config.history_file, "w") - for line in self.history: - hfile.write(line + "\n") - # Close the file - hfile.close() - - 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() - # Is there a matching command? - if t in self.commands: - # Run the valid_sentence_command if there is a valid sentence command - 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) - else: - self.run_command(cmd) - self.log_history(text) - else: - # Run the invalid_sentence_command if there is an invalid sentence command - 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: - blather.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/blather/", "/usr/local/share/blather", 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 blather object - blather = Blather() - # 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 blather - blather.run() - # Start the main loop - try: - main_loop.run() - except: - print("time to quit") - main_loop.quit() - sys.exit() - diff --git a/kaylee.py b/kaylee.py new file mode 100755 index 0000000..7aedb22 --- /dev/null +++ b/kaylee.py @@ -0,0 +1,186 @@ +#!/usr/bin/env python3 + +# This is part of Kaylee +# -- this code is licensed GPLv3 +# Copyright 2013 Jezra +# Copyright 2015 Clayton G. Hobbs + +from __future__ import print_function +import sys +import signal +import hashlib +import os.path +import subprocess +from gi.repository import GObject, GLib +import json + +from recognizer import Recognizer +from config import Config +from languageupdater import LanguageUpdater + + +class Kaylee: + + def __init__(self): + self.ui = None + self.options = {} + ui_continuous_listen = False + self.continuous_listen = False + + self.commands = {} + + # Load configuration + self.config = Config() + self.options = vars(self.config.options) + + # Read the commands + self.read_commands() + + 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) + + def read_commands(self): + # Read the commands file + file_lines = open(self.config.command_file) + strings = open(self.config.strings_file, "w") + for line in file_lines: + # Trim the white spaces + line = line.strip() + # If the line has length and the first char isn't a hash + if len(line) and line[0] != "#": + # This is a parsible line + (key, value) = line.split(":", 1) + self.commands[key.strip().lower()] = value.strip() + strings.write(key.strip() + "\n") + # Close the strings file + strings.close() + + 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 + hfile = open(self.config.history_file, "w") + for line in self.history: + hfile.write(line + "\n") + # Close the file + hfile.close() + + 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() + # Is there a matching command? + if t in self.commands: + # Run the valid_sentence_command if there is a valid sentence command + 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) + else: + self.run_command(cmd) + self.log_history(text) + else: + # Run the invalid_sentence_command if there is an invalid sentence command + 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 + kaylee.run() + # Start the main loop + try: + main_loop.run() + except: + print("time to quit") + main_loop.quit() + sys.exit() + -- cgit 1.4.1