示例#1
0
        public async static Task SearchWebAsync(IDialogContext context, string key, string query)
        {
            IWebSearchAPI client = new WebSearchAPI(new Microsoft.Azure.CognitiveServices.Search.WebSearch.ApiKeyServiceClientCredentials(key));
            var           result = await client.Web.SearchAsync(query : query, count : 3, safeSearch : Microsoft.Azure.CognitiveServices.Search.WebSearch.Models.SafeSearch.Strict);

            if (result?.WebPages?.Value?.Count > 0)
            {
                await context.PostAsync($"Here is top 3 web search result for **{query}**");

                foreach (var item in result.WebPages.Value)
                {
                    HeroCard card = new HeroCard
                    {
                        Title   = item.Name,
                        Text    = item.Snippet,
                        Buttons = new List <CardAction>
                        {
                            new CardAction(ActionTypes.OpenUrl, "Open Page", value: item.Url)
                        }
                    };

                    var message = context.MakeMessage();
                    message.Attachments.Add(card.ToAttachment());
                    await context.PostAsync(message);
                }
            }
        }
        public static void WebResultsWithCountAndOffset(string subscriptionKey)
        {
            var client = new WebSearchAPI(new ApiKeyServiceClientCredentials(subscriptionKey));

            try
            {
                var webData = client.Web.SearchAsync(query: "Best restaurants in Seattle", offset: 10, count: 20).Result;
                Console.WriteLine("Searched for Query# \" Best restaurants in Seattle \"");

                if (webData?.WebPages?.Value?.Count > 0)
                {
                    // find the first web page
                    var firstWebPagesResult = webData.WebPages.Value.FirstOrDefault();

                    if (firstWebPagesResult != null)
                    {
                        Console.WriteLine("Web Results#{0}", webData.WebPages.Value.Count);
                        Console.WriteLine("First web page name: {0} ", firstWebPagesResult.Name);
                        Console.WriteLine("First web page URL: {0} ", firstWebPagesResult.Url);
                    }
                    else
                    {
                        Console.WriteLine("Couldn't find first web result!");
                    }
                }
                else
                {
                    Console.WriteLine("Didn't see any Web data..");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Encountered exception. " + ex.Message);
            }
        }
示例#3
0
        public async static Task SearchUlsterAsync(IDialogContext context, string key, string query)                                                                                 //gets the query from ResumeAfterEnteringQuery. Uses NuGet packages
        {
            IWebSearchAPI client = new WebSearchAPI(new Microsoft.Azure.CognitiveServices.Search.WebSearch.ApiKeyServiceClientCredentials(key));                                     //buils new api search
            var           result = await client.Web.SearchAsync(query : query, count : 5, safeSearch : Microsoft.Azure.CognitiveServices.Search.WebSearch.Models.SafeSearch.Strict); //settings for result amount and safe search

            if (result?.WebPages?.Value?.Count > 0)
            {
                await context.PostAsync($"Here are top 5 web search results for **{query}**"); //returns this message to user

                foreach (var item in result.WebPages.Value)                                    //runs a loop for 5 items
                {
                    HeroCard card = new HeroCard                                               //creates new hero card
                    {
                        Title   = item.Name,                                                   //sets title from resultWebPages.Value
                        Text    = item.Snippet,                                                //gets text from search results
                        Buttons = new List <CardAction>                                        //creates button
                        {
                            new CardAction(ActionTypes.OpenUrl, "Open Page", value: item.Url)  //sets url for button to open
                        }
                    };

                    var message = context.MakeMessage();          //creates the message
                    message.Attachments.Add(card.ToAttachment()); //adds attachement to the message
                    await context.PostAsync(message);             //posts message
                }
            }
        }
示例#4
0
        private static async Task <Bing.WebWebAnswer> QueryBing(string searchTerms, int startIndex, int count)
        {
            var bingApiKey  = ConfigurationManager.AppSettings["BingApiKey"];
            var credentials = new ApiKeyServiceClientCredentials(bingApiKey);
            var client      = new WebSearchAPI(credentials);
            var query       = string.Format("site:parliament.uk {0}", searchTerms);
            var filter      = new List <string> {
                "Webpages"
            };
            var response = await client.Web.SearchAsync(
                query,
                responseFilter : filter,
                offset : startIndex - 1,
                count : count,
                market : "en-GB",
                textDecorations : true,
                textFormat : "HTML");

            return(response.WebPages);
        }
        public static void WebSearchWithResponseFilter(string subscriptionKey)
        {
            var client = new WebSearchAPI(new ApiKeyServiceClientCredentials(subscriptionKey));

            try
            {
                IList <string> responseFilterstrings = new List <string>()
                {
                    "news"
                };
                var webData = client.Web.SearchAsync(query: "Microsoft", responseFilter: responseFilterstrings).Result;
                Console.WriteLine("Searched for Query# \" Microsoft \" with response filters \"news\"");

                //News
                if (webData?.News?.Value?.Count > 0)
                {
                    // find the first news result
                    var firstNewsResult = webData.News.Value.FirstOrDefault();

                    if (firstNewsResult != null)
                    {
                        Console.WriteLine("News Results#{0}", webData.News.Value.Count);
                        Console.WriteLine("First news result name: {0} ", firstNewsResult.Name);
                        Console.WriteLine("First news result URL: {0} ", firstNewsResult.Url);
                    }
                    else
                    {
                        Console.WriteLine("Couldn't find first News results!");
                    }
                }
                else
                {
                    Console.WriteLine("Didn't see any News data..");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Encountered exception. " + ex.Message);
            }
        }
        public static void WebSearchWithAnswerCountPromoteAndSafeSearch(string subscriptionKey)
        {
            var client = new WebSearchAPI(new ApiKeyServiceClientCredentials(subscriptionKey));

            try
            {
                IList <string> promoteAnswertypeStrings = new List <string>()
                {
                    "videos"
                };
                var webData = client.Web.SearchAsync(query: "Lady Gaga", answerCount: 2, promote: promoteAnswertypeStrings, safeSearch: SafeSearch.Strict).Result;
                Console.WriteLine("Searched for Query# \" Lady Gaga \"");

                if (webData?.Videos?.Value?.Count > 0)
                {
                    var firstVideosResult = webData.Videos.Value.FirstOrDefault();

                    if (firstVideosResult != null)
                    {
                        Console.WriteLine("Video Results#{0}", webData.Videos.Value.Count);
                        Console.WriteLine("First Video result name: {0} ", firstVideosResult.Name);
                        Console.WriteLine("First Video result URL: {0} ", firstVideosResult.ContentUrl);
                    }
                    else
                    {
                        Console.WriteLine("Couldn't find videos results!");
                    }
                }
                else
                {
                    Console.WriteLine("Didn't see any data..");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Encountered exception. " + ex.Message);
            }
        }
示例#7
0
        //Se usa el API de Bing para la interpretacion de lo que se dice para posterior busqueda
        public static async Task RecognizeSpeechAsync(SpeechRecognizer recognizer, TextBox t, ListBox l, SpeechSynthesizer jarvis, Button btnCopy,
                                                      Button btnPause, Button btnSearch, Button btnStart, Button btnStop)
        {
            Console.WriteLine("Say something...");


            // Realiza el reconocimiento. RecognizeOnceAsync() devuelve cuando se reconoce la primera emisión
            var result = await recognizer.RecognizeOnceAsync();

            // Chequea los resultados.
            if (result.Reason == ResultReason.RecognizedSpeech)
            {
                Console.WriteLine($"RECOGNIZED: Text={result.Text}");
                string s = result.Text;
                switch (s)
                {
                case "Hello.":
                    jarvis.SpeakAsync("Hello Sir!");
                    break;

                case "Start reading.":
                    btnCopy.PerformClick();
                    btnStart.PerformClick();
                    break;

                case "Read the result.":
                case "Read the results.":
                case "Whats the result.":
                case "Whats the results.":
                case "What is the result.":
                    l.Items.Clear();
                    try
                    {
                        //Clave para usar lectura en base a una busqueda web con azure, las puedes encontrar en su pagina oficial
                        WebSearchAPI client  = new WebSearchAPI(new ApiKeyServiceClientCredentials("YourSubscriptionKey"));
                        var          webData = client.Web.SearchAsync(query: t.Text, offset: 10, count: 20).Result;

                        if (webData?.WebPages?.Value?.Count > 0)
                        {
                            //Enlista el resultado de paginas encontradas y en base a lo que se necesita se hace el llamado
                            //En este caso llamamos el nombre de la pagina y descripcion
                            var firstWebPagesResult = webData.WebPages.Value.ToList();
                            foreach (var f in firstWebPagesResult)
                            {
                                l.Items.Add(f.Name + ": " + f.Snippet);
                                jarvis.SpeakAsync("Web Page Name: " + f.Name + ". Description: " + f.Snippet);
                                Console.WriteLine("Name: " + f.Name + "\nSnippet: " + f.Snippet);
                            }

                            if (firstWebPagesResult != null)
                            {
                                Console.WriteLine("Web Results #{0}", webData.WebPages.Value.Count);
                                //Console.WriteLine("First web page name: {0} ", firstWebPagesResult.Name);
                                //Console.WriteLine("First web page URL: {0} ", firstWebPagesResult.Url);
                            }
                            else
                            {
                                Console.WriteLine("Couldn't find first web result!");
                            }
                        }
                        else
                        {
                            Console.WriteLine("Didn't see any Web data..");
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Encountered exception. " + ex.Message);
                    }
                    break;

                case "Search.":
                    btnSearch.PerformClick();
                    break;

                case "Pause.":

                    btnPause.PerformClick();
                    break;

                case "Resume.":

                    btnPause.PerformClick();
                    break;

                case "Stop.":

                    btnStop.PerformClick();
                    break;

                case "What is the date?":
                    jarvis.Speak("Today is " + DateTime.Now.ToString("yyyy-MM-dd"));
                    break;

                case "How are you?":
                    jarvis.Speak("I'm fine Sir");
                    break;

                case "Close.":
                    jarvis.SpeakAsync("See you in next time!");
                    Application.Exit();
                    break;

                default:
                    jarvis.SpeakAsync("Searching for: " + result.Text);
                    t.Text = result.Text;
                    btnSearch.PerformClick();
                    break;
                }
            }
            else if (result.Reason == ResultReason.NoMatch)
            {
                Console.WriteLine($"NOMATCH: Speech could not be recognized.");
            }
            else if (result.Reason == ResultReason.Canceled)
            {
                var cancellation = CancellationDetails.FromResult(result);
                Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                if (cancellation.Reason == CancellationReason.Error)
                {
                    Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                    Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                    Console.WriteLine($"CANCELED: Did you update the subscription info?");
                }
            }
        }
        public static void WebSearchResultTypesLookup(string subscriptionKey)
        {
            var client = new WebSearchAPI(new ApiKeyServiceClientCredentials(subscriptionKey));

            try
            {
                var webData = client.Web.SearchAsync(query: "Xbox").Result;
                Console.WriteLine("Searched for Query# \" Xbox \"");

                //WebPages
                if (webData?.WebPages?.Value?.Count > 0)
                {
                    // find the first web page
                    var firstWebPagesResult = webData.WebPages.Value.FirstOrDefault();

                    if (firstWebPagesResult != null)
                    {
                        Console.WriteLine("Webpage Results#{0}", webData.WebPages.Value.Count);
                        Console.WriteLine("First web page name: {0} ", firstWebPagesResult.Name);
                        Console.WriteLine("First web page URL: {0} ", firstWebPagesResult.Url);
                    }
                    else
                    {
                        Console.WriteLine("Couldn't find web results!");
                    }
                }
                else
                {
                    Console.WriteLine("Didn't see any Web data..");
                }

                //Images
                if (webData?.Images?.Value?.Count > 0)
                {
                    // find the first image result
                    var firstImageResult = webData.Images.Value.FirstOrDefault();

                    if (firstImageResult != null)
                    {
                        Console.WriteLine("Image Results#{0}", webData.Images.Value.Count);
                        Console.WriteLine("First Image result name: {0} ", firstImageResult.Name);
                        Console.WriteLine("First Image result URL: {0} ", firstImageResult.ContentUrl);
                    }
                    else
                    {
                        Console.WriteLine("Couldn't find first image results!");
                    }
                }
                else
                {
                    Console.WriteLine("Didn't see any image data..");
                }

                //News
                if (webData?.News?.Value?.Count > 0)
                {
                    // find the first news result
                    var firstNewsResult = webData.News.Value.FirstOrDefault();

                    if (firstNewsResult != null)
                    {
                        Console.WriteLine("News Results#{0}", webData.News.Value.Count);
                        Console.WriteLine("First news result name: {0} ", firstNewsResult.Name);
                        Console.WriteLine("First news result URL: {0} ", firstNewsResult.Url);
                    }
                    else
                    {
                        Console.WriteLine("Couldn't find any News results!");
                    }
                }
                else
                {
                    Console.WriteLine("Didn't see first news data..");
                }

                //Videos
                if (webData?.Videos?.Value?.Count > 0)
                {
                    // find the first video result
                    var firstVideoResult = webData.Videos.Value.FirstOrDefault();

                    if (firstVideoResult != null)
                    {
                        Console.WriteLine("Video Results#{0}", webData.Videos.Value.Count);
                        Console.WriteLine("First Video result name: {0} ", firstVideoResult.Name);
                        Console.WriteLine("First Video result URL: {0} ", firstVideoResult.ContentUrl);
                    }
                    else
                    {
                        Console.WriteLine("Couldn't find first video results!");
                    }
                }
                else
                {
                    Console.WriteLine("Didn't see any video data..");
                }
            }

            catch (Exception ex)
            {
                Console.WriteLine("Encountered exception. " + ex.Message);
            }
        }