Calchistogram Method In Cognitive Service Academic Knowledge API

Before reading this article, please go through the articles, mentioned below.

  1. Introduction To Universal Windows Platform (UWP) App Development Using Windows 10 And Visual Studio 2015
  2. How To Create Microsoft Cognitive Service Academic Knowledge API On Azure Portal
  3. 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,

  1. Windows 10 (Recommended)
  2. Visual Studio 2015 Community Edition (It is a free software available online)
  3. 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.

  1. <Grid.Resources>  
  2.    <CollectionViewSource x:Name="SearchResultsCol" Source="{x:Bind SearchResults}" IsSourceGrouped="False" />  
  3. </Grid.Resources>  
Add a GridView Control and set the ItemSource properties.
  1. <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.
  1. <GridView.ItemTemplate>  
  2.     <DataTemplate>  
  3.         <Border Background="AliceBlue" Width="110" Height="36" Margin="8">  
  4.             <Grid>  
  5.                 <Grid.ColumnDefinitions>  
  6.                     <ColumnDefinition Width="auto" />  
  7.                     <ColumnDefinition/>  
  8.                 </Grid.ColumnDefinitions>  
  9.                 <Grid Grid.Column="1" Margin="3,3,3,10">  
  10.                     <Grid.RowDefinitions>  
  11.                         <RowDefinition Height="auto" />  
  12.                     </Grid.RowDefinitions>  
  13.                     <StackPanel Margin="0,0,0,-143">  
  14.                         <TextBlock TextWrapping="Wrap" Text="{Binding hisco.Title}" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" />  
  15.                         <TextBlock TextWrapping="Wrap" Text="{Binding hisco.Value}" Style="{StaticResource CaptionTextBlockStyle}" />  
  16.                         <TextBlock TextWrapping="Wrap" Text="{Binding hisco.Count}" Style="{StaticResource CaptionTextBlockStyle}" />  
  17.                     </StackPanel>  
  18.                 </Grid>  
  19.             </Grid>  
  20.         </Border>  
  21.     </DataTemplate>  
  22. </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#).
  1. xmlns:Charting="using:WinRTXamlToolkit.Controls.DataVisualization.Charting"  
  2.   
  3. <Charting:Chart x:Name="Column" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="559,312,0,0" Width="555" Height="279" >  
  4. <Charting:ColumnSeries Margin="0" IndependentValuePath="Value" DependentValuePath="Count" Title="Journal Details" />  
  5. </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.
  1. <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">  
  2.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  3.         <Grid.Resources>  
  4.             <CollectionViewSource x:Name="SearchResultsCol" Source="{x:Bind SearchResults}" IsSourceGrouped="False" />  
  5.         </Grid.Resources>  
  6.         <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" />  
  7.         <AutoSuggestBox x:Name="asbHisto" HorizontalAlignment="Left" Margin="302,176,0,0" VerticalAlignment="Top" Width="549" Height="36" />  
  8.         <Button x:Name="btnAcKnHist" HorizontalAlignment="Left" Margin="856,178,0,0" VerticalAlignment="Top" Click="btnAcKnHist_Click">  
  9. <SymbolIcon Symbol="Find"></SymbolIcon>  
  10. </Button>  
  11.         <TextBox x:Name="tblTotal" HorizontalAlignment="Left" Margin="82,254,0,0" TextWrapping="Wrap" Text="Total Count : " VerticalAlignment="Top" FontWeight="Bold" />  
  12.         <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">  
  13.             <GridView.ItemTemplate>  
  14.                 <DataTemplate>  
  15.                     <Border Background="AliceBlue" Width="110" Height="36" Margin="8">  
  16.                         <Grid>  
  17.                             <Grid.ColumnDefinitions>  
  18.                                 <ColumnDefinition Width="auto" />  
  19.                                 <ColumnDefinition/>  
  20.                             </Grid.ColumnDefinitions>  
  21.                             <Grid Grid.Column="1" Margin="3,3,3,10">  
  22.                                 <Grid.RowDefinitions>  
  23.                                     <RowDefinition Height="auto" />  
  24.                                 </Grid.RowDefinitions>  
  25.                                 <StackPanel Margin="0,0,0,-143">  
  26.                                     <TextBlock TextWrapping="Wrap" Text="{Binding hisco.Title}" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" />  
  27.                                     <TextBlock TextWrapping="Wrap" Text="{Binding hisco.Value}" Style="{StaticResource CaptionTextBlockStyle}" />  
  28.                                     <TextBlock TextWrapping="Wrap" Text="{Binding hisco.Count}" Style="{StaticResource CaptionTextBlockStyle}" />  
  29.                                 </StackPanel>  
  30.                             </Grid>  
  31.                         </Grid>  
  32.                     </Border>  
  33.                 </DataTemplate>  
  34.             </GridView.ItemTemplate>  
  35.         </GridView>  
  36.         <Charting:Chart x:Name="Column" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="559,312,0,0" Width="555" Height="279">  
  37.             <Charting:ColumnSeries Margin="0" IndependentValuePath="Value" DependentValuePath="Count" Title="Journal Details" />  
  38.         </Charting:Chart>  
  39.     </Grid>  
  40. </Page>  
Step 6

Add the namespaces, mentioned below in Mainpage.xaml.cs for Academic Knowledge.
  1. using System.Net.Http;  
  2. using System.Threading.Tasks;  
  3. using Newtonsoft.Json.Linq;  
  4. using System.Collections.ObjectModel;  
  5. using System.Net;  
  6. 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.

  1. public class Interpretc {  
  2.     public string Title {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string output {  
  7.         get;  
  8.         set;  
  9.     }  
  10. }  
  11. private async void btnAcKnHist_Click(object sender, RoutedEventArgs e) {  
  12.         query = asbHisto.Text.Trim();  
  13.         var res = await interpret();  
  14.         for (int i = 0; i < res.Count(); i++) {  
  15.             Interpret wres = res.ElementAt(i);  
  16.             expr = wres.output;  
  17.         }  
  18.         var hist = await histogram();  
  19.         for (int i = 0; i < hist.Count(); i++) {  
  20.             Histogramc hh = hist.ElementAt(i);  
  21.             SearchResults.Add(new histocol {  
  22.                 hisco = hh  
  23.             });  
  24.             tblTotal.Text = hh.TotalCount;  
  25.         }  
  26.         (Column.Series[0] as ColumnSeries).ItemsSource = hist; // Column chart display  
  27.     }  
  28.     //Interpret Method  
  29. static async Task < IEnumerable < Interpretc >> interpret() {  
  30.     var iclient = new HttpClient();  
  31.     List < Interpretc > ire = new List < Interpretc > ();  
  32.     iclient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key""<Your Acadamic Knowledge API Key>");  
  33.     string count = "2";  
  34.     string Complete = "1";  
  35.     var inteSearchEndPoint = "https://api.projectoxford.ai/academic/v1.0/interpret?";  
  36.     var result = await iclient.GetAsync(string.Format("{0}query=papers by +{1}&complete={2}&count={3}", inteSearchEndPoint, WebUtility.UrlEncode(query), Complete, count));  
  37.     result.EnsureSuccessStatusCode();  
  38.     var json = await result.Content.ReadAsStringAsync();  
  39.     dynamic data = JObject.Parse(json);  
  40.     for (int i = 0; i < 1; i++) {  
  41.         ire.Add(new Interpretc {  
  42.             Title = data.interpretations[i].rules[i].name,  
  43.                 output = data.interpretations[i].rules[i].output.value  
  44.         });  
  45.     }  
  46.     return ire;  
  47. }  
Add the code, mentioned below with an Azure generated key for using the expression value from Interpret Method in evaluate method, as shown below.
  1. public class Histogramc {  
  2.     public string TotalCount {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Value {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string Count {  
  11.         get;  
  12.         set;  
  13.     }  
  14. }  
  15. public class histocol {  
  16.     public Histogramc hisco {  
  17.         get;  
  18.         set;  
  19.     }  
  20. }  
  21. public static string expr, query;  
  22. public ObservableCollection < histocol > SearchResults {  
  23.     get;  
  24.     set;  
  25. } = new ObservableCollection < histocol > ();  
  26. async Task < IEnumerable < Histogramc >> histogram() {  
  27.     List < Histogramc > hiscc = new List < Histogramc > ();  
  28.     var client = new HttpClient();  
  29.     client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key""<Your Acadamic Knowledge API Key>");  
  30.     string count = "4";  
  31.     string offset = "0";  
  32.     string model = "latest";  
  33.     string attributes = "Y,F.FN";  
  34.     var histoEndPoint = "https://api.projectoxford.ai/academic/v1.0/calchistogram?";  
  35.     var result = await client.GetAsync(string.Format("{0}expr={1}&model={2}&count={3}&offset={4}&attributes={5}", histoEndPoint, expr, model, count, offset, attributes));  
  36.     result.EnsureSuccessStatusCode();  
  37.     var json = await result.Content.ReadAsStringAsync();  
  38.     dynamic data = JObject.Parse(json);  
  39.     for (int i = 0; i < 4; i++) {  
  40.         hiscc.Add(new Histogramc {  
  41.             TotalCount = "Total Count : " + data.histograms[0].total_count,  
  42.                 Value = data.histograms[0].histogram[i].value,  
  43.                 Count = data.histograms[0].histogram[i].count  
  44.         });  
  45.     }  
  46.     return hiscc;  
  47. }  


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.
  1. Cognitive Service Academic Knowledge API - Evaluate Method Using UWP With Azure, XAML And C#
  2. Cognitive Service Bing Video Search API Using UWP With Azure, XAML And C#
  3. Cognitive Service Bing Image Search API Using UWP With Azure, XAML And C#
  4. Cognitive Service Bing News Search API Using UWP With Azure, XAML And C#
  5. Cognitive Service Bing AutoSuggest API Using UWP With Azure, XAML And C#
  6. Multiple People Emotions In UWP With Cognitive Service Emotion API, Azure, XAML And C#
  7. Cognitive Service Emotion API In UWP With Azure, XAML And C#
  8. Retrieving Face Attributes Using Cognitive Service Face API In UWP With Azure, XAML And C#
  9. Cognitive Service Face API, Using UWP With Azure, XAML And C#