private async void OnConfirmImportButtonClicked(object sender, RoutedEventArgs e) { this.addPeopleInBatchesFlyout.Hide(); this.commandBar.IsOpen = false; this.progressControl.IsActive = true; try { string[] names = this.importNamesTextBox.Text.Split('\n'); foreach (var name in names) { string personName = Util.CapitalizeString(name.Trim()); if (string.IsNullOrEmpty(personName) || this.PersonsInCurrentGroup.Any(p => p.Name == personName)) { continue; } CreatePersonResult newPersonResult = await FaceServiceHelper.CreatePersonAsync(this.CurrentPersonGroup.PersonGroupId, personName); Person newPerson = new Person { Name = name, PersonId = newPersonResult.PersonId }; IEnumerable <string> faceUrls = await BingSearchHelper.GetImageSearchResults(string.Format("{0} {1} {2}", this.importImageSearchKeywordPrefix.Text, name, this.importImageSearchKeywordSufix.Text), count : 2); foreach (var url in faceUrls) { try { ImageAnalyzer imageWithFace = new ImageAnalyzer(url); await imageWithFace.DetectFacesAsync(); if (imageWithFace.DetectedFaces.Count() == 1) { await FaceServiceHelper.AddPersonFaceAsync(this.CurrentPersonGroup.PersonGroupId, newPerson.PersonId, imageWithFace.ImageUrl, imageWithFace.ImageUrl, imageWithFace.DetectedFaces.First().FaceRectangle); } } catch (Exception) { // Ignore errors with any particular image and continue } // Force a delay to reduce the chance of hitting API call rate limits await Task.Delay(250); } this.needsTraining = true; this.PersonsInCurrentGroup.Add(newPerson); } } catch (Exception ex) { await Util.GenericApiCallExceptionHandler(ex, "Failure during batch processing"); } this.progressControl.IsActive = false; }
static void Main(string[] args) { try { ///Search about Bill Gates ///NB: This should be done asynchronously... SearchResult result = BingSearchHelper.Query("Bill Gates", new BingQueryParameters( apiKey: "", count: 10, offset: 0, mkt: "en-us", safeSearch: "Moderate") ).Result; ///Get a link to the first image of the search result. Console.WriteLine(result.Images.Value[0].ThumbnailUrl); Console.WriteLine("Done!!!!!!"); } catch (Exception e) { Console.WriteLine(e.Message + "\n" + e.StackTrace); } Console.ReadKey(); }
private async Task SearchAsync(string query) { try { this.TopKeyPhrases.Clear(); this.latestSearchResult.Clear(); this.FilteredNewsResults.Clear(); this.sentimentDistributionControl.UpdateData(Enumerable.Empty <double>()); this.progressRing.IsActive = true; string userLanguage = this.languageComboBox.SelectedValue.ToString(); var news = await BingSearchHelper.GetNewsSearchResults(query, count : 50, offset : 0, market : GetBingSearchMarketFromLanguage(userLanguage)); Task <SentimentResult> sentimentTask = TextAnalyticsHelper.GetTextSentimentAsync(news.Select(n => n.Title).ToArray(), language: GetTextAnalyticsLanguageCodeFromLanguage(userLanguage)); Task <KeyPhrasesResult> keyPhrasesTask; if (IsLanguageSupportedByKeyPhraseAPI(userLanguage)) { keyPhrasesTask = TextAnalyticsHelper.GetKeyPhrasesAsync(news.Select(n => n.Title).ToArray(), language: GetTextAnalyticsLanguageCodeFromLanguage(userLanguage)); } else { keyPhrasesTask = Task.FromResult(new KeyPhrasesResult { KeyPhrases = new string[][] { new string [] { "Not available in this language" } } }); } await Task.WhenAll(sentimentTask, keyPhrasesTask); var sentiment = sentimentTask.Result; for (int i = 0; i < news.Count(); i++) { NewsArticle article = news.ElementAt(i); this.latestSearchResult.Add(new NewsAndSentimentScore { Article = article, TitleSentiment = Math.Round(sentiment.Scores.ElementAt(i), 2) }); } UpdateFilteredResults(); this.sentimentDistributionControl.UpdateData(this.latestSearchResult.Select(n => n.TitleSentiment)); var wordGroups = keyPhrasesTask.Result.KeyPhrases.SelectMany(k => k).GroupBy(phrase => phrase, StringComparer.OrdinalIgnoreCase).OrderByDescending(g => g.Count()).Take(10).OrderBy(g => g.Key); this.TopKeyPhrases.AddRange(wordGroups.Select(g => new KeyPhraseCount { KeyPhrase = g.Key, Count = g.Count() })); } catch (HttpRequestException ex) { await Util.GenericApiCallExceptionHandler(ex, "Processing error"); } finally { this.progressRing.IsActive = false; } }
private async void UpdateResults(ImageAnalyzer img) { this.searchErrorTextBlock.Visibility = Visibility.Collapsed; IEnumerable <VisualSearchResult> result = null; try { if (this.similarImagesResultType.IsSelected) { if (img.ImageUrl != null) { result = await BingSearchHelper.GetVisuallySimilarImages(img.ImageUrl); } else { result = await BingSearchHelper.GetVisuallySimilarImages(await Util.ResizePhoto(await img.GetImageStreamCallback(), 360)); } } else if (this.celebrityResultType.IsSelected) { if (img.ImageUrl != null) { result = (await BingSearchHelper.GetVisuallySimilarCelebrities(img.ImageUrl)).OrderByDescending(r => r.SimilarityScore); } else { result = (await BingSearchHelper.GetVisuallySimilarCelebrities(await Util.ResizePhoto(await img.GetImageStreamCallback(), 360))).OrderByDescending(r => r.SimilarityScore); } } else if (this.similarProductsResultType.IsSelected) { if (img.ImageUrl != null) { result = await BingSearchHelper.GetVisuallySimilarProducts(img.ImageUrl); } else { result = await BingSearchHelper.GetVisuallySimilarProducts(await Util.ResizePhoto(await img.GetImageStreamCallback(), 360)); } } } catch (Exception) { // We just ignore errors for now and default to a generic error message } this.resultsGridView.ItemsSource = result; this.progressRing.IsActive = false; if (result == null || !result.Any()) { this.searchErrorTextBlock.Visibility = Visibility.Visible; } }
private async void OnPersonNameTextBoxChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args) { try { this.personNameTextBox.ItemsSource = await BingSearchHelper.GetAutoSuggestResults(this.personNameTextBox.Text); } catch (HttpRequestException) { // default to no suggestions this.personNameTextBox.ItemsSource = null; } }
public void ShouldSearchWeb() { SearchResult result = BingSearchHelper.Query(query, new BingQueryParameters( apiKey: reamera, count: 10, offset: 0, mkt: "en-us", safeSearch: "Moderate")).Result; Assert.That(result, Is.Not.Null); }
private async void OnTagNameTextBoxChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args) { if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput) { try { this.tagNameTextBox.ItemsSource = await BingSearchHelper.GetAutoSuggestResults(this.tagNameTextBox.Text); } catch (HttpRequestException) { // default to no suggestions this.tagNameTextBox.ItemsSource = null; } } }
private async void OnSearchTextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args) { if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput) { try { this.searchBox.ItemsSource = await BingSearchHelper.GetAutoSuggestResults(this.searchBox.Text, GetBingSearchMarketFromLanguage(this.languageComboBox.SelectedValue.ToString())); } catch (HttpRequestException) { // default to no suggestions this.searchBox.ItemsSource = null; } } }
private async Task SearchAsync(string query) { try { this.TopKeyPhrases.Clear(); this.latestSearchResult.Clear(); this.FilteredNewsResults.Clear(); this.sentimentDistributionControl.UpdateData(Enumerable.Empty <double>()); this.progressRing.IsActive = true; // get language and bing news titles string userLanguage = this.languageComboBox.SelectedValue.ToString(); IEnumerable <NewsArticle> news = await BingSearchHelper.GetNewsSearchResults(query, count : TotalNewsCount, offset : 0, market : GetBingSearchMarketFromLanguage(userLanguage)); // analyze news titles AnalyzeTextResult analyzedNews = news.Any() ? await AnalyzeNewsAsync(news, userLanguage) : null; List <double> scores = analyzedNews?.Scores ?? new List <double>(); List <KeyPhraseCount> topKeyPhrases = analyzedNews?.TopKeyPhrases ?? new List <KeyPhraseCount>(); // display result for (int i = 0; i < news.Count(); i++) { NewsArticle article = news.ElementAt(i); double score = i < scores.Count ? scores.ElementAt(i) : 0.5; this.latestSearchResult.Add(new NewsAndSentimentScore { Article = article, TitleSentiment = Math.Round(score, 2) }); } UpdateFilteredResults(); this.sentimentDistributionControl.UpdateData(this.latestSearchResult.Select(n => n.TitleSentiment)); this.TopKeyPhrases.AddRange(topKeyPhrases); } catch (HttpRequestException ex) { await Util.GenericApiCallExceptionHandler(ex, "Processing error"); } finally { this.progressRing.IsActive = false; } }
private async void onTextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args) { if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput) { try { this.autoSuggestBox.ItemsSource = await BingSearchHelper.GetAutoSuggestResults(this.autoSuggestBox.Text); } catch (Exception) { this.autoSuggestBox.ItemsSource = null; } } else if (args.Reason == AutoSuggestionBoxTextChangeReason.ProgrammaticChange && !string.IsNullOrEmpty(this.autoSuggestBox.Text)) { await QueryBingImages(this.autoSuggestBox.Text); } }
private async Task QueryBingImages(string query) { this.progressRing.IsActive = true; try { IEnumerable <string> imageUrls = await BingSearchHelper.GetImageSearchResults(query, imageContent : this.ImageContentType, count : 30); this.imageResultsGrid.ItemsSource = imageUrls.Select(url => new ImageAnalyzer(url)); } catch (Exception ex) { this.imageResultsGrid.ItemsSource = null; await Util.GenericApiCallExceptionHandler(ex, "Failure querying Bing Images"); } this.progressRing.IsActive = false; }
private async Task QueryBingImages(string query) { this.progressRing.IsActive = true; try { IEnumerable <string> imageUrls = await BingSearchHelper.GetImageSearchResults(query, imageContent : string.Empty, count : 30); this.imageResultsGrid.ItemsSource = imageUrls.Select(url => new ImageAnalyzer(url)); this.autoSuggestBox.IsSuggestionListOpen = false; } catch (Exception ex) { this.imageResultsGrid.ItemsSource = null; if (!string.IsNullOrEmpty(query)) { await Util.GenericApiCallExceptionHandler(ex, "Failure searching on Bing Images"); } } this.progressRing.IsActive = false; }
public ActionResult SearchCompleted(string query, SearchResponse searchResponse) { ViewData["query"] = query ?? string.Empty; if (searchResponse != null && searchResponse.Web != null) { if (searchResponse.Web.Results != null && searchResponse.Web.Results.Any()) { //Format results try { var results = BingSearchHelper.CreateResults(searchResponse.Web.Results); return(View(results)); } catch (Exception) { //Failed. fallback to our search } } } return(View(Documentation.Search(query ?? string.Empty))); }
private async Task ResumeAfterEnteringQuery(IDialogContext context, IAwaitable <string> result) { query = (await result)as string; switch (searchType) { case searchWeb: { await BingSearchHelper.SearchWebAsync(context, BING_KEY, query); break; } case searchImage: { await BingSearchHelper.SearchImageAsync(context, BING_KEY, query); break; } case searchVideo: { await BingSearchHelper.SearchVideoAsync(context, BING_KEY, query); break; } case searchNews: { await BingSearchHelper.SearchNewsAsync(context, BING_KEY, query); break; } case treadingNews: { await BingSearchHelper.GetTreadingNewsAsync(context, BING_KEY); break; } case searchEntity: { await BingSearchHelper.SearchEntityAsync(context, BING_KEY, query); break; } case searchPlace: { await BingSearchHelper.SearchPlaceAsync(context, BING_KEY, query); break; } case checkSpell: { await BingSearchHelper.CheckSpellAsync(context, BING_KEY, query); break; } } context.Wait(MessageReceivedAsync); }