Processing Wikipedia search messages

The second function of our microservice is to conduct a search of a specific term on Wikipedia. In order to do this we are using the Wikipedia open source library found on GitHub. At the time of the writing of this book a NuGet package was not available so the source was downloaded and compiled. Both the source and the compiled binary are available via the website information listed in the front of the book. The function will look like this:

bool ProcessWikipediaSearchMessage(WikipediaSearchMessage msg)
{
SearchWikipedia(msg.searchTerm, msg.maxReturns, msg.maxReturns, Language.English);
return true;
}

Why search Wikipedia? Why not, Alexa does, right? So you now will have the capability to perform searches for things and have their results spoken back to you. You might be wanting to learn the definition of a word, understand what a new term means, or perhaps you just like the sound of synthesized voices. You might also be one of the many people that suffer from learning disabilities to the extent that having the text spoken to you would be a huge benefit.

Our SearchWikipedia function takes in the search term, the maximum number of results we will allow back, the maximum number of events we wish read back to us, and the language. Our API supports all 283 languages of the Wikipedia site:

If no results are found then the TTS engine will say that to us, otherwise, the results will be spoken to us until the maximum number of responses is reached:

private void SearchWikipedia(string text, int maxResults, int resultsToRead)
{
wikipedia.Limit = maxResults;
voice.SpeakAsync("I just received a search request for the term " + text);
QueryResult results = wikipedia.Search(text);
if (results.Search.Count == 0)
{
voice.SpeakAsync("I'm sorry, I could not find anything for " + text);
}
else
{
if (results.Search.Count < resultsToRead)
resultsToRead = results.Search.Count;
voice.SpeakAsync("I found " + results.Search.Count +
" results for " + text + ". According to Wikipedia, here are the top " + resultsToRead + " results I found");
PromptBuilder builder = new PromptBuilder();
builder.StartSentence();
for (intx = 0; x < resultsToRead; x++)
{
builder.AppendText(results.Search[x].Snippet.Substring(
results.Search[x].Snippet.LastIndexOf("</span>") + 7));
}
builder.EndSentence();
voice.SpeakAsync(builder);
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset