Before reading this article, please go through the articles, mentioned below.
- Introduction To Universal Windows Platform (UWP) App Development Using Windows 10 And Visual Studio 2015
- How To Create Microsoft Cognitive Service Academic Knowledge API On Azure Portal
- Cognitive Service Academic Knowledge API - Evaluate Method Using UWP With Azure, XAML And C#
Microsoft Cognitive Services (formerly Project Oxford) is a set of APIs, SDKs and Services available to developers to make their Applications more intelligent, engaging and discoverable. Microsoft Cognitive Services expands on Microsoft’s evolving portfolio of the machine learning APIs and enables the developers to easily add intelligent features. Microsoft Cognitive Services allows you to build apps with the powerful algorithms, using a few lines of code. They work across the devices and platforms such as iOS, Android and Windows, keep improving, and are easy to set up.
Academic Knowledge API With an Academic Knowledge API, we will be able to interpret the user queries for the academic intent, which helps to retrieve the rich information from Microsoft Academic Graph (MAG). MAG knowledge base is a Web-scale heterogeneous entity graph, made of entities that model scholarly activities: field of study, author, institution, paper, venue and event.
Academic Knowledge API consists of three related REST endpoints.
- Interpret
interpret REST API takes an end user query string (i.e., a query entered by a user of your Application) and returns the formatted interpretations of the user intent, which is based on the Academic Graph data and the Academic Grammar.
- Evaluate
It evaluates a query expression and returns Academic Knowledge entity results.
- Calchistogram
It calculates a histogram of the distribution of the attribute values for the academic entities, which are returned by a query expression, such as the distribution of citations by year for a given author.
After reading this article, you can learn how to do Cognitive Service Academic Knowledge API - Calchistogram Method in Universal Windows apps development with Azure, XAML and Visual C#.
The following important tools are required to develop UWP,
- Windows 10 (Recommended)
- Visual Studio 2015 Community Edition (It is a free software available online)
- Cognitive Service Academic Knowledge API Key (using azure How To Create Microsoft Cognitive Service Academic Knowledge API On Azure Portal)
Now, we can discuss step by step app development.
Step 1
Open Visual studio 2015 -> Start -> New Project-> Select Universal (under Visual C#->Windows)-> Blank App -> Give the Suitable Name for your app (UWPCogAcadCalchistogram)->OK.
After choosing the target and minimum platform version your Windows Universal, Application will support the Project creates App.xaml and MainPage.xaml.
Step 2
Open (double click) the file MainPage.xaml in the Solution Explorer and add the Newtonsoft.Json reference in the project. Right click on your Project (UWPCogAcadCalchistogram) and select Manage NuGet Packages.
For adding Newtonsoft.Json Reference, choose Browse and search Newtonsoft.Json. Select the package and install it.
Reference is added to your project.
For adding WinRTXamlToolkit.Controls.DataVisualization.UWP Reference, choose Browse and search WinRTXamlToolkit.Controls.DataVisualization.UWP select the package and install it. Reference is used to create chart.
Reference is added to your project.
Step 3
Add TextBlock control, change the name and text property for title.
Add AutoSuggestBox control, set the Name and Text Properties for search query.
Add a Button Control, set the Name and add the search icon to find the details.
Add TextBlock control, change the Name and text property for total count of entities.
Step 4
Add GridView Resources, using the code, mentioned below.
- <Grid.Resources>
- <CollectionViewSource x:Name="SearchResultsCol" Source="{x:Bind SearchResults}" IsSourceGrouped="False" />
- </Grid.Resources>
Add a GridView Control and set the ItemSource properties.
- <GridView x:Name="gvHisto" ItemsSource="{Binding Source={StaticResource SearchResultsCol}}" SelectionMode="None" IsItemClickEnabled="False" HorizontalAlignment="Left" Height="276" Margin="82,313,0,0" VerticalAlignment="Top" Width="428">
Add the code, mentioned below for Grid View Item Template for Journal details.
- <GridView.ItemTemplate>
- <DataTemplate>
- <Border Background="AliceBlue" Width="110" Height="36" Margin="8">
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="auto" />
- <ColumnDefinition/>
- </Grid.ColumnDefinitions>
- <Grid Grid.Column="1" Margin="3,3,3,10">
- <Grid.RowDefinitions>
- <RowDefinition Height="auto" />
- </Grid.RowDefinitions>
- <StackPanel Margin="0,0,0,-143">
- <TextBlock TextWrapping="Wrap" Text="{Binding hisco.Title}" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" />
- <TextBlock TextWrapping="Wrap" Text="{Binding hisco.Value}" Style="{StaticResource CaptionTextBlockStyle}" />
- <TextBlock TextWrapping="Wrap" Text="{Binding hisco.Count}" Style="{StaticResource CaptionTextBlockStyle}" />
- </StackPanel>
- </Grid>
- </Grid>
- </Border>
- </DataTemplate>
- </GridView.ItemTemplate>
Step 5
Add the column chart Namespace for displaying entity details to Chart (Column Chart - More Details :
Creating Column Chart In Universal Application Development With XAML And C#).
- xmlns:Charting="using:WinRTXamlToolkit.Controls.DataVisualization.Charting"
-
- <Charting:Chart x:Name="Column" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="559,312,0,0" Width="555" Height="279" >
- <Charting:ColumnSeries Margin="0" IndependentValuePath="Value" DependentValuePath="Count" Title="Journal Details" />
- </Charting:Chart>
Step 6
Add the Click event method for Search button.
Note
Automatically, the code will be generated in XAML code view, while we are done in the design view.
- <Page x:Class="UWPCogAcadCalchistogram.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UWPCogAcadCalchistogram" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:Charting="using:WinRTXamlToolkit.Controls.DataVisualization.Charting" mc:Ignorable="d">
- <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
- <Grid.Resources>
- <CollectionViewSource x:Name="SearchResultsCol" Source="{x:Bind SearchResults}" IsSourceGrouped="False" />
- </Grid.Resources>
- <TextBlock x:Name="tblTitle" HorizontalAlignment="Left" Margin="274,82,0,0" TextWrapping="Wrap" Text="UWP Cognitive Service Academic Knowledge Calchistogram Method Demo " VerticalAlignment="Top" Height="32" Width="716" FontWeight="Bold" FontSize="20" />
- <AutoSuggestBox x:Name="asbHisto" HorizontalAlignment="Left" Margin="302,176,0,0" VerticalAlignment="Top" Width="549" Height="36" />
- <Button x:Name="btnAcKnHist" HorizontalAlignment="Left" Margin="856,178,0,0" VerticalAlignment="Top" Click="btnAcKnHist_Click">
- <SymbolIcon Symbol="Find"></SymbolIcon>
- </Button>
- <TextBox x:Name="tblTotal" HorizontalAlignment="Left" Margin="82,254,0,0" TextWrapping="Wrap" Text="Total Count : " VerticalAlignment="Top" FontWeight="Bold" />
- <GridView x:Name="gvHisto" ItemsSource="{Binding Source={StaticResource SearchResultsCol}}" SelectionMode="None" IsItemClickEnabled="False" HorizontalAlignment="Left" Height="276" Margin="82,313,0,0" VerticalAlignment="Top" Width="428">
- <GridView.ItemTemplate>
- <DataTemplate>
- <Border Background="AliceBlue" Width="110" Height="36" Margin="8">
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="auto" />
- <ColumnDefinition/>
- </Grid.ColumnDefinitions>
- <Grid Grid.Column="1" Margin="3,3,3,10">
- <Grid.RowDefinitions>
- <RowDefinition Height="auto" />
- </Grid.RowDefinitions>
- <StackPanel Margin="0,0,0,-143">
- <TextBlock TextWrapping="Wrap" Text="{Binding hisco.Title}" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" />
- <TextBlock TextWrapping="Wrap" Text="{Binding hisco.Value}" Style="{StaticResource CaptionTextBlockStyle}" />
- <TextBlock TextWrapping="Wrap" Text="{Binding hisco.Count}" Style="{StaticResource CaptionTextBlockStyle}" />
- </StackPanel>
- </Grid>
- </Grid>
- </Border>
- </DataTemplate>
- </GridView.ItemTemplate>
- </GridView>
- <Charting:Chart x:Name="Column" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="559,312,0,0" Width="555" Height="279">
- <Charting:ColumnSeries Margin="0" IndependentValuePath="Value" DependentValuePath="Count" Title="Journal Details" />
- </Charting:Chart>
- </Grid>
- </Page>
Step 6
Add the namespaces, mentioned below in Mainpage.xaml.cs for Academic Knowledge.
- using System.Net.Http;
- using System.Threading.Tasks;
- using Newtonsoft.Json.Linq;
- using System.Collections.ObjectModel;
- using System.Net;
- using WinRTXamlToolkit.Controls.DataVisualization.Charting;
Step 7
To add the Academic Knowledge, Client keys use Azure Service and generate it ( for More Information, please refer the article
How To Create Microsoft Cognitive Service Academic Knowledge API On Azure Portal)
Academic Knowledge REST endpoint.
- Interpret Method
https://api.projectoxford.ai/academic/v1.0/interpret?
- Calchistogram Method
https://api.projectoxford.ai/academic/v1.0/calchistogram?
Add the code with Azure generated key to get the expression from Interpret Method, as shown below.
- public class Interpretc {
- public string Title {
- get;
- set;
- }
- public string output {
- get;
- set;
- }
- }
- private async void btnAcKnHist_Click(object sender, RoutedEventArgs e) {
- query = asbHisto.Text.Trim();
- var res = await interpret();
- for (int i = 0; i < res.Count(); i++) {
- Interpret wres = res.ElementAt(i);
- expr = wres.output;
- }
- var hist = await histogram();
- for (int i = 0; i < hist.Count(); i++) {
- Histogramc hh = hist.ElementAt(i);
- SearchResults.Add(new histocol {
- hisco = hh
- });
- tblTotal.Text = hh.TotalCount;
- }
- (Column.Series[0] as ColumnSeries).ItemsSource = hist;
- }
-
- static async Task < IEnumerable < Interpretc >> interpret() {
- var iclient = new HttpClient();
- List < Interpretc > ire = new List < Interpretc > ();
- iclient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "<Your Acadamic Knowledge API Key>");
- string count = "2";
- string Complete = "1";
- var inteSearchEndPoint = "https://api.projectoxford.ai/academic/v1.0/interpret?";
- var result = await iclient.GetAsync(string.Format("{0}query=papers by +{1}&complete={2}&count={3}", inteSearchEndPoint, WebUtility.UrlEncode(query), Complete, count));
- result.EnsureSuccessStatusCode();
- var json = await result.Content.ReadAsStringAsync();
- dynamic data = JObject.Parse(json);
- for (int i = 0; i < 1; i++) {
- ire.Add(new Interpretc {
- Title = data.interpretations[i].rules[i].name,
- output = data.interpretations[i].rules[i].output.value
- });
- }
- return ire;
- }
Add the code, mentioned below with an Azure generated key for using the expression value from Interpret Method in evaluate method, as shown below.
- public class Histogramc {
- public string TotalCount {
- get;
- set;
- }
- public string Value {
- get;
- set;
- }
- public string Count {
- get;
- set;
- }
- }
- public class histocol {
- public Histogramc hisco {
- get;
- set;
- }
- }
- public static string expr, query;
- public ObservableCollection < histocol > SearchResults {
- get;
- set;
- } = new ObservableCollection < histocol > ();
- async Task < IEnumerable < Histogramc >> histogram() {
- List < Histogramc > hiscc = new List < Histogramc > ();
- var client = new HttpClient();
- client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "<Your Acadamic Knowledge API Key>");
- string count = "4";
- string offset = "0";
- string model = "latest";
- string attributes = "Y,F.FN";
- var histoEndPoint = "https://api.projectoxford.ai/academic/v1.0/calchistogram?";
- var result = await client.GetAsync(string.Format("{0}expr={1}&model={2}&count={3}&offset={4}&attributes={5}", histoEndPoint, expr, model, count, offset, attributes));
- result.EnsureSuccessStatusCode();
- var json = await result.Content.ReadAsStringAsync();
- dynamic data = JObject.Parse(json);
- for (int i = 0; i < 4; i++) {
- hiscc.Add(new Histogramc {
- TotalCount = "Total Count : " + data.histograms[0].total_count,
- Value = data.histograms[0].histogram[i].value,
- Count = data.histograms[0].histogram[i].count
- });
- }
- return hiscc;
- }
Step 8
Deploy our app in Local Machine and the output of the UWPCogAcadCalchistogram app, as shown below.
After clicking the Find button, the screenshot will appear, as shown below.
Summary
Now, you have successfully tested Cognitive Service Academic Knowledge API – Calchistogram Method, using Azure, XAML and C# with UWP environment.
If you are interested, please read UWP Cognitive Service articles, as mentioned below.
- Cognitive Service Academic Knowledge API - Evaluate Method Using UWP With Azure, XAML And C#
- Cognitive Service Bing Video Search API Using UWP With Azure, XAML And C#
- Cognitive Service Bing Image Search API Using UWP With Azure, XAML And C#
- Cognitive Service Bing News Search API Using UWP With Azure, XAML And C#
- Cognitive Service Bing AutoSuggest API Using UWP With Azure, XAML And C#
- Multiple People Emotions In UWP With Cognitive Service Emotion API, Azure, XAML And C#
- Cognitive Service Emotion API In UWP With Azure, XAML And C#
- Retrieving Face Attributes Using Cognitive Service Face API In UWP With Azure, XAML And C#
- Cognitive Service Face API, Using UWP With Azure, XAML And C#