一个Nuance 的语音识别的例子

               
  1. #include "stdafx.h"    
  2. #include "GetToken.h"    
  3.    
  4.    
  5. #ifdef _DEBUG    
  6. #define new DEBUG_NEW    
  7. #undef THIS_FILE    
  8. static char THIS_FILE[] = __FILE__;   
  9. #endif    
  10.    
  11.   
  12. // The one and only application object    
  13.    
  14. //CWinApp theApp;    
  15. //using namespace std;    
  16.    
  17. #ifdef __UNICODE__    
  18. #undef __UNICODE__    
  19. #endif    
  20.    
  21. //#define __UNICODE__    
  22.    
  23. #ifdef __UNICODE__    
  24. #define dictName "dict.uni"    
  25. #define alpha "freepy.uni"    
  26.    
  27. #else//gb2312    
  28. #define dictName "dict.gb"    
  29. #define alpha "freepy.gb"    
  30. #endif    
  31.    
  32. const int  maxlength = 256;   
  33. #define dictSize 0x10000    
  34. const char pyIndex[]  = "pyindex";   
  35.    
  36.    
  37. BOOL ISHanzi(char *src)   
  38. {   
  39.     int asc=*src;   
  40.     if(asc<0)  asc+=256;   
  41.     if(asc>127)    
  42.         return TRUE;   
  43.     return FALSE;      
  44. }   
  45.    
  46. char * GetPhoneticByWideString(WCHAR * str,const WORD * dict,const char * pyTable)   
  47. {   
  48.     char phonetic[10];   
  49.     char * widestr = (char*)str;   
  50.     char *returnvalue=NULL;   
  51.     BOOL hz;   
  52.     hz=FALSE;   
  53.     while (*widestr != 0)   
  54.     {   
  55.         memset(phonetic,0,10);   
  56.         bool bEnglish = true;   
  57.         if(*widestr <= 'Z' && *widestr >='A')   
  58.         {   
  59.             if(hz)   
  60.             {   
  61.                 *phonetic=' ';     
  62.                 *(phonetic+1) = *widestr + 'a' - 'A';   
  63.                 *(phonetic+2) = NULL;   
  64.             }else   
  65.             {   
  66.                 *phonetic = *widestr + 'a' - 'A';   
  67.                 *(phonetic+1) = NULL;   
  68.             }   
  69.             hz=FALSE;   
  70.             widestr ++;   
  71.         }   
  72.         else if(*widestr <= 'z' && *widestr >='a' || *widestr == ' ')   
  73.         {   
  74.             if(hz&&*widestr != ' ')   
  75.             {   
  76.                 *(phonetic)=' ';   
  77.                 *(phonetic+1) = *widestr;   
  78.                 *(phonetic+2) = NULL;   
  79.             }   
  80.             else   
  81.             {   
  82.                 *phonetic = *widestr;   
  83.                 *(phonetic+1) = NULL;   
  84.             }   
  85.             hz=FALSE;   
  86.             widestr ++;   
  87.         }   
  88.         else if(*widestr >= '0' && *widestr <='9')   
  89.         {   
  90. /*          *phonetic = *widestr;  
  91.             *(phonetic+1) = NULL;         
  92.             hz=FALSE;  
  93.             widestr ++;  
  94. */   
  95.    
  96.             GetDigits(*widestr,phonetic);   
  97.             hz=TRUE;   
  98.             bEnglish = false;   
  99.             widestr++;   
  100.         }   
  101.    
  102.         else if(ISHanzi(widestr))   
  103.         {   
  104.             if (-1 == GetPhoneticByHanZi(*(WCHAR*)widestr,dict,pyTable,phonetic))   
  105.                 return NULL;   
  106.             widestr ++;   
  107.             if(!((*widestr <= 'Z' && *widestr >='A') || (*widestr <= 'a' && *widestr >='a') || *widestr == ' '))   
  108.                 widestr ++;   
  109.             hz=TRUE;   
  110.             bEnglish = false;   
  111.         }else   
  112.         {   
  113.             widestr ++;   
  114.             continue;   
  115.         }   
  116.         if (!returnvalue)   
  117.         {   
  118.             int i = strlen(phonetic);   
  119.             returnvalue = strdup(phonetic);   
  120.         }   
  121.         else   
  122.         {   
  123.             if(!bEnglish && returnvalue[strlen(returnvalue)-1] != ' ')   
  124.             {   
  125.                 returnvalue  = (char*)realloc(returnvalue,strlen(returnvalue)+strlen(phonetic)+2);   
  126.                 strcat(returnvalue," ");   
  127.             }   
  128.             else   
  129.             {   
  130.                 returnvalue  = (char*)realloc(returnvalue,strlen(returnvalue)+strlen(phonetic)+1);   
  131.             }   
  132.             strcat(returnvalue,phonetic);   
  133.         }   
  134.            
  135.     }   
  136.     return returnvalue;   
  137. }   
  138.    
  139. void LoadDictAndPYTable(WORD* pDict,char** pPyTable)   
  140. {   
  141.     CFile dictFile(dictName,CFile::modeRead|CFile::typeBinary);   
  142.     //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)    
  143.       {   
  144.           b=*(Big5Source+1);   
  145.           if (b<0x7e) b=b - 0x40;   
  146.             if (b>=0x80) b = b - 0x80 + 0x7e - 0x40 + 1;    
  147.           fseek(tabfile, 2 * ((a -0x81) * (0xfe - 0x80 + 1 + 0x7e - 0x40 + 1 ) + b), SEEK_SET);   
  148.           fread(& a,1,1,tabfile);    
  149.             fread(& b,1,1,tabfile);   
  150.           fwrite(&a,1,1,destfile);   
  151.           fwrite(&b,1,1,destfile);   
  152.             *outString=a;   
  153.           *(outString+1)=b;   
  154.           outString+=2;   
  155.           Big5Source+=2;   
  156.           m+=2;   
  157.       }   
  158.       else   
  159.       {   
  160.           fwrite(&a,1,1,destfile);   
  161.           *outString=a;   
  162.             outString++;   
  163.           Big5Source++;   
  164.           m++;   
  165.       }   
  166.               
  167.     }   
  168.      
  169.     fclose(destfile);   
  170.     fclose(tabfile);   
  171.     Big5Source=BigSource;   
  172.     outString[lstr-1]='/0';   
  173.     delete[] Big5Source;   
  174.     delete[] WidecharStr;   
  175.      
  176.     return GBString;   
  177.      
  178.  }   

 

  1. #include "stdafx.h"    
  2. #include "GrammarThread.h"    
  3. #include "GetToken.h"    
  4. #include "HandleDataThread.h"    
  5. #include "process.h"    
  6. #ifdef _DEBUG    
  7. #define new DEBUG_NEW    
  8. #undef THIS_FILE    
  9. static char THIS_FILE[] = __FILE__;   
  10. #endif    
  11. #define MAX_STRING_PARAM_LEN 300     
  12.   
  13. // CGrammarThread    
  14. using namespace std;   
  15. extern WORD dictArray[0X10000];   
  16. extern char* pyTable;   
  17. void ShowMsg(CString msg);   
  18.    
  19.    
  20.    
  21. CGrammarThread::CGrammarThread()   
  22. {   
  23.    
  24. }   
  25.    
  26. CGrammarThread::CGrammarThread(NuanceConfig &config,   
  27.                DispatcherInterface & d,   
  28.                DBDescriptor * db_desc,CHandleDataThread* _p):   m_pConfig(&config), m_pDispatcher(&d), m_pRcEngine(NULL),// m_kb(NULL),    
  29.                m_pDynagram_Db_Desc(db_desc)   
  30. {   
  31.     m_parent=_p;   
  32.     NuanceStatus   status;     
  33.     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);   
  34.     if (m_pDynagram_Db_Desc) {   
  35.         DeleteDBDescriptor(m_pDynagram_Db_Desc);   
  36.     }   
  37.     m_pDynagram_Db_Desc = NULL;   
  38.     delete m_pRcEngine;   
  39.     m_pRcEngine = NULL;   
  40.     CloseHandle(h_newevent);   
  41.     CloseHandle(h_queryevent);   
  42.     CloseHandle(h_delevent);   
  43.     CloseHandle(h_closedbevent);   
  44.     CloseHandle(h_insertevent);   
  45.     CloseHandle(h_playevent);   
  46.     CloseHandle(h_dtmfevent);   
  47.     CloseHandle(h_recognevent);   
  48.     CloseHandle(h_parameterevent);   
  49.     CloseHandle(h_recordevent);   
  50.     for(int i=0;slotnamelist[i]!=NULL;i++)   
  51.     {   
  52.         free(slotnamelist[i]);         
  53.     }   
  54.    
  55. }   
  56.    
  57. void NUANCE_MEMBER_FUNCTION   
  58. CGrammarThread::HandleCallTransferFailed(CallTransferFailedNotification const & n)   
  59. {   
  60. }    
  61.    
  62. void NUANCE_MEMBER_FUNCTION    
  63. CGrammarThread::HandleNuanceDBRecordDeleted(NuanceDBRecordDeletedNotification const & n)   
  64. {   
  65.     NuanceStatus status = n.GetStatus();   
  66.     if (NUANCE_OK != status) {   
  67.     }else   
  68.     {   
  69.         CString msg;   
  70.         msg="记录删除";   
  71.         ShowMsg(msg);   
  72.     }   
  73.     SetEvent(h_delevent);   
  74. }    
  75.    
  76. void NUANCE_MEMBER_FUNCTION   
  77. CGrammarThread::HandleNuanceDBRecordModified(NuanceDBRecordModifiedNotification const & n)   
  78. {   
  79.     CString msg;   
  80.     NuanceStatus status = n.GetStatus();   
  81.     if (NUANCE_OK != status) {   
  82.         msg.Format("Dynamic Grammar Modified 失败:%s",n.GetErrorString());   
  83.         ShowMsg(msg);   
  84.            
  85.     }else   
  86.     {   
  87.         msg="Dynamic Grammar Modified";   
  88.         ShowMsg(msg);   
  89.     }   
  90.     SetEvent(h_newevent);   
  91. }    
  92.    
  93. void NUANCE_MEMBER_FUNCTION   
  94. CGrammarThread::HandleNuanceDBRecordCreated(NuanceDBRecordCreatedNotification const & n)   
  95. {   
  96.     CString msg;   
  97.     NuanceStatus status = n.GetStatus();   
  98.     if (NUANCE_OK != status) {   
  99.         msg.Format("Dynamic Grammar Create 失败:%s",n.GetErrorString());   
  100.         ShowMsg(msg);   
  101.     }else   
  102.     {   
  103.         msg="Dynamic Grammar Create";   
  104.         ShowMsg(msg);   
  105.     }   
  106.     SetEvent(h_newevent);   
  107. }   
  108.    
  109. void NUANCE_MEMBER_FUNCTION    
  110. CGrammarThread::HandleConferenceCallStarted(ConferenceCallStartedNotification const & n)   
  111. {   
  112. }   
  113.    
  114. void NUANCE_MEMBER_FUNCTION    
  115. CGrammarThread::HandleConferenceEnded(ConferenceEndedNotification const & n)   
  116. {   
  117. }   
  118.    
  119. void NUANCE_MEMBER_FUNCTION    
  120. CGrammarThread::HandleDTMFTones(DTMFTonesNotification const & dn)   
  121. {   
  122.        
  123.     char const *m_cNum = dn.GetTones();    
  124.     printf("have gotten dtmf :%c/r/n",*m_cNum);   
  125.     if(m_uRecord_ID>0)   
  126.     {   
  127.         if(m_termdtmf!='a'&&m_termdtmf!='A')   
  128.         {   
  129.             if(*m_cNum==m_termdtmf)   
  130.                 StopRecord();   
  131.         }else   
  132.         {   
  133.             StopRecord();   
  134.         }   
  135.     }   
  136.     if(m_dtmfstate==1)   
  137.     {   
  138.         if(m_uRecognition_ID>0)   
  139.             StopRecognize(8);   
  140.            
  141.         StopPrompt();   
  142.         if(*m_cNum==m_termno)   
  143.         {   
  144.             m_dtmfreason=1;   
  145.             m_dtmfstate=0;   
  146.             return;   
  147.         }   
  148.         memcpy(m_dtmfbuf+m_dtmfcnt,m_cNum,strlen(m_cNum));   
  149.         m_dtmfcnt+=strlen(m_cNum);   
  150.         time(&m_dt);   
  151.         if(m_dtmfcnt==1&&*m_cNum==m_specdtmf)   
  152.         {   
  153.             m_dtmfreason=1;   
  154.             m_dtmfstate=0;   
  155.         }else if(m_dtmfcnt==m_dtmflen)   
  156.         {   
  157.             m_dtmfreason=1;   
  158.             m_dtmfstate=0;   
  159.         }   
  160.     }   
  161.        
  162. }    
  163.    
  164. void NUANCE_MEMBER_FUNCTION   
  165. CGrammarThread::HandleAcknowledgment (AcknowledgmentNotification const & an)   
  166. {   
  167.     CString strfname;   
  168.     strfname.Format("%s",an.GetRequestTypeAsString(an.GetRequestType()));   
  169.    
  170.     NuanceStatus status = an.GetStatus();   
  171.     if (NUANCE_OK != status) {   
  172.    
  173.         switch(an.GetRequestType())   
  174.         {   
  175.         case AcknowledgmentNotification::RequestType::StartRecording:   
  176.             m_recordreason=2;   
  177.             m_uRecord_ID=0;   
  178.             SetEvent(h_recordevent);   
  179.             break;   
  180.         case AcknowledgmentNotification::RequestType::RecognizeUtterance:   
  181.             m_uRecognition_ID=0;   
  182.             SetEvent(h_recognevent);   
  183.             break;   
  184.         case AcknowledgmentNotification::RequestType::SetParameter:   
  185.             SetEvent(h_parameterevent);   
  186.             break;   
  187.         case AcknowledgmentNotification::RequestType::PlayPrompts:   
  188.             if(m_dtmfstate==1) time(&m_ft);   
  189.             m_uPlay_Prompt_ID=0;   
  190.             m_parent->RemoveAllWav();   
  191.             ShowMsg("***playback Stop for error");   
  192.             SetEvent(h_playevent);     
  193.             break;   
  194.         case AcknowledgmentNotification::RequestType::DeleteRecord:   
  195.             SetEvent(h_delevent);   
  196.             break;   
  197.         case AcknowledgmentNotification::RequestType::CloseDatabase:   
  198.             SetEvent(h_closedbevent);   
  199.             break;   
  200.         case AcknowledgmentNotification::RequestType::NewDynamicGrammar:   
  201.         case AcknowledgmentNotification::RequestType::NewDynamicGrammarFromGSL:   
  202.         case AcknowledgmentNotification::RequestType::NewDynamicGrammarFromPhraseList:   
  203.         case AcknowledgmentNotification::RequestType::AddPhraseListToDynamicGrammar:   
  204.         case AcknowledgmentNotification::RequestType::AddPhraseToDynamicGrammar:       
  205.         case AcknowledgmentNotification::RequestType::RemovePhraseFromDynamicGrammar:      
  206.             SetEvent(h_newevent);   
  207.             break;   
  208.         case AcknowledgmentNotification::RequestType::InsertDynamicGrammar:   
  209.             SetEvent(h_insertevent);   
  210.             break;   
  211.         case AcknowledgmentNotification::RequestType::QueryDynamicGrammarContents:   
  212.         case AcknowledgmentNotification::RequestType::QueryRecordExists:   
  213.             SetEvent(h_queryevent);   
  214.             break;             
  215.         }   
  216.     }   
  217. }   
  218.    
  219. void NUANCE_MEMBER_FUNCTION   
  220. CGrammarThread::HandleInitializationCompleted (InitializationCompletedNotification const & icn)   
  221. {   
  222.     StateDynagramOpenDB();   
  223. }   
  224.    
  225. void NUANCE_MEMBER_FUNCTION   
  226. CGrammarThread::HandlePlaybackStopped (PlaybackStoppedNotification const & pcn)   
  227. {   
  228.    
  229.     PlaybackStoppedNotification::Reason reason = pcn.GetReason();   
  230.     switch (reason)    
  231.     {   
  232.         //完全播放结束    
  233.     case PlaybackStoppedNotification::PLAYBACK_COMPLETED:   
  234.         {   
  235.             m_playreason=1;   
  236.         }   
  237.         break;         
  238.         //若是调用了 Abort 函数 或 挂断电话    
  239.     case PlaybackStoppedNotification::PLAYBACK_ABORTED:   
  240.         {   
  241.             m_playreason=2;   
  242.         }   
  243.         break;   
  244.            
  245.     case PlaybackStoppedNotification::HANG_UP:   
  246.         {              
  247.             m_playreason=3;   
  248.         }   
  249.         break;   
  250.     default:   
  251.         {   
  252.             m_playreason=4;   
  253.         }   
  254.     }//end of case    
  255.     if(m_dtmfstate==1) time(&m_ft);   
  256.     m_uPlay_Prompt_ID=0;   
  257.     m_parent->RemoveAllWav();   
  258.     ShowMsg("***playback Stop");   
  259.     SetEvent(h_playevent);     
  260. }   
  261.    
  262. void NUANCE_MEMBER_FUNCTION   
  263. CGrammarThread::HandleRecordingStopped (RecordingStoppedNotification const & rsn)   
  264. {   
  265.     m_uRecord_ID=0;   
  266.     SetEvent(h_recordevent);   
  267. }   
  268.    
  269. void NUANCE_MEMBER_FUNCTION   
  270. CGrammarThread::HandleStartOfSpeech (StartOfSpeechNotification const & sosn)   
  271. {   
  272.     printf("^^^^^^^^^HandleStartOfSpeech/r/n");   
  273.     if(m_dtmfstate==1)  StopDtmf(8);   
  274. }   
  275.    
  276. void NUANCE_MEMBER_FUNCTION   
  277. CGrammarThread::HandleEndOfSpeech (EndOfSpeechNotification const & eosn)   
  278. {   
  279. }   
  280.    
  281. void NUANCE_MEMBER_FUNCTION   
  282. CGrammarThread::HandlePartialResult (PartialResultNotification const & prn)   
  283. {   
  284. }   
  285.    
  286. void NUANCE_MEMBER_FUNCTION   
  287. CGrammarThread::HandleRecognitionStopped (RecognitionStoppedNotification const & rsn)   
  288. {   
  289.     //取的识别 ID    
  290.     unsigned id = rsn.GetUtteranceID();   
  291.        
  292.     //判断识别结束原因    
  293.     int iReason = rsn.GetReason();    
  294.     switch (iReason)    
  295.     {   
  296.         //识别顺利完成    
  297.         case RecognitionStoppedNotification::COMPLETED:   
  298.         {   
  299.             RecResult const * rr = rsn.GetRecResult();     
  300.             RecognizeNotify((RecResult*)rr);                            
  301.         }   
  302.         break;   
  303.            
  304.         //挂断电话    
  305.         case RecognitionStoppedNotification::HANG_UP:   
  306.         {   
  307.                
  308.         }   
  309.         break;   
  310.            
  311.         //调用 Abort    
  312.         case RecognitionStoppedNotification::ABORTED:   
  313.         {   
  314.             ShowMsg("00000000000000000");   
  315.         }   
  316.         break;   
  317.            
  318.     default:   
  319.         {              
  320.             ShowMsg("111111111111111111111");   
  321.         }   
  322.     }   
  323.     ShowMsg("Recognize Stop");   
  324.     m_uRecognition_ID=0;   
  325.     SetEvent(h_recognevent);   
  326.    
  327. }   
  328.    
  329. void NUANCE_MEMBER_FUNCTION   
  330. CGrammarThread::HandleIncomingCall (IncomingCallNotification const & icn)   
  331. {   
  332.     ShowMsg("Incoming call--------------");   
  333.     strcpy(m_parent->m_callingID,icn.GetCallerID());   
  334.     strcpy(m_parent->m_calledID,icn.GetDID());   
  335.     unsigned id = icn.GetCallID();     
  336.     m_uAnswer_Call_ID=id;          
  337.     m_pRcEngine->AnswerCall(m_uAnswer_Call_ID);   
  338. }   
  339.    
  340. void NUANCE_MEMBER_FUNCTION   
  341. CGrammarThread::HandleOutgoingCallConnected (OutgoingCallConnectedNotification const & occn)   
  342. {   
  343. }   
  344.    
  345. void NUANCE_MEMBER_FUNCTION   
  346. CGrammarThread::HandleCallAnswered (CallAnsweredNotification const & can)   
  347. {   
  348.     ShowMsg("Call Answered--------------");   
  349.     m_parent->m_callstate=1;   
  350.     m_parent->PostThreadMessage(WM_CALL_CONNECT,0,0);   
  351. }   
  352.    
  353. void NUANCE_MEMBER_FUNCTION   
  354. CGrammarThread::HandleCallEnded (CallEndedNotification const & cen)   
  355. {   
  356.     ShowMsg("Call Ended--------------");   
  357.     m_parent->m_callstate=2;   
  358.     StopAll();   
  359.     m_parent->PostThreadMessage(WM_CALL_DISCONNECT,0,0);   
  360. }   
  361.    
  362. void NUANCE_MEMBER_FUNCTION   
  363. CGrammarThread::HandleNuanceDBOpened (NuanceDBOpenedNotification const & ndbon)   
  364. {   
  365.     // check status    
  366.     NuanceStatus status = ndbon.GetStatus();   
  367.     if (NUANCE_OK != status) {   
  368.         CString msg="OpenDB 失败";   
  369.         ShowMsg(msg);   
  370.         m_pRcEngine->Abort(NullID);   
  371.         return;   
  372.     }   
  373.        
  374.     unsigned id = ndbon.GetID();   
  375.        
  376.     if (m_uDynagram_Db_ID != id) {   
  377.         CString msg="OpenDB ID 失败";   
  378.         ShowMsg(msg);   
  379.         m_pRcEngine->Abort(NullID);   
  380.         return;   
  381.     }   
  382.     CString msg="Open DB 成功";   
  383.     ShowMsg(msg);   
  384.        
  385. }   
  386.    
  387. void NUANCE_MEMBER_FUNCTION   
  388. CGrammarThread::HandleNuanceDBClosed (NuanceDBClosedNotification const & ndbcn)   
  389. {   
  390.     NuanceStatus status = ndbcn.GetStatus();   
  391.     if (NUANCE_OK != status) {   
  392.         CString msg="CloseDB 失败";   
  393.         ShowMsg(msg);   
  394.    
  395.     }else   
  396.     {   
  397.         CString msg="CloseDB 成功";   
  398.         ShowMsg(msg);          
  399.     }   
  400.     SetEvent(h_closedbevent);   
  401. }   
  402.    
  403. void NUANCE_MEMBER_FUNCTION   
  404. CGrammarThread::HandleNuanceDBRecordExists (NuanceDBRecordExistsNotification const & ndben)   
  405. {   
  406.     NuanceStatus status = ndben.GetStatus();   
  407.     if (NUANCE_OK != status) {   
  408.         CString msg="Grammar 记录不存在";   
  409.         ShowMsg(msg);      
  410.         r_exist=false;   
  411.     }else   
  412.     {   
  413.         CString msg="Grammar 记录存在";   
  414.         ShowMsg(msg);      
  415.    
  416.         r_exist=true;   
  417.     }   
  418.     SetEvent(h_queryevent);   
  419.    
  420. }   
  421.    
  422. void NUANCE_MEMBER_FUNCTION   
  423. CGrammarThread::HandleDynamicGrammarInserted (DynamicGrammarInsertedNotification const & dgin)   
  424. {   
  425.     NuanceStatus status = dgin.GetStatus();   
  426.     if (NUANCE_OK != status) {   
  427.         ShowMsg("Grammar insert 失败");   
  428.     }else   
  429.     {   
  430.         ShowMsg("Grammar insert 成功");   
  431.     }   
  432.     SetEvent(h_insertevent);   
  433. }   
  434.    
  435. void NUANCE_MEMBER_FUNCTION   
  436. CGrammarThread::HandleParameterSet (ParameterSetNotification const & psn)   
  437. {   
  438.     SetEvent(h_parameterevent);   
  439. }   
  440.    
  441. void NUANCE_MEMBER_FUNCTION   
  442. CGrammarThread::HandleParameterGotten (ParameterGottenNotification const & pgn)   
  443. {   
  444. }   
  445.    
  446. void NUANCE_MEMBER_FUNCTION   
  447. CGrammarThread::HandleException (ExceptionNotification const & en)   
  448. {   
  449. }   
  450.    
  451. void   
  452. CGrammarThread::   
  453. StateDynagramOpenDB (void)   
  454. {   
  455.     m_uDynagram_Db_ID = m_pRcEngine->GetUniqueID();   
  456.     m_pRcEngine->OpenDynamicGrammarDatabase(m_pDynagram_Db_Desc, m_uDynagram_Db_ID);   
  457. }   
  458.    
  459.    
  460. int CGrammarThread::NewDynaGrammar(char *key,char *text,bool overwrite)   
  461. {   
  462.        
  463.     bool t_ret=false;   
  464.     PhraseList* m_pPhrase_List;   
  465.     m_pPhrase_List=NewPhraseList();   
  466.    
  467.     char* token = strtok(text, "|");   
  468.     while( token != NULL )   
  469.     {      
  470.         CString tmpstr;   
  471.         tmpstr.Format("%s",token);   
  472.         bool ret=CreatePhraseList(m_pPhrase_List,(char*)(LPCTSTR)tmpstr);   
  473.         if(ret) t_ret=ret;   
  474.         token = strtok( NULL, "|" );   
  475.     }   
  476.    
  477.     if(!t_ret)   
  478.     {   
  479.         SetEvent(h_newevent);   
  480.         DeletePhraseList(m_pPhrase_List);   
  481.         return 1;   
  482.     }   
  483.        
  484.     unsigned m_uDynagram_New_ID = m_pRcEngine->GetUniqueID();   
  485.     if(overwrite)   
  486.     {   
  487.         ResetEvent(h_newevent);   
  488.         m_pRcEngine->NewDynamicGrammar(m_uDynagram_Db_ID,key,   
  489.             m_uDynagram_New_ID,m_pPhrase_List,NULL,1);   
  490.     }else   
  491.     {   
  492.         if(QueryRecord(key))   
  493.         {   
  494.             ResetEvent(h_newevent);   
  495.             m_pRcEngine->AddPhraseListToDynamicGrammar(m_uDynagram_Db_ID,key,   
  496.                 m_uDynagram_New_ID,m_pPhrase_List,NULL);   
  497.         }   
  498.         else   
  499.         {   
  500.             ResetEvent(h_newevent);   
  501.             m_pRcEngine->NewDynamicGrammar(m_uDynagram_Db_ID,key,   
  502.             m_uDynagram_New_ID,m_pPhrase_List,NULL,1);   
  503.         }   
  504.                
  505.     }   
  506.        
  507.     DeletePhraseList(m_pPhrase_List);   
  508.     WaitForSingleObject(h_newevent,INFINITE);   
  509.     return 0;   
  510. }   
  511.    
  512. CString ParseGrammar(CString src)   
  513. {   
  514.     CString result="";   
  515.     CString tmp="";   
  516.     char ch;   
  517.     bool haso=false;   
  518.     for(int i=0;i<SRC.GETLENGTH();I++) { } ; slotname="" char int if(i="=0)" breakif(!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;   
  519.     }   
  520.     if(stext==""return false;   
  521.     bool srcz=false;   
  522.     if (ntext.Left(2)=="c:"||ntext.Left(2)=="C:")   
  523.     {   
  524.         ntext=ntext.Right(ntext.GetLength()-2);   
  525.         srcz=true;   
  526.     }   
  527.     if(ntext=="")   
  528.     {   
  529.    
  530.         char *phFood =GetPhoneticByWideString((WCHAR*)(char*)(LPCTSTR)stext,dictArray,   
  531.             pyTable);   
  532.         if(!phFood)   
  533.         {   
  534.             return false;   
  535.         }   
  536.         ntext.Format("%s",phFood);   
  537.         free(phFood);      
  538.     }else   
  539.     {   
  540.         if(srcz)   
  541.         {   
  542.             ntext=ParseGrammar(ntext);   
  543.             srcz=false;   
  544.         }   
  545.     }   
  546.        
  547.     if(ptext=="")   
  548.     {   
  549.         ptext=ntext;   
  550.         ptext.Replace(" ","");   
  551.     }   
  552.     CString ctmp=ntext;   
  553.     ntext="("+ctmp+")";   
  554.     ctmp=stext;   
  555.     if(slotname=="")   
  556.     {   
  557.         slotname="peoplename";   
  558.     }   
  559.     stext="{<" + slotname+" /""+ctmp+"/">}";   
  560.     status = PhraseListAdd(   
  561.         p_list,         // phrase list pointer    
  562.         ptext,          // used to reference phrase in db    
  563.         ntext,  // actual text to pronounce    
  564.         stext,             // nl entry    
  565.         1.0                     // probability    
  566.         );         
  567.     if(status == NUANCE_OK)   
  568.         return true;       
  569.     return 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值