Interpret Method Output Value To Evaluate Method Expression In Cognitive Service Academic Knowledge API

Before reading this article, please go through the following articles.

  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 Services available to 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 developers to easily add intelligent features. Microsoft Cognitive Services let you 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

With an Academic Knowledge API, we will be able to interpret the user queries for the academic intent and 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
    The interpret REST API takes an end user query string (i.e., a query entered by a user of your application) and returns formatted interpretations of user intent based n 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 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 do Cognitive Service Academic Knowledge API - Interpret Method output value is assigned to Evaluate Method expression value in Universal Windows Apps development, with Azure, XAML and Visual C#.

The following important tools are required for developing 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 suitable name to your app (UWPCogAcadInterpret) -> OK.

Visual studio

After choosing the target and minimum platform version that your Windows Universal Application will support, the Project creates App.xaml and MainPage.xaml.

Visual studio

Step 2

Add AutoSuggestBox control, set the name and text properties for Search.

Visual studio

Add a Button control, set the name and add the search icon for Find video.

Visual studio

Step 3

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="gvCogAcKnIn" ItemsSource="{Binding Source={StaticResource SearchResultsCol}}" SelectionMode="None" IsItemClickEnabled="False" HorizontalAlignment="Left" Height="433" Margin="71,233,0,0" VerticalAlignment="Top" Width="1117" Background="#FFF7FFFF">  
Visual studio

Add the following code for Grid View Item Template for Journal 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>  
Step 4

Add the Click event method for Search button.

Visual studio

Step 5

Open (double click) the file MainPage.xaml in the Solution Explorer and add the Newtonsoft.Json reference in the project. Right click your project (UWPCogAcadInterpret) and select "Manage NuGet Packages".

Visual studio

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

Visual studio

Reference is added to your project.

Visual studio

Note

Automatically, the following code will be generated in XAML code view, while we are done in the design view.
  1. <Page x:Class="UWPCogAcadInterpret.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UWPCogAcadInterpret" 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="296,83,0,0" TextWrapping="Wrap" Text="UWP Coginitive Service Academy Knowledge Interpret Method - Demo" VerticalAlignment="Top" FontWeight="Bold" FontSize="22" />  
  7.         <AutoSuggestBox x:Name="asbinter" HorizontalAlignment="Left" Margin="325,174,0,0" VerticalAlignment="Top" Width="523" FontSize="18" />  
  8.         <Button x:Name="btnAcKnInt" HorizontalAlignment="Left" Margin="856,174,0,0" VerticalAlignment="Top" Click="btnAcKnInt_Click">  
  9. <SymbolIcon Symbol="Find"></SymbolIcon>  
  10. </Button>  
  11.         <GridView x:Name="gvCogAcKnIn" ItemsSource="{Binding Source={StaticResource SearchResultsCol}}" SelectionMode="None" IsItemClickEnabled="False" HorizontalAlignment="Left" Height="433" Margin="71,233,0,0" VerticalAlignment="Top" Width="1117" Background="#FFF7FFFF">  
  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 following namespaces 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;  
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
  • Interpret Method
    https://api.projectoxford.ai/academic/v1.0/interpret?

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

Add the following code with Azure generated key for getting expression from Interpret Method.

  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 btnAcKnInt_Click(object sender, RoutedEventArgs e) {  
  12.         query = asbinter.Text.Trim();  
  13.         //interpret Method  
  14.         var res = await interpret();  
  15.         for (int i = 0; i < res.Count(); i++) {  
  16.             Interpretc iout = res.ElementAt(i);  
  17.             expr = iout.output; // Evalute expression value is assigned from interpret output  
  18.         }  
  19.         var eva = await eval();  
  20.         for (int i = 0; i < eva.Count(); i++) {  
  21.             evalc ev = eva.ElementAt(i);  
  22.             SearchResults.Add(new evalCol {  
  23.                 evco = ev  
  24.             });  
  25.         }  
  26.     }  
  27.     //Interpret Method  
  28. static async Task < IEnumerable < Interpretc >> interpret() {  
  29.     var iclient = new HttpClient();  
  30.     List < Interpretc > ire = new List < Interpretc > ();  
  31.     iclient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key""<Your Acadamic Knowledge API Key>");  
  32.     string count = "2";  
  33.     string Complete = "1";  
  34.     var inteSearchEndPoint = "https://api.projectoxford.ai/academic/v1.0/interpret?";  
  35.     var result = await iclient.GetAsync(string.Format("{0}query=papers by +{1}&complete={2}&count={3}", inteSearchEndPoint, WebUtility.UrlEncode(query), Complete, count));  
  36.     result.EnsureSuccessStatusCode();  
  37.     var json = await result.Content.ReadAsStringAsync();  
  38.     dynamic data = JObject.Parse(json);  
  39.     for (int i = 0; i < 1; i++) {  
  40.         ire.Add(new Interpretc {  
  41.             Title = data.interpretations[i].rules[i].name,  
  42.                 output = data.interpretations[i].rules[i].output.value  
  43.         });  
  44.     }  
  45.     return ire;  
  46. }  
  47.   
  48. Add the following code with azure generated key  
  49. for using expression value from Interpret Method in evaluate method,  
  50.     public class evalc {  
  51.         public string Title {  
  52.             get;  
  53.             set;  
  54.         }  
  55.         public string Year {  
  56.             get;  
  57.             set;  
  58.         }  
  59.         public string Citationcount {  
  60.             get;  
  61.             set;  
  62.         }  
  63.         public string Authorname {  
  64.             get;  
  65.             set;  
  66.         }  
  67.         public string AuthorID {  
  68.             get;  
  69.             set;  
  70.         }  
  71.     }  
  72. public class evalCol {  
  73.     public evalc evco {  
  74.         get;  
  75.         set;  
  76.     }  
  77. }  
  78. public sealed partial class MainPage: Page {  
  79.         public static string expr, query;  
  80.         public string s;  
  81.         public ObservableCollection < evalCol > SearchResults {  
  82.             get;  
  83.             set;  
  84.         } = new ObservableCollection < evalCol > ();  
  85.         //Evaluate Method  
  86.         async Task < IEnumerable < evalc >> eval() {  
  87.             List < evalc > evalcc = new List < evalc > ();  
  88.             var client = new HttpClient();  
  89.             client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key""<Your Acadamic Knowledge API Key>");  
  90.             string count = "10";  
  91.             string offset = "0";  
  92.             string model = "latest";  
  93.             string attributes = "Ti,Y,CC,AA.AuN,AA.AuId";  
  94.             var EvalEndPoint = "https://api.projectoxford.ai/academic/v1.0/evaluate?";  
  95.             var result = await client.GetAsync(string.Format("{0}expr={1}&model={2}&count={3}&offset={4}&attributes={5}", EvalEndPoint, expr, model, count, offset, attributes));  
  96.             result.EnsureSuccessStatusCode();  
  97.             var json = await result.Content.ReadAsStringAsync();  
  98.             dynamic data = JObject.Parse(json);  
  99.             for (int i = 0; i < 10; i++) {  
  100.                 evalcc.Add(new evalc {  
  101.                     Title = "Title : " + data.entities[i].Ti,  
  102.                         Year = "Year :" + data.entities[i].Y,  
  103.                         Citationcount = "Citation Count :" + data.entities[i].CC,  
  104.                         Authorname = "Authorname :" + data.entities[i].AA[0].AuN,  
  105.                         AuthorID = "AuthorID :" + data.entities[i].AA[0].AuId  
  106.                 });  
  107.             }  
  108.             return evalcc;  
  109.         }  
Visual studio

Step 8

Deploy your app in Local Machine. The output of the UWPCogAcadInterpret app is,

Visual studio

Summary

Now, you have successfully tested Cognitive Service Academic Knowledge API –Interpret Method output value is used to Evaluate Method expression 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#


Similar Articles