Skip to content
Snippets Groups Projects
Commit b9bbf763 authored by Benoit Favre's avatar Benoit Favre
Browse files

improve slu synchronization

parent d813dc2b
No related branches found
No related tags found
No related merge requests found
slu: 1 slu: 0
xml_filename: data/homeostasis_25nov.xml xml_filename: data/homeostasis_25nov.xml
osc_host: 127.0.0.1 osc_host: 127.0.0.1
osc_port: 1234 osc_port: 1234
asr_model: asr/mika-fred-2.cfg asr_model: asr/mika-fred-1.cfg
...@@ -60,7 +60,10 @@ class ScriptedASR(Gtk.Window): ...@@ -60,7 +60,10 @@ class ScriptedASR(Gtk.Window):
found = re.search('section(\d+)\.fst$', section_fst) found = re.search('section(\d+)\.fst$', section_fst)
if found: if found:
section_id = int(found.group(1)) section_id = int(found.group(1))
self.slu[section_id - 1] = slu.SLU(prefix % 'dico_word.txt', prefix % 'dico_action.txt', section_fst, prefix % 'clean_tail.fst', library=library) self.slu[section_id - 1] = slu.SLU(prefix % 'dico_word.txt', prefix % 'dico_action.txt', section_fst, prefix % 'clean_tail.fst', 'slu/homeostasis_25nov.action', library=library)
if slu_type == 0:
for keyword in self.xmlview.keywords:
keyword.add_listener(self.set_slu_history)
import alternate_slu import alternate_slu
self.slu2 = alternate_slu.SLU(xml_filename) self.slu2 = alternate_slu.SLU(xml_filename)
...@@ -124,7 +127,8 @@ class ScriptedASR(Gtk.Window): ...@@ -124,7 +127,8 @@ class ScriptedASR(Gtk.Window):
self.xmlview.set_section(section_id) self.xmlview.set_section(section_id)
def slu_finished(self, model, slu_output): def slu_finished(self, model, slu_output):
slu_output = str(slu_output) #slu_output = str(slu_output)
#print slu_output
self.slu_actions = model.get_actions().split() self.slu_actions = model.get_actions().split()
self.slu_output = slu_output self.slu_output = slu_output
print 'SLU output: "%s", actions: "%s"' % (self.slu_output, ' '.join(self.slu_actions)) print 'SLU output: "%s", actions: "%s"' % (self.slu_output, ' '.join(self.slu_actions))
...@@ -133,22 +137,22 @@ class ScriptedASR(Gtk.Window): ...@@ -133,22 +137,22 @@ class ScriptedASR(Gtk.Window):
for action_id in range(len(self.kept_actions), len(self.slu_actions)): for action_id in range(len(self.kept_actions), len(self.slu_actions)):
print 'NEW ACTION:', action_id, self.slu_actions[action_id] print 'NEW ACTION:', action_id, self.slu_actions[action_id]
action = actions.parse_slu_action(self.slu_actions[action_id]) action = actions.parse_slu_action(self.slu_actions[action_id])
print action.text
if action.text.startswith('#ENDSEQUENCE('): if action.text.startswith('#ENDSEQUENCE('):
pass pass
elif action.text.startswith('#ENDSECTION('): elif action.text.startswith('#ENDSECTION('):
new_section = self.xmlview.get_section() + 1 new_section = self.xmlview.get_section() + 1
self.confirmer.confirm('Go to section %d?' % (new_section + 1), 3, lambda: self.set_section(new_section)) self.confirmer.confirm('Go to section %d?' % (new_section + 1), 3, lambda: self.set_section(new_section))
else: else:
self.xmlview.highlight(action)
actions.perform_action(action, False) actions.perform_action(action, False)
self.kept_history = self.slu_output
self.kept_actions = self.slu_actions
def hyp_changed(self, hyp): def hyp_changed(self, hyp):
#hyp = ' '.join(hyp).replace('[noise]', ' ').split() #hyp = ' '.join(hyp).replace('[noise]', ' ').split()
if len(hyp) > len(self.current_section_history): if len(hyp) > len(self.current_section_history):
self.current_section_history.append('') self.current_section_history.append('')
self.kept_history = self.slu_output #self.kept_history = self.slu_output
self.kept_actions = self.slu_actions #self.kept_actions = self.slu_actions
words = hyp[-1].strip().replace('_', ' ').split() words = hyp[-1].strip().replace('_', ' ').split()
if len(words) == 0: if len(words) == 0:
...@@ -172,6 +176,17 @@ class ScriptedASR(Gtk.Window): ...@@ -172,6 +176,17 @@ class ScriptedASR(Gtk.Window):
model.process(words, self.kept_history, self.slu_finished, False) model.process(words, self.kept_history, self.slu_finished, False)
self.current_section_history[-1] = ' '.join(words) self.current_section_history[-1] = ' '.join(words)
def set_slu_history(self, keyword, uri, num):
for keyword in self.xmlview.keywords[:num + 1]:
keyword.highlight()
for keyword in self.xmlview.keywords[num + 1:]:
keyword.highlight(False)
self.kept_actions = [x.action for x in self.xmlview.keywords[:num + 1]]
self.kept_history = self.slu[self.current_section].get_action_history(num)
print 'SLU set history: history="%s", actions="%s"' % (self.kept_history, ' '.join(self.kept_actions))
self.slu_output = ''
self.slu_actions = []
def hyp_changed2(self, hyp): def hyp_changed2(self, hyp):
section_id = self.xmlview.get_section() section_id = self.xmlview.get_section()
if self.slu2_last_section != section_id: if self.slu2_last_section != section_id:
......
...@@ -7,7 +7,7 @@ _semaphore = None ...@@ -7,7 +7,7 @@ _semaphore = None
class SLU: class SLU:
#/src.new/rocio_slu -word "$prefix"_dico_word.txt -action "$prefix"_dico_action.txt -fstmodel "$prefix".fst -fstclean "$prefix"_clean_tail.fst #/src.new/rocio_slu -word "$prefix"_dico_word.txt -action "$prefix"_dico_action.txt -fstmodel "$prefix".fst -fstclean "$prefix"_clean_tail.fst
def __init__(self, word_lexicon, action_lexicon, model_fst, cleaner_fst, library='slu/src/librocio_slu.so'): def __init__(self, word_lexicon, action_lexicon, model_fst, cleaner_fst, action_history, library='slu/src/librocio_slu.so'):
global _backend, _semaphore global _backend, _semaphore
if _backend == None: if _backend == None:
_backend = cdll.LoadLibrary(library) _backend = cdll.LoadLibrary(library)
...@@ -45,11 +45,23 @@ class SLU: ...@@ -45,11 +45,23 @@ class SLU:
_backend.free.restype = None _backend.free.restype = None
_semaphore = threading.Semaphore() _semaphore = threading.Semaphore()
self.load_action_history(action_history)
_semaphore.acquire() _semaphore.acquire()
thread = threading.Thread(target=self._init_thread, args=[word_lexicon, action_lexicon, model_fst, cleaner_fst]) thread = threading.Thread(target=self._init_thread, args=[word_lexicon, action_lexicon, model_fst, cleaner_fst])
thread.daemon = True thread.daemon = True
thread.start() thread.start()
def load_action_history(self, action_history):
self.action_history = []
with open(action_history) as fp:
for line in fp:
action, history = line.strip().split('\t')
if not('#ENDSEQUENCE' in action or '#ENDSECTION' in action):
self.action_history.append([action, history])
def get_action_history(self, action_num):
return self.action_history[action_num][1]
def _init_thread(self, word_lexicon, action_lexicon, model_fst, cleaner_fst): def _init_thread(self, word_lexicon, action_lexicon, model_fst, cleaner_fst):
global _backend, _semaphore global _backend, _semaphore
self.slu = _backend.init_slu(word_lexicon, action_lexicon, model_fst, cleaner_fst) self.slu = _backend.init_slu(word_lexicon, action_lexicon, model_fst, cleaner_fst)
...@@ -69,7 +81,7 @@ class SLU: ...@@ -69,7 +81,7 @@ class SLU:
_semaphore.acquire() _semaphore.acquire()
c_output = _backend.run_slu(self.slu, c_words, len(words), -1, old_words) c_output = _backend.run_slu(self.slu, c_words, len(words), -1, old_words)
_semaphore.release() _semaphore.release()
output = cast(c_output, c_char_p).value output = str(cast(c_output, c_char_p).value)
_backend.free(c_output) _backend.free(c_output)
GLib.idle_add(callback, self, output) GLib.idle_add(callback, self, output)
......
This diff is collapsed.
...@@ -86,13 +86,13 @@ class Line(Gtk.HBox): ...@@ -86,13 +86,13 @@ class Line(Gtk.HBox):
self.elements = elements self.elements = elements
self.get_style_context().add_class('text-line') self.get_style_context().add_class('text-line')
def highlight(self, active=True, scrollable=None): #def highlight(self, active=True, scrollable=None):
if active: # if active:
self.label.get_style_context().add_class('highlighted') # self.label.get_style_context().add_class('highlighted')
else: # else:
self.label.get_style_context().remove_class('highlighted') # self.label.get_style_context().remove_class('highlighted')
if scrollable != None: # if scrollable != None:
animate.scroll_to(scrollable, self) # animate.scroll_to(scrollable, self)
class Keyword(Gtk.Label): class Keyword(Gtk.Label):
def __init__(self, text, action, lang): def __init__(self, text, action, lang):
...@@ -103,15 +103,29 @@ class Keyword(Gtk.Label): ...@@ -103,15 +103,29 @@ class Keyword(Gtk.Label):
self.set_markup(text + ' [<a href="%s">%s</a>] ' % (action, action)) self.set_markup(text + ' [<a href="%s">%s</a>] ' % (action, action))
self.get_style_context().add_class('keyword') self.get_style_context().add_class('keyword')
self.connect('activate-link', self.link_clicked) self.connect('activate-link', self.link_clicked)
self.listeners = []
self.num = 0
def highlight(self, scrollable=None): def highlight(self, active=True, scrollable=None):
if active:
self.get_style_context().remove_class('keyword') self.get_style_context().remove_class('keyword')
self.get_style_context().add_class('keyword-highlighted') self.get_style_context().add_class('keyword-highlighted')
if scrollable != None: if scrollable != None:
animate.scroll_to(scrollable, self) animate.scroll_to(scrollable, self)
else:
self.get_style_context().add_class('keyword')
self.get_style_context().remove_class('keyword-highlighted')
def add_listener(self, listener):
self.listeners.append(listener)
def set_num(self, num):
self.num = num
def link_clicked(self, widget, uri): def link_clicked(self, widget, uri):
actions.perform_action(actions.Action(uri, keyword=widget), False) actions.perform_action(actions.Action(uri, keyword=widget), False)
for listener in self.listeners:
listener(self, uri, self.num)
return True return True
class Text(Gtk.Label): class Text(Gtk.Label):
...@@ -144,6 +158,7 @@ class XmlView(Gtk.ScrolledWindow): ...@@ -144,6 +158,7 @@ class XmlView(Gtk.ScrolledWindow):
for line in sequence.lines: for line in sequence.lines:
for element in line.elements: for element in line.elements:
if hasattr(element, 'action'): if hasattr(element, 'action'):
element.set_num(len(self.keywords))
self.keywords.append(element) self.keywords.append(element)
self.last_highlighted = -1 self.last_highlighted = -1
...@@ -172,16 +187,19 @@ class XmlView(Gtk.ScrolledWindow): ...@@ -172,16 +187,19 @@ class XmlView(Gtk.ScrolledWindow):
def section_clicked(self, current): def section_clicked(self, current):
self.set_section(int(current.name) - 1) self.set_section(int(current.name) - 1)
for keyword in self.keywords:
keyword.highlight(False)
def highlight(self, action): def highlight(self, action):
if hasattr(action, 'keyword'): if hasattr(action, 'keyword'):
action.keyword.highlight(self) action.keyword.highlight(True, self)
else: else:
i = self.last_highlighted + 1 i = self.last_highlighted + 1
while i < len(self.keywords): while i < len(self.keywords):
if self.keywords[i].action == action.text: if self.keywords[i].action == action.text:
self.keywords[i].highlight(self) self.keywords[i].highlight(True, self)
self.last_highlighted = i self.last_highlighted = i
#print 'HIGHLIGHT:', action.text, self.last_highlighted
break break
i += 1 i += 1
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment