如何使用 Microsoft SAPI 捕获句子中间的单词?

发布于 2024-08-19 22:03:43 字数 458 浏览 6 评论 0原文

这个SAPI语法捕获单词名称是句子的中间。

<GRAMMAR LANGID="409">
    <RULE NAME="SOUNDLOG" TOPLEVEL="ACTIVE">
        <OPT>
            <DICTATION MAX="INF"/>
        </OPT>
        <L>
            <P>name</P>
        </L>
        <OPT>
            <DICTATION MAX="INF"/>
        </OPT>
    </RULE>
</GRAMMAR>

因此,如果我说“我的名字是 André”,则会识别出“名字”这个词。 有更好的方法吗?

this SAPI grammar catch the word name is the middle of a sentence.

<GRAMMAR LANGID="409">
    <RULE NAME="SOUNDLOG" TOPLEVEL="ACTIVE">
        <OPT>
            <DICTATION MAX="INF"/>
        </OPT>
        <L>
            <P>name</P>
        </L>
        <OPT>
            <DICTATION MAX="INF"/>
        </OPT>
    </RULE>
</GRAMMAR>

So, if I say "My name is André", the word "name" is identified.
Is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不交电费瞎发啥光 2024-08-26 22:03:43

当您获得识别时,与识别关联的 SPPHRASE 数据包含子规则和属性数据以及它们在识别中出现的位置。

因此,如果您有“名称”的规则或属性标记,则可以找到与“名称”关联的单词。

例如,给定您的语法,

<GRAMMAR LANGID="409"> 
    <RULE NAME="SOUNDLOG" TOPLEVEL="ACTIVE"> 
        <OPT> 
            <DICTATION MAX="INF"/> 
        </OPT> 
        <L> 
            <P>name</P> 
        </L> 
        <OPT> 
            <DICTATION MAX="INF"/> 
        </OPT> 
    </RULE> 
</GRAMMAR> 

如果将其更改为,

<GRAMMAR LANGID="409"> 
    <RULE NAME="SOUNDLOG" TOPLEVEL="ACTIVE"> 
        <OPT> 
            <DICTATION MAX="INF"/> 
        </OPT> 
        <L PROPNAME='name'> 
            <P VAL='name'>name</P> 
        </L> 
        <OPT> 
            <DICTATION MAX="INF"/> 
        </OPT> 
    </RULE> 
</GRAMMAR> 

您可以找到与“名称”相对应的单词,如下所示:

HRESULT OnRecognition(ISpRecoResult* pResult)
{
    SPPHRASE *pPhrase;
    HRESULT hr = pResult->GetPhrase(&pPhrase);
    if (SUCCEEDED(hr))
    {
         const SPPHRASEPROPERTY pProp = FindProperty(pPhrase->pProperty, L"name");
         if (pRule)
         {
             LPWSTR text(NULL);
             hr = pResult->GetText(pProp->ulFirstElement, pProp->ulCountOfElements, TRUE, &text, NULL);
             if (SUCCEEDED(hr))
             {
                 // do something with text
                 ::CoTaskMemFree(text);
             }
         }
    }
    return hr;
}
const SPPHRASEPROPERTY* FindProperty(const SPPHRASEPROPERTY* pProp, LPCWSTR what) const
{
    while (pProp!=NULL)
    {
        if (pProp->pFirstChild != NULL)
        {
            const SPPHRASEPROPERTY* pFoundProp = FindRule(pProp->pFirstChild, what);
            if (pFoundProp)
            {
                return pFoundProp;
            }
        }
        if (pProp->pszName != NULL && wcsstr(pProp->pszName, what) != NULL)
        {
            return pProp;
        }
        pProp = pProp->pNextSibling;
    }
    return NULL;
}

此代码专门查找属性所涵盖的文本。但是,通常最好使用 val 属性来标识项目,而不将代码显式绑定到语法。这允许您在不更改代码的情况下调整语法(或添加等效值)。要使用这些值,只需在获取属性后使用 SPPHRASEPROPERTY.pszValue 或 vValue 字段(而不是使用 ISpPhrase::GetText)。

When you get the recognition, the SPPHRASE data associated with the recognition contains the subrules and property data and where they occur in the recognition.

So, if you had a rule or property tag for 'Name', you could find the words associated with 'Name'.

For example, given your grammar

<GRAMMAR LANGID="409"> 
    <RULE NAME="SOUNDLOG" TOPLEVEL="ACTIVE"> 
        <OPT> 
            <DICTATION MAX="INF"/> 
        </OPT> 
        <L> 
            <P>name</P> 
        </L> 
        <OPT> 
            <DICTATION MAX="INF"/> 
        </OPT> 
    </RULE> 
</GRAMMAR> 

if you change it to

<GRAMMAR LANGID="409"> 
    <RULE NAME="SOUNDLOG" TOPLEVEL="ACTIVE"> 
        <OPT> 
            <DICTATION MAX="INF"/> 
        </OPT> 
        <L PROPNAME='name'> 
            <P VAL='name'>name</P> 
        </L> 
        <OPT> 
            <DICTATION MAX="INF"/> 
        </OPT> 
    </RULE> 
</GRAMMAR> 

you could find the words that correspond to 'name' like this:

HRESULT OnRecognition(ISpRecoResult* pResult)
{
    SPPHRASE *pPhrase;
    HRESULT hr = pResult->GetPhrase(&pPhrase);
    if (SUCCEEDED(hr))
    {
         const SPPHRASEPROPERTY pProp = FindProperty(pPhrase->pProperty, L"name");
         if (pRule)
         {
             LPWSTR text(NULL);
             hr = pResult->GetText(pProp->ulFirstElement, pProp->ulCountOfElements, TRUE, &text, NULL);
             if (SUCCEEDED(hr))
             {
                 // do something with text
                 ::CoTaskMemFree(text);
             }
         }
    }
    return hr;
}
const SPPHRASEPROPERTY* FindProperty(const SPPHRASEPROPERTY* pProp, LPCWSTR what) const
{
    while (pProp!=NULL)
    {
        if (pProp->pFirstChild != NULL)
        {
            const SPPHRASEPROPERTY* pFoundProp = FindRule(pProp->pFirstChild, what);
            if (pFoundProp)
            {
                return pFoundProp;
            }
        }
        if (pProp->pszName != NULL && wcsstr(pProp->pszName, what) != NULL)
        {
            return pProp;
        }
        pProp = pProp->pNextSibling;
    }
    return NULL;
}

This code looks specifically for the text covered by the property. However, it's often better to use the val attributes to identify items without binding your code explicitly to the grammar. This allows you to tweak the grammar (or add equivalent values) without changing your code. To use the values, simply use the SPPHRASEPROPERTY.pszValue or vValue fields after fetching the property (instead of using ISpPhrase::GetText).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文