您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:使用 C# 构建实体链接应用Tutorial: Build an Entity Linking app with C#

Microsoft 实体链接是一款自然语言处理工具,可用于分析文本,并将命名实体链接到知识库中的相关实体。Microsoft's Entity Linking is a natural language processing tool to analyze text and link named-entities to relevant entries in a knowledge base.

本教程通过将实体链接客户端库用作 NuGet 包来介绍实体链接。This tutorial explores entity linking by using the Entity Linking Client Library as a NuGet package.

先决条件Prerequisites

  • Visual Studio 2015Visual Studio 2015
  • Microsoft 认知服务 API 密钥A Microsoft Cognitive Services API Key
  • 获取客户端库和示例Get the client library and example
  • Microsoft 实体链接 NuGet 包Microsoft Entity Linking NuGet Package

可以通过 SDK 下载实体链接智能服务 API 客户端库。You may download the Entity Linking Intelligence Service API Client Library via SDK. 必须将下载的 zip 文件解压缩到选定文件夹,许多用户选择解压缩到 Visual Studio 2015 文件夹。The downloaded zip file needs to be extracted to a folder of your choice, many users choose the Visual Studio 2015 folder.

步骤 1:订阅实体链接智能服务并获取密钥Step 1: Subscribe to Entity Linking Intelligence Service and get your key

必须先注册 API 密钥,然后才能使用实体链接智能服务。Before using Entity Linking Intelligence Service, you must sign up for an API key. 请参阅订阅See Subscriptions. 主密钥和辅助密钥都可用于本教程。Both the primary and secondary key can be used in this tutorial.

步骤 2:在 Visual Studio 中新建项目 Step 2: Create a new project in Visual Studio

接下来,先在 Visual Studio 中新建项目。Let’s start by creating a new project in Visual Studio. 首先,通过“开始”菜单启动 Visual Studio 2015。First, launch Visual Studio 2015 from the Start Menu. 然后,依次选择“已安装 → 模板 → Visual C# → Windows 通用 → 空白应用”作为项目模板,从而新建项目:Then, create a new project by selecting Installed → Templates → Visual C# → Windows Universal → Blank App for your project template:

创建通用应用

步骤 3:向项目添加实体链接 NuGet 包Step 3: Add the Entity Linking NuGet Package to your project

将认知服务的实体链接发布为 NuGet.org 包。必须先安装此包,然后才能使用它。Entity Linking of Cognitive Services is released as a NuGet.org package and needs to be installed before you can use it. 若要将它添加到项目中,请转到“解决方案资源管理器”选项卡,右键单击项目,并选择“管理 Nuget 包”。To add it to your project, go to the Solution Explorer tab, right click your project, and select Manage Nuget Packages.

首先,在“NuGet 包管理器”窗口中的右上角,选择“NuGet.org”作为“包源”。First, in the NuGet Package Manager window, select NuGet.org as your Package Source in the upper right corner. 选择左上角中的“浏览”,并在搜索框中键入“ProjectOxford.EntityLinking”。Select Browse in the upper left corner and in the search box type “ProjectOxford.EntityLinking”. 选择“Microsoft.ProjectOxford.EntityLinking”NuGet 包,并单击“安装”。Select the Microsoft.ProjectOxford.EntityLinking NuGet package and click Install.

接下来,搜索并安装“Newtonsoft.Json”。Next, search for Newtonsoft.Json and install. 如果系统提示查看更改,请单击“确定”。If you are prompted to review changes, click OK. 如果看到 EntityLinking 许可条款,请单击“我接受”。If you are presented with the EntityLinking license terms, click I Accept.

实体链接现已安装为应用的一部分。Entity Linking is now installed as part of your application. 若要确认这一点,可检查解决方案资源管理器内的项目中是否显示 Microsoft.ProjectOxford.EntityLinking 参考。You can confirm this by checking that the** Microsoft.ProjectOxford.EntityLinking** reference is present as part of your project in Solution Explorer.

项目中包含的 nuget 库

步骤 4:向应用的 XAML 添加输入和输出文本块Step 4: Add an input and output text block to your app’s XAML

导航到“解决方案资源管理器”中的 MainPage.xaml,并双击此文件,在新窗口中打开它。Navigate to MainPage.xaml in Solution Explorer, then double click the file which will open it in a new window. 为方便起见,可以双击“设计器”选项卡中的“XAML”按钮,这会隐藏“可视化设计器”,并保留代码视图的所有空间。For convenience, you can double click on the XAML button in the Designer tab, this will hide the Visual Designer and reserve all of the space for the code view.

项目中包含的 nuget 库

作为文本服务,功能可视化的最佳方法是,创建输入和输出文本块。As a text service, the best way to visualize the functionality is creating an input and an output text block. 为此,请在“网格”中添加下列 XAML。To do this, add the following XAML in the Grid. 此代码添加以下三个组件:输入文本框、输出文本块和“开始”按钮。This code adds three components, an input text box, an output text block, and a start button.

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
   <Grid.RowDefinitions>
       <RowDefinition Height="*"/>
       <RowDefinition Height="*" />
       <RowDefinition Height="50" />
   </Grid.RowDefinitions>
   <TextBox x:Name="inputBox" Grid.Row="0" TextWrapping="Wrap" Text="Enter a paragraph" Margin="10" AcceptsReturn="True" />
   <TextBlock x:Name="outputBlock" Grid.Row="1" TextWrapping="Wrap" Text="Result will be here" Margin="10" />
   <Button x:Name="button" Grid.Row="2" Content="Get Result" />
</Grid>

步骤 5:继续添加实体链接智能服务Step 5: Proceed to add Entity Linking Intelligence Service

现已创建用户界面。The user interface is now created. 必须先添加按钮单击处理程序,然后才能使用实体链接服务。Before using the Entity Linking service, we need to add the button-Click handler. 在“解决方案资源管理器”中,打开“MainPage.xaml”。Open MainPage.xaml from Solution Explorer. 在按钮末尾处添加 button_Click 处理程序。Add a button_Click handler in the end of the button.

<Button x:Name="button" Grid.Row="2" Content="Get Result" Click="button_Click" />

必须在代码中实现按钮单击处理程序。A button-Click handler needs to be implemented in the code. 在“解决方案资源管理器”中,打开“MainPage.xaml.cs”,以实现按钮单击处理程序。Open MainPage.xaml.cs from Solution Explorer to implement the button-Click. EntityLinkingServiceClient 是用于检索实体链接响应的包装器。The EntityLinkingServiceClient is a wrapper to retrieve Entity Linking responses. EntityLinkingServiceClient 的构造函数参数是认知服务订阅密钥。The constructor argument of EntityLinkingServiceClient is the Cognitive Services subscription key. 粘贴在“第 1 步”中获取的订阅密钥,以调用实体链接服务。Paste in the subscription key you got in Step 1 to call the Entity Linking service.

下面的示例代码使用实体链接服务在响应中添加“wikipediaId”。Below is example code, which adds the "wikipediaId" to the response by using Entity Linking Service.

private async void button_Click(object sender, RoutedEventArgs e)
{
   var text = this.inputBox.Text;
   var client = new EntityLinkingServiceClient("Your subscription key","https://api.labs.cognitive.microsoft.com");
   var linkResponse = await client.LinkAsync(text);
   var result = string.Join(", ", linkResponse.Select(i => i.WikipediaID).ToList());
   this.outputBlock.Text = result;
}

现在可运行你的首个自然语言处理实体链接应用。Now you are ready to run your first natural language processing Entity Linking App. 按 F5 键编译和发布应用。Press the F5 key to compile and launch the application. 将文本片段或段落粘贴到输入框中。Paste text snippets or paragraphs into the input box. 按“获取结果”按钮,并观察输出块中确定的实体。Press the "Get Result" button and observe the identified entities in the output block.

UWP 示例结果

摘要Summary

本教程介绍了如何只使用几行 C# 和 XAML 代码,即可创建利用实体链接智能服务客户端库的应用。In this tutorial you’ve learned how to create an application to leverage Entity Linking Intelligence Service Client Library with just a few lines of C# and XAML code.