Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning Microsoft Cognitive Services

You're reading from  Learning Microsoft Cognitive Services

Product type Book
Published in Mar 2017
Publisher Packt
ISBN-13 9781786467843
Pages 372 pages
Edition 1st Edition
Languages
Author (1):
Leif Larsen Leif Larsen
Profile icon Leif Larsen

Table of Contents (20) Chapters

Learning Microsoft Cognitive Services
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Getting Started with Microsoft Cognitive Services Analyzing Images to Recognize a Face Analyzing Videos Letting Applications Understand Commands Speak with Your Application Understanding Text Extending Knowledge Based on Context Querying Structured Data in a Natural Way Adding Specialized Searches Connecting the Pieces LUIS Entities and Intents Additional Information on Linguistic Analysis License Information

Chapter 9. Adding Specialized Searches

The previous chapter explored the relationship between academic papers and journals, learning how to search for academic papers. This chapter will move us into the last of the top-level APIs, Search. Through this chapter, we will learn how to search for web content. We will see how we can search for the latest news for certain key words or categories. Further on, we will search for images and videos, and learn how to automatically suggest search queries for the end-user.

When the chapter is complete, we will have learned the following:

  • How to search for web pages and documents

  • How to search for news articles

  • How to search for images and videos

  • How to add auto suggestions in applications

  • Filtering search results based on safe search policies

Searching the Web from the Smart-House application


The Bing Web Search API provides us with a search experience similar to what we find at http://bing.com/search. It returns results relevant to any queries, also specifying a ranking order.

A response for any request to this API will contain web pages, images, videos, and news articles. In a typical scenario, this is the API you would use for any of these searches.

Note that, in a real-life scenario, all requests should be made from a server-side application, not from a client, like we are doing in the example.

Note

If you have not already done so, sign up for the Bing Web Search API at https://www.microsoft.com/cognitive-services/en-us/bing-web-search-api.

Preparing the application for web searches

Before diving into the required technicalities for web searches, we are going to prepare our Smart-House application.

Add a new View in the Views folder, called BingSearchView.xaml. At the very least, this should contain two Combobox elements, one for...

Getting the news


Using the Bing News Search API, we can search for news in several ways. There are three endpoints we use for this API:

  • /news - Get top news articles, based on category

  • /news/search - Get news articles based on a search query

  • /news/trendingtopics - Get top trending news topics

In our Smart-House application, we will add the first two, while we will only cover the last one theoretically.

Note

If you have not already done so, sign up for the Bing News Search API at https://www.microsoft.com/cognitive-services/en-us/bing-news-search-api.

News from queries

A lot of the groundwork for query-based news searches have already been done in the web search sample. To search for news based on given queries, we need to add a new function in the BingSearch class.

Open BingSearch.cs and add a new function called SearchNews. This should accept a string and a SafeSearch parameter. The function should be marked as async, and return a Task<BingNewsResponse> object:

    public async Task<BingNewsResponse...

Searching for images and videos


The Bing Image Search API and Bing Video Search API allow us to search directly for images or videos. These APIs should only be used if you just need image or video content. There is a possibility that calling these APIs will affect performance and relevance negatively, and as such, one should aim to use the Bing Web Search API.

Note

If you have not already done so, sign up for the Bing Image Search API and the Bing Video Search API at https://www.microsoft.com/cognitive-services/en-us/bing-image-search-api and https://www.microsoft.com/cognitive-services/en-us/bing-video-search-api, respectively.

Using a common user interface

As we do not need image or video search in our Smart House application, we will go on to create a new project. Create this project using the MVVM template created in Chapter 1, Getting Started with Microsoft Cognitive Services.

These APIs do not come with any client packages. As before, we should really make these calls from the server-side...

Helping the user with auto suggestions


Auto suggestions are a great way to enhance the user experience. The typical use case is where, whenever a user enters some text into a text field, a list of suggested words is displayed.

Note

If you have not already done so, sign up for the Bing Autosuggest API at https://www.microsoft.com/cognitive-services/en-us/bing-autosuggest-api.

Adding Autosuggest to the user interface

As textboxes in WPF do not contain any auto-suggestion features, we need to add some on our own. We are going to use a third-party package, so install the WPFTextBoxAutoComplete package through the NuGet package manager, in our example project.

In the MainView.xaml file, add the following attribute to the starting Window tag:

    xmlns:behaviors="clr-namespace: WPFTextBoxAutoComplete; assembly=WPFTextBoxAutoComplete" 

We will also need to make sure that the TextBox binding for our search query updates whenever the user enters data. This can be done by making sure the Text attribute...

Search commonalities


For all the APIs we have covered, there are a few similarities. We will cover those now.

Languages

It is highly recommended to specify which market you want results for. Searches will typically return results for the local market and language of the user, based on the current location. As you can imagine, this is not always what the user wants. By specifying the market, you can tailor the search results for the user.

How you choose to solve this technically is dependent on the requirements of your application. For a smart-home application, you would probably allow the user to set the market in the settings. For a web application created only for French users in France, you would probably not allow the user to change the market.

Specifying the market is done by adding a mkt parameter to the GET request. This should then specify the market code, for example, en-US, for English in the United States.

Tip

While any API may support a specific market, some features may not support...

Summary


Throughout this chapter, we have looked at the different Bing Search APIs. We started by looking at how we could use Bing Web Search to search for all kinds of content. Next, we found the latest news, based on query strings and categories. From there, we moved on to image and video searches. In addition to this, we looked at how to enhance the user experience, by adding auto suggestions, using the Bing Autosuggestion API.

In the next, and final, chapter, we will wrap things up. We will complete our Smart House application by connecting the pieces; we will also take a look at the road ahead.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Learning Microsoft Cognitive Services
Published in: Mar 2017 Publisher: Packt ISBN-13: 9781786467843
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}