一个Nuance 的语音识别的例子
Posted wicnwicnwh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个Nuance 的语音识别的例子相关的知识,希望对你有一定的参考价值。
- #include "stdafx.h"
- #include "GetToken.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // The one and only application object
- //CWinApp theApp;
- //using namespace std;
- #ifdef __UNICODE__
- #undef __UNICODE__
- #endif
- //#define __UNICODE__
- #ifdef __UNICODE__
- #define dictName "dict.uni"
- #define alpha "freepy.uni"
- #else//gb2312
- #define dictName "dict.gb"
- #define alpha "freepy.gb"
- #endif
- const int maxlength = 256;
- #define dictSize 0x10000
- const char pyIndex[] = "pyindex";
- BOOL ISHanzi(char *src)
- {
- int asc=*src;
- if(asc<0) asc+=256;
- if(asc>127)
- return TRUE;
- return FALSE;
- }
- char * GetPhoneticByWideString(WCHAR * str,const WORD * dict,const char * pyTable)
- {
- char phonetic[10];
- char * widestr = (char*)str;
- char *returnvalue=NULL;
- BOOL hz;
- hz=FALSE;
- while (*widestr != 0)
- {
- memset(phonetic,0,10);
- bool bEnglish = true;
- if(*widestr <= ‘Z‘ && *widestr >=‘A‘)
- {
- if(hz)
- {
- *phonetic=‘ ‘;
- *(phonetic+1) = *widestr + ‘a‘ - ‘A‘;
- *(phonetic+2) = NULL;
- }else
- {
- *phonetic = *widestr + ‘a‘ - ‘A‘;
- *(phonetic+1) = NULL;
- }
- hz=FALSE;
- widestr ++;
- }
- else if(*widestr <= ‘z‘ && *widestr >=‘a‘ || *widestr == ‘ ‘)
- {
- if(hz&&*widestr != ‘ ‘)
- {
- *(phonetic)=‘ ‘;
- *(phonetic+1) = *widestr;
- *(phonetic+2) = NULL;
- }
- else
- {
- *phonetic = *widestr;
- *(phonetic+1) = NULL;
- }
- hz=FALSE;
- widestr ++;
- }
- else if(*widestr >= ‘0‘ && *widestr <=‘9‘)
- {
- /* *phonetic = *widestr;
- *(phonetic+1) = NULL;
- hz=FALSE;
- widestr ++;
- */
- GetDigits(*widestr,phonetic);
- hz=TRUE;
- bEnglish = false;
- widestr++;
- }
- else if(ISHanzi(widestr))
- {
- if (-1 == GetPhoneticByHanZi(*(WCHAR*)widestr,dict,pyTable,phonetic))
- return NULL;
- widestr ++;
- if(!((*widestr <= ‘Z‘ && *widestr >=‘A‘) || (*widestr <= ‘a‘ && *widestr >=‘a‘) || *widestr == ‘ ‘))
- widestr ++;
- hz=TRUE;
- bEnglish = false;
- }else
- {
- widestr ++;
- continue;
- }
- if (!returnvalue)
- {
- int i = strlen(phonetic);
- returnvalue = strdup(phonetic);
- }
- else
- {
- if(!bEnglish && returnvalue[strlen(returnvalue)-1] != ‘ ‘)
- {
- returnvalue = (char*)realloc(returnvalue,strlen(returnvalue)+strlen(phonetic)+2);
- strcat(returnvalue," ");
- }
- else
- {
- returnvalue = (char*)realloc(returnvalue,strlen(returnvalue)+strlen(phonetic)+1);
- }
- strcat(returnvalue,phonetic);
- }
- }
- return returnvalue;
- }
- void LoadDictAndPYTable(WORD* pDict,char** pPyTable)
- {
- CFile dictFile(dictName,CFile::modeRead|CFile::typeBinary);
- //for (int i = 0;i<DICTSIZE;I++){ (a if a="wch" (m<lstr){ while tabfile=‘fopen("table.txt","r+b");‘ tabfile; * FILE destfile=‘fopen("big.txt","w+b");‘ destfile; BigSource="Big5Source;" char* GBString="outString;" 输出字符串 char[lstr]; outString="new" lstr="bigStrlen+1;" m="0;" long lstr; b; char unsigned a; Big5Source="new" else } NULL); NULL, bigStrlen, Big5Source, -1, WidecharStr, 0, WideCharToMultiByte(936, MultiByteToWideChar(950,0,SourceText,bigStrlen,WidecharStr,bigStrlen); { (strstr(SourceText,?big5?)!="NULL)" bigStrlen+1); memset(Big5Source, (bigStrlen+1)*sizeof(WCHAR)); memset(WidecharStr, String WideChar WCHAR[bigStrlen+1]; WidecharStr="new" WCHAR Big5字符串 char[bigStrlen+1]; LPSTR 源字符串长度 bigStrlen="strlen(SourceText);" *SourceText) Big2GB(char ?);} strcpy(returnvalue,?ling2 (digits="=‘1‘)" strcpy(returnvalue,?jiu3 strcpy(returnvalue,?ba1 strcpy(returnvalue,?qi1 strcpy(returnvalue,?liu4 strcpy(returnvalue,?wu3 strcpy(returnvalue,?si4 strcpy(returnvalue,?san1 strcpy(returnvalue,?er4 strcpy(returnvalue,?yi1 returnvalue) digits,char* GetDigits(char void count; return count++; *(lpStrArr+count*wMaxArrSize+i-cursor+1)="_T(‘/0‘);" _tcsncpy(lpStrArr+count*wMaxArrSize,lpBuf+cursor,i-cursor+1); ){ ?) *(lpBuf+i)!="_T(‘" && wBufLen-1 if(i="=" cursor="0,count=0,wBufLen;" *(lpStrArr+count*wMaxArrSize+i-cursor)="_T(‘/0‘);" _tcsncpy(lpStrArr+count*wMaxArrSize,lpBuf+cursor,i-cursor); *(lpBuf+i-1)!="_T(‘" if(i!="0" _T(?/t?)) *(lpBuf+i)="=" || _T(? if(*(lpBuf+i)="=" (i="0;i<wBufLen;i++){" for wBufLen="strlen(lpBuf);" WORD i; int wMaxArrSize) lpStrArr,WORD lpBuf,LPTSTR String2Array(LPTSTR 1; phonetic[len+1]="0;" phonetic[len]="shengdiao+0x30;" len="strlen(phonetic);" strcpy(phonetic,pyTable+index-shengdiao); 8; % shengdiao="index" -1; (!index) ); !="NULL" pyTable ASSERT(phonetic!="NULL" phonetic) *pyTable,char* index,const GetPYByIndex(int 返回只为* *根据索引值获得汉字的发音,为上函数的逆函数 find/n?); printf(?not break; 415) (count++="=" +="8;" index="(char*)" while(strcmp(index,phonetic)!="0)" count="0;" pyTable; pyTable) void* phonetic,const GetIndexByPY(char* 顺序查找,可以修改为这般查找* +音调 返回为索引植="音标序号*8" pyTable为存储音标的表, *phonetic为要查的音标,如ang, GetPYByIndex(dict[a],pyTable,phonetic); ; short phonetic pyTable,char* dict,const wch,const GetPhoneticByHanZi(WCHAR +音调phonetic* 返回为该汉字的拼音代码="音标序号*8" pyTable为字典数组的起始地址, *从一个汉字取得数组下标 pyFile.Read(*pPyTable,4096); pyFile(pyIndex,CFile::modeRead|CFile::typeBinary); CFile *pPyTable="(char*)malloc(4096);" (!*pPyTable) TRACE(dictName); dictFile.ReadHuge(pDict,dictSize*2);>=0x81 && a <=0xfe)
- {
- b=*(Big5Source+1);
- if (b<0x7e) b=b - 0x40;
- if (b>=0x80) b = b - 0x80 + 0x7e - 0x40 + 1;
- fseek(tabfile, 2 * ((a -0x81) * (0xfe - 0x80 + 1 + 0x7e - 0x40 + 1 ) + b), SEEK_SET);
- fread(& a,1,1,tabfile);
- fread(& b,1,1,tabfile);
- fwrite(&a,1,1,destfile);
- fwrite(&b,1,1,destfile);
- *outString=a;
- *(outString+1)=b;
- outString+=2;
- Big5Source+=2;
- m+=2;
- }
- else
- {
- fwrite(&a,1,1,destfile);
- *outString=a;
- outString++;
- Big5Source++;
- m++;
- }
- }
- fclose(destfile);
- fclose(tabfile);
- Big5Source=BigSource;
- outString[lstr-1]=‘/0‘;
- delete[] Big5Source;
- delete[] WidecharStr;
- return GBString;
- }
- #include "stdafx.h"
- #include "GrammarThread.h"
- #include "GetToken.h"
- #include "HandleDataThread.h"
- #include "process.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define MAX_STRING_PARAM_LEN 300
- /////////////////////////////////////////////////////////////////////////////
- // CGrammarThread
- using namespace std;
- extern WORD dictArray[0X10000];
- extern char* pyTable;
- void ShowMsg(CString msg);
- CGrammarThread::CGrammarThread()
- {
- }
- CGrammarThread::CGrammarThread(NuanceConfig &config,
- DispatcherInterface & d,
- DBDescriptor * db_desc,CHandleDataThread* _p): m_pConfig(&config), m_pDispatcher(&d), m_pRcEngine(NULL),// m_kb(NULL),
- m_pDynagram_Db_Desc(db_desc)
- {
- m_parent=_p;
- NuanceStatus status;
- for(int i=0;i<MAX_SLOT_CNT;I++) m_pRcEngine- { CGrammarThread::~CGrammarThread() } -------- NotifiableAdapter from inherited WarnForUnhandledNotifications(); m_uRecord_ID="0;" m_uRecognition_ID="0;" m_specdtmf="0;" ; m_termno="#" m_iddtime="0;" m_firsttime="0;" m_dtmflen="40;" m_dtmfreason="0;" m_dtmfcnt="0;" memset(m_dtmfbuf,0,200); m_uPlay_Prompt_ID="0;" m_playreason="0;" h_recordevent="CreateEvent(NULL,FALSE,TRUE,NULL);" h_parameterevent="CreateEvent(NULL,FALSE,TRUE,NULL);" h_recognevent="CreateEvent(NULL,FALSE,TRUE,NULL);" h_dtmfevent="CreateEvent(NULL,FALSE,TRUE,NULL);" h_playevent="CreateEvent(NULL,FALSE,TRUE,NULL);" h_insertevent="CreateEvent(NULL,FALSE,FALSE,NULL);" h_closedbevent="CreateEvent(NULL,FALSE,FALSE,NULL);" h_delevent="CreateEvent(NULL,FALSE,FALSE,NULL);" h_queryevent="CreateEvent(NULL,FALSE,FALSE,NULL);" h_newevent="CreateEvent(NULL,FALSE,FALSE,NULL);" return; !="status)" (NUANCE_OK if status="NLGetSlotNamesFromConfig(m_pConfig,slotname);" m_pRcEngine) (NULL="=" status); *this, *m_pDispatcher, RCEngine(m_pConfig, m_pRcEngine="new" NLFreeValue(slotname); NLFreeValue(slotstr); strcpy(slotnamelist[i],slot); slotnamelist[i]="NULL;" NLGetStringFromValue(slotstr,slot,200); for(i="0;i<slen;i++)" slotstr="NLNewValue(&status);" NLValue* slotname="NLNewValue(&status);" slot[200]; char slen="0;" int>Abort(NullID);
- if (m_pDynagram_Db_Desc) {
- DeleteDBDescriptor(m_pDynagram_Db_Desc);
- }
- m_pDynagram_Db_Desc = NULL;
- delete m_pRcEngine;
- m_pRcEngine = NULL;
- CloseHandle(h_newevent);
- CloseHandle(h_queryevent);
- CloseHandle(h_delevent);
- CloseHandle(h_closedbevent);
- CloseHandle(h_insertevent);
- CloseHandle(h_playevent);
- CloseHandle(h_dtmfevent);
- CloseHandle(h_recognevent);
- CloseHandle(h_parameterevent);
- CloseHandle(h_recordevent);
- for(int i=0;slotnamelist[i]!=NULL;i++)
- {
- free(slotnamelist[i]);
- }
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleCallTransferFailed(CallTransferFailedNotification const & n)
- {
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleNuanceDBRecordDeleted(NuanceDBRecordDeletedNotification const & n)
- {
- NuanceStatus status = n.GetStatus();
- if (NUANCE_OK != status) {
- }else
- {
- CString msg;
- msg="记录删除";
- ShowMsg(msg);
- }
- SetEvent(h_delevent);
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleNuanceDBRecordModified(NuanceDBRecordModifiedNotification const & n)
- {
- CString msg;
- NuanceStatus status = n.GetStatus();
- if (NUANCE_OK != status) {
- msg.Format("Dynamic Grammar Modified 失败:%s",n.GetErrorString());
- ShowMsg(msg);
- }else
- {
- msg="Dynamic Grammar Modified";
- ShowMsg(msg);
- }
- SetEvent(h_newevent);
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleNuanceDBRecordCreated(NuanceDBRecordCreatedNotification const & n)
- {
- CString msg;
- NuanceStatus status = n.GetStatus();
- if (NUANCE_OK != status) {
- msg.Format("Dynamic Grammar Create 失败:%s",n.GetErrorString());
- ShowMsg(msg);
- }else
- {
- msg="Dynamic Grammar Create";
- ShowMsg(msg);
- }
- SetEvent(h_newevent);
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleConferenceCallStarted(ConferenceCallStartedNotification const & n)
- {
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleConferenceEnded(ConferenceEndedNotification const & n)
- {
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleDTMFTones(DTMFTonesNotification const & dn)
- {
- char const *m_cNum = dn.GetTones();
- printf("have gotten dtmf :%c/r/n",*m_cNum);
- if(m_uRecord_ID>0)
- {
- if(m_termdtmf!=‘a‘&&m_termdtmf!=‘A‘)
- {
- if(*m_cNum==m_termdtmf)
- StopRecord();
- }else
- {
- StopRecord();
- }
- }
- if(m_dtmfstate==1)
- {
- if(m_uRecognition_ID>0)
- StopRecognize(8);
- StopPrompt();
- if(*m_cNum==m_termno)
- {
- m_dtmfreason=1;
- m_dtmfstate=0;
- return;
- }
- memcpy(m_dtmfbuf+m_dtmfcnt,m_cNum,strlen(m_cNum));
- m_dtmfcnt+=strlen(m_cNum);
- time(&m_dt);
- if(m_dtmfcnt==1&&*m_cNum==m_specdtmf)
- {
- m_dtmfreason=1;
- m_dtmfstate=0;
- }else if(m_dtmfcnt==m_dtmflen)
- {
- m_dtmfreason=1;
- m_dtmfstate=0;
- }
- }
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleAcknowledgment (AcknowledgmentNotification const & an)
- {
- CString strfname;
- strfname.Format("%s",an.GetRequestTypeAsString(an.GetRequestType()));
- NuanceStatus status = an.GetStatus();
- if (NUANCE_OK != status) {
- switch(an.GetRequestType())
- {
- case AcknowledgmentNotification::RequestType::StartRecording:
- m_recordreason=2;
- m_uRecord_ID=0;
- SetEvent(h_recordevent);
- break;
- case AcknowledgmentNotification::RequestType::RecognizeUtterance:
- m_uRecognition_ID=0;
- SetEvent(h_recognevent);
- break;
- case AcknowledgmentNotification::RequestType::SetParameter:
- SetEvent(h_parameterevent);
- break;
- case AcknowledgmentNotification::RequestType::PlayPrompts:
- if(m_dtmfstate==1) time(&m_ft);
- m_uPlay_Prompt_ID=0;
- m_parent->RemoveAllWav();
- ShowMsg("***playback Stop for error");
- SetEvent(h_playevent);
- break;
- case AcknowledgmentNotification::RequestType::DeleteRecord:
- SetEvent(h_delevent);
- break;
- case AcknowledgmentNotification::RequestType::CloseDatabase:
- SetEvent(h_closedbevent);
- break;
- case AcknowledgmentNotification::RequestType::NewDynamicGrammar:
- case AcknowledgmentNotification::RequestType::NewDynamicGrammarFromGSL:
- case AcknowledgmentNotification::RequestType::NewDynamicGrammarFromPhraseList:
- case AcknowledgmentNotification::RequestType::AddPhraseListToDynamicGrammar:
- case AcknowledgmentNotification::RequestType::AddPhraseToDynamicGrammar:
- case AcknowledgmentNotification::RequestType::RemovePhraseFromDynamicGrammar:
- SetEvent(h_newevent);
- break;
- case AcknowledgmentNotification::RequestType::InsertDynamicGrammar:
- SetEvent(h_insertevent);
- break;
- case AcknowledgmentNotification::RequestType::QueryDynamicGrammarContents:
- case AcknowledgmentNotification::RequestType::QueryRecordExists:
- SetEvent(h_queryevent);
- break;
- }
- }
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleInitializationCompleted (InitializationCompletedNotification const & icn)
- {
- StateDynagramOpenDB();
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandlePlaybackStopped (PlaybackStoppedNotification const & pcn)
- {
- PlaybackStoppedNotification::Reason reason = pcn.GetReason();
- switch (reason)
- {
- //完全播放结束
- case PlaybackStoppedNotification::PLAYBACK_COMPLETED:
- {
- m_playreason=1;
- }
- break;
- //若是调用了 Abort 函数 或 挂断电话
- case PlaybackStoppedNotification::PLAYBACK_ABORTED:
- {
- m_playreason=2;
- }
- break;
- case PlaybackStoppedNotification::HANG_UP:
- {
- m_playreason=3;
- }
- break;
- default:
- {
- m_playreason=4;
- }
- }//end of case
- if(m_dtmfstate==1) time(&m_ft);
- m_uPlay_Prompt_ID=0;
- m_parent->RemoveAllWav();
- ShowMsg("***playback Stop");
- SetEvent(h_playevent);
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleRecordingStopped (RecordingStoppedNotification const & rsn)
- {
- m_uRecord_ID=0;
- SetEvent(h_recordevent);
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleStartOfSpeech (StartOfSpeechNotification const & sosn)
- {
- printf("^^^^^^^^^HandleStartOfSpeech/r/n");
- if(m_dtmfstate==1) StopDtmf(8);
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleEndOfSpeech (EndOfSpeechNotification const & eosn)
- {
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandlePartialResult (PartialResultNotification const & prn)
- {
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleRecognitionStopped (RecognitionStoppedNotification const & rsn)
- {
- //取的识别 ID
- unsigned id = rsn.GetUtteranceID();
- //判断识别结束原因
- int iReason = rsn.GetReason();
- switch (iReason)
- {
- //识别顺利完成
- case RecognitionStoppedNotification::COMPLETED:
- {
- RecResult const * rr = rsn.GetRecResult();
- RecognizeNotify((RecResult*)rr);
- }
- break;
- //挂断电话
- case RecognitionStoppedNotification::HANG_UP:
- {
- }
- break;
- //调用 Abort
- case RecognitionStoppedNotification::ABORTED:
- {
- ShowMsg("00000000000000000");
- }
- break;
- default:
- {
- ShowMsg("111111111111111111111");
- }
- }
- ShowMsg("Recognize Stop");
- m_uRecognition_ID=0;
- SetEvent(h_recognevent);
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleIncomingCall (IncomingCallNotification const & icn)
- {
- ShowMsg("Incoming call--------------");
- strcpy(m_parent->m_callingID,icn.GetCallerID());
- strcpy(m_parent->m_calledID,icn.GetDID());
- unsigned id = icn.GetCallID();
- m_uAnswer_Call_ID=id;
- m_pRcEngine->AnswerCall(m_uAnswer_Call_ID);
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleOutgoingCallConnected (OutgoingCallConnectedNotification const & occn)
- {
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleCallAnswered (CallAnsweredNotification const & can)
- {
- ShowMsg("Call Answered--------------");
- m_parent->m_callstate=1;
- m_parent->PostThreadMessage(WM_CALL_CONNECT,0,0);
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleCallEnded (CallEndedNotification const & cen)
- {
- ShowMsg("Call Ended--------------");
- m_parent->m_callstate=2;
- StopAll();
- m_parent->PostThreadMessage(WM_CALL_DISCONNECT,0,0);
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleNuanceDBOpened (NuanceDBOpenedNotification const & ndbon)
- {
- // check status
- NuanceStatus status = ndbon.GetStatus();
- if (NUANCE_OK != status) {
- CString msg="OpenDB 失败";
- ShowMsg(msg);
- m_pRcEngine->Abort(NullID);
- return;
- }
- unsigned id = ndbon.GetID();
- if (m_uDynagram_Db_ID != id) {
- CString msg="OpenDB ID 失败";
- ShowMsg(msg);
- m_pRcEngine->Abort(NullID);
- return;
- }
- CString msg="Open DB 成功";
- ShowMsg(msg);
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleNuanceDBClosed (NuanceDBClosedNotification const & ndbcn)
- {
- NuanceStatus status = ndbcn.GetStatus();
- if (NUANCE_OK != status) {
- CString msg="CloseDB 失败";
- ShowMsg(msg);
- }else
- {
- CString msg="CloseDB 成功";
- ShowMsg(msg);
- }
- SetEvent(h_closedbevent);
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleNuanceDBRecordExists (NuanceDBRecordExistsNotification const & ndben)
- {
- NuanceStatus status = ndben.GetStatus();
- if (NUANCE_OK != status) {
- CString msg="Grammar 记录不存在";
- ShowMsg(msg);
- r_exist=false;
- }else
- {
- CString msg="Grammar 记录存在";
- ShowMsg(msg);
- r_exist=true;
- }
- SetEvent(h_queryevent);
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleDynamicGrammarInserted (DynamicGrammarInsertedNotification const & dgin)
- {
- NuanceStatus status = dgin.GetStatus();
- if (NUANCE_OK != status) {
- ShowMsg("Grammar insert 失败");
- }else
- {
- ShowMsg("Grammar insert 成功");
- }
- SetEvent(h_insertevent);
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleParameterSet (ParameterSetNotification const & psn)
- {
- SetEvent(h_parameterevent);
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleParameterGotten (ParameterGottenNotification const & pgn)
- {
- }
- void NUANCE_MEMBER_FUNCTION
- CGrammarThread::HandleException (ExceptionNotification const & en)
- {
- }
- void
- CGrammarThread::
- StateDynagramOpenDB (void)
- {
- m_uDynagram_Db_ID = m_pRcEngine->GetUniqueID();
- m_pRcEngine->OpenDynamicGrammarDatabase(m_pDynagram_Db_Desc, m_uDynagram_Db_ID);
- }
- int CGrammarThread::NewDynaGrammar(char *key,char *text,bool overwrite)
- {
- bool t_ret=false;
- PhraseList* m_pPhrase_List;
- m_pPhrase_List=NewPhraseList();
- char* token = strtok(text, "|");
- while( token != NULL )
- {
- CString tmpstr;
- tmpstr.Format("%s",token);
- bool ret=CreatePhraseList(m_pPhrase_List,(char*)(LPCTSTR)tmpstr);
- if(ret) t_ret=ret;
- token = strtok( NULL, "|" );
- }
- if(!t_ret)
- {
- SetEvent(h_newevent);
- DeletePhraseList(m_pPhrase_List);
- return 1;
- }
- unsigned m_uDynagram_New_ID = m_pRcEngine->GetUniqueID();
- if(overwrite)
- {
- ResetEvent(h_newevent);
- m_pRcEngine->NewDynamicGrammar(m_uDynagram_Db_ID,key,
- m_uDynagram_New_ID,m_pPhrase_List,NULL,1);
- }else
- {
- if(QueryRecord(key))
- {
- ResetEvent(h_newevent);
- m_pRcEngine->AddPhraseListToDynamicGrammar(m_uDynagram_Db_ID,key,
- m_uDynagram_New_ID,m_pPhrase_List,NULL);
- }
- else
- {
- ResetEvent(h_newevent);
- m_pRcEngine->NewDynamicGrammar(m_uDynagram_Db_ID,key,
- m_uDynagram_New_ID,m_pPhrase_List,NULL,1);
- }
- }
- DeletePhraseList(m_pPhrase_List);
- WaitForSingleObject(h_newevent,INFINITE);
- return 0;
- }
- CString ParseGrammar(CString src)
- {
- CString result="";
- CString tmp="";
- char ch;
- bool haso=false;
- for(int i=0;i<SRC.GETLENGTH();I++) { } ; slotname="" char int if(i="=0)" break; if(!mrun) i++; slotname.TrimRight(); slotname.TrimLeft(); ntext.TrimRight(); ntext.TrimLeft(); ntext="" ptext.TrimRight(); ptext.TrimLeft(); ptext="" stext.TrimRight(); stext.TrimLeft(); stext="" pos1=‘srctext.Find("*",0);‘ pos2="0;" ttext="" }else mrun="true;" if(pos1<0) CString while(1) i="0;" bool srctext.Format(?%s?,text); srctext; status; NuanceStatus *text) *p_list, CGrammarThread::CreatePhraseList(PhraseList result; return ?); result.TrimRight(? tmp="" free(phFood); result+="tmp;" ?,phFood); tmp.Format(?%s if(phFood) pyTable); *phFood="GetPhoneticByWideString((WCHAR*)(char*)(LPCTSTR)tmp,dictArray," tmp.Format(??(%s) if(haso) ) if(tmp!="" tmp+="ch;" haso="true;" if(ch="=‘[‘)" ch="src.GetAt(i);">3) break;
- }
- if(stext=="") return false;
- bool srcz=false;
- if (ntext.Left(2)=="c:"||ntext.Left(2)=="C:")
- {
- ntext=ntext.Right(ntext.GetLength()-2);
- srcz=true;
- }
- if(ntext=="")
- {
- char *phFood =GetPhoneticByWideString((WCHAR*)(char*)(LPCTSTR)stext,dictArray,
- pyTable);
- if(!phFood)
- {
- return false;
- }
- ntext.Format("%s",phFood);
- free(phFood);
- }else
- {
- if(srcz)
- {
- ntext=ParseGrammar(ntext);
- srcz=false;
- }
- }
- if(ptext=="")
- {
- ptext=ntext;
- ptext.Replace(" ","");
- }
- CString ctmp=ntext;
- ntext="("+ctmp+")";
- ctmp=stext;
- if(slotname=="")
- {
- slotname="peoplename";
- }
- stext="{<" + slotname+" /""+ctmp+"/">}";
- status = PhraseListAdd(
- p_list, // phrase list pointer
- ptext, // used to reference phrase in db
- ntext, // actual text to pronounce
- stext, // nl entry
- 1.0 // probability
- );
- if(status == NUANCE_OK)
- return true;
- return 再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net
以上是关于一个Nuance 的语音识别的例子的主要内容,如果未能解决你的问题,请参考以下文章