⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch18.htm

📁 MAPI__SAPI__TAPI
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<p>You can define rules that show that some of the input is optional-that it may or may 
not occur in the input stream. The <tt><font FACE="Courier">opt()</font></tt> function can 
simplify rules while still giving them a great deal of flexibility. Listing 18.12 shows 
how to apply the <tt><font FACE="Courier">opt()</font></tt> function to the <tt><font
FACE="Courier">&lt;Names&gt;</font></tt> rule. </p>

<hr>

<blockquote>
  <b><p>Listing 18.12. An example of the <tt><font FACE="Courier">opt()</font></tt> rule 
  function.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>&lt;Names&gt; = alt(<br>
  &nbsp;&nbsp;&nbsp;&nbsp;seq( Scott opt( Ivey )opt )seq<br>
  &nbsp;&nbsp;&nbsp;&nbsp;seq( Wayne opt( Ivey )opt )seq<br>
  &nbsp;&nbsp;&nbsp;&nbsp;seq( Curt opt( Smith )opt )seq<br>
  &nbsp;&nbsp;&nbsp;&nbsp;)alt ;</font></tt> </p>
</blockquote>

<hr>

<p>The <tt><font FACE="Courier">&lt;Names&gt;</font></tt> rule now has only three 
alternative inputs again. This time, each input has an optional last name to match the 
first name. </p>

<h4>Using the <tt><font FACE="Courier">rep()</font></tt> Rule Function</h4>

<p>The <tt><font FACE="Courier">rep()</font></tt> rule function can be used to tell the SR 
engine to expect more than one of the objects within the context of the rule. A good 
example would be the creation of a phone-dialing rule. First, you can define a rule that 
dials each phone number (see Listing 18.13). </p>

<hr>

<blockquote>
  <b><p>Listing 18.13. A phone-dialing rule.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>&lt;Dial&gt; = alt(<br>
  &nbsp;&nbsp;&nbsp;&nbsp;3215002<br>
  &nbsp;&nbsp;&nbsp;&nbsp;4975501<br>
  &nbsp;&nbsp;&nbsp;&nbsp;3336363<br>
  &nbsp;&nbsp;&nbsp;&nbsp;)alt ;</font></tt> </p>
</blockquote>

<hr>

<p>Listing 18.13 meets all the requirements of a well-formed rule, but it has some 
problems. First, SR engines are not very good at recognizing objects such as 
&quot;3336363&quot; as individual words. Second, this list can easily grow to tens, even 
hundreds, of entries. As the list grows, accuracy will decrease, especially since it is 
likely that several phone numbers will sound alike. </p>

<p>Instead of defining a rule that contains all the phone numbers, you can define a rule 
using the <tt><font FACE="Courier">rep()</font></tt> function that tells the engine to 
listen for a set of numbers. Listing 18.14 is an improved version of the <tt><font
FACE="Courier">&lt;Dial&gt;</font></tt> rule. </p>

<hr>

<blockquote>
  <b><p>Listing 18.14. An improved <tt><font FACE="Courier">Dial</font></tt> rule.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>&lt;Dial&gt; = alt(<br>
  &nbsp;&nbsp;&nbsp;&nbsp;seq( Dial rep( &lt;Numbers&gt; )rep )seq <br>
  &nbsp;&nbsp;&nbsp;&nbsp;)alt ;<br>
  <br>
  &lt;Numbers&gt; = alt(<br>
  &nbsp;&nbsp;&nbsp;&nbsp;zero<br>
  &nbsp;&nbsp;&nbsp;&nbsp;one<br>
  &nbsp;&nbsp;&nbsp;&nbsp;two<br>
  &nbsp;&nbsp;&nbsp;&nbsp;three<br>
  &nbsp;&nbsp;&nbsp;&nbsp;four<br>
  &nbsp;&nbsp;&nbsp;&nbsp;five<br>
  &nbsp;&nbsp;&nbsp;&nbsp;six<br>
  &nbsp;&nbsp;&nbsp;&nbsp;seven<br>
  &nbsp;&nbsp;&nbsp;&nbsp;eight<br>
  &nbsp;&nbsp;&nbsp;&nbsp;nine<br>
  &nbsp;&nbsp;&nbsp;&nbsp;)alt ;</font></tt> </p>
</blockquote>

<hr>

<p>Now the <tt><font FACE="Courier">&lt;Dial&gt;</font></tt> rule knows to wait for a 
series of numbers. This allows it to be used for any possible combination of digits that 
can be used to dial a telephone number. </p>
<div align="center"><center>

<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
  <tr>
    <td><b>Tip</b></td>
  </tr>
  <tr>
    <td><blockquote>
      <p>The <tt><font FACE="Courier">&lt;Dial&gt;</font></tt> rule described here is still not 
      very good. The SR engine has a hard time interpreting long sets of numbers. It is better 
      to define words that will aid in the dialing of phone numbers. For example, <i>Dial New 
      York Office</i> is more likely to be understood than <i>Dial 1-800-555-1212.</i> </p>
    </blockquote>
    </td>
  </tr>
</table>
</center></div>

<h4>Using Run-Time Lists with CFG Rules</h4>

<p>You can define a rule that uses the contents of a list built at run-time. This allows 
the SR engine to collect information about the workstation (loadable applications, 
available Word documents, and so on) while the system is up and running rather than having 
to build everything into the grammar itself. The list name is added to the rule surrounded 
by braces ({}). At run-time, programmers can use the <tt><font FACE="Courier">SetList</font></tt> 
method of the <tt><font FACE="Courier">Voice Menu</font></tt> object in the OLE library to 
create and populate the list. Listing 18.15 shows how to build a rule that refers to a 
run-time list. </p>

<hr>

<blockquote>
  <b><p>Listing 18.15. An example of referring to a run-time list. <br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>&lt;RunProgram&gt; = alt( seq( Run {ProgList} )seq )alt ;</font></tt> 
  </p>
</blockquote>

<hr>

<p><tt><font FACE="Courier">&lt;RunProgram&gt;</font></tt> allows the user to say 
&quot;Run <i>name</i>&quot; where <i>name</i> is one of the program names that was loaded 
into the list at run-time. </p>

<h3><a NAME="CreatingandCompilingaSAPIContextFr">Creating and Compiling a SAPI 
Context-Free Grammar</a></h3>

<p>Now that you know the basic building blocks used to create CFGs, it is time to build 
and compile actual grammar rules using <tt><font FACE="Courier">NOTEPAD.EXE</font></tt> 
and the <tt><font FACE="Courier">GRAMCOMP.EXE</font></tt> grammar compiler that ships with 
the Microsoft Speech SDK. The first step in the process is to define the general scope and 
function of the grammar. For example, you might want a grammar that can handle typical 
customer requests for directions in a shopping center. </p>

<p>Once you define the scope and function of a grammar, you need to identify the words and 
rules needed to populate the CFG. Since an SR engine can only recognize words it already 
knows, you must be sure to include all the words needed to complete operations. </p>
<div align="center"><center>

<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
  <tr>
    <td><b>Tip</b></td>
  </tr>
  <tr>
    <td><blockquote>
      <p>You do not, however, need to include all the possible words users may utter to the SR 
      engine. The software package that is using the SR engine should have some type of error 
      response in cases where the audio input cannot be interpreted.</p>
    </blockquote>
    </td>
  </tr>
</table>
</center></div>

<p>To use the shopping center example, you'd need a list of all the locations that users 
might request. This would include all the stores, restaurants, major landmarks within the 
building, public services such as restrooms, exits, drinking fountains, security office, 
and so on. Then you need to collect a set of typical phrases that you expect users to 
utter. Examples might be &quot;Where is the ....?&quot; or &quot;Show me ... on the 
map,&quot; or &quot;How can I locate the ....?&quot; After you have collected all this 
material, you are ready to create the grammar. </p>

<h4>Coding the <tt><font FACE="Courier">MALL.TXT</font></tt> Context-Free Grammar</h4>

<p>Let's assume you have the job of building a workstation that will allow shoppers to ask 
directions in order to locate their favorite shops within the mall. Listing 18.16 shows a 
list of the store names and some other major landmarks in the mall. Load <tt><font
FACE="Courier">NOTEPAD.EXE</font></tt> and enter this information into a file called <tt><font
FACE="Courier">MALL.TXT</font></tt>. </p>

<hr>

<blockquote>
  <b><p>Listing 18.16. Adding words to the <tt><font FACE="Courier">MALL</font></tt> 
  grammar.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>//&nbsp;******************************************************** 
  <br>
  //&nbsp;MALL GRAMMAR RULES<br>
  //&nbsp;******************************************************** <br>
  //<br>
  //&nbsp;Title:&nbsp;&nbsp;&nbsp;&nbsp;MALL.TXT<br>
  //&nbsp;Version:&nbsp;&nbsp;&nbsp;&nbsp;1.0 - 05/16/96 (MCA)<br>
  //<br>
  //&nbsp;Site:&nbsp;&nbsp;&nbsp;&nbsp;Win95 SAPI<br>
  //&nbsp;Compiler:&nbsp;&nbsp;&nbsp;&nbsp;GRAMCOMP.EXE<br>
  //<br>
  //&nbsp;Desc:&nbsp;&nbsp;&nbsp;&nbsp;Used to direct customers to their favorite<br>
  //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shops in the mall.<br>
  //<br>
  //&nbsp;******************************************************** <br>
  <br>
  <br>
  //<br>
  //&nbsp;define words<br>
  //<br>
  J = 9900 ;<br>
  C = 9901 ;<br>
  Penneys = 9902 ;<br>
  Sears = 9903 ;<br>
  Bobs = 9904 ;<br>
  Bagels = 9905 ;<br>
  Michelles = 9906 ;<br>
  Supplies = 9906 ;<br>
  The = 9907 ;<br>
  Sports = 9908 ;<br>
  Barn = 9909 ;<br>
  <br>
  Security = 9910 ;<br>
  Office = 9911 ;<br>
  Main = 9912 ;<br>
  Food = 9913 ;<br>
  Court = 9914 ;<br>
  Shops = 9915 ;<br>
  Specialty = 9916 ;<br>
  <br>
  Exits = 9917 ;<br>
  Restroom = 9918 ;<br>
  Fountain = 9919 ;</font></tt> </p>
</blockquote>

<hr>

<p>Next, you need to define a top-level rule that calls all other rules. This one rule 
should be relatively simple and provide for branches to other more complicated rules. By 
creating branches, you can limit SR errors since the possible words or phrases are limited 
to those defined in a branch. In other words, by creating branches to other rules, you 
limit the scope of words and rules that must be analyzed by the SR engine at any one 
moment. This improves accuracy. </p>

<p>Listing 18.17 shows a top-level rule that calls several other possible rules. Add this 
to your <tt><font FACE="Courier">MALL.TXT</font></tt> grammar file. </p>

<hr>

<blockquote>
  <b><p>Listing 18.17. Adding the top-level rule to the <tt><font FACE="Courier">MALL</font></tt> 
  grammar file.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>//&nbsp;************************************** <br>
  //&nbsp;Define starting rule<br>
  //<br>
  //&nbsp;This rule calls any one of the other<br>
  //&nbsp;internal rules.<br>
  //<br>
  &lt;Start&gt; = alt(<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&lt;_Locations&gt;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&lt;_TellMeWhere&gt;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&lt;_HowCanIFind&gt;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&lt;_ShowMe&gt;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&lt;_WhereIs&gt;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;)alt ;</font></tt> </p>
</blockquote>

<hr>

<p>Notice that each of the rules called by <tt><font FACE="Courier">&lt;Start&gt;</font></tt> 
begins with an underscore (_). This underscore tells the compiler that this is an internal 
rule and should not be exported to the user. The more exported rules the SR engine has to 
review, the greater the chance of failure. It is a good idea to limit the number of 
exported rules to a bare minimum. </p>

<p>The first internal rule on the list is called <tt><font FACE="Courier">&lt;_Locations&gt;</font></tt>. 
This rule contains a list of all the locations that customers may ask about. Notice the 
use of <tt><font FACE="Courier">seq()</font></tt> and <tt><font FACE="Courier">opt()</font></tt> 
in the rule. This allows customers to ask for the same locations in several different ways 
without having to add many items to the vocabulary. Enter the data shown in Listing 18.18 
into the <tt><font FACE="Courier">MALL.TXT</font></tt> grammar file. </p>

<hr>

<blockquote>
  <b><p>Listing 18.18. Adding the <tt><font FACE="Courier">Locations</font></tt> rule.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>//&nbsp;************************************* <br>
  //&nbsp;Define Locations rule<br>
  //<br>
  //&nbsp;This rule lists all possible locations<br>
  //<br>
  &lt;_Locations&gt; = alt(<br>
  &nbsp;&nbsp;&nbsp;&nbsp;// JC Penneys, Penneys<br>
  &nbsp;&nbsp;&nbsp;&nbsp;seq( opt( seq( J C )seq )opt Penneys )seq <br>
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;// sears<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Sears<br>
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;// Bobs, Bobs Bagels<br>
  &nbsp;&nbsp;&nbsp;&nbsp;seq( Bobs opt( Bagels )opt )seq<br>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -