#!/usr/bin/env python2 import sys import os import glob, re # set to location of libgstkaldionline2.so os.environ['GST_PLUGIN_PATH'] = './asr/' os.environ['GTK_THEME'] = 'light' # import gtk stuff from threading import Thread import gi gi.require_version('Gst', '1.0') from gi.repository import GObject, Gst, Gtk, Gdk GObject.threads_init() Gdk.threads_init() Gst.init(None) # make sure ctrl-c works import signal signal.signal(signal.SIGINT, signal.SIG_DFL) # import local stuff import confirm, asr, action, xmlview_widgets import levenstein, SLU class ScriptedASR(Gtk.Window): def __init__(self, xml_filename, asr_config_file): super(ScriptedASR, self).__init__() self.connect("destroy", self.quit) self.set_default_size(800,600) self.set_border_width(10) self.set_title('ScriptedASR [%s]' % xml_filename) vbox = Gtk.VBox() import section self.sections = section.SectionManager() vbox.pack_start(self.sections, False, True, 5) self.xmlview = xmlview_widgets.XmlView(xml_filename) vbox.pack_start(self.xmlview, True, True, 5) self.lines = [x for x in self.xmlview.get_line_iterator()] self.current_line = -1 self.xmlview.set_action_clicked_handler(self.keyword_clicked) self.confirmer = confirm.ConfirmationBox() vbox.pack_start(self.confirmer, False, True, 5) #self.actions = action.ActionView() #vbox.pack_start(self.actions, False, True, 5) self.sections.set_confirmer(self.confirmer) #self.actions.set_confirmer(self.confirmer) self.sections.set_highlight(self.xmlview) self.sections.set_section(1) # transcript view self.asr = asr.ASR(asr_config_file, self.hyp_changed, self.hyp_changed) vbox.pack_start(self.asr, False, True, 5) # slu prefix = 'slu/automate/homeostasis_25nov_%s' self.slu = {} for section_fst in glob.glob(prefix % 'section*.fst'): found = re.search('section(\d+)\.fst$', section_fst) if found: print section_fst section_id = int(found.group(1)) self.slu[section_id] = SLU.SLU(prefix % 'dico_word.txt', prefix % 'dico_action.txt', section_fst, prefix % 'clean_tail.fst') self.add(vbox) self.show_all() self.confirmer.hide() # load css style style_provider = Gtk.CssProvider() style_provider.load_from_data(open('data/style.css', 'rb').read()) Gtk.StyleContext.add_provider_for_screen( Gdk.Screen.get_default(), style_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) def line_clicked(self, widget, event): if self.current_line >= 0: self.lines[self.current_line].highlight(False) for i, line in enumerate(self.lines): if widget is line: self.current_line = i self.lines[self.current_line].highlight(True) def keyword_clicked(self, action): self.confirmer.confirm('Force action \"%s\"?' % action, 3, None) def hyp_changed(self, hyp): #hyp = ' '.join(hyp).replace('[noise]', ' ').split() words = hyp[-1].strip().replace('_', ' ').split() section_id = self.sections.get_section() print section_id, self.slu.keys() if section_id in self.slu: print 'SLU: ', section_id slu = self.slu[section_id] previous_actions = slu.num_actions() slu.process(words) for action_id in range(previous_actions, slu.num_actions()): action = slu.get_action(action_id) self.confirmer.confirm('Perform action \"%s\"?' % action, 3, None) #if self.current_line >= len(self.lines) - 1: # print "FINISHED" # return #line = self.lines[self.current_line + 1].text.split() #import levenstein #num_errors, num_ref, alignment, score = levenstein.align(line, words) #num_matches = 0 #for ref_word, hyp_word in alignment: # if ref_word == hyp_word and ref_word != None: # num_matches += 1 #score = float(num_matches) / max(len(line), len(words)) #print 'ASR:', hyp[-1], 'REF:', self.lines[self.current_line + 1].text, 'score:', score #levenstein.print_alignment(alignment) #if score >= 0.5: # if self.current_line >= 0: # self.lines[self.current_line].highlight(False) # self.current_line += 1 # self.lines[self.current_line].highlight(True) def quit(self, window): for slu in self.slu.values(): slu.shutdown() Gtk.main_quit() if __name__ == '__main__': import selector xml_filename = 'data/homeostasis_25nov.xml' asr_config_file = 'asr/mika-fred-1.cfg' asr_config_file = 'asr/fisher-benoit-1.cfg' if len(sys.argv) > 1: xml_filename = sys.argv[1] if len(sys.argv) > 2: asr_config_file = sys.argv[2] xml_filename, asr_config_file = selector.ModelSelector(xml_filename, asr_config_file).run() if xml_filename == None or asr_config_file == None: sys.exit(0) app = ScriptedASR(xml_filename, asr_config_file) Gtk.main()