HarmonyOS AI基礎技術賦能之關鍵字獲取
引言
在實際應用開發(fā)中,時不時的會遇到AI領域相關的一些技術,本節(jié)旨在詳細講述關鍵字獲取技術,關鍵字獲取涉及各個領域中,如:游記摘要、新聞標簽、雜志文刊等。所以對于HarmonyOS開發(fā)者而言,了解和掌握HarmonyOS AI領域相關技術是至關重要的,也是每一個HarmonyOS開發(fā)者的一項必不可少的專業(yè)技能。
功能介紹
關鍵字提取主要用于從新聞和郵件里提取出關鍵字,便于用戶快速獲取新聞和郵件的主題。關鍵字可以為有意義的實體,比如,人名、電影,也可以為非實體的關鍵詞匯,如,上課、考研。
開發(fā)指南
1、使用NluClient靜態(tài)類進行初始化,通過異步方式獲取服務的連接
- NluClient.getInstance().init(context, new OnResultListener<Integer>(){
- @Override
- public void onResult(Integer result){
- // 初始化成功回調,在服務初始化成功調用該函數
- }
- }, true);
2、調用獲取關鍵詞提取方法得到分析結果,同一個接口提供了同步和異步兩個方法
同步:
- String requestData= "{number:2,body:'今天我們一起去上課吧',title:'一起去上課'}";
- ResponseResult respResult = NluClient.getInstance().getKeywords(requestData, NluRequestType.REQUEST_TYPE_LOCAL);
- if (null != respResult){
- // 獲取接口返回結果,參考接口文檔返回使用
- String result = respResult.getResponseResult();
- }
異步:
- // 待分析文本
- 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();
- }
- }
- });
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="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>
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靜態(tài)類初始化,通過異步方式獲取服務連接
- NluClient.getInstance().init(this, null, true);
- }
- @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);
- }
- }
實現效果

