forked from microsoft/Cognitive-Samples-IntelligentKiosk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBingNewsAnalytics.xaml.cs
293 lines (255 loc) · 10.6 KB
/
BingNewsAnalytics.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Cognitive Services: http://www.microsoft.com/cognitive
//
// Microsoft Cognitive Services Github:
// https://github.com/Microsoft/Cognitive
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using ServiceHelpers;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Popups;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace IntelligentKioskSample.Views
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
[KioskExperience(Title = "Bing News Analytics", ImagePath = "ms-appx:/Assets/BingNewsAnalytics.png", ExperienceType = ExperienceType.Kiosk)]
public sealed partial class BingNewsAnalyticsPage : Page
{
public ObservableCollection<NewsAndSentimentScore> FilteredNewsResults { get; set; } = new ObservableCollection<NewsAndSentimentScore>();
public ObservableCollection<KeyPhraseCount> TopKeyPhrases { get; set; } = new ObservableCollection<KeyPhraseCount>();
private List<NewsAndSentimentScore> latestSearchResult = new List<NewsAndSentimentScore>();
public BingNewsAnalyticsPage()
{
this.InitializeComponent();
this.searchBox.Focus(FocusState.Programmatic);
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
EnterKioskMode();
if (string.IsNullOrEmpty(SettingsHelper.Instance.TextAnalyticsKey))
{
await new MessageDialog("Missing Text Analytics Key. Please enter the key in the Settings page.", "Missing API Key").ShowAsync();
}
base.OnNavigatedTo(e);
}
private void EnterKioskMode()
{
ApplicationView view = ApplicationView.GetForCurrentView();
if (!view.IsFullScreenMode)
{
view.TryEnterFullScreenMode();
}
}
private async void SearchClicked(object sender, RoutedEventArgs e)
{
await SearchAsync(this.searchBox.Text);
}
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.GetSentimentAsync(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 (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Processing error");
}
finally
{
this.progressRing.IsActive = false;
}
}
private static string GetBingSearchMarketFromLanguage(string language)
{
switch (language)
{
case "English": return "en-US";
case "Spanish": return "es-MX";
case "French": return "fr-FR";
case "Portuguese": return "pt-BR";
case "Japanese": return "ja-JP";
default:
return "en-US";
}
}
private static string GetTextAnalyticsLanguageCodeFromLanguage(string language)
{
switch (language)
{
case "English": return "en";
case "Spanish": return "es";
case "French": return "fr";
case "Portuguese": return "pt";
case "Japanese": return "ja";
default:
return "en";
}
}
private static bool IsLanguageSupportedByKeyPhraseAPI(string language)
{
switch (language)
{
case "English":
case "Spanish":
case "German":
case "Japanese":
return true;
default:
return false;
}
}
private void UpdateFilteredResults()
{
this.FilteredNewsResults.Clear();
if (this.sortBySentimentToggle.IsOn)
{
this.FilteredNewsResults.AddRange(this.latestSearchResult.OrderByDescending(n => n.TitleSentiment));
}
else
{
this.FilteredNewsResults.AddRange(this.latestSearchResult);
}
}
private async void OnSearchQuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
if (args.ChosenSuggestion != null)
{
await SearchAsync(args.ChosenSuggestion.ToString());
}
else
{
await SearchAsync(args.QueryText);
}
sender.IsSuggestionListOpen = false;
}
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 void SortBySentimentToggleChanged(object sender, RoutedEventArgs e)
{
this.UpdateFilteredResults();
}
private async void LanguageSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!string.IsNullOrEmpty(this.searchBox.Text))
{
await SearchAsync(this.searchBox.Text);
}
}
}
public class NewsAndSentimentScore
{
public NewsArticle Article { get; set; }
public double TitleSentiment { get; set; }
public IEnumerable<string> KeyPhrases { get; set; }
}
public class KeyPhraseCount
{
public string KeyPhrase { get; set; }
public int Count { get; set; }
}
public class SentimentToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
double score = (double)value;
// Linear gradient function, from a red 0x99 when score = 0 to a green 0x77 when score = 1.
return new SolidColorBrush(Color.FromArgb(0xff, (byte) (0x99 - (score * 0x99)), (byte) (score * 0x77), 0));
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
// not used
return 0.5;
}
}
public class WordCountToFontSizeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return 10 + Math.Min(36, (int)value * 1.5);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
// not used
return 0;
}
}
}