Share via


ISpPhraseAlt::Commit (SAPI 5.4)

Microsoft Speech API 5.4

ISpPhraseAlt::Commit

ISpPhraseAlt::Commit replaces a section of the parent phrase to which the alternate corresponds.

  
    HRESULT  Commit  ( void );

Parameters

None

Return values

Value
S_OK
SPERR_NOT_FOUND

Remarks

After an alternate has been committed, the parent phrase will be modified to reflect the substitution.

Upon committing the alternate phrase, the SR engine also has the ability to update its language model to improve future recognitions of the same or similar phrases (see ISpSRAlternates::Commit).

Example

The following code snippet illustrates the use ISpPhraseAlt::Commit to commit an alternate phrase.

  
// Declare local identifiers:
HRESULT                    hr = S_OK;
const USHORT               MY_MAX_ALTERNATES = 10;
CComPtr<ISpRecoResult>     cpRecoResult;
CComPtr<ISpPhrase>         cpPhrase;
CComPtr<ISpPhraseAlt>      pcpPhraseAlt[MY_MAX_ALTERNATES];
SPPHRASE                   *pPhrase;
WCHAR                      *pwszText;
WCHAR                      *pwszAlternate;
BOOL                       fMoreAppropriate;
LONG                       lCount;

// ... Obtain a recognition result object from the recognizer ...

// Get the recognized phrase object.
hr = cpRecoResult->GetPhrase(&pPhrase;);

if (SUCCEEDED(hr))
{
   // Get the phrase's text.
   hr = cpPhrase->GetText(SP_GETWHOLEPHRASE, SP_GETWHOLEPHRASE, TRUE, &pwszText;, NULL);
}

if (SUCCEEDED(hr))
{
   // Check the phrase's text... Assume that
   // the phrase isn't a correct recognition.

   // Get the top MY_MAX_ALTERNATES alternates
   // to the entire recognized phrase.
   hr = cpRecoResult->GetAlternates(pPhrase->Rule.ulFirstElement,
        pPhrase->Rule.lCountOfElements,
        MY_MAX_ALTERNATES,
        pcpPhraseAlt,
        &lCount;);
}

if (SUCCEEDED(hr))
{
   // Check each alternate in order of highest likelihood.
   for (int i = 0; i < lCount; i++)
   {
      hr = pcpPhraseAlt[i]->GetText(SP_GETWHOLEPHRASE, SP_GETWHOLEPHRASE, TRUE, &pwszAlternate;, NULL);

      if (SUCCEEDED(hr))
      {
         // ... Check if this alternate is more appropriate ...

         // If it is more appropriate, then commit the alternate--
         if (fMoreAppropriate)
         {
            hr = pcpPhraseAlt[i]->Commit();
         }

         if (SUCCEEDED(hr))
         {
            // Release system resources:
            if (pwszAlternate) ::CoTaskMemFree(pwszAlternate);
            if (pwszText) ::CoTaskMemFree(pwszText);
         }
      }
   }
}

// Free the initial phrase object.
if (pPhrase) ::CoTaskMemFree(pPhrase);