Development of HarmonyOS learning path - AI function development (keyword extraction)

Keyword Extraction Overview

Daily life is full of various information, which is ever-changing. As a carrier of information transmission, text language also faces the problem of mixing useful information and useless information. Keyword extraction helps users quickly extract key information and core content from numerous text information, saving time and improving efficiency.

Operation Mechanism

The Keyword Extraction API provides an interface for extracting keywords. Through this API, the core content that the text wants to express can be extracted from a large amount of information, which can be entities with specific meanings, such as: names of people, places, movies, etc. It can also be some basic but critical words in the text. Through this API, the extracted keywords can be sorted from high to low according to their weight in the text. The higher the ranking, the higher the weight, and the more accurate the extraction of the core content of the text.

Constraints and Restrictions

  • Currently only Chinese context is supported.
  • Keyword extraction The title text is limited to 100 characters, the body text is limited to 5,000 characters, and the number of keywords extracted is less than or equal to 20. The text is in UTF-8 format. If the format is wrong, no error will be reported, but the analysis result will be incorrect.
  • Engine supports simultaneous access by multiple users, but does not support concurrent invocation of the same feature by the same user. If a feature is called multiple times by the same process at the same time, a system busy error will be returned; if the same feature is called by different processes, only one process is processing business at the same time, and other processes enter the queue.

Keyword Extraction Development

scene introduction

  • travel summary

    This API can extract key words that can reflect the core ideas from long texts of travel notes edited by users. The keywords are output in order of weight from key to relatively non-key, helping users quickly extract key information from long texts and quickly give travel notes Select the corresponding label to upload the key information of the article.

  • news tab

    When users browse articles, they can use this API to extract key information and generate corresponding tags if they want to collect the articles they are interested in. When users want to continue to browse the favorite articles next time, they can quickly understand the core content of the articles through the generated tags without opening the articles.

Interface Description

It can be used to extract keywords from news and emails, so that users can quickly obtain the topics of news and emails. Keywords can be meaningful entities, such as names and movies, or non-entity key words, such as classes and postgraduate entrance exams.

main interface

Table 1  Keyword extraction main interface

interface name

Functional description

ResponseResult getKeywords(String requestData, int requestType);

The synchronous interface can be used to extract keywords from news and emails so that users can quickly obtain the topics of news and emails.

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

The asynchronous interface can be used to extract keywords from news and emails so that users can quickly obtain the topics of news and emails.

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

The initialization service must be called to bind the NLU service. When using multiple NLU service interfaces, it only needs to be called once, which is an asynchronous interface.

void destroy(Context context);

The unbinding service needs to be unbound when it is destroyed after initialization, so that resources can be released in time.

Interface input value description

The JSON format of requestData is as follows:

parameter name

Is it required?

type

illustrate

title

false

String

The title of the article, the number of characters does not exceed 100.

body

true

String

The text of the article, the number of characters does not exceed 5000.

number

true

int

The number of keywords to be extracted, the value is between 1 and 20.

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

requestType indicates the request type, and the value can be selected from ohos.ai.nlu.NluRequestType.

type

illustrate

static final int

REQUEST_TYPE_LOCAL=0, local request.

Interface return value description

responseResult in ResponseResult is a JSON string, reflecting the result of keyword extraction:

parameter name

Is it required?

type

illustrate

code

true

int

result code. Values ​​are:

  • 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

error message

keywords

true

JSONArray

A list of keywords, returned in order of weight

Reference examples are as follows:

{
    "code":0,
    "message":"成功",
    "keywords":[
        "上课",
        "一起"
    ]
}

development steps

When using the keyword extraction API, add related classes that implement keyword extraction 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 keyword extraction method to get the analysis results. The same interface provides two methods, synchronous and asynchronous, and developers can choose according to their needs.

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

title is an optional parameter, if not filled, only the body will be analyzed.

  • asynchronous
// 待分析文本
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();
        }
    }
});

Call the destroy() method at the end to release process resources. If you continue to use it, it is recommended to release it at the end of the process. After release, you need to repeat the second step to use it again.

Guess you like

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