Click here to Skip to main content
15,888,202 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have completed a Voice Recognition program written in Visual Basic 2013, which I would like to enhance by loading data dynamically into my Grammar. Below I have found an example that does EXACTLY what I need!! Unfortunately, it is not written in Basic and I can not seem to convert it into Basic.
Can anyone help me.
Following is the code that I would like to converted into Basic.

<grammar langid="409"><!-- american english grammar -->
<rule name="E-MAIL" toplevel="INACTIVE"><!-- inactive by default, to prevent premature recognitions -->
<phrase>send new e-mail to
<ruleref name="ADDRESS_BOOK" propname="NAME"><!-- add TRACK_PROP semantic property tag for easy information retrieval -->

<rule name="ADDRESS_BOOK" dynamic="TRUE">
<phrase>placeholder<!-- we'll stick placeholder text here that we'll replace immediately at runtime -->



The source code to manipulate the dynamic rule, "ADDRESS_BOOK" follows:

HRESULT hr = S_OK;

// create a new grammar object
hr = cpRecoContext->CreateGrammar(GRAM_ID, &cpRecoGrammar);
// Check hr

// deactivate the grammar to prevent premature recognitions to an "under-construction" grammar
hr = cpRecoGrammar->SetGrammarState(SPGS_DISABLED);
// Check hr

// load the email grammar dynamically, so changes can be made at runtime
hr = cpRecoGrammar->LoadCmdFromFile(L"email.xml", SPLO_DYNAMIC);
// Check hr

SPSTATEHANDLE hRule;

// first retrieve the dynamic rule ADDRESS_BOOK
hr = cpRecoGrammar->GetRule(L"ADDRESS_BOOK", NULL, SPRAF_Dynamic, FALSE, &hRule);
// Check hr

// clear the placeholder text, and everything else in the dynamic ADDRESS_BOOK rule
hr = cpRecoGrammar->ClearRule(hRule);
// Check hr

// add the real address book (e.g. "Frank Lee", "self", "SAPI beta", etc.).
// Note that ISpRecoGrammar inherits from ISpGrammarBuilder,
// so application gets the grammar compiler and ::AddWordTransition for free!

hr = cpRecoGrammar->AddWordTransition(hRule, NULL, L"Frank Lee", NULL, SPWT_LEXICAL, 1, NULL);
// Check hr
hr = cpRecoGrammar->AddWordTransition(hRule, NULL, L"self", NULL, SPWT_LEXICAL, 1, NULL);
// Check hr
hr = cpRecoGrammar->AddWordTransition(hRule, NULL, L"SAPI beta", NULL, SPWT_LEXICAL, 1, NULL);
// Check hr
// ... add rest of address book

// commit the grammar changes, which updates the grammar inside SAPI,
// and notifies the SR Engine about the rule change (i.e. "ADDRESS_BOOK"
hr = cpRecoGrammar->Commit(NULL);
// Check hr

// activate the grammar since "construction" is finished,
// and ready for receiving recognitions
hr = cpRecoGrammar->SetGrammarState(SPGS_ENABLED);
// Check hr


// activate the e-mail rule to begin receiving recognitions
hr = cpRecoGrammar->SetRuleState(L"EMAIL", NULL, SPRS_ACTIVE);
// Check hr

PWCHAR pwszEmailName = NULL;

// default event interest is recognition, so wait for recognition event
// NOTE: this could be placed in a loop to process multiple recognitions
hr = cpRecoContext->WaitForNotifyEvent(MY_REASONABLE_TIMEOUT);
// Check hr

// event notification fired
if (S_OK == hr) {

CSpEvent spEvent;
// if event retrieved and it is a recognition
if (S_OK == spEvent.GetFrom(cpRecoContext) && SPEI_RECOGNITION == spEvent.eEventId) {

// get the recognition result
CComPtr<isprecoresult> cpRecoResult = spEvent.RecoResult();

if (cpRecoResult) {
SPPHRASE* pPhrase = NULL;

// get the phrase object from the recognition result
hr = cpRecoResult->GetPhrase(&pPhrase);
if (SUCCEEDED(hr) && pPhrase) {

// if "EMAIL" rule was recognized ...
if (0 == wcscmp(L"EMAIL", pPhrase->Rule.pszName) {

// ... ensure that first property is "NAME"
if (0 == wcscmp(L"NAME", pPhrase->pProperties->pszName)) {

// store the user's spoken "send-to" name
// in a variable for later processing
hr = pPhrase->GetText(pPhrase->pProperties->ulFirstElement,
pPhrase->pProperties->ulCountOfElements,
FALSE,
&pwszEmailName,
NULL);
// Check hr

}
}
// free the phrase object
if (pPhrase) ::CoTaskMemFree(pPhrase);

}
}
}
}
Posted
Comments
Sergey Alexandrovich Kryukov 2-Jul-15 23:15pm    
Why not using System.Speech, which is bundled with .NET and wraps SAPI for .NET?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900