深圳幻海软件技术有限公司 欢迎您!

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

2023-02-27

想了解更多内容,请访问:51CTO和华为官方合作共建的鸿蒙技术社区https://harmonyos.51cto.com引言在实际应用开发中,时不时的会遇到AI领域相关的一些技术,本节旨在详细讲述关键字获取技术,关键字获取涉及各个领域中,如:游记摘要、新闻标签、杂志文刊等。所以对于HarmonyOS

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

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

https://harmonyos.51cto.com

引言

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

功能介绍

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

开发指南

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

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

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

同步:

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

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

异步:

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

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

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

示例代码

1、xml布局

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

2、案例代码

package com.example.keywords.slice; 
 
import com.example.keywords.ResourceTable; 
import ohos.aafwk.ability.AbilitySlice; 
import ohos.aafwk.content.Intent; 
import ohos.agp.components.Component; 
import ohos.agp.components.Component.ClickedListener; 
import ohos.agp.components.Text; 
import ohos.ai.nlu.NluClient; 
import ohos.ai.nlu.NluRequestType; 
import ohos.ai.nlu.ResponseResult; 
import ohos.ai.nlu.util.NluError; 
 
public class MainAbilitySlice extends AbilitySlice implements ClickedListener { 
 
  private Text text_content; 
 
  @Override 
  public void onStart(Intent intent) { 
    super.onStart(intent); 
    super.setUIContent(ResourceTable.Layout_ability_main); 
    text_content = (Text) findComponentById(ResourceTable.Id_text_content); 
    Text text_synchronization = (Text) findComponentById(ResourceTable.Id_text_synchronization); 
    Text text_asynchronous = (Text) findComponentById(ResourceTable.Id_text_asynchronous); 
    text_synchronization.setClickedListener(this); 
    text_asynchronous.setClickedListener(this); 
    // 使用NluClient静态类初始化,通过异步方式获取服务连接 
    NluClient.getInstance().init(this, nulltrue); 
  } 
 
  @Override 
  public void onClick(Component component) { 
    switch (component.getId()) { 
      case ResourceTable.Id_text_synchronization: 
        String requestData = "{number:3,body:'同步-今天我们一起去上课吧',title:'同步-一起去上课'}"
        ResponseResult responseResult = NluClient.getInstance() 
            .getKeywords(requestData, NluRequestType.REQUEST_TYPE_LOCAL); 
        if (responseResult != null) { 
          text_content.setText(responseResult.getResponseResult()); 
        } 
        break; 
      case ResourceTable.Id_text_asynchronous: 
        String rData = "{number:3,body:'异步-今天我们一起去上班吧',title:'异步-我们去上班'}"
        NluClient.getInstance().getKeywords(rData, NluRequestType.REQUEST_TYPE_LOCAL, 
                responseResult1 -> { 
                  if (responseResult1 != null && NluError.SUCCESS_RESULT == responseResult1.getCode()) { 
                    getAbility().getUITaskDispatcher().syncDispatch(() -> text_content.setText(responseResult1.getResponseResult())); 
                  } 
                }); 
        break; 
      default
        break; 
    } 
  } 
 
  @Override 
  protected void onBackground() { 
    super.onBackground(); 
    NluClient.getInstance().destroy(this); 
  } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.

实现效果



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

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

https://harmonyos.51cto.com