HarmonyOS AI基础技术赋能之关键字获取

开发 OpenHarmony
关键字提取主要用于从新闻和邮件里提取出关键字,便于用户快速获取新闻和邮件的主题。关键字可以为有意义的实体,比如,人名、电影,也可以为非实体的关键词汇,如,上课、考研。

[[419347]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

引言

在实际应用开发中,时不时的会遇到AI领域相关的一些技术,本节旨在详细讲述关键字获取技术,关键字获取涉及各个领域中,如:游记摘要、新闻标签、杂志文刊等。所以对于HarmonyOS开发者而言,了解和掌握HarmonyOS AI领域相关技术是至关重要的,也是每一个HarmonyOS开发者的一项必不可少的专业技能。

功能介绍

关键字提取主要用于从新闻和邮件里提取出关键字,便于用户快速获取新闻和邮件的主题。关键字可以为有意义的实体,比如,人名、电影,也可以为非实体的关键词汇,如,上课、考研。

开发指南

1、使用NluClient静态类进行初始化,通过异步方式获取服务的连接

  1. NluClient.getInstance().init(context, new OnResultListener<Integer>(){ 
  2.       @Override 
  3.       public void onResult(Integer result){ 
  4.        // 初始化成功回调,在服务初始化成功调用该函数 
  5.       } 
  6.   }, true); 

2、调用获取关键词提取方法得到分析结果,同一个接口提供了同步和异步两个方法

同步:

  1. String requestData= "{number:2,body:'今天我们一起去上课吧',title:'一起去上课'}"
  2. ResponseResult respResult = NluClient.getInstance().getKeywords(requestData, NluRequestType.REQUEST_TYPE_LOCAL); 
  3. if (null != respResult){ 
  4.      // 获取接口返回结果,参考接口文档返回使用 
  5.      String result = respResult.getResponseResult(); 

异步:

  1. // 待分析文本 
  2. String requestData= "{number:2,body:'今天我们一起去上课吧',title:'一起去上课'}"
  3. // 调用接口 
  4. NluClient.getInstance().getKeywords(requestData, NluRequestType.REQUEST_TYPE_LOCAL,new OnResultListener<ResponseResult>(){ 
  5.     @Override 
  6.     public void onResult(ResponseResult respResult) 
  7.     { 
  8.     // 异步返回 
  9.     if(null != respResult && NluError.SUCCESS_RESULT == respResult.getCode()) 
  10.         { 
  11.         // 获取接口返回结果,参考接口文档返回使用 
  12.         String result = respResult.getResponseResult(); 
  13.         } 
  14.     } 
  15. }); 

3、使用结束调用destroy()方法释放进程资源

  1. NluClient.getInstance().destroy(this); 

示例代码

1、xml布局

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.   xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.   ohos:height="match_parent" 
  5.   ohos:orientation="vertical" 
  6.   ohos:width="match_parent"
  7.   <Text 
  8.     ohos:background_element="$graphic:background_ability_main" 
  9.     ohos:height="match_content" 
  10.     ohos:layout_alignment="horizontal_center" 
  11.     ohos:multiple_lines="true" 
  12.     ohos:text="$string:title" 
  13.     ohos:top_margin="50vp" 
  14.     ohos:text_size="50" 
  15.     ohos:width="match_content" 
  16.     /> 
  17.   <Text 
  18.     ohos:background_element="$graphic:background_ability_main" 
  19.     ohos:height="match_content" 
  20.     ohos:layout_alignment="horizontal_center" 
  21.     ohos:multiple_lines="true" 
  22.     ohos:left_margin="10vp" 
  23.     ohos:right_margin="10vp" 
  24.     ohos:text="body:'同步-今天我们一起去上课吧',title:'同步-一起去上课'-body:'异步-今天我们一起去上班吧',title:'异步-我们去上班'" 
  25.     ohos:top_margin="50vp" 
  26.     ohos:text_size="50" 
  27.     ohos:width="match_content" 
  28.     /> 
  29.   <Text 
  30.     ohos:background_element="$graphic:background_ability_main" 
  31.     ohos:height="match_content" 
  32.     ohos:id="$+id:text_content" 
  33.     ohos:layout_alignment="horizontal_center" 
  34.     ohos:multiple_lines="true" 
  35.     ohos:top_margin="50vp" 
  36.     ohos:left_margin="10vp" 
  37.     ohos:right_margin="10vp" 
  38.     ohos:text_size="50" 
  39.     ohos:width="match_content" 
  40.     /> 
  41.   <Text 
  42.     ohos:background_element="$graphic:background_ability_main" 
  43.     ohos:height="match_content" 
  44.     ohos:id="$+id:text_asynchronous" 
  45.     ohos:layout_alignment="horizontal_center" 
  46.     ohos:text="$string:asynchronous" 
  47.     ohos:top_margin="50vp" 
  48.     ohos:text_size="50" 
  49.     ohos:width="match_content" 
  50.     /> 
  51.   <Text 
  52.     ohos:background_element="$graphic:background_ability_main" 
  53.     ohos:height="match_content" 
  54.     ohos:id="$+id:text_synchronization" 
  55.     ohos:layout_alignment="horizontal_center" 
  56.     ohos:text="$string:synchronization" 
  57.     ohos:top_margin="50vp" 
  58.     ohos:text_size="50" 
  59.     ohos:width="match_content" 
  60.     /> 
  61. </DirectionalLayout> 

2、案例代码

  1. package com.example.keywords.slice; 
  2.  
  3. import com.example.keywords.ResourceTable; 
  4. import ohos.aafwk.ability.AbilitySlice; 
  5. import ohos.aafwk.content.Intent; 
  6. import ohos.agp.components.Component; 
  7. import ohos.agp.components.Component.ClickedListener; 
  8. import ohos.agp.components.Text; 
  9. import ohos.ai.nlu.NluClient; 
  10. import ohos.ai.nlu.NluRequestType; 
  11. import ohos.ai.nlu.ResponseResult; 
  12. import ohos.ai.nlu.util.NluError; 
  13.  
  14. public class MainAbilitySlice extends AbilitySlice implements ClickedListener { 
  15.  
  16.   private Text text_content; 
  17.  
  18.   @Override 
  19.   public void onStart(Intent intent) { 
  20.     super.onStart(intent); 
  21.     super.setUIContent(ResourceTable.Layout_ability_main); 
  22.     text_content = (Text) findComponentById(ResourceTable.Id_text_content); 
  23.     Text text_synchronization = (Text) findComponentById(ResourceTable.Id_text_synchronization); 
  24.     Text text_asynchronous = (Text) findComponentById(ResourceTable.Id_text_asynchronous); 
  25.     text_synchronization.setClickedListener(this); 
  26.     text_asynchronous.setClickedListener(this); 
  27.     // 使用NluClient静态类初始化,通过异步方式获取服务连接 
  28.     NluClient.getInstance().init(this, nulltrue); 
  29.   } 
  30.  
  31.   @Override 
  32.   public void onClick(Component component) { 
  33.     switch (component.getId()) { 
  34.       case ResourceTable.Id_text_synchronization: 
  35.         String requestData = "{number:3,body:'同步-今天我们一起去上课吧',title:'同步-一起去上课'}"
  36.         ResponseResult responseResult = NluClient.getInstance() 
  37.             .getKeywords(requestData, NluRequestType.REQUEST_TYPE_LOCAL); 
  38.         if (responseResult != null) { 
  39.           text_content.setText(responseResult.getResponseResult()); 
  40.         } 
  41.         break; 
  42.       case ResourceTable.Id_text_asynchronous: 
  43.         String rData = "{number:3,body:'异步-今天我们一起去上班吧',title:'异步-我们去上班'}"
  44.         NluClient.getInstance().getKeywords(rData, NluRequestType.REQUEST_TYPE_LOCAL, 
  45.                 responseResult1 -> { 
  46.                   if (responseResult1 != null && NluError.SUCCESS_RESULT == responseResult1.getCode()) { 
  47.                     getAbility().getUITaskDispatcher().syncDispatch(() -> text_content.setText(responseResult1.getResponseResult())); 
  48.                   } 
  49.                 }); 
  50.         break; 
  51.       default
  52.         break; 
  53.     } 
  54.   } 
  55.  
  56.   @Override 
  57.   protected void onBackground() { 
  58.     super.onBackground(); 
  59.     NluClient.getInstance().destroy(this); 
  60.   } 

实现效果

【软通动力】HarmonyOS AI基础技术赋能之关键字获取-鸿蒙HarmonyOS技术社区
【软通动力】HarmonyOS AI基础技术赋能之关键字获取-鸿蒙HarmonyOS技术社区

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-09-23 10:00:57

鸿蒙HarmonyOS应用

2021-09-13 15:14:01

鸿蒙HarmonyOS应用

2021-08-31 14:58:52

鸿蒙HarmonyOS应用

2011-06-27 15:08:15

SEO

2021-09-03 15:27:17

鸿蒙HarmonyOS应用

2021-01-05 10:26:50

鸿蒙Javafinal

2019-12-20 15:19:41

Synchroinze线程安全

2022-08-17 07:53:10

Volatile关键字原子性

2011-06-21 09:50:51

volatile

2009-12-18 11:37:54

Ruby关键字yiel

2009-11-26 19:24:54

PHP类CMS

2021-08-27 09:57:18

鸿蒙HarmonyOS应用

2021-04-18 07:58:22

SQL Server数据库Apply

2024-03-15 11:52:03

C++关键字编程

2023-11-10 09:29:30

MySQLExplain

2021-08-15 08:11:54

AndroidSynchronize关键字

2022-01-04 16:35:42

C++Protected关键字

2009-09-17 09:30:00

Linq LET关键字

2009-09-02 09:24:03

C# this关键字
点赞
收藏

51CTO技术栈公众号