Browse DevX
Sign up for e-mail newsletters from DevX


Buy and Sell Stocks with the Sound of Your Voice Using the .NET Speech SDK : Page 8

Some applications are even more useful when people can interact with them using nothing but a telephone. We used the .NET Speech SDK to voice-enable the existing FMStocks sample application—and learned some useful lessons along the way.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Step 1—Prompt Functions and Grammar
The first step in the Buy Stock process is to tell the user what we want. We need to set up some text to be read to the user, so they know what question they are expected to answer. We do this by entering our text and our logic into a client-side "prompt function."

Each prompt function is responsible for determining what is said to a user based on certain criteria. Specifically, we want to read something different if the user says "Help," if the user just entered the page for the first time, or if the user has returned to this QA from a later question.

For BuyStock.aspx, we have one prompt function file that holds all of the prompt functions for all of the speech controls, named BuyStock.pf. Our first function, CompanyOrTickerQA_Prompt will ask the user which company they would like.

function CompanyOrTickerQA_prompt_inner( lastCommandOrException, count, missingEntry, userCanceled, zeroMaxShares, company) { var help= "Please say the name of the company " + "or spell the ticker " + "symbol, leaving clear pauses between letters..."; var text= "Please say the name of the company " + "or spell the ticker..."; if(userCanceled) text= "You canceled. " + text; else if(missingEntry != null) { text= "I did not find any matches for the " + "entry " + missingEntry + ". Please make a new entry to search again."; } else if(zeroMaxShares) { text= "I understood " + company + " . You do not have sufficient " + "funds in your account to buy " + "shares in this company..."; } else text= "To buy stock, " + text; return PromptGenerator.Generate ( lastCommandOrException, count, text, help); }

NOTE: Some of the longer strings have been shortened in the above code sample to save space.

Since it is possible for the user to come back to this QA later, we have had to add some parameters to this prompt function, so that we know which text to prompt the user with.

missingEntry: Holds either null or a Ticker entry which the user entered, but which was not found (i.e. "ZZZZ"

userCanceled: Either true or false, indicating if the user is returning to this prompt after canceling out of the Selectable Navigator

zeroMaxShares: Either true or false. If the user picks a company, and the code-behind determines that the user does not have enough money to buy even one share, we send the user back to this first QA, indicate that they cannot buy any shares, and ask them to enter another company name.

company: Holds a company name. Used with the zeroMaxShares parameter, to tell the user which company they don't have enough money to buy.

We choose which text to send based on the values of the parameters, and then we call the PromptGenerator.Generate function, which will determine if the user spoke one of the global commands, such as "Help," "Repeat," or "Instructions."

Next, we set up a Grammar file to define what phrases we expect the user to say (see Figure 5). Our grammar file contains a list of acceptable companies and their corresponding values, as well as a reference to a grammar rule for ticker symbols.

Figure 5: The grammar file contains a list of acceptable companies and their corresponding values.
When one of the choices is matched, the speech engine returns an SML document containing the name of the SML element matched, the text recognized, and the corresponding value. The SML element name and its corresponding value are set in the Assignments window of the grammar editor, as in Figure 5.

In our case, we wanted to set up two different SML element names, to distinguish whether the grammar matched a company name, or matched a ticker symbol. When the speech engine makes a match, it will return one of two SML documents, such as these:

<SML text="m. s. f. t." confidence="1.0"> <Ticker text="m. s. f. t." confidence="1.0">MSFT</Ticker> </SML> Ticker Match <SML text="microsoft" confidence="1.0"> <Company text="microsoft" confidence="1.0"> Microsoft Corporation </Company> </SML> Company Match

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date