HarmonyOS AI基础技术赋能之实体识别 原创 精华

发布于 2021-8-31 14:47
浏览
18收藏

HarmonyOS AI基础技术赋能之实体识别-开源基础软件社区引言

在实际应用开发中,时不时的会遇到AI领域相关的一些技术,本节主要详细讲述一下实体识别技术,实体识别可能涉及在各领域中,如:双指按压文本弹出卡片、实体信息高亮等。对于HarmonyOS开发者而言,也需要了解和掌握HarmonyOS AI领域相关技术能力。

功能介绍

实体识别主要是提供识别文本中具有特定意义实体的能力,包含电影、电视剧、综艺、动漫、单曲、专辑、图书、火车车次、航班号、球队、人名、快递单号、电话号码、url、邮箱、联赛、时间、地点(包含酒店、餐馆、景点、学校、道路、省、市、县、区、镇等)、验证码。

指南

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

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

2、调用实体识别的接口,获取分析结果,同一个接口提供了同步和异步两个方法

同步:

String requestData= "{text:'我要看电影魔兽',module:'movie'}"; // module为可选参数,如果不设置该参数,则默认分析所有实体
ResponseResult respResult = NluClient.getInstance().getEntity(requestData, NluRequestType.REQUEST_TYPE_LOCAL);
if (null != respResult && NluError.SUCCESS_RESULT == respResult.getCode()) {
    // 获取接口返回结果,参考接口文档返回使用
    String result = respResult.getResponseResult();
}

异步:

String requestData= "{text:'我要看电影魔兽',module:'movie'}"; // module为可选参数,如果不设置该参数,则默认分析所有实体
// 调用接口
NluClient.getInstance().getEntity(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();
        }
    }
});

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

 NluClient.getInstance().destroy(this);

示例代码

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="{text:'我要看电影魔兽',module:'movie'}-{text:'我的手机号码是18229044325',module:'phoneNum'}"
    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>

2、案例代码

package com.example.entityidentfication.slice;
import com.example.entityidentfication.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.OnResultListener;
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, null, true);
  }

  @Override
  public void onClick(Component component) {
    switch (component.getId()) {
      case ResourceTable.Id_text_synchronization:
        String requestData= "{text:'我要看电影魔兽',module:'movie'}"; // module为可选参数,如果不设置该参数,则默认分析所有实体
        ResponseResult responseResult = NluClient.getInstance().getEntity(requestData, NluRequestType.REQUEST_TYPE_LOCAL);
        if (responseResult != null) {
          text_content.setText(responseResult.getResponseResult());
        }
        break;
      case ResourceTable.Id_text_asynchronous:
        String rData= "{text:'我的手机号码是18229044325',module:'phoneNum'}";
        NluClient.getInstance().getEntity(rData,
            NluRequestType.REQUEST_TYPE_LOCAL, responseResult1 -> {
              if (responseResult1 != null && NluError.SUCCESS_RESULT == responseResult1.getCode()) {
                getAbility().getUITaskDispatcher().syncDispatch(new Runnable() {
                  @Override
                  public void run() {
                    text_content.setText(responseResult1.getResponseResult());
                  }
                });
              }
            });
        break;
      default:
        break;
    }
  }

  @Override
  protected void onBackground() {
    super.onBackground();
    NluClient.getInstance().destroy(this);
  }
}

实现效果

HarmonyOS AI基础技术赋能之实体识别-开源基础软件社区

更多原创内容请关注:软通动力HarmonyOS学院

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
19
收藏 18
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐