检索数字短语
Retrieving number phrases
我在这里找到了另一篇文章,但我无法对此发表评论。它是一个包含数字的语法文件。我将如何检索数字?我知道我可以使用wcscmp
函数,但假设有 200 个数字;那将是彼此内部的许多if else
陈述。我如何能够搜索 pPhrase->规则层次结构并分配规则指针。
请注意,这可以具有另一个规则和另一组数字,以便能够确定多个数字。
这是语法文件的片段;但是,我将不在引号内的数字更改为措辞数字。
<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
}
}
}
您可以在此处阅读有关如何执行此操作的更多信息。你还需要在针对语法文件运行 gc.exe 时指定/H 开关。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- 打印数字图案
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 如何检查一个c++字符串中有多少相同的字符/数字
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 检索数字短语
- 如何将数字"Bind"到一串单词/短语中,以便我可以循环调用它?
- 使用字符串变量将短语分配给数字?C++