Пример #1
0
def config_stt(cache_dir, keywords, kws_last_modification_time_in_sec = None):
    _ = Core()
    cache_path = lambda x: os.path.join(cache_dir, x)
    _.lang_file = cache_path('lm')
    _.fsg_file = None
    _.dic_file = cache_path('dic')
    _.hmm_dir = cache_path('hmm')

    # Save keywords for further pattern matching. Call `config_stt()` when switching minds to check for command file changes.
    _.kwords = {}
    _.max_w_cnt = 3
    for phrase in keywords:
        spl_ph = phrase.strip().split(' ')
        w_cnt = len(spl_ph)
        if w_cnt > _.max_w_cnt:
            _.max_w_cnt = w_cnt
        for kword in spl_ph:
            # Combine all phrases which use keywords.
            r_phrases = _.kwords.setdefault(kword,{})
            r_phrases[phrase] = w_cnt

    # Save phrases.
    _.phrases = [x.strip().replace('%d', '').upper() for x in sorted(keywords)]

    # Check if commands file was modified.
    if kws_last_modification_time_in_sec:
        if os.path.exists(_.dic_file) and (kws_last_modification_time_in_sec < stat_mtime(_.dic_file)):
            return _
    _.strings_file = cache_path("sentences.corpus")
    data = '\n'.join(_.phrases)
    write_file(_.strings_file, data)

    # Download language model data from `speech.cs.cmu.edu`.
    update_language(_) 
    return _
Пример #2
0
def get_decoder():
    # XXX: race condition when mind isn't set yet
    mind = oa.core.mind
    if not hasattr(_decoders, mind.name):
        # Configure Speech to text dictionaries.
        ret = config_stt(mind.cache_dir, mind.kws.keys(), stat_mtime(mind.module))
        
        # Process audio chunk by chunk. On a keyphrase detected perform the action and restart search.
        config = Decoder.default_config()

        # Set paths for the language model files.
        config.set_string('-hmm', ret.hmm_dir)
        config.set_string("-lm", ret.lang_file)
        config.set_string("-dict", ret.dic_file)
        config.set_string("-logfn", os.devnull)  # Disable logging.

        ret.decoder = Decoder(config)
        _decoders[mind.name] = ret
    else:
        return _decoders[mind.name]

    return ret