⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 osbrec.cpp

📁 Open VXI. This is a open source.
💻 CPP
字号:
/****************License************************************************ * * Copyright 2000-2001.  SpeechWorks International, Inc.     * * Use of this software is subject to notices and obligations set forth * in the SpeechWorks Public License - Software Version 1.1 which is * included with this software. * * SpeechWorks is a registered trademark, and SpeechWorks Here, * DialogModules and the SpeechWorks logo are trademarks of SpeechWorks * International, Inc. in the United States and other countries. *  *********************************************************************** * * An implementation of the VXIrecInterface object designed for scripted *   QA of the VXI interpreter * ***********************************************************************/#define OSBREC_EXPORTS#include "OSBrec.h"#include "osbrec_utils.h"#include <cstdio>#include <cstring>#include "VXIvalue.h"// ------*---------*---------*---------*---------*---------*---------*---------class VXIVectorHolder {public:  VXIVectorHolder() : _vector(NULL)  { _vector = VXIVectorCreate(); }  VXIVectorHolder(VXIVector * m) : _vector(m) { }  ~VXIVectorHolder()        { if (_vector != NULL) VXIVectorDestroy(&_vector);}  VXIVectorHolder & operator=(const VXIVectorHolder & x)  { if (this != &x) {      if (_vector != NULL) VXIVectorDestroy(&_vector);      _vector = VXIVectorClone(x._vector); }    return *this; }  // GetValue returns the internal vector.  The standard vector manipulation  // functions may then be used.  VXIVector * GetValue() const       { return _vector; }  // These functions allow the holder to take ownership of an existing vector  // or to release the internal one.  VXIVector * Release()    { VXIVector * m = _vector; _vector = NULL; return m; }  void Acquire(VXIVector * m)  { if (_vector != NULL) VXIVectorDestroy(&_vector); else _vector = m; }private:  VXIVectorHolder(const VXIVectorHolder &);   // intentionally not defined.  VXIVector * _vector;};// ------*---------*---------*---------*---------*---------*---------*---------// Global for the base diagnostic tag ID, see osbrec_utils.h for tags//static VXIunsigned gblDiagLogBase = 0;// OSBrec implementation of the VXIrec interface//extern "C" {  struct OSBrecImpl {    // Base interface, must be first    VXIrecInterface intf;    // Class for managing grammars    OSBrecData *recData;  };}// A few conversion functions...static inline VXIrecGrammar * ToVXIrecGrammar(OSBrecGrammar * i){ return reinterpret_cast<VXIrecGrammar *>(i); }static inline OSBrecGrammar * FromVXIrecGrammar(VXIrecGrammar * i){ return reinterpret_cast<OSBrecGrammar *>(i); }static inline OSBrecData * GetRecData(VXIrecInterface * i){  if (i == NULL) return NULL;  return reinterpret_cast<OSBrecImpl *>(i)->recData;}/******************************************************* * Method routines for VXIrecInterface structure *******************************************************/ // Get the VXIrec interface version supported//static VXIint32 OSBrecGetVersion(void){  return VXI_CURRENT_VERSION;}// Get the implementation name//static const VXIchar* OSBrecGetImplementationName(void){  static const VXIchar IMPLEMENTATION_NAME[] = COMPANY_DOMAIN L".OSBrec";  return IMPLEMENTATION_NAME;}static VXIrecResult OSBrecBeginSession(VXIrecInterface * pThis, VXIMap *){  if (GetRecData(pThis) == NULL) return VXIrec_RESULT_INVALID_ARGUMENT;  return VXIrec_RESULT_SUCCESS;}static VXIrecResult OSBrecEndSession(VXIrecInterface *pThis, VXIMap *){  OSBrecData* tp = GetRecData(pThis);  if (tp == NULL) return VXIrec_RESULT_INVALID_ARGUMENT;  tp->Clear();  return VXIrec_RESULT_SUCCESS;}static VXIrecResult OSBrecLoadGrammarFromURI(struct VXIrecInterface *pThis,                                             const VXIMap *properties,                                             const VXIchar *type,                                              const VXIchar *uri,                                             const VXIMap *uriArgs,                                             VXIrecGrammar **gram){  OSBrecData* tp = GetRecData(pThis);  if (tp == NULL) return VXIrec_RESULT_INVALID_ARGUMENT;  return VXIrec_RESULT_UNSUPPORTED;  /*  if (!properties && wcsncmp(uri, L"builtin:", 8) != 0)    return VXIrec_RESULT_FATAL_ERROR;  gp = new rec_gram_t;  gp->in_line = 0;  tp->grams[tp->ngrams++] = gp;  if (!properties)    gp->fetchobj = VXIMapCreate();  else    gp->fetchobj = VXIMapClone(properties);  *gram = (VXIrecGrammar*)gp;  return VXIrec_RESULT_SUCCESS;  */}static VXIrecResult OSBrecLoadGrammarFromString(VXIrecInterface *pThis,                                                const VXIMap *prop,                                                const VXIchar *type,                                                const VXIchar *str,                                                VXIrecGrammar **gram){  // Check the arguments  OSBrecData* tp = GetRecData(pThis);  if (tp == NULL) return VXIrec_RESULT_INVALID_ARGUMENT;  if (str == NULL) return VXIrec_RESULT_INVALID_ARGUMENT;  OSBrecGrammar * gp = NULL;  // We ignore the type field, but different types could be created in future.  if (1) {    // Create a wordlist.    gp = tp->CreateWordListFromString(str);    if (gp == NULL) return VXIrec_RESULT_FAILURE;  }  tp->AddGrammar(gp);  *gram = ToVXIrecGrammar(gp);  return VXIrec_RESULT_SUCCESS;}static VXIrecResult OSBrecFreeGrammar(VXIrecInterface *pThis,                                      VXIrecGrammar **gram){  // Check the arguments  OSBrecData* tp = GetRecData(pThis);  if (tp == NULL) return VXIrec_RESULT_INVALID_ARGUMENT;  // If the grammar pointer is null, there is nothing to free.  if (gram == NULL || *gram == NULL) VXIrec_RESULT_SUCCESS;  tp->FreeGrammar(FromVXIrecGrammar(*gram));  *gram = NULL;  return VXIrec_RESULT_SUCCESS;}static VXIrecResult OSBrecActivateGrammar(VXIrecInterface *pThis,                                          const VXIMap *properties,                                          VXIrecGrammar *gram){  OSBrecData* tp = GetRecData(pThis);  if (tp == NULL) return VXIrec_RESULT_INVALID_ARGUMENT;  if (gram == NULL) return VXIrec_RESULT_INVALID_ARGUMENT;  FromVXIrecGrammar(gram)->SetEnabled(true);  return VXIrec_RESULT_SUCCESS;}static VXIrecResult OSBrecDeactivateGrammar(VXIrecInterface *pThis,                                            VXIrecGrammar *gram){  OSBrecData* tp = GetRecData(pThis);  if (tp == NULL) return VXIrec_RESULT_INVALID_ARGUMENT;  if (gram == NULL) return VXIrec_RESULT_INVALID_ARGUMENT;  FromVXIrecGrammar(gram)->SetEnabled(false);  return VXIrec_RESULT_SUCCESS;}static void RecognitionResultDestroy(VXIrecRecognitionResult **Result){  if (Result == NULL || *Result == NULL) return;  VXIrecRecognitionResult * result = *Result;  if (result->waveform != NULL)    VXIContentDestroy(&result->waveform);  if (result->results != NULL)    VXIVectorDestroy(&result->results);  delete result;  *Result = NULL;}static VXIrecResult OSBrecRecognize(VXIrecInterface *pThis,                                    const VXIMap *properties,                                    VXIrecRecognitionResult **recogResult){  OSBrecData* tp = GetRecData(pThis);  if (tp == NULL) return VXIrec_RESULT_INVALID_ARGUMENT;  // default behavior, pretend person said 'berry'  VXIchar* best = L"berry";  VXIMap*  res  = NULL;  VXIchar* str  = NULL;  VXIchar console[512];      if (properties != NULL) {    VXIString* vect =(VXIString*)VXIMapGetProperty(properties, L"RecResult");    if (vect != NULL) best = (VXIchar*) VXIStringCStr(vect);    if (wcscmp(best, L"-") == 0) {      unsigned int i;      VXIchar* cp = console;      char lbuf[512];      printf("Console: ");      fgets(lbuf, 511, stdin);      // copy to VXIchar      for(i = 0; i < strlen(lbuf); ++i) {        if (lbuf[i] == '\r' || lbuf[i] == '\n') continue;        *cp++ = lbuf[i] & 0x7f;      }      *cp++ = 0;      best = console;    }  }  VXIrecRecognitionResult * result = new VXIrecRecognitionResult();  if (result == NULL) return VXIrec_RESULT_OUT_OF_MEMORY;  *recogResult = result;  tp->Diag(DIAG_TAG_RECOGNITION, NULL, L"best = %s", best);  if (1) {      const VXIchar* val = best;    result->Destroy = RecognitionResultDestroy;    result->status = REC_STATUS_SUCCESS;    result->mode   = REC_INPUT_MODE_SPEECH;    result->waveform = NULL;    res = VXIMapCreate();    OSBrecGrammar * gp = tp->FindGrammarForPhrase(best, &val);    if (gp == NULL) {      if (best[0] == 0)        result->status = REC_STATUS_TIMEOUT;      else         result->status = REC_STATUS_FAILURE;    }              else {      VXIMapSetProperty(res, REC_GRAMMAR, (VXIValue*)VXIPtrCreate(gp));    }    // Create answer vectors.    VXIVectorHolder raws, keys, vals, confs;    if (raws.GetValue() == NULL || keys.GetValue() == NULL ||        vals.GetValue() == NULL || confs.GetValue() == NULL)      return VXIrec_RESULT_OUT_OF_MEMORY;    // Q: Is the recognition returning a single answer?    if (val[0] != '?') {      // A: Yes, we'll call it 'best_answer'.      const VXIchar * const KEY = L"best_answer";      VXIVectorAddElement(raws.GetValue(),  (VXIValue*)VXIStringCreate(best));      VXIVectorAddElement(keys.GetValue(),  (VXIValue*)VXIStringCreate(KEY));      VXIVectorAddElement(vals.GetValue(),  (VXIValue*)VXIStringCreate(val));      VXIVectorAddElement(confs.GetValue(), (VXIValue*)VXIFloatCreate(0.95F));    } else {      // A: No, we must extract the keys and values.      vxistring::size_type pos = 1; // skip leading '?'      vxistring data(val);      do {        vxistring::size_type itembreak = data.find(';', pos);        vxistring::size_type seperator = data.find('=', pos);        if (itembreak == vxistring::npos) itembreak = data.length();        if (seperator == vxistring::npos) seperator = data.length();        if (pos == itembreak || pos == seperator || itembreak <= seperator) {          result->status = REC_STATUS_FAILURE;          break;        }        VXIString * key   = VXIStringCreateN(data.c_str() + pos,                                             seperator - pos);        VXIString * value = VXIStringCreateN(data.c_str() + seperator + 1,                                             itembreak - seperator - 1);        VXIVectorAddElement(keys.GetValue(), (VXIValue*) key);        VXIVectorAddElement(vals.GetValue(), (VXIValue*) value);        VXIVectorAddElement(raws.GetValue(), (VXIValue*)VXIStringCreate(best));        VXIVectorAddElement(confs.GetValue(),(VXIValue*)VXIFloatCreate(0.95F));        pos = itembreak + 1;      } while (pos < data.length());    }    // Add vectors to result map.    VXIMapSetProperty(res, REC_RAW,    (VXIValue *) raws.Release());    VXIMapSetProperty(res, REC_KEYS,   (VXIValue *) keys.Release());    VXIMapSetProperty(res, REC_VALUES, (VXIValue *) vals.Release());    VXIMapSetProperty(res, REC_CONF,   (VXIValue *) confs.Release());    result->results = VXIVectorCreate();    VXIVectorAddElement(result->results, (VXIValue*) res);    return VXIrec_RESULT_SUCCESS;  }  // Should never get here  return VXIrec_RESULT_FATAL_ERROR;}static VXIrecResult OSBrecRecord(VXIrecInterface *pThis,                                 const VXIMap *props,                                 VXIrecRecordResult **recordResult){  OSBrecData* tp = GetRecData(pThis);  if (tp == NULL) return VXIrec_RESULT_INVALID_ARGUMENT;  if (recordResult == NULL) return VXIrec_RESULT_INVALID_ARGUMENT;  return VXIrec_RESULT_UNSUPPORTED;}/******************************************************* * Init and factory routines *******************************************************/ static inline OSBrecImpl * ToOSBrecImpl(VXIrecInterface * i){ return reinterpret_cast<OSBrecImpl *>(i); }// Global init - Don't need to do much here//OSBREC_API VXIrecResult OSBrecInit(VXIlogInterface *log,				   VXIunsigned      diagLogBase){  if (!log) return VXIrec_RESULT_INVALID_ARGUMENT;  gblDiagLogBase = diagLogBase;  return VXIrec_RESULT_SUCCESS;}// Global shutdown//OSBREC_API VXIrecResult OSBrecShutDown(VXIlogInterface *log){  if (!log) return VXIrec_RESULT_INVALID_ARGUMENT;  return VXIrec_RESULT_SUCCESS;}// Create an VXIrecInterface object and return.//OSBREC_API VXIrecResult OSBrecCreateResource(VXIlogInterface   *log,					     VXIrecInterface  **rec){  if (!log) return VXIrec_RESULT_INVALID_ARGUMENT;  OSBrecImpl* pp = new OSBrecImpl();  if (pp == NULL) return VXIrec_RESULT_OUT_OF_MEMORY;  OSBrecData* tp = new OSBrecData(gblDiagLogBase, log);  if (tp == NULL) return VXIrec_RESULT_OUT_OF_MEMORY;  pp->recData = tp;  pp->intf.GetVersion = OSBrecGetVersion;  pp->intf.GetImplementationName = OSBrecGetImplementationName;  pp->intf.BeginSession = OSBrecBeginSession;  pp->intf.EndSession = OSBrecEndSession;  pp->intf.LoadGrammarURI = OSBrecLoadGrammarFromURI;    pp->intf.LoadGrammarString = OSBrecLoadGrammarFromString;  pp->intf.ActivateGrammar = OSBrecActivateGrammar;  pp->intf.DeactivateGrammar = OSBrecDeactivateGrammar;  pp->intf.FreeGrammar = OSBrecFreeGrammar;  pp->intf.Recognize = OSBrecRecognize;  pp->intf.Record = OSBrecRecord;  *rec = &pp->intf;  return VXIrec_RESULT_SUCCESS;}// Free OSBrec structure allocated in OSBrecCreateResource.//OSBREC_API VXIrecResult OSBrecDestroyResource(VXIrecInterface **rec){  if (rec == NULL || *rec == NULL) return VXIrec_RESULT_INVALID_ARGUMENT;  OSBrecImpl* recImpl = reinterpret_cast<OSBrecImpl*>(*rec);  OSBrecData * tp = recImpl->recData;  if (tp != NULL) delete tp;  delete recImpl;  *rec = NULL;  return VXIrec_RESULT_SUCCESS;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -