📄 osbrec_utils.cpp
字号:
/****************License************************************************ * * Copyright 2000. 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. * ************************************************************************ * * $Id: osbrec_utils.cpp,v 1.1.2.6 2001/11/10 00:30:17 jerry Exp $ * * OSBrec integration utils * ****************************************************************************/#include "osbrec_utils.h"#include <cstdio>#include <string>#include <cstring>#include <ctype.h>#include <wctype.h>#include "VXIvalue.h"/****************************************** * OSBrecWordList : A list of words ******************************************/struct OSBrecWordListEntry { const vxistring word; const vxistring return_val; OSBrecWordListEntry(const vxistring & w, const vxistring & val) : word(w), return_val(val) { }};class OSBrecWordList : public OSBrecGrammar { enum GTYPE { GTYPE_NONE, GTYPE_DTMF, GTYPE_SPEECH }; typedef std::list<OSBrecWordListEntry *> WORDS; WORDS words; bool enabled; GTYPE gtype;public: enum AddWordResult { ADDWORD_RESULT_SUCCESS = 0, ADDWORD_RESULT_SPEECH_AND_DTMF = 1, ADDWORD_RESULT_GENERIC_ERROR = 2 };public: // OSBrecGrammar fuctions... virtual bool CheckPhrase(const VXIchar* best, const VXIchar** val) const; virtual void SetEnabled(bool e) { enabled = e; } virtual bool IsEnabled() const { return enabled; } virtual bool is_dtmf (const VXIchar* word) const;public: AddWordResult AddWord(const vxistring & word, const vxistring & value); // Adds a new word/value pair to the list. // // Returns: AddWordResult as defined above OSBrecWordList(); virtual ~OSBrecWordList();};OSBrecWordList::OSBrecWordList() : enabled(false), gtype(OSBrecWordList::GTYPE_NONE){ }OSBrecWordList::~OSBrecWordList(){ for (WORDS::iterator i = words.begin(); i != words.end(); ++i) delete *i;}bool OSBrecWordList::CheckPhrase(const VXIchar* best, const VXIchar** val) const{ vxistring phrase(best); for (WORDS::const_iterator i = words.begin(); i != words.end(); ++i) { if ((*i)->word == phrase) { *val = (*i)->return_val.c_str(); return true; } } return false;}// We allow 1 char DTMF grammars//inline bool OSBrecWordList::is_dtmf(const VXIchar* word) const{ if (word[1] != 0) return false; if (word[0] >= '0' && word[0] <= '9') return true; if (word[0] == '*') return true; if (word[0] == '#') return true; return false;}OSBrecWordList::AddWordResult OSBrecWordList::AddWord(const vxistring & word, const vxistring & value){ if (word.empty() || value.empty()) return ADDWORD_RESULT_GENERIC_ERROR; if (is_dtmf(word.c_str())) { if (gtype == OSBrecWordList::GTYPE_SPEECH) return ADDWORD_RESULT_SPEECH_AND_DTMF; gtype = OSBrecWordList::GTYPE_DTMF; } else { if (gtype == OSBrecWordList::GTYPE_DTMF) return ADDWORD_RESULT_SPEECH_AND_DTMF; gtype = OSBrecWordList::GTYPE_SPEECH; } OSBrecWordListEntry * entry = new OSBrecWordListEntry(word, value); words.push_front(entry); return ADDWORD_RESULT_SUCCESS;}//////////////////////////////////////////////////////// These routines process JSGF-like grammars. ////////////////////////////////////////////////////////void OSBrecData::GrammarGetNextToken(const vxistring & grammar, vxistring::size_type pos, vxistring::size_type end, vxistring & token) const{ // Find first real character while (pos != grammar.length() && isspace(grammar[pos])) ++pos; if (pos == grammar.length()) { token.erase(); return; } // Extract wordRaw; we know there is at least one character while (iswspace(grammar[end - 1])) --end; token = grammar.substr(pos, end - pos);}bool OSBrecData::GrammarConvertJSGFString(const vxistring & language, const vxistring & incoming, OSBrecWordList * gp) const{ int count = 0; // These define the symbols used in the JSGF-lite that defines these grammars const char NEXT = '|'; const char BEGIN_VAL = '{'; const char END_VAL = '}'; // If the language is not en-US, return an error. if (language != L"en-US") return false; // These are positions within the string that we're parsing. vxistring::size_type pos = 0; vxistring::size_type next; vxistring::size_type valmark; vxistring wordRaw; vxistring wordValue; while (pos < incoming.length()) { // Find the next item next = incoming.find(NEXT, pos); if (next == vxistring::npos) next = incoming.length(); // Find the start of the value valmark = incoming.find(BEGIN_VAL, pos); if (valmark == vxistring::npos || valmark > next) valmark = next; // Extract left hand side (raw text) GrammarGetNextToken(incoming, pos, valmark, wordRaw); if (wordRaw.empty()) { pos = next + 1; continue; } pos = valmark + 1; // Extract right hand side (value) if (valmark != next && pos < incoming.length()) { valmark = incoming.find(END_VAL, pos); if (valmark == vxistring::npos || valmark > next) { Error(201, L"%s%s", L"MSG", L"OSBrec::ConvertGrammar - Mismatched { } pair"); return false; } GrammarGetNextToken(incoming, pos, valmark, wordValue); if (wordValue.empty()) { Error(202, L"%s%s", L"MSG", L"OSBrec::ConvertGrammar - Empty { } pair"); return false; } pos = next + 1; } else wordValue.erase(); // Add word to grammar OSBrecWordList::AddWordResult addRc; if (!wordValue.empty()) { // Got tag value Diag(DIAG_TAG_GRAMMARS, L"OSBrec::ConvertGrammar", L"Adding #%d (%s, %s)", count, wordRaw.c_str(), wordValue.c_str()); addRc = gp->AddWord(wordRaw, wordValue); } else { // Didn't get tag value Diag(DIAG_TAG_GRAMMARS, L"OSBrec::ConvertGrammar", L"Adding #%d (%s, %s)", count, wordRaw.c_str(), wordRaw.c_str()); addRc = gp->AddWord(wordRaw, wordRaw); } switch (addRc) { case OSBrecWordList::ADDWORD_RESULT_SUCCESS: break; case OSBrecWordList::ADDWORD_RESULT_SPEECH_AND_DTMF: Error(200, L"%s%s", L"MSG", L"OSBrec::ConvertGrammar - No support for mixed DTMF and " L"speech grammars"); return false; break; case OSBrecWordList::ADDWORD_RESULT_GENERIC_ERROR: default: Error(203, L"%s%s", L"MSG", L"OSBrec::ConvertGrammar - Add word failed"); return false; } ++count; } Diag(DIAG_TAG_GRAMMARS, L"OSBrec::ConvertGrammar", L"Found %d item(s).", count); return count != 0;}OSBrecGrammar * OSBrecData::CreateWordListFromString(const VXIchar* text) const{ OSBrecWordList * gp = new OSBrecWordList(); if (gp == NULL) return NULL; if (!GrammarConvertJSGFString(L"en-US", text, gp)) { delete gp; return NULL; } return gp;}/****************************************** * OSBrecData : The grammar container ******************************************/OSBrecData::OSBrecData(VXIunsigned b, VXIlogInterface *l) : diagLogBase(b), log(l), grammars(){ }OSBrecData::~OSBrecData(){ for (GRAMMARS::iterator i = grammars.begin(); i != grammars.end(); ++i) delete *i;}void OSBrecData::Clear(){ for (GRAMMARS::iterator i = grammars.begin(); i != grammars.end(); ++i) delete *i; grammars.clear();}void OSBrecData::AddGrammar(OSBrecGrammar * l){ if (l == NULL) return; grammars.push_front(l);}void OSBrecData::FreeGrammar(OSBrecGrammar * l){ if (l == NULL) return; grammars.remove(l); delete l;}OSBrecGrammar * OSBrecData::FindGrammarForPhrase(const VXIchar* best, const VXIchar** val){ OSBrecGrammar * match = NULL; for (GRAMMARS::iterator i = grammars.begin(); i != grammars.end(); ++i) if ((*i)->IsEnabled() && (*i)->CheckPhrase(best, val)) { if (match != NULL) Diag(DIAG_TAG_GRAMMARS, L"OSBrec::FindGrammarForPhrase", L"Multiple grammars matched %s", best); match = *i; } return match;}VXIlogResult OSBrecData::Error(VXIunsigned errorID, const VXIchar *format, ...) const{ VXIlogResult rc; va_list args; if (log) return VXIlog_RESULT_NON_FATAL_ERROR; if (format) { va_start(args, format); rc = (*log->VError)(log, COMPANY_DOMAIN L".OSBrec", errorID, format, args); va_end(args); } else { rc = (*log->Error)(log, COMPANY_DOMAIN L".OSBrec", errorID, NULL); } return rc;}VXIlogResult OSBrecData::Diag(VXIunsigned tag, const VXIchar *subtag, const VXIchar *format, ...) const{ VXIlogResult rc; va_list args; if (log) return VXIlog_RESULT_NON_FATAL_ERROR; if (format) { va_start(args, format); rc = (*log->VDiagnostic)(log, tag + diagLogBase, subtag, format, args); va_end(args); } else { rc = (*log->Diagnostic)(log, tag + diagLogBase, subtag, NULL); } return rc;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -