c++ - 检索数字短语

标签 c++ sapi

我在这里找到了另一篇文章,但我无法对其发表评论。它是一个带有数字的语法文件。我将如何检索这些数字?我知道我可以使用 wcscmp 函数,但假设有 200 个数字;这将是很多 if else 语句相互之间。我怎样才能同时搜索 pPhrase->Rule 规则层次结构并分配一个规则指针。

注意这可以有另一个规则和另一组数字,以便能够确定多个数字。

这是语法文件的片段;但是,我将不在引号内的数字更改为措辞数字。

<rule name="phoneno">
  <phrase min="7" max="10">
    <ruleref name="digit" propname="digit"/>
  </phrase>
</rule>

<rule name="digit">
  <l>
    <p val="0">zero</p>
    <p val="1">one</p>
    <p val="2">two</p>
    <p val="3">three</p>
    <p val="4">four</p>
    <p val="5">five</p>
    <p val="6">six</p>
    <p val="7">seven</p>
    <p val="8">eight</p>
    <p val="9">nine</p>
  </l>
</rule>

编辑代码片段以检索和处理某些短语。

SPPHRASE *pElements;
std::wstring str;

// Get the phrase elements, one of which is the rule id we specified in
// the grammar.  Switch on it to figure out which command was recognized.

if (SUCCEEDED(pPrhase->GetPhrase(&pElements))) {
    SPPHRASE phrase = *pElements;
    WCHAR *pText;
    const SPPHRASEPROPERTY *pProp = phrase.pProperties;

    if (SUCCEEDED(pPhrase->GetText(SP_GETWHOLEPHRASE, SP_GETWHOLEPHRASE, TRUE, &pText, NULL))) {
        str = pText;
    }
    else {

    }
}

switch (pElements->Rule.ulId) {
    case digit:
        while (pProp != NULL) {
            if (wcscmp(L"digit", phrase.Rule.pszName) == 0) {
                if (wcscmp(L"one", pProp->pFirstChild->pszValue) == 0) {
                    pProp = pProp->pNextSibling;
                }
                else if (wcscmp(L"two", pProp->pFirstChild->pszValue) == 0) {
                    pProp = pProp->pNextSibling;
                }
                else if (wcscmp(L"three", pProp->pFirstChild->pszValue) == 0) {
                    pProp = pProp->pNextSibling;
                }
                // all the way up to nine
            }
            // now let us say there is a another digit after the first digit. 
            // so the number can be from 11 - 99, would I need to place the second digit
            // within each of the "first digit if / else if statements"?    Or is there
            // an efficient way to do this? 
            }
         break;
    }
}

最佳答案

在某些时候,使用 Microsoft 的语法编译器 (gc.exe) 生成 ID 列表会变得更容易,因此您可以轻松地只比较 ID,而不是按照规则名称进行。代码的最终结果类似于:

SPPHRASE *pElements;

// Get the phrase elements, one of which is the rule id we specified in
// the grammar.  Switch on it to figure out which command was recognized.
if (SUCCEEDED(pPhrase->GetPhrase(&pElements)))
{        
    switch ( pElements->Rule.ulId )
    {
        case VID_RuleNameHere:
        {
           //Do stuff here
        }
    }
}

您可以阅读更多关于如何做到这一点的信息 here.在对语法文件运行 gc.exe 时,您还需要指定/H 开关。

关于c++ - 检索数字短语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35486099/

相关文章:

c++ - 模板类中单个方法的模板特化

c++ - 隐藏 SDL 窗口中的标题栏和框架

c++ - 如何消除这种与继承相关的代码异味?

Delphi SAPI 文本转语音

c++ - 涉及引用数组的技术细节

C++ 到 D 的互操作性

c++ - 在 "event.GetFrom(m_cpVoice)==S_OK"时调用函数(因此事件发生时)[SAPI 5.1 和 C++]

c++ - SAPI5 语音/使用 32 位语音

c# - 如何在 C# 的 visual studio 中安装 SAPI?

speech-recognition - 有谁知道在 SAPI 中以编程方式创建和/或选择语音配置文件的方法吗?