Cognitive Service Academic Knowledge API - Evaluate Method Using UWP With Azure, XAML And C#

Cognitive Service Academic Knowledge API - Evaluate Method using UWP with Azure, XAML AND C#

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

Microsoft Cognitive Services

Microsoft Cognitive Services (formerly Project Oxford) are a set of APIs, SDKs and the Services available to the developers to make their Applications more intelligent, engaging and discoverable. Microsoft Cognitive Services expands on Microsoft’s evolving portfolio of machine learning APIs and enables the developers to easily add intelligent features. Microsoft Cognitive Services allows you to build apps with powerful algorithms, using just a few lines of code. They work across devices and platforms such as iOS, Android and Windows, keep improving and are easy to set up.

Academic Knowledge API

Academic Knowledge API is able to interpret the user queries for academic intent and retrieve rich information from Microsoft Academic Graph (MAG). MAG knowledge base is a Web-scale heterogeneous entity graph, made of entities that model scholarly activities like field of study, author, institution, paper, venue and event.

Academic Knowledge API consists of three related REST endpoints.

Interpret

Interpret is a natural language user query string. It returns the annotated interpretations to enable rich search-box auto-completion experiences, which anticipates what the user is typing.

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 returned by a query expression, such as the distribution of citations by year for a given author.

Reading this article, you can learn how to use Cognitive Service Academic Knowledge API - evaluate method in Universal Windows apps development with Azure, XAML and Visual C#.

The important tools, mentioned below, 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 (UWPCogAcdSerEval)->OK.

UWP

After choosing the Target and minimum platform version your Windows Universal Application will support, the project creates App.xaml and MainPage.xaml.

UWP

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 (UWPCogAcdSerEval) and select Manage NuGet Packages.

UWP

For adding Newtonsoft.Json Reference, choose Browse and search Newtonsoft.Json. Select the package and install it.

UWP

Reference is added to your project

UWP

Step 3

Add TextBlock control, change the Name and text property for the title.

UWP

Add TextBlock control, set the Name, Text and Text Properties for expression string.

UWP

Add a Button control, set the Name and add the search icon for Find Academy info.

UWP

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>  
  4. Add a GridView Control and set the ItemSource properties  
  5. <GridView x:Name="gv" ItemsSource="{Binding Source={StaticResource SearchResultsCol}}" SelectionMode="None" IsItemClickEnabled="False" HorizontalAlignment="Left" Height="467" Margin="71,199,0,0" VerticalAlignment="Top" Width="1117" Background="#FFE7FDFD">  
UWP

Add the code, mentioned below for Grid View Item Template for News details.
  1. <GridView.ItemTemplate>  
  2.     <DataTemplate>  
  3.         <Border Background="#FF07FDF2" Width="1098" Height="123" 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 evco.Title}" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" />  
  15.                         <TextBlock TextWrapping="Wrap" Text="{Binding evco.Year}" Style="{StaticResource CaptionTextBlockStyle}" />  
  16.                         <TextBlock TextWrapping="Wrap" Text="{Binding evco.Citationcount}" Style="{StaticResource CaptionTextBlockStyle}" />  
  17.                         <TextBlock TextWrapping="Wrap" Text="{Binding evco.Authorname}" Style="{StaticResource CaptionTextBlockStyle}" />  
  18.                         <TextBlock TextWrapping="Wrap" Text="{Binding evco.AuthorID}" Style="{StaticResource CaptionTextBlockStyle}" />  
  19.                     </StackPanel>  
  20.                 </Grid>  
  21.             </Grid>  
  22.         </Border>  
  23.     </DataTemplate>  
  24. </GridView.ItemTemplate>  
UWP

Step 5

Add the Click event method for Search button.

UWP

Note

Automatically, the code, mentioned below will be generated in XAML code view, while we are done in the design view.
  1. <Page x:Class="UWPCogAcdSerEval.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UWPCogAcdSerEval" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 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="279,83,0,0" TextWrapping="Wrap" Text="UWP Cognitive Service Academic Search API Evaluate Method" VerticalAlignment="Top" FontWeight="Bold" Height="33" Width="672" FontSize="22" />  
  7.         <TextBlock x:Name="tblExpr" HorizontalAlignment="Left" Margin="215,165,0,0" TextWrapping="Wrap" Text="Expr String : Composite(AA.AuN=='jaime teevan')" VerticalAlignment="Top" Height="25" Width="477" FontSize="16" />  
  8.         <Button x:Name="btnAcdSerEval" HorizontalAlignment="Left" Margin="856,162,0,0" VerticalAlignment="Top" Click="btnAcdSerEval_Click">  
  9. <SymbolIcon Symbol="Find"></SymbolIcon>  
  10. </Button>  
  11.         <GridView x:Name="gv" ItemsSource="{Binding Source={StaticResource SearchResultsCol}}" SelectionMode="None" IsItemClickEnabled="False" HorizontalAlignment="Left" Height="467" Margin="71,199,0,0" VerticalAlignment="Top" Width="1117" Background="#FFE7FDFD">  
  12.             <GridView.ItemTemplate>  
  13.                 <DataTemplate>  
  14.                     <Border Background="#FF07FDF2" Width="1098" Height="123" Margin="8">  
  15.                         <Grid>  
  16.                             <Grid.ColumnDefinitions>  
  17.                                 <ColumnDefinition Width="auto" />  
  18.                                 <ColumnDefinition/>  
  19.                             </Grid.ColumnDefinitions>  
  20.                             <Grid Grid.Column="1" Margin="3,3,3,10">  
  21.                                 <Grid.RowDefinitions>  
  22.                                     <RowDefinition Height="auto" />  
  23.                                 </Grid.RowDefinitions>  
  24.                                 <StackPanel Margin="0,0,0,-143">  
  25.                                     <TextBlock TextWrapping="Wrap" Text="{Binding evco.Title}" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" />  
  26.                                     <TextBlock TextWrapping="Wrap" Text="{Binding evco.Year}" Style="{StaticResource CaptionTextBlockStyle}" />  
  27.                                     <TextBlock TextWrapping="Wrap" Text="{Binding evco.Citationcount}" Style="{StaticResource CaptionTextBlockStyle}" />  
  28.                                     <TextBlock TextWrapping="Wrap" Text="{Binding evco.Authorname}" Style="{StaticResource CaptionTextBlockStyle}" />  
  29.                                     <TextBlock TextWrapping="Wrap" Text="{Binding evco.AuthorID}" Style="{StaticResource CaptionTextBlockStyle}" />  
  30.                                 </StackPanel>  
  31.                             </Grid>  
  32.                         </Grid>  
  33.                     </Border>  
  34.                 </DataTemplate>  
  35.             </GridView.ItemTemplate>  
  36.         </GridView>  
  37.     </Grid>  
  38. </Page>  
Step 6

Add the namespaces, mentioned below in Mainpage.xaml.cs for Bing Video search,
  1. using System.Net.Http;  
  2. using System.Threading.Tasks;  
  3. using Newtonsoft.Json.Linq;  
  4. using System.Collections.ObjectModel;  
Step 7

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 is mentioned below.

https://api.projectoxford.ai/academic/v1.0/evaluate?

Add the code, mentioned below with Azure generated key.
  1. public class evalc {  
  2.     public string Title {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Year {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string Citationcount {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public string Authorname {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public string AuthorID {  
  19.         get;  
  20.         set;  
  21.     }  
  22. }  
  23. public class evalCol {  
  24.     public evalc evco {  
  25.         get;  
  26.         set;  
  27.     }  
  28. }  
  29. public ObservableCollection < evalCol > SearchResults {  
  30.     get;  
  31.     set;  
  32. } = new ObservableCollection < evalCol > ();  
  33. private async void btnAcdSerEval_Click(object sender, RoutedEventArgs e) {  
  34.     var eva = await eval();  
  35.     for (int i = 0; i < eva.Count(); i++) {  
  36.         evalc ev = eva.ElementAt(i);  
  37.         SearchResults.Add(new evalCol {  
  38.             evco = ev  
  39.         });  
  40.     }  
  41. }  
  42. async Task < IEnumerable < evalc >> eval() {  
  43.     https: //api.projectoxford.ai/academic/v1.0/evaluate?  
  44.         List < evalc > evalcc = new List < evalc > ();  
  45.     var client = new HttpClient();  
  46.     client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key""<Your Key>");  
  47.     var result = await client.GetAsync(string.Format("https://api.projectoxford.ai/academic/v1.0/evaluate?expr=Composite(AA.AuN=='jaime teevan')&model=latest&count=10&offset=0&attributes=Ti,Y,CC,AA.AuN,AA.AuId"));  
  48.     result.EnsureSuccessStatusCode();  
  49.     var json = await result.Content.ReadAsStringAsync();  
  50.     dynamic data = JObject.Parse(json);  
  51.     for (int i = 0; i < 10; i++) {  
  52.         evalcc.Add(new evalc {  
  53.             Title = "Title : " + data.entities[i].Ti,  
  54.                 Year = "Year :" + data.entities[i].Y,  
  55.                 Citationcount = "Citation Count :" + data.entities[i].CC,  
  56.                 Authorname = "Authorname :" + data.entities[i].AA[0].AuN,  
  57.                 AuthorID = "AuthorID :" + data.entities[i].AA[0].AuId  
  58.         });  
  59.     }  
  60.     return evalcc;  
  61. }  
UWP

Step 8

Deploy your app in the local machine and the output of UWPCogAcdSerEvalaApp is mentioned below.

UWP

Summary

Now, you have successfully tested Cognitive Service Academic Knowledge API - evaluation method, using Azure, XAML and C# with UWP environment.

If you are interested, please read the UWP Cognitive Service articles, mentioned below.

 

  1.  Cognitive Service Bing Video Search API Using UWP With Azure, XAML And C#
  2. Cognitive Service Bing Image Search API Using UWP With Azure, XAML And C#
  3. Cognitive Service Bing News Search API Using UWP With Azure, XAML And C#
  4. Cognitive Service Bing AutoSuggest API Using UWP With Azure, XAML And C#
  5. Multiple People Emotions In UWP With Cognitive Service Emotion API, Azure, XAML And C#
  6. Cognitive Service Emotion API In UWP With Azure, XAML And C#
  7. Retrieving Face Attributes Using Cognitive Service Face API In UWP With Azure, XAML And C#
  8. Cognitive Service Face API, Using UWP With Azure, XAML And C#