예제 #1
0
 def sleep(force=False):
     global sleeping
     if not sleeping or force:
         sleeping = True
         sleep_grammar.set_exclusiveness(True)
         get_engine().process_grammars_context()
     notify('sleep')
예제 #2
0
 def wake(force=False):
     get_engine().start_saving_adaptation_state()
     global sleeping
     if sleeping or force:
         sleeping = False
         sleep_grammar.set_exclusiveness(False)
         notify('wake')
예제 #3
0
    def setupFinalDflyGrammar(self):
        log.info("Setting up final grammar.")

        assert not self.dflyGrammar
        self.dflyGrammar = Grammar(self.fullName + "Grammar")
        if self.finalDflyRule:
            self.dflyGrammar.add_rule(self.finalDflyRule)
        for r in self.independentRules:
            self.dflyGrammar.add_rule(self.concreteRules[r])
        loadStart = time.time()
        self.dflyGrammar.load()
        loadEnd = time.time()
        log.info("Grammar load time: %ss" % (loadEnd - loadStart))
        get_engine().set_exclusiveness(self.dflyGrammar, 1)

        # These should never be recognized on their own, only as part of the
        # master rule, quirk of dragonfly that you have to do this even though
        # they're only pulled in by ruleref.
        for r in self.seriesRules:
            self.concreteRules[r].disable()
        if self.terminatorRule:
            self.concreteRules[self.terminatorRule].disable()

        # independent rules only enabled via being a dependency need to have disable
        # called on their dragonfly version so that they don't get recognized by
        # themselves, same quirk.
        notEnabledRules = self.dependencyRuleSet - self.baseRuleSet
        for r in notEnabledRules:
            self.concreteRules[r].disable()

        # they're enabled by default, don't activate until explicitly made to
        self.dflyGrammar.disable()
예제 #4
0
    def __init__(self):
        # Natlink doesn't provide a way to poll files or sockets,
        # and it runs in the same thread as Dragon itself so we can't
        # block, so we run on a periodic timer.
        DragonflyNode.__init__(self)
        self.timer = get_engine().create_timer(self._eventLoop, 1)
        self.buf = ""

        # rule cache, stores types:
        # hash -> HashedRule
        # hash of component rule hashes -> MasterGrammar
        # hash of unloaded rule -> NeedDependency
        self.hashedRules = {}
        self.activatedRules = set() # set of HashRules

        self.activeMasterGrammar = None

        self.lastMicState = None
        self.lastLoadState = None
        self.recognitionState = "success"

        # hashes we've asked for but haven't got a reply for yet
        self.requestedLoads = set()

        self.globalRule = GlobalRules(name="GlobalRules")
        self.globalRuleGrammar = FailureReportingGrammar(self.globalRule.name)
        self.globalRuleGrammar.setClient(self)
        self.globalRuleGrammar.add_rule(self.globalRule)
        self.globalRuleGrammar.load()
        get_engine().set_exclusiveness(self.globalRuleGrammar, 1)

        self.wordLists = {}
예제 #5
0
def run_pytest_suite(engine_name, pytest_options):
    # Get test file paths.
    paths = []
    for name in engine_tests_dict[engine_name]:
        if name.startswith("test_"):
            # Use full module paths so pytest can import the files
            # correctly.
            name = "dragonfly/test/" + name + ".py"

        paths.append(name)

    # Initialize the engine and call connect().
    engine = get_engine(engine_name)
    engine.connect()

    # Prevent the engine from running timers on its own. This lets us
    # avoid race conditions.
    engine._timer_manager.disable()
    try:
        # Run doctests through pytest.main() now that the engine is set up.
        # Pass any specified pytest options, followed by doctest options for
        # compatibility with both Python 2.7 and 3.
        args = pytest_options + [
            '-o', 'doctest_optionflags=ALLOW_UNICODE IGNORE_EXCEPTION_DETAIL'
        ] + paths
        return pytest.main(args)
    finally:
        # Disconnect after the tests.
        engine.disconnect()

        # Check that the dragonfly engine was not changed during the tests.
        assert engine is get_engine(), \
            "The registered engine changed during the test suite."
예제 #6
0
    def __init__(self):
        # Natlink doesn't provide a way to poll files or sockets,
        # and it runs in the same thread as Dragon itself so we can't
        # block, so we run on a periodic timer.
        DragonflyNode.__init__(self)
        self.timer = get_engine().create_timer(self._eventLoop, 1)
        self.buf = ""

        # rule cache, stores types:
        # hash -> HashedRule
        # hash of component rule hashes -> MasterGrammar
        # hash of unloaded rule -> NeedDependency
        self.hashedRules = {}
        self.activatedRules = set()  # set of HashRules

        self.activeMasterGrammar = None

        self.lastMicState = None
        self.lastLoadState = None
        self.recognitionState = "success"

        # hashes we've asked for but haven't got a reply for yet
        self.requestedLoads = set()

        self.globalRule = GlobalRules(name="GlobalRules")
        self.globalRuleGrammar = FailureReportingGrammar(self.globalRule.name)
        self.globalRuleGrammar.setClient(self)
        self.globalRuleGrammar.add_rule(self.globalRule)
        self.globalRuleGrammar.load()
        get_engine().set_exclusiveness(self.globalRuleGrammar, 1)

        self.wordLists = {}
예제 #7
0
def drop_keep_clipboard(nnavi500, capitalization, spacing):
    # Maintain standard spark functionality for non-strings
    if capitalization == 0 and spacing == 0 and nnavi500 == 1:
        Key("c-v").execute()
        return
    # Get clipboard text
    if nnavi500 > 1:
        key = str(nnavi500)
        if key in _CLIP:
            text = _CLIP[key]
        else:
            get_engine().speak("slot empty")
            text = None
    else:
        text = Clipboard.get_system_text()
    # Format if necessary, and paste
    if text is not None:
        cb = Clipboard(from_system=True)
        if capitalization != 0 or spacing != 0:
            text = textformat.TextFormat.formatted_text(
                capitalization, spacing, text)
        Clipboard.set_system_text(text)
        time.sleep(settings.settings([u'miscellaneous', u'keypress_wait'])/1000.)
        Key("c-v").execute()
        time.sleep(settings.settings([u'miscellaneous', u'keypress_wait'])/1000.)
        # Restore the clipboard contents.
        cb.copy_to_system()
예제 #8
0
파일: navigation.py 프로젝트: seekM/caster
def drop_keep_clipboard(nnavi500, nexus, capitalization, spacing):
    # Maintain standard spark functionality for non-strings
    if capitalization == 0 and spacing == 0 and nnavi500 == 1:
        Key("c-v").execute()
        return
    # Get clipboard text
    if nnavi500 > 1:
        key = str(nnavi500)
        if key in nexus.clip:
            text = nexus.clip[key]
        else:
            dragonfly.get_engine().speak("slot empty")
            text = None
    else:
        text = Clipboard.get_system_text()
    # Format if necessary, and paste
    if text is not None:
        cb = Clipboard(from_system=True)
        if capitalization != 0 or spacing != 0:
            text = textformat.TextFormat.formatted_text(capitalization, spacing, text)
        Clipboard.set_system_text(text)
        time.sleep(settings.SETTINGS["miscellaneous"]["keypress_wait"]/1000.)
        Key("c-v").execute()
        time.sleep(settings.SETTINGS["miscellaneous"]["keypress_wait"]/1000.)
        # Restore the clipboard contents.
        cb.copy_to_system()
예제 #9
0
 def sleep(force=False):
     get_engine().stop_saving_adaptation_state()
     global sleeping
     if not sleeping or force:
         sleeping = True
         sleep_grammar.set_exclusiveness(True)
         notify('sleep')
예제 #10
0
    def setupFinalDflyGrammar(self):
        log.info("Setting up final grammar.")

        assert not self.dflyGrammar
        self.dflyGrammar = Grammar(self.fullName + "Grammar")
        if self.finalDflyRule:
            self.dflyGrammar.add_rule(self.finalDflyRule)
        for r in self.independentRules:
            self.dflyGrammar.add_rule(self.concreteRules[r])
        loadStart = time.time()
        self.dflyGrammar.load()
        loadEnd = time.time()
        log.info("Grammar load time: %ss" % (loadEnd - loadStart))
        get_engine().set_exclusiveness(self.dflyGrammar, 1)

        # These should never be recognized on their own, only as part of the
        # master rule, quirk of dragonfly that you have to do this even though
        # they're only pulled in by ruleref.
        for r in self.seriesRules:
            self.concreteRules[r].disable()
        if self.terminatorRule:
            self.concreteRules[self.terminatorRule].disable()

        # independent rules only enabled via being a dependency need to have disable
        # called on their dragonfly version so that they don't get recognized by
        # themselves, same quirk.
        notEnabledRules = self.dependencyRuleSet - self.baseRuleSet
        for r in notEnabledRules:
            self.concreteRules[r].disable()

        # they're enabled by default, don't activate until explicitly made to
        self.dflyGrammar.disable()
예제 #11
0
 def wake(force=False):
     global sleeping
     if sleeping or force:
         sleeping = False
         sleep_grammar.set_exclusiveness(False)
         get_engine().process_grammars_context()
     notify('wake')
예제 #12
0
def report(message, speak=False, console=True, log=False):
    import dragonfly
    if console:
        print message
    if speak:
        dragonfly.get_engine().speak(message)
    if log:
        settings.report_to_file(message)
예제 #13
0
 class SleepRule(MappingRule):
     mapping = {
         "rise from your grave":
         Function(wake) +
         Function(lambda: get_engine().start_saving_adaptation_state()),
         "quiet you":
         Function(lambda: get_engine().stop_saving_adaptation_state()) +
         Function(sleep),
     }
def main(args):
    logging.basicConfig(level=logging.INFO)

    try:
        path = os.path.dirname(__file__)
    except NameError:
        # The "__file__" name is not always available, for example
        # when this module is run from PythonWin.  In this case we
        # simply use the current working directory.
        path = os.getcwd()
        __file__ = os.path.join(path, "kaldi_module_loader_plus.py")

    os.environ.setdefault("GOOGLE_APPLICATION_CREDENTIALS", "gcloud.json")

    # Set any configuration options here as keyword arguments.
    engine = get_engine(
        "kaldi",
        model_dir='kaldi_model_zamia',
        # tmp_dir='kaldi_tmp',  # default for temporary directory
        # vad_aggressiveness=3,  # default aggressiveness of VAD
        # vad_padding_ms=300,
        #vad_padding_start_ms=300,  # default ms of required silence before VAD
        #vad_padding_end_ms=300,  # default ms of required silence after VAD
        #vad_complex_padding_end_ms=1000,  # default ms of required silence after VAD for complex utterances
        # input_device_index=None,  # set to an int to choose a non-default microphone
        auto_add_to_user_lexicon=
        True,  # set to True to possibly use cloud for pronunciations
        cloud_dictation_lang="en-US",  # set to 'gcloud' to use cloud dictation
    )

    if len(args) >= 1 and args[0] == "-l":
        # Show the list of attached microphone devices.
        # Note that this code should only be called after the engine has been initialized above with configuration options,
        # otherwise if this line of code happens before the engine is created, get_engine will create the engine with default arguments.
        get_engine("kaldi").print_mic_list()

    # Call connect() now that the engine configuration is set.
    engine.connect()

    # Register a recognition observer
    observer = Observer()
    observer.register()

    load_sleep_wake_grammar(True)

    directory = CommandModuleDirectory(path, excludes=[__file__])
    directory.load()

    # Start the engine's main recognition loop
    try:
        # Loop forever
        print("Listening...")
        engine.do_recognition()
    except KeyboardInterrupt:
        pass
예제 #15
0
def main():
    logging.basicConfig(level=logging.INFO)

    try:
        path = os.path.dirname(__file__)
    except NameError:
        # The "__file__" name is not always available, for example
        # when this module is run from PythonWin.  In this case we
        # simply use the current working directory.
        path = os.getcwd()
        __file__ = os.path.join(path, "kaldi_module_loader_plus.py")

    if 'devices' in sys.argv:
        print('microphones:', get_engine('kaldi').print_mic_list())
        exit(0)
    # Set any configuration options here as keyword arguments.
    if 'test' in sys.argv:
        engine = get_engine('text')
    else:
        engine = get_engine(
            "kaldi",
            model_dir='kaldi_model',
            # tmp_dir='kaldi_model_zamia.tmp',  # default for temporary directory
            # vad_aggressiveness=3,  # default aggressiveness of VAD
            # vad_padding_start_ms=300,  # default ms of required silence before VAD
            vad_padding_end_ms=500,  # default ms of required silence after VAD
            # vad_complex_padding_end_ms=500,  # default ms of required silence after VAD for complex utterances
            audio_input_device='Blue',
            # auto_add_to_user_lexicon=True,  # set to True to possibly use cloud for pronunciations
            # lazy_compilation=True,  # set to True to parallelize & speed up loading
            # cloud_dictation=None,  # set to 'gcloud' to use cloud dictation
        )

    # Call connect() now that the engine configuration is set.
    engine.connect()

    # Register a recognition observer
    observer = Observer()
    observer.register()

    load_sleep_wake_grammar(True)
    # load_noise_grammar()

    directory = CommandModuleDirectory(path, excludes=[__file__])
    directory.load()

    # Start the engine's main recognition loop
    if 'test' not in sys.argv:
        engine.prepare_for_recognition()
    try:
        # Loop forever
        print("Listening...")
        engine.do_recognition()
    except KeyboardInterrupt:
        pass
예제 #16
0
    class SleepRule(MappingRule):
        mapping = {
            "listen to me":
            Function(wake) +
            Function(lambda: get_engine().start_saving_adaptation_state()),
            "(go to sleep)|(stop listening)":
            Function(lambda: get_engine().stop_saving_adaptation_state()) +
            Function(sleep),

            # "halt listening":   Function(lambda: get_engine().stop_saving_adaptation_state()) + Function(sleep),
        }
예제 #17
0
 class SleepRule(MappingRule):
     mapping = {
         'dragonfly exit':
         Function(halt),
         "start listening":
         Function(wake) +
         Function(lambda: get_engine().start_saving_adaptation_state()),
         "stop listening":
         Function(lambda: get_engine().stop_saving_adaptation_state()) +
         Function(sleep),
         "halt listening":
         Function(lambda: get_engine().stop_saving_adaptation_state()) +
         Function(sleep),
     }
예제 #18
0
파일: modes.py 프로젝트: mrob95/MR-caster
 def __init__(self):
     self.mode = "normal"
     self.frequency = 5
     self.command_titles = ["sublime", "jupyter", "rstudio", "mingw64", "lyx", "scientific notebook"]
     self.command_executables = ["code.exe", "kindle.exe"]
     self.command_contexts = AppContext(title=self.command_titles) | AppContext(self.command_executables)
     self.timer = get_engine().create_timer(self.check_context, self.frequency)
예제 #19
0
def main():
    try:
        path = os.path.dirname(__file__)
    except NameError:
        # The "__file__" name is not always available, for example
        #  when this module is run from PythonWin.  In this case we
        #  simply use the current working directory.
        path = os.getcwd()
        __file__ = os.path.join(path, "dfly-loader-natlink.py")

    engine = get_engine("natlink")
    engine.connect()

    # Register a recognition observer
    observer = Observer()
    observer.register()

    directory = CommandModuleDirectory(path, excludes=[__file__])
    directory.load()

    # Recognize from Dragon in a loop (opens a dialogue window).
    engine.natlink.waitForSpeech()

    # Unload all grammars from the engine so that Dragon doesn't keep
    # recognizing them.
    for grammar in engine.grammars:
        grammar.unload()

    # Disconnect after the dialogue is closed.
    engine.disconnect()
def main():
    desc = "Example script for using the " \
        "'SphinxEngine.process_wave_file' method"
    parser = argparse.ArgumentParser(description=desc)
    parser.add_argument("file", type=argparse.FileType("r"),
                        help="A .wav file.")
    args = parser.parse_args()
    file_path = args.file.name
    args.file.close()

    # Set up the engine.
    # process_wave() handles START_ASLEEP = True (default), but a warning
    # message will be logged. We can suppress that here.
    engine = get_engine("sphinx")
    engine.config.START_ASLEEP = False

    # Voice activity detection config values may need to be adjusted to
    # recognise speech. Do this by changing the decoder's config.
    # Note that these values represent a number of audio frames, not time
    # intervals.
    # For example:
    engine.config.DECODER_CONFIG.set_int("-vad_postspeech", 30)

    # Process the wave file and print any results.
    # Note: process_wave_file() is a generator function.
    engine.connect()
    had_result = False
    for result in engine.process_wave_file(file_path):
        print(result)
        had_result = True

    # Exit with 1 if there were no results.
    if not had_result:
        exit(1)
예제 #21
0
def disconnect():
    print("Disconnecting engine.")
    engine = get_engine()
    if engine.name == 'natlink' and 'natspeak' in sys.executable:
        print("Not calling disconnect() for embedded natlink.")
    else:
        engine.disconnect()
def main():
    logging.basicConfig(level=logging.INFO)

    try:
        path = os.path.dirname(__file__)
    except NameError:
        # The "__file__" name is not always available, for example
        # when this module is run from PythonWin.  In this case we
        # simply use the current working directory.
        path = os.getcwd()
        __file__ = os.path.join(path, "wsr_module_loader_plus.py")

    # Set any configuration options here as keyword arguments.
    engine = get_engine("sapi5inproc")

    # Call connect() now that the engine configuration is set.
    engine.connect()

    # Register a recognition observer
    observer = Observer()
    observer.register()

    load_sleep_wake_grammar(True)

    directory = CommandModuleDirectory(path, excludes=[__file__])
    directory.load()

    # Start the engine's main recognition loop
    try:
        # Recognize from WSR in a loop.
        print("Listening...")
        engine.recognize_forever()
    except KeyboardInterrupt:
        pass
예제 #23
0
def _init_engine(args):
    # Retrieve the engine option pairs from the arguments.
    # TODO Remove multiple engine options per -o arg in version 1.0.0.
    options = {}
    for argument in args.engine_options:
        for options_list in argument:
            for option, value in options_list:
                options[option] = value

    try:
        # Initialize the specified engine with options, catching and
        #  reporting errors.
        engine = get_engine(args.engine, **options)
    except EngineError as e:
        LOG.error(e)
        engine = None

    # Set the engine language if necessary.
    if engine and args.language != engine.language:
        try:
            engine.language = args.language
        except AttributeError:
            LOG.error("Cannot set language for engine %r", engine.name)
            engine = None

    return engine
def disconnect():
    # You shouldn't really run this module in other engines because it uses
    # sphinx-specific engine methods.
    print("Disconnecting engine (only for sphinx)")
    engine = get_engine()
    if engine.name == "sphinx":
        engine.disconnect()
예제 #25
0
def drop(nnavi500, nexus):
    key = str(nnavi500)
    while True:
        failure = False
        try:
            if key in nexus.clip:
                Clipboard.set_system_text(nexus.clip[key])
                Key("c-v").execute()
            else:
                dragonfly.get_engine().speak("slot empty")
            time.sleep(settings.SETTINGS["miscellaneous"]["keypress_wait"] /
                       1000.)
        except Exception:
            failure = True
        if not failure:
            break
예제 #26
0
 def run(self, result):
     self.engine = dragonfly.get_engine(self.engine_name)
     self.engine.connect()
     try:
         return unittest.TestSuite.run(self, result)
     finally:
         self.engine.disconnect()
예제 #27
0
def add_vocab():
    engine = dragonfly.get_engine()
    if engine.name != "natlink":
        utilities.report(
            "feature unavailable in your speech recognition engine",
            speak=True)
        return

    # attempts to get what was highlighted first
    highlighted = context.read_selected_without_altering_clipboard(True)

    # change the following regex to accept alphabetical only
    disallow = "^[A-Za-z]*$"
    selected = None

    if highlighted[0] == 0 and highlighted[1] != "":
        if not re.match(disallow, highlighted[1]):
            utilities.report("only used for single words", speak=True)
            return

        selected = highlighted[1]
    try:
        h_launch.launch(settings.QTYPE_SET, process_set, selected)
    except Exception:
        utilities.simple_log(False)
예제 #28
0
def main():
    try:
        path = os.path.dirname(__file__)
    except NameError:
        # The "__file__" name is not always available, for example
        #  when this module is run from PythonWin.  In this case we
        #  simply use the current working directory.
        path = os.getcwd()
        __file__ = os.path.join(path, "dfly-loader-wsr.py")

    # Initialize and connect the engine.
    # Set any configuration options here as keyword arguments.
    engine = get_engine("sapi5inproc")
    engine.connect()

    # Load grammars.
    directory = CommandModuleDirectory(path, excludes=[__file__])
    directory.load()

    # Define recognition callback functions.
    def on_begin():
        print("Speech start detected.")

    def on_recognition(words):
        print("Recognized: %s" % " ".join(words))

    def on_failure():
        print("Sorry, what was that?")

    # Recognize from WSR in a loop.
    try:
        engine.do_recognition(on_begin, on_recognition, on_failure)
    except KeyboardInterrupt:
        pass
예제 #29
0
파일: utilities.py 프로젝트: paychex/Caster
def clear_log():
    # Function to clear status window.
    # Natlink status window not used an out-of-process mode.
    # ToDo: window_exists utilized when engine launched through Dragonfly CLI via bat in future
    try:
        if sys.platform.startswith('win'):
            clearcmd = "cls"  # Windows OS
        else:
            clearcmd = "clear"  # Linux
        if get_engine()._name == 'natlink':
            import natlinkstatus  # pylint: disable=import-error
            status = natlinkstatus.NatlinkStatus()
            if status.NatlinkIsEnabled() == 1:
                import win32gui  # pylint: disable=import-error
                handle = get_window_by_title(
                    "Messages from Python Macros") or get_window_by_title(
                        "Messages from Natlink")
                rt_handle = win32gui.FindWindowEx(handle, None, "RICHEDIT",
                                                  None)
                win32gui.SetWindowText(rt_handle, "")
            else:
                if window_exists(windowname="Caster: Status Window"):
                    os.system(clearcmd)
        else:
            if window_exists(windowname="Caster: Status Window"):
                os.system(clearcmd)
            else:
                printer.out("clear_log: Not implemented with GUI")
    except Exception as e:
        printer.out(e)
예제 #30
0
 def run(self, result):
     engine = dragonfly.get_engine(self.engine_name)
     engine.connect()
     try:
         return unittest.TestSuite.run(self, result)
     finally:
         engine.disconnect()
예제 #31
0
def main():
    desc = "Example script for using the " \
        "'SphinxEngine.process_wave_file' method"
    parser = argparse.ArgumentParser(description=desc)
    parser.add_argument("file",
                        type=argparse.FileType("r"),
                        help="A .wav file.")
    args = parser.parse_args()
    file_path = args.file.name
    args.file.close()

    # Set up the engine.
    # process_wave() handles START_ASLEEP = True (default), but a warning
    # message will be logged. We can suppress that here.
    engine = get_engine("sphinx")
    engine.config.START_ASLEEP = False

    # Voice activity detection config values may need to be adjusted to
    # recognise speech. Do this by changing the decoder's config.
    # Note that these values represent a number of audio frames, not time
    # intervals.
    # For example:
    engine.config.DECODER_CONFIG.set_int("-vad_postspeech", 30)

    # Process the wave file and print any results.
    # Note: process_wave_file() is a generator function.
    engine.connect()
    had_result = False
    for result in engine.process_wave_file(file_path):
        print(result)
        had_result = True

    # Exit with 1 if there were no results.
    if not had_result:
        exit(1)
예제 #32
0
def main():
    logging.basicConfig(level=logging.INFO)

    try:
        path = os.path.dirname(__file__)
    except NameError:
        # The "__file__" name is not always available, for example
        #  when this module is run from PythonWin.  In this case we
        #  simply use the current working directory.
        path = os.getcwd()
        __file__ = os.path.join(path, "dfly-loader-wsr.py")

    engine = get_engine("sapi5inproc")
    engine.connect()

    # Register a recognition observer
    observer = Observer()
    observer.register()

    directory = CommandModuleDirectory(path, excludes=[__file__])
    directory.load()

    engine.speak('beginning loop!')
    while 1:
        pythoncom.PumpWaitingMessages()
        time.sleep(.1)
def setup_show_spectrum():
    global timer
    audio = Audio()
    recognizer = lambda spectrogram: plot_spectrum(processor.freqs,
                                                   spectrogram[-1])
    processor = Processor(audio, recognizer, length_s=1)
    timer = get_engine().create_timer(make_timer_func(audio, processor), 0.02)
예제 #34
0
def main():
    try:
        path = os.path.dirname(__file__)
    except NameError:
        # The "__file__" name is not always available, for example
        # when this module is run from PythonWin.  In this case we
        # simply use the current working directory.
        path = os.getcwd()
        __file__ = os.path.join(path, "sphinx_module_loader.py")

    # Initialize the engine.
    engine = get_engine("sphinx")

    # Try to import the local engine configuration object first. If there isn't one,
    # use the default engine configuration.
    log = logging.getLogger("config")
    try:
        import config
        engine.config = config
        log.info("Using local engine configuration module 'config.py'")
    except ImportError:
        pass
    except Exception as e:
        # Log errors caught when setting the configuration.
        log.exception("Failed to set config using 'config.py': %s" % e)
        log.warning("Falling back to the default engine configuration")

    # You can also set any configuration options here instead of using a
    # config.py file. For example:
    # engine.config.START_ASLEEP = False

    # Call connect() now that the engine configuration is set.
    engine.connect()

    # Load grammars.
    directory = CommandModuleDirectory(path, excludes=[__file__])
    directory.load()

    # Define recognition callback functions.
    def on_begin():
        print("Speech start detected.")

    def on_recognition(words):
        message = u"Recognized: %s" % u" ".join(words)

        # This only seems to be an issue with Python 2.7 on Windows.
        if six.PY2:
            encoding = sys.stdout.encoding or "ascii"
            message = message.encode(encoding, errors='replace')
        print(message)

    def on_failure():
        print("Sorry, what was that?")

    # Start the engine's main recognition loop
    try:
        engine.do_recognition(on_begin, on_recognition, on_failure)
    except KeyboardInterrupt:
        pass
예제 #35
0
def test_with_engine(args):
    # Initialise the specified engine, catching and reporting errors.
    try:
        engine = get_engine(args.engine)
        _log.debug("Testing with engine '%s'" % args.engine)
    except Exception as e:
        _log.error(e)
        return 1

    # Set the logging level of the root logger.
    if args.quiet:
        args.log_level = "WARNING"
    logging.root.setLevel(getattr(logging, args.log_level))

    # Set the engine language if necessary.
    if engine.name == "text":
        engine.language = args.language

    # Connect to the engine, load grammar modules, take input from stdin and
    # disconnect from the engine if interrupted or if EOF is received.
    with engine.connection():
        # Load each module. Errors during loading will be caught and logged.
        # Use the overall success of module loading and/or mimic calls as
        # the return code.
        return_code = 0
        for f in args.files:
            module_ = CommandModule(f.name)
            module_.load()

            if not module_.loaded:
                return_code = 1

            # Also close each file object created by argparse.
            f.close()

        # Read lines from stdin and pass them to engine.mimic. Strip excess
        # white space from each line. Report any mimic failures.
        if args.no_input:
            # Return early if --no-input was specified.
            return return_code
        _log.info("Enter commands to mimic followed by new lines.")
        try:
            # Use iter to avoid a bug in Python 2.x:
            # https://bugs.python.org/issue3907
            for line in iter(sys.stdin.readline, ''):
                line = line.strip()
                if not line:  # skip empty lines.
                    continue

                try:
                    engine.mimic(line.split())
                    _log.info("Mimic success for words: %s" % line)
                except MimicFailure:
                    _log.error("Mimic failure for words: %s" % line)
                    return_code = 1
        except KeyboardInterrupt:
            pass

    return return_code
예제 #36
0
def main():
    try:
        path = os.path.dirname(__file__)
    except NameError:
        # The "__file__" name is not always available, for example
        # when this module is run from PythonWin.  In this case we
        # simply use the current working directory.
        path = os.getcwd()
        __file__ = os.path.join(path, "sphinx_module_loader.py")

    # Initialize the engine.
    engine = get_engine("sphinx")

    # Try to import the local engine configuration object first. If there isn't one,
    # use the default engine configuration.
    log = logging.getLogger("config")
    try:
        import config
        engine.config = config
        log.info("Using local engine configuration module 'config.py'")
    except ImportError:
        pass
    except Exception as e:
        # Log errors caught when setting the configuration.
        log.exception("Failed to set config using 'config.py': %s" % e)
        log.warning("Falling back to the default engine configuration")

    # You can also set any configuration options here instead of using a
    # config.py file. For example:
    engine.config.START_ASLEEP = False
    engine.config.DECODER_CONFIG.set_float("-vad_threshold", 3.1)
    engine.config.LANGUAGE = LANGUAGE

    # Call connect() now that the engine configuration is set.
    engine.connect()

    # Load grammars.
    directory = CommandModuleDirectory(path, excludes=[__file__])
    directory.load()

    class Observer(RecognitionObserver):
        def on_begin(self):
            print("=> Speech start detected.")

        def on_recognition(self, words):
            message = u"Recognized: %s" % u" ".join(words)
            print("=>", message)

        def on_failure(self):
            print("=> Sorry, what was that?")

    observer = Observer()
    observer.register()

    # Start the engine's main recognition loop
    try:
        engine.recognise_forever()
    except KeyboardInterrupt:
        pass
예제 #37
0
    def _send_request(self, request_function):
        # Send requests to the server using new threads to emulate requests
        # from a different process.
        request_thread = threading.Thread(target=request_function)
        request_thread.start()

        # Manually process server requests by calling the timer manager's
        # main callback directly.
        while request_thread.is_alive():
            get_engine()._timer_manager.main_callback()
            time.sleep(0.05)

        # Handle errors and results from the request.
        if isinstance(self.current_response, Exception):
            raise self.current_response
        else:
            return self.current_response
예제 #38
0
    def _send_request(self, request_function):
        # Send requests to the server using new threads to emulate requests
        # from a different process.
        request_thread = threading.Thread(target=request_function)
        request_thread.start()

        # Manually process server requests by calling the timer manager's
        # main callback directly.
        while request_thread.isAlive():
            get_engine()._timer_manager.main_callback()
            time.sleep(0.05)

        # Handle errors and results from the request.
        if isinstance(self.current_response, Exception):
            raise self.current_response
        else:
            return self.current_response
예제 #39
0
def drop(nnavi500):
    key = str(nnavi500)
    while True:
        failure = False
        try:
            if key in control.nexus().clip:
                win32clipboard.OpenClipboard()
                win32clipboard.EmptyClipboard()
                win32clipboard.SetClipboardText(control.nexus().clip[key])
                win32clipboard.CloseClipboard()
                Key("c-v").execute()
            else:
                dragonfly.get_engine().speak("slot empty")
            time.sleep(0.05)
        except Exception:
            failure = True
        if not failure:
            break
예제 #40
0
def drop(nnavi500, nexus):
    key = str(nnavi500)
    while True:
        failure = False
        try:
            if key in nexus.clip:
                win32clipboard.OpenClipboard()
                win32clipboard.EmptyClipboard()
                win32clipboard.SetClipboardText(nexus.clip[key])
                win32clipboard.CloseClipboard()
                Key("c-v").execute()
            else:
                dragonfly.get_engine().speak("slot empty")
            time.sleep(settings.SETTINGS["miscellaneous"]["keypress_wait"]/1000.) 
        except Exception:
            failure = True
        if not failure:
            break
예제 #41
0
    def _connect_engine(self):
        engine_name = self._resolve_engine_name(self.engine)

        try:
            log.info("Importing Dragonfly library.")
            import dragonfly

            log.info("Locating SR engine {0}."
                     "".format(engine_name or "(automatic selection)"))
            self._engine = dragonfly.get_engine(engine_name)

            log.info("Connecting to SR engine {0}.".format(self._engine))
            self._engine.connect()
        except Exception, e:
            log.exception("Error during Dragonfly setup: {0}".format(e))
            raise
def main():
    try:
        path = os.path.dirname(__file__)
    except NameError:
        # The "__file__" name is not always available, for example
        # when this module is run from PythonWin.  In this case we
        # simply use the current working directory.
        path = os.getcwd()
        __file__ = os.path.join(path, "sphinx_module_loader.py")

    engine = get_engine("sphinx")

    # Try to import the local engine configuration object first. If there isn't one,
    # use the default engine configuration.
    log = logging.getLogger("config")
    try:
        import config
        engine.config = config
        log.info("Using local engine configuration module 'config.py'")
    except ImportError:
        pass
    except Exception as e:
        # Log errors caught when setting the configuration.
        log.exception("Failed to set config using 'config.py': %s" % e)
        log.warning("Falling back to the default engine configuration")

    # You can also set any configuration options here instead of using a
    # config.py file. For example:
    # engine.config.START_ASLEEP = False

    # Call connect() now that the engine configuration is set.
    engine.connect()

    # Register a recognition observer
    observer = Observer()
    observer.register()

    directory = CommandModuleDirectory(path, excludes=[__file__])
    directory.load()

    # Start the engine's main recognition loop
    try:
        engine.recognise_forever()
    except KeyboardInterrupt:
        pass
예제 #43
0
    def setUpClass(cls):
        cls.current_response = None

        # Start the server on a different port for the tests and esure the
        # engine is connected.
        cls.server = RPCServer(port=50052)
        engine = get_engine()
        engine.connect()
        cls.server.start()

        # If the engine's timer manager is a ThreadedTimerManager, disable
        # the main callback to prevent race conditions.
        timer_manager = engine._timer_manager
        if isinstance(timer_manager, ThreadedTimerManager):
            cls.threaded_timer_manager = timer_manager
            timer_manager.disable()
        else:
            cls.threaded_timer_manager = None
예제 #44
0
    def collectValues(self, node, values=None):
        if values is None:
            values = {}

        refTypes = (dfly.Repetition, dfly.RuleRef)
        isRefType = any([isinstance(node.actor, t) for t in refTypes])

        if node.name and isinstance(node.actor, ElementBase) and not isRefType:
            v = node.value()
            w = node.words()
            if isinstance(v, get_engine().DictationContainer):
                # The value vs. words distinction is to help with things
                # like numbers where the value is 3 but the words are "three".
                # For dictated text there is no distinction.
                v = w
            # elif isinstance(v, ActionBase):
            #     # When we have rule refs to other mapping rules, the value ends up
            #     # being the looked up action, which is not what we want. We want to
            #     # know which phrse was triggered.
            #     v = v._action.grammarString
            log.info("extra [%s] value [%s] words [%s] realWords [%s]" % (node.name, v, w, node.words()))
            self.addValue(values, node.name, v)

        if node.name and isRefType:
            repValues = {}
            for n in node.children:
                self.collectValues(n, repValues)
            log.info("node.name [%s] repValues [%s]" % (node.name, repValues))
            repValues.update({ 'words' : node.words() })
            self.addValue(values, node.name, repValues)
            #values.update({ node.name : repValues })
        else:
            for n in node.children:
                self.collectValues(n, values)

        return values
예제 #45
0
def add_vocab():
    engine=dragonfly.get_engine()
    if engine.name!="natlink":
        utilities.report("feature unavailable in your speech recognition engine", speak=True)
        return
    
    
    # attempts to get what was highlighted first
    highlighted=context.read_selected_without_altering_clipboard(True)
    
    # change the following regex to accept alphabetical only
    disallow="^[A-Za-z]*$"
    selected=None
    
    if highlighted[0]==0 and highlighted[1]!="":
        if not re.match(disallow, highlighted[1]):
            utilities.report("only used for single words", speak=True)
            return
        
        selected=highlighted[1]
    try: 
        h_launch.launch(settings.QTYPE_SET, process_set, selected)
    except Exception:
        utilities.simple_log(False)
 def setUp(self):
     engine = get_engine("text")
     assert isinstance(engine, EngineBase)
     assert engine.name == "text"
     engine.connect()
     self.engine = engine
예제 #47
0
 def tearDownClass(cls):
     # Check that the dragonfly engine was not changed during the tests.
     assert cls.engine is dragonfly.get_engine()
def write_transcripts():
    engine = get_engine()
    if engine.name == "sphinx":
        engine.write_transcript_files(
            "training.fileids", "training.transcription"
        )
"""
Dragonfly command module designed for use with the CMU Pocket Sphinx engine,
either with the module loader or as a script.

Shows use of dragonfly Functions, Lists, Repetition, Dictation, IntegerRefs,
Mimic, etc.
"""

from dragonfly import (Dictation, Function, Grammar, IntegerRef, List,
                       ListRef, MappingRule, RecognitionObserver, Mimic,
                       Repetition, get_engine, Text)


if __name__ == '__main__':
    # If this file is being run, not imported, then use the sphinx engine.
    sphinx_engine = get_engine("sphinx")


def disconnect():
    # You shouldn't really run this module in other engines because it uses
    # sphinx-specific engine methods.
    print("Disconnecting engine (only for sphinx)")
    engine = get_engine()
    if engine.name == "sphinx":
        engine.disconnect()


def print_(x):
    print(x)

예제 #50
0
 def tearDownClass(cls):
     cls.server.stop()
     get_engine().disconnect()
     # Re-enable the timer manager's callback if necessary.
     if cls.threaded_timer_manager:
         cls.threaded_timer_manager.enable()
예제 #51
0
        # Set the grammar as exclusive.
        # The sapi5shared engine apparently requires this for mimic() to
        # work, making the method kind of useless. This does not apply to
        # sapi5inproc.
        g.set_exclusiveness(True)

        response = self.send_request("mimic", ["testing mimicry"])
        try:
            self.assertIn("result", response)
            self.assertEqual(response["result"], True)
        finally:
            g.set_exclusiveness(False)
            g.unload()

    def test_speak(self):
        response = self.send_request("speak", ["testing speak"])
        self.assertIn("result", response)
        self.assertEqual(response["result"], None)

    def test_get_engine_language(self):
        """ Verify that the 'speak' RPC method works correctly. """
        response = self.send_request("get_engine_language", [])
        self.assertIn("result", response)
        self.assertEqual(response["result"], "en")


if __name__ == '__main__':
    # Use the "text" engine by default.
    get_engine("text")
    unittest.main()