public static void Error(string subscriptionKey, string endpoint)
        {
            // Example Endpoints are https://westus2.api.cognitive.microsoft.com. You can find this under your subscription
            // Dont use /bing/v7.0/entities/ in the enpoint
            var client = new EntitySearchClient(new ApiKeyServiceClientCredentials(subscriptionKey))
            {
                Endpoint = endpoint
            };

            try
            {
                var entityData = client.Entities.Search(query: "Satya Nadella", market: "no-ty");
            }
            catch (ErrorResponseException ex)
            {
                // The status code of the error should be a good indication of what occurred. However, if you'd like more details, you can dig into the response.
                // Please note that depending on the type of error, the response schema might be different, so you aren't guaranteed a specific error response schema.
                Console.WriteLine("Exception occurred, status code {0} with reason {1}.", ex.Response.StatusCode, ex.Response.ReasonPhrase);

                // if you'd like more descriptive information (if available), attempt to parse the content as an ErrorResponse
                var issue = JsonConvert.DeserializeObject <ErrorResponse>(ex.Response.Content);

                if (issue != null && issue.Errors?.Count > 0)
                {
                    if (issue.Errors[0].SubCode == ErrorSubCode.ParameterInvalidValue)
                    {
                        Console.WriteLine("Turns out the issue is parameter \"{0}\" has an invalid value \"{1}\". Detailed message is \"{2}\"", issue.Errors[0].Parameter, issue.Errors[0].Value, issue.Errors[0].Message);
                    }
                }
            }
        }
예제 #2
0
        public void EntitySearch()
        {
            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                HttpMockServer.Initialize(this.GetType().FullName, "EntitySearch");

                IEntitySearchClient client = new EntitySearchClient(new ApiKeyServiceClientCredentials(SubscriptionKey), HttpMockServer.CreateInstance());

                var resp = client.Entities.Search(query: "tom cruise");

                Assert.NotNull(resp);
                Assert.NotNull(resp.Entities);
                Assert.NotNull(resp.Entities.Value);
                Assert.Equal(1, resp.Entities.Value.Count);

                Assert.NotNull(resp.Entities.Value[0].ContractualRules);

                var licenseAttribution = resp.Entities.Value[0].ContractualRules.Where(rule => rule is ContractualRulesLicenseAttribution).FirstOrDefault();

                Assert.NotNull(licenseAttribution);
                Assert.Equal("description", licenseAttribution.TargetPropertyName);

                var image = resp.Entities.Value[0].Image;

                Assert.NotNull(image);
                Assert.NotNull(image.Provider);

                var provider = image.Provider.FirstOrDefault();

                Assert.NotNull(provider);
                Assert.IsType <Organization>(provider);
            }
        }
        public static void HandlingDisambiguation(string subscriptionKey, string endpoint)
        {
            // Example Endpoints are https://westus2.api.cognitive.microsoft.com. You can find this under your subscription
            // Dont use /bing/v7.0/entities/ in the enpoint
            var client = new EntitySearchClient(new ApiKeyServiceClientCredentials(subscriptionKey))
            {
                Endpoint = endpoint
            };

            try
            {
                var entityData = client.Entities.Search(query: "William Gates");

                if (entityData?.Entities?.Value?.Count > 0)
                {
                    // find the entity that represents the dominant one
                    var mainEntity       = entityData.Entities.Value.Where(thing => thing.EntityPresentationInfo.EntityScenario == EntityScenario.DominantEntity).FirstOrDefault();
                    var disambigEntities = entityData.Entities.Value.Where(thing => thing.EntityPresentationInfo.EntityScenario == EntityScenario.DisambiguationItem).ToList();

                    if (mainEntity != null)
                    {
                        Console.WriteLine("Searched for \"William Gates\" and found a dominant entity with type hint \"{0}\" with this description:", mainEntity.EntityPresentationInfo.EntityTypeDisplayHint);
                        Console.WriteLine(mainEntity.Description);
                    }
                    else
                    {
                        Console.WriteLine("Couldn't find a reliable dominant entity for William Gates!");
                    }

                    if (disambigEntities?.Count > 0)
                    {
                        Console.WriteLine();
                        Console.WriteLine("This query is pretty ambiguous and can be referring to multiple things. Did you mean one of these: ");

                        var sb = new StringBuilder();

                        foreach (var disambig in disambigEntities)
                        {
                            sb.AppendFormat(", or {0} the {1}", disambig.Name, disambig.EntityPresentationInfo.EntityTypeDisplayHint);
                        }

                        Console.WriteLine(sb.ToString().Substring(5) + "?");
                    }
                    else
                    {
                        Console.WriteLine("We didn't find any disambiguation items for William Gates, so we must be certain what you're talking about!");
                    }
                }
                else
                {
                    Console.WriteLine("Didn't see any data..");
                }
            }
            catch (ErrorResponseException ex)
            {
                Console.WriteLine("Encountered exception. " + ex.Message);
            }
        }
예제 #4
0
        public SearchService(
            IConfiguration configuration,
            IHttpClientFactory clientFactory)
        {
            _httpClient   = clientFactory.CreateClient();
            _searchAPIKey = configuration["BingSearchAPIKey"];
            var credential = new ApiKeyServiceClientCredentials(_searchAPIKey);

            _client       = new WebSearchClient(credential);
            _entityClient = new EntitySearchClient(credential);
        }
        public static void MultipleRestaurantLookup(string subscriptionKey, string endpoint)
        {
            // Example Endpoints are https://westus2.api.cognitive.microsoft.com. You can find this under your subscription
            // Dont use /bing/v7.0/entities/ in the enpoint
            var client = new EntitySearchClient(new ApiKeyServiceClientCredentials(subscriptionKey))
            {
                Endpoint = endpoint
            };

            try
            {
                var restaurants = client.Entities.Search(query: "seattle restaurants");

                if (restaurants?.Places?.Value?.Count > 0)
                {
                    // get all the list items that relate to this query
                    var listItems = restaurants.Places.Value.Where(thing => thing.EntityPresentationInfo.EntityScenario == EntityScenario.ListItem).ToList();

                    if (listItems?.Count > 0)
                    {
                        var sb = new StringBuilder();

                        foreach (var item in listItems)
                        {
                            var place = item as Place;

                            if (place == null)
                            {
                                Console.WriteLine("Unexpectedly found something that isn't a place named \"{0}\"", item.Name);
                                continue;
                            }

                            sb.AppendFormat(",{0} ({1}) ", place.Name, place.Telephone);
                        }

                        Console.WriteLine("Ok, we found these places: ");
                        Console.WriteLine(sb.ToString().Substring(1));
                    }
                    else
                    {
                        Console.WriteLine("Couldn't find any relevant results for \"seattle restaurants\"");
                    }
                }
                else
                {
                    Console.WriteLine("Didn't see any data..");
                }
            }
            catch (ErrorResponseException ex)
            {
                Console.WriteLine("Encountered exception. " + ex.Message);
            }
        }
        public static void DominantEntityLookup(string subscriptionKey, string endpoint)
        {
            // Example Endpoints are https://westus2.api.cognitive.microsoft.com. You can find this under your subscription.
            // Dont use /bing/v7.0/entities/ in the enpoint

            var client = new EntitySearchClient(new ApiKeyServiceClientCredentials(subscriptionKey))
            {
                Endpoint = endpoint
            };

            try
            {
                var entityData = client.Entities.SearchAsync(query: "Satya Nadella");

                if (entityData?.Result.Entities?.Value?.Count > 0)
                {
                    // find the entity that represents the dominant one
                    var mainEntity = entityData.Result.Entities.Value.Where(thing => thing.EntityPresentationInfo.EntityScenario == EntityScenario.DominantEntity).FirstOrDefault();

                    if (mainEntity != null)
                    {
                        Console.WriteLine("Searched for \"Satya Nadella\" and found a dominant entity with this description:");
                        Console.WriteLine(mainEntity.Description);
                    }
                    else
                    {
                        Console.WriteLine("Couldn't find main entity Satya Nadella!");
                    }
                }
                else
                {
                    Console.WriteLine("Didn't see any data..");
                }
            }
            catch (ErrorResponseException ex)
            {
                Console.WriteLine("Encountered exception. " + ex.Message);
            }
        }
        public static void RestaurantLookup(string subscriptionKey, string endpoint)
        {
            // Example Endpoints are https://westus2.api.cognitive.microsoft.com. You can find this under your subscription
            // Dont use /bing/v7.0/entities/ in the enpoint
            var client = new EntitySearchClient(new ApiKeyServiceClientCredentials(subscriptionKey))
            {
                Endpoint = endpoint
            };

            try
            {
                var entityData = client.Entities.Search(query: "john howie bellevue");

                if (entityData?.Places?.Value?.Count > 0)
                {
                    // Some local entities will be places, others won't be. Depending on the data you want, try to cast to the appropriate schema
                    // In this case, the item being returned is technically a Restaurant, but the Place schema has the data we want (telephone)
                    var restaurant = entityData.Places.Value.FirstOrDefault() as Place;

                    if (restaurant != null)
                    {
                        Console.WriteLine("Searched for \"John Howie Bellevue\" and found a restaurant with this phone number:");
                        Console.WriteLine(restaurant.Telephone);
                    }
                    else
                    {
                        Console.WriteLine("Couldn't find a place!");
                    }
                }
                else
                {
                    Console.WriteLine("Didn't see any data..");
                }
            }
            catch (ErrorResponseException ex)
            {
                Console.WriteLine("Encountered exception. " + ex.Message);
            }
        }
 public BingSearchClient(string bingSearchKey, string bingAnswerSearchKey)
 {
     _entitySearchClient  = new EntitySearchClient(new Microsoft.Azure.CognitiveServices.Search.EntitySearch.ApiKeyServiceClientCredentials(bingSearchKey));
     _webSearchClient     = new WebSearchClient(new Microsoft.Azure.CognitiveServices.Search.WebSearch.ApiKeyServiceClientCredentials(bingSearchKey));
     _bingAnswerSearchKey = bingAnswerSearchKey;
 }
예제 #9
0
        /// <summary>
        /// Bing Entity Search - Entities
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static List <BingEntityData> BingEntities(List <string> entities)
        {
            // set up main search client
            var entityClient = new EntitySearchClient(new ApiKeyServiceClientCredentialsEntitySearch());

            var entityDataSet = new List <BingEntityData>();

            Parallel.ForEach(entities,
                             new ParallelOptions {
                MaxDegreeOfParallelism = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 0.75) * 2.0))
            },
                             (entity) =>
                             //foreach (var entity in entities)
            {
                var entityData  = entityClient.Entities.Search(query: entity);
                var description = "NOTFOUND";
                var taxonomy    = "NOTFOUND";

                // 1) Lookup main entities
                if (entityData?.Entities?.Value?.Count > 0)
                {
                    // Find the entity that represents the dominant one
                    var mainEntity = entityData.Entities.Value.Where(thing => thing.EntityPresentationInfo.EntityScenario == Microsoft.Azure.CognitiveServices.Search.EntitySearch.Models.EntityScenario.DominantEntity).FirstOrDefault();

                    // If null, use disambiguaation item
                    if (mainEntity == null)
                    {
                        mainEntity = entityData.Entities.Value.Where(thing => thing.EntityPresentationInfo.EntityScenario == Microsoft.Azure.CognitiveServices.Search.EntitySearch.Models.EntityScenario.DisambiguationItem).FirstOrDefault();
                    }
                    Console.WriteLine("\t\t" + entity);

                    // Extract key properties
                    description = mainEntity == null ? "NOTFOUND" : mainEntity.Description;
                    taxonomy    = mainEntity == null ? "NOTFOUND" : (mainEntity.EntityPresentationInfo.EntityTypeHints != null) ?
                                  mainEntity.EntityPresentationInfo.EntityTypeHints.FirstOrDefault() :
                                  (mainEntity.EntityPresentationInfo == null) ? "NOTFOUND" : mainEntity.EntityPresentationInfo.EntityTypeDisplayHint;

                    // Workaround for some entities being labeled as movies
                    if (taxonomy == "Movie" || taxonomy == "MusicRecording")
                    {
                        taxonomy    = "Generic";
                        description = string.Empty;
                    }
                }
                // 2) Lookup places
                else if (entityData?.Places?.Value?.Count > 0)
                {
                    // Find the entity that represents the dominant one
                    var mainEntity = entityData.Places.Value.Where(place => place.EntityPresentationInfo.EntityScenario == Microsoft.Azure.CognitiveServices.Search.EntitySearch.Models.EntityScenario.ListItem).FirstOrDefault();

                    // If null, use disambiguaation item
                    if (mainEntity == null)
                    {
                        mainEntity = entityData.Places.Value.Where(place => place.EntityPresentationInfo.EntityScenario == Microsoft.Azure.CognitiveServices.Search.EntitySearch.Models.EntityScenario.DisambiguationItem).FirstOrDefault();
                    }

                    // Extract key properties
                    description = mainEntity == null ? "NOTFOUND" : mainEntity.Description;
                    taxonomy    = mainEntity == null ? "NOTFOUND" : (mainEntity.EntityPresentationInfo.EntityTypeHints != null) ?
                                  mainEntity.EntityPresentationInfo.EntityTypeHints.FirstOrDefault() :
                                  (mainEntity.EntityPresentationInfo == null) ? "NOTFOUND" : mainEntity.EntityPresentationInfo.EntityTypeDisplayHint;
                }


                entityDataSet.Add(
                    new BingEntityData()
                {
                    EntityName  = entity,
                    Description = description,
                    Taxony      = taxonomy
                }
                    );
            }     // EOF foreach
                             );


            return(entityDataSet);
        }