Get Weather by Speaking App with Azure Cognitive Services

1 Comment

In our previous posts, we signed up for a cognitive service subscription key and built a C# console app that listens to what a user is saying, sends the input to Microsoft Azure Cognitive Services Speech Service and returns the text.

In this post, we will extend our app to get the real-time weather of a city that the user speaks when prompted.

Yahoo! has a Weather API that we can use to send a weather request to:

We will pass the request in the format below. In this example where we are searching for weather in New York, NY:

https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22New%20York%2C%20NY%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

The response will be JSON, something like:

Let’s add Newtonsoft.Json to our project through NuGet so we can parse the response:

Now the code. We will wait for the user voice input, then send the speech to the Azure Cognitive Services Speech Service to convert it to text. The result is then sent to Yahoo! Weather API and the JSON response is parsed to give us the temperature of the city and state we requested. For yourapikey, use the key created here. For the region, use your region such as westus:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using Newtonsoft.Json.Linq;
using System.Net;

namespace Carl.SpeechRecognitionHelloWorld
{
    class Program
    {
        public static async Task RecognizeSpeechAsync()
        {
            var factory = SpeechFactory.FromSubscription(yourapikey, yourapiregion);

            using (var recognizer = factory.CreateSpeechRecognizer())
            {
                Console.WriteLine("Name a city and state to get the weather:");

                var result = await recognizer.RecognizeAsync();
                if (result.RecognitionStatus != RecognitionStatus.Recognized)
                {
                    Console.WriteLine($"Recognition status: {result.RecognitionStatus.ToString()}");
                    if (result.RecognitionStatus == RecognitionStatus.Canceled)
                        Console.WriteLine($"There was an error, reason: {result.RecognitionFailureReason}");
                    else
                        Console.WriteLine("No speech could be recognized.\n");
                }
                else
                {
                    // Get the weather
                    string response = "";
                    string temperature = string.Empty;
                    string place = result.Text.Replace(".", "");

                    using (WebClient wc = new WebClient())
                    {
                        response = wc.DownloadString("https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22" + place + "%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
                    }

                    dynamic j = JObject.Parse(response);
                    var items = j.query.results.channel.item.condition.temp;
                    temperature = items.Value;
                    Console.WriteLine("The weather in {0} is {1}", place, temperature);
                }
            }
        }

        static void Main()
        {
            RecognizeSpeechAsync().Wait();
            Console.WriteLine("Please press a key to continue.");
            Console.ReadLine();
        }
    }
}

Now, run the app by pressing F5. You will be asked to speak a city and state to get the weather.

I speak “New York NY”. This is translated as “New York, NY” through the Speech Service, and we can see the Yahoo! response is 89 degrees in New York:

Let’s try a different city, for example, Springfield which is in different states such as MO and MA.

I speak “Springfield MO”:

I speak “Springfield MA”:

What if we send just a city and no state? Let’s try some international examples.

I speak “London”:

I speak “Tokyo”:

As you can see, both the speech service and the weather API handle variations well, and we can build a pretty powerful app with very little coding.

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

One Response to Get Weather by Speaking App with Azure Cognitive Services

  1. Hi,
    I created sample program in C# using VS2015 and getting error –
    Cognitiveservices does not exist in Microsoft.
    What am i missing?
    I downloaded and installed Nugen package successfully.

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *