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

软通动力HOS
发布于 2021-8-31 14:47
1.6w浏览
18收藏

HarmonyOS AI基础技术赋能之实体识别-鸿蒙开发者社区引言

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

功能介绍

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

指南

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= "{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();
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

异步:

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();
        }
    }
});
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

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="{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>
  • 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.
  • 62.

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);
  }
}
  • 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.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.

实现效果

HarmonyOS AI基础技术赋能之实体识别-鸿蒙开发者社区

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

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
19
收藏 18
回复
举报
19
7
18
7条回复
按时间正序
/
按时间倒序
软通小精灵
软通小精灵

真是爆发

回复
2021-8-31 15:30:01
软通田可辉
软通田可辉

这两天更新真快

回复
2021-8-31 15:40:38
芒果爱学习
芒果爱学习

加油加油!!

回复
2021-8-31 15:46:23
小梁学鸿蒙
小梁学鸿蒙

软通老师们加油

回复
2021-9-2 09:28:37
董昱
董昱

这个厉害了,赞一个

回复
2021-9-6 10:24:59
软通动力HOS
软通动力HOS 回复了 董昱
这个厉害了,赞一个

感谢支持

回复
2021-9-6 11:00:57
鸿联
鸿联

HarmonyOS  AI  赞!

回复
2021-9-6 11:15:03


回复
    相关推荐