【问题标题】:Retrieving number phrases检索数字短语
【发布时间】:2016-02-18 15:49:28
【问题描述】:

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

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

这里是语法文件的sn-p;但是,我将不在引号内的数字更改为带文字的数字。

<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;
    }
}

【问题讨论】:

    标签: c++ sapi


    【解决方案1】:

    在某些时候,使用 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++ 部分。我刚刚发布的内容是否正确?我将如何做多个数字?我想保持这种格式。另外,我不太确定我是否正确使用了pProp = pProp-&gt;pNextSibling
    • 对于一组完整的数字,您可以将每个数字设为与 Rule.ulId 对应的每个单独的规则,然后您只需执行 atoi(Rule.uIId)。
    • 对于多个数字,您可以定义一个具有多个数字的新规则,例如 专门监听三个数字
    猜你喜欢
    • 2012-08-12
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    相关资源
    最近更新 更多