Development of HarmonyOS learning path - AI function development (entity recognition)

Entity Recognition Overview

Entity recognition can extract entities with specific meaning from natural language, and complete a series of related operations and functions such as search on this basis.

Entity recognition covers a wide range, which can meet the needs of entity recognition in daily development and make the application experience better. The recognition accuracy is high, and the entity information can be accurately extracted, which has a key impact on the information-based follow-up services of the application.

constraints

  • Currently only Chinese context is supported.
  • The entity recognition text is limited to 500 characters, and if the character limit is exceeded, a parameter error will be returned; the text must be in UTF-8 format, and an error will not be reported if the format is wrong, but it will cause an error in the analysis result.
  • Engine supports simultaneous access by multiple users, but does not support concurrent calls of the same feature by the same user. If a feature is called multiple times by the same process at the same time, it will return a system busy error; if different processes call the same feature, only one process will be processed at the same time. business, other processes enter the queue to queue.

Entity Recognition Development

scene introduction

  • Two-finger press text pop-up card

    Based on the entity content contained in the text, such as celebrities, movies, TV series, etc. Press with two fingers to quickly pop up the card introduction information corresponding to the entity. Allow users to easily and quickly obtain the information they want to know.

  • Entity Information Highlighting

    Highlight the relevant entity information and set the quick operation entry. For example, if the phone number in the text message is highlighted, the user can directly dial the number.

Interface Description

Entity recognition provides the ability to identify entities with specific meanings in text, including movies, TV dramas, variety shows, animations, singles, albums, books, train numbers, flight numbers, teams, names, courier numbers, phone numbers, urls, email addresses , league, time, location (including hotels, restaurants, scenic spots, schools, roads, provinces, cities, counties, districts, towns, etc.), verification codes.

main interface

interface name

describe

void init(Context context, OnResultListener<Integer> listener, boolean isLoadModel);

Initialize the NLU service. Before calling functional interfaces such as NLU, you need to call this interface first, and then call the NLU functional interface after obtaining the callback result in the onResult(T) method of OnResultListener. The developer passes in the listener parameter as a callback to wait for the calling process and result of the NLU functional interface.

ResponseResult getEntity(String requestData, int requestType);

Synchronously identify entities with specific meaning in text, including movies, TV dramas, variety shows, animations, singles, albums, books, train numbers, flight numbers, teams, names, courier numbers, phone numbers, urls, email addresses, League, time, location (including hotels, restaurants, attractions, schools, roads, provinces, cities, counties, districts, towns, etc.), verification codes.

ResponseResult getEntity(final String requestData, final int requestType, final OnResultListener<ResponseResult> listener);

Asynchronously identify entities with specific meaning in text, including movies, TV dramas, variety shows, animations, singles, albums, books, train numbers, flight numbers, teams, names, courier numbers, phone numbers, urls, email addresses, League, time, location (including hotels, restaurants, attractions, schools, roads, provinces, cities, counties, districts, towns, etc.), verification codes.

void destroy(Context context);

Cancel all NLU tasks and destroy the NLU engine service. After calling this method, the NLU service can no longer be used. If you need to reuse the NLU service, you need to call init(Context, OnResultListener<Integer>, boolean)} again to initialize the NLU service.

Interface input value description

requestType indicates the request type, which is defined by the NluRequestType class as follows:

enumerated type

enumeration value

static final int

REQUEST_TYPE_LOCAL = 0 local request

The JSON format of requestData is as follows:

parameter name

Is it required?

type

illustrate

text

true

String

The text to be analyzed is encoded in UTF-8, limited to 500 characters.

module

false

String

Define the entities that need to be analyzed. By default, all entities will be analyzed.

To analyze an entity, pass the entity key value, for example: only need to analyze the time entity, pass "time". You can pass more than one, indicating to analyze multiple entities, separated by a comma ",", for example: to analyze time and location, pass "time,location".

取值范围:name、time、location、phoneNum、email、url、movie、tv、anime、league、team、trainNo、flightNo、expressNo、idNo、verificationCode、app、carNo

callPkg

false

String

caller name.

callType

false

int

Caller type:

  • 0: normal application (default)
  • 1: Quick App

callVersion

callState

false

false

String

int

The caller version number.

Caller state:

  • -1: unknown (default)
  • 0: foreground
  • 1: background

Interface return value description

The return value ResponseResult is a JSONObject string, reflecting the result of entity recognition:

parameter name

Is it required?

type

illustrate

code

true

int

The result code of the entity recognition. Values ​​include:
  • 0: success
  • 1: The system is initializing
  • 2: Parameter error
  • 3: The system is busy
  • 4: System exception
  • 5: Task timed out
  • 6: Other errors

message

true

String

Description of the result of entity recognition.

entity

false

JSONObject

Entity analysis results.

development steps

When using entity recognition-related interfaces, you need to add entity-recognition-related classes to the project.

import ohos.ai.nlu.ResponseResult; // 接口返回的结果类
import ohos.ai.nlu.NluClient; // 接口服务类
import ohos.ai.nlu.NluRequestType; // 接口调用时传入的类型
import ohos.ai.nlu.OnResultListener; // 异步函数,执行成功的回调结果类
import ohos.ai.nlu.util.NluError;// 接口返回码

Use the NluClient static class to initialize and obtain the service connection asynchronously.

  • context: application context information, should be an instance or subclass instance of ohos.aafwk.ability.Ability or ohos.aafwk.ability.AbilitySlice.
  • listener: the callback of the initialization result, null can be passed.
  • isLoadModel: Whether to load the model, if true, the model will be loaded during initialization; if false, the model will not be loaded during initialization.
NluClient.getInstance().init(context, new OnResultListener<Integer>(){
        @Override
        public void onResult(Integer result){
         // 初始化成功回调,在服务初始化成功调用该函数
        }
}, true);

Call the entity recognition interface to obtain the analysis results.

  • Entity recognition in a synchronous manner:
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();
}
  • Entity recognition is performed asynchronously:
// 待分析文本
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();
        }
    }
});

Destroy the NLU service.

NluClient.getInstance().destroy();

Guess you like

Origin blog.csdn.net/weixin_47094733/article/details/131429162