在vc++6.0/2003/2005/2008中调用HTK
2009-04-27 20:06:39 来源:WEB开发网 AGram *g1 = new AGram(name,gramfile);
rman->StoreGram(g1);
ask = rman->NewGroup(name+"-ask");
ask->AddHMMs(hset); // Add the global resources
ask->AddDict(dict);
ask->AddGram(ggrm); // include global grammar in parallel with
ask->AddGram(g1); // qa specific grammar
// create copy of ask but prepend "No" to front of qa grammar
// and add confirm in parallel
chk = rman->NewGroup(name+"-chk");
chk->AddHMMs(hset); // Add the global resources
chk->AddDict(dict);
chk->AddGram(ggrm); // include global grammar
AGram *g2 = new AGram(*g1); // copy g1
rman->StoreGram(g2);
g2->OpenEdit(); // Modify copy of g1 by prepending
GramSubN *s = g2->main; // "NO" to front with skip to end
GramNode *ent = s->NewNullNode(99);
GramNode *no = s->NewWordNode("NO","no");
GramNode *yes = s->NewCallNode("confirm","yes"); // Add confirm in //
s->AddLink(ent,no); s->AddLink(ent,yes); s->AddLink(yes,s->exit);
s->AddLink(no,s->entry); s->AddLink(no,s->exit);
s->entry = ent;
g2->CloseEdit();
chk->AddGram(g2); // Add the new "correction" grammar
// initial status is unknown
status = unknown; value = "";
}
// Reset slot values for a new dialogue
void QA::Reset()
{
status = unknown; value = "";
}
// For debugging only
void QA::Show()
{
string s = "???";
switch (status){
case unknown: s = "unknown"; break;
case unconfirmed: s = "unconfirmed"; break;
case grounded: s = "grounded"; break;
case cancelled: s = "cancelled"; break;
}
printf("Slot: %s: status=%s value=%s [cur=%s(%s) %.1f]n",
name.c_str(),s.c_str(),value.c_str(),curtag.c_str(),curwords.c_str(),curconf);
}
// listen to rec packets and construct slot value
// return system status when finished listening
ASRStatus QA::Listen(string prompt, string rgroup, SynStatus& ss)
{
APacket p;
string s="";
float conf=0.0;
int numwords=0;
ASRStatus as;
curtag=""; curwords="";
// set the resource group to use and start recognising
string grp = "setgroup("+rgroup+")";
string prmpt = "ask(""+prompt+"")";
printf("%s --> %sn",prmpt.c_str(),grp.c_str());
aio->SendMessage(grp);
aio->SendMessage(prmpt);
// listen to response of form "tag(w1 w2 ...)"
// Following implements a simple state machine ss= syn state as=asr state
ss = sActive; as = aActive;
do {
AStringData *sd;
ACommandData *cd;
APhraseData *pd;
string cmd;
p = inChan->GetPacket();
switch(p.GetKind()) {
case StringPacket:
sd = (AStringData *)p.GetData();
if (sd->data.find("TERMINATED") != string::npos) {
as = aTerminated;
}
break;
case CommandPacket:
cd = (ACommandData *)p.GetData();
cmd = cd->GetCommand();
if (cmd=="synFinished")
ss = sDone;
else if (cmd=="synInterrupted")
ss = sInt;
else if (cmd=="asrTimeOut")
as = aTimeout;
break;
case PhrasePacket:
pd = (APhraseData *)p.GetData();
if (as == aActive && pd->ptype == OpenTag_PT){
assert(s == "" && numwords == 0);
as = aInProcess;
}else if (as == aInProcess && pd->ptype == Word_PT) {
if (s=="") s = pd->word; else s = s + " " + pd->word;
// printf("{%s[%.2f]}",pd->word.c_str(),pd->confidence);
conf += pd->confidence; ++numwords;
}else if (as == aInProcess &&
(pd->ptype == CloseTag_PT || pd->ptype == End_PT)){
curtag = pd->tag;
curwords = s;
as = aDone;
}
break;
}
} while (as <= aInProcess);
curconf = numwords==0?0.0:conf/numwords;
printf("n Response: %s(%s)[%.1f]n",curtag.c_str(),curwords.c_str(),curconf);
return as;
}
// ask for information
void QA::Ask()
{
Boolean ok = FALSE;
ASRStatus as; SynStatus ss;
do {
as=Listen(prompt,ask->gname,ss);
if (as==aDone){
if (curtag == "command") {
if (curwords == "HELP") {
aio->SendMessage("tell(""+help+"")");
}else if (curwords == "CANCEL"){
aio->SendMessage("tell("Cancelled")");
ok = TRUE; status = cancelled;
}
}
if (curtag == name){
if (curconf > minconf){
status = grounded;
更多精彩
赞助商链接