Last active
October 3, 2016 06:30
-
-
Save naoh16/41c4445f6c21fadf8a239efd47cc4676 to your computer and use it in GitHub Desktop.
Example of Dialog Management using Finite State Transducer (FST)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Example of Dialog Management using Finite State Transducer (FST) | |
""" | |
import csv | |
from collections import deque | |
## | |
## Load FST file | |
## | |
fst = {} # 空のdict | |
with open('sample.fst', 'r') as fpin: | |
tsv = csv.reader(fpin, delimiter = "\t") | |
for row in tsv: | |
if len(row) < 4: | |
continue | |
src = row.pop(0) | |
fst.setdefault(src, []) | |
fst[src].append(row) | |
## | |
## Dialog Management with FST | |
## | |
cur_cmd = '' | |
cur_state = '0' | |
# The cmd_queue should updated by a Speech-Understanding (SU) unit. | |
# The SU module is a kind of filter for outputs of a Speech-Recognition (SR) unit. | |
cmd_queue = deque(['RECOG_END', 'SLOT(リンゴ,)', 'RECOG_END', 'SLOT(リンゴ,1)', | |
'RECOG_END', 'RECOG(はい)']) | |
# Main loop | |
while True: | |
## Display the information of current state | |
print('----') | |
print('Current: ' + cur_state) | |
if cur_state == '-1': | |
break | |
for hyp in fst[cur_state]: | |
print(' Hypotyesis: ' + ','.join(hyp)) | |
print('----') | |
# This line should be changed, or, `cmd_queue` is updated by the SU unit in another thread. | |
cur_cmd = cmd_queue.popleft() | |
# Drive one cycle in FST according to the SU result | |
print('Receive: ' + cur_cmd) | |
for hyp in fst[cur_state]: | |
# NULL transition | |
if hyp[1] == '<eps>': | |
print(' Response: ' + hyp[2]) | |
cur_state = hyp[0] | |
cmd_queue.appendleft(cur_cmd) # rewind. | |
break | |
# This condition should be replaced by another matching methods | |
if hyp[1] == cur_cmd: | |
print(' Response: ' + hyp[2]) | |
cur_state = hyp[0] | |
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0 1 <eps> SYNTH(リンゴとミカンがありますよ。) | |
1 100 <eps> SYNTH(何かほしいものはありますか?) | |
100 103 RECOG_END <eps> | |
103 200 SLOT(リンゴ,) SYNTH(リンゴですね。いくつ欲しいですか?) | |
103 300 SLOT(ミカン,) SYNTH(ミカンですね。いくつ欲しいですか?) | |
103 100 SLOT(,1) SYNTH(もう一度おっしゃってください。) | |
103 100 SLOT(,2) SYNTH(もう一度おっしゃってください。) | |
200 201 RECOG_END <eps> | |
201 202 SLOT(リンゴ,1) SYNTH(リンゴを1個ですね。100円になります。) | |
201 202 SLOT(リンゴ,2) SYNTH(リンゴを2個ですね。200円になります。) | |
202 400 <eps> SYNTH(買いますか?) | |
300 301 RECOG_END <eps> | |
301 302 SLOT(ミカン,1) SYNTH(ミカンを1個ですね。100円になります。) | |
301 302 SLOT(ミカン,2) SYNTH(ミカンを2個ですね。200円になります。) | |
302 303 <eps> SYNTH(買いますか?) | |
400 401 RECOG_END <eps> | |
401 -1 RECOG(はい) SYNTH(お買い上げありがとうございました。) | |
401 -1 RECOG(いいえ) SYNTH(またのお越しをお待ちしております。) | |
401 400 RECOG(*) SYNTH(もう一度おっしゃってください。) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment