想了解更多内容, 请访问:
51CTO 和华为官方战略合作共建的鸿蒙技术社区
https://harmonyos.51cto.com
在实际应用开发中, 时不时的会遇到 AI 领域相关的一些技术, 本节旨在详细讲述关键字获取技术, 关键字获取涉及各个领域中, 如: 游记摘要, 新闻标签, 杂志文刊等. 所以对于 HarmonyOS 开发者而言, 了解和掌握 HarmonyOS AI 领域相关技术是至关重要的, 也是每一个 HarmonyOS 开发者的一项必不可少的专业技能.
功能介绍
关键字提取主要用于从新闻和邮件里提取出关键字, 便于用户快速获取新闻和邮件的主题. 关键字可以为有意义的实体, 比如, 人名, 电影, 也可以为非实体的关键词汇, 如, 上课, 考研.
开发指南
1, 使用 NluClient 静态类进行初始化, 通过异步方式获取服务的连接
- NluClient.getInstance().init(context, new OnResultListener(){
- @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(){
- @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 布局
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:orientation="vertical"
- ohos:width="match_parent">
- 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"
- />
- 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"
- />
- 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"
- />
- 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"
- />
- 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"
- />
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 静态类初始化, 通过异步方式获取服务连接
- 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);
- }
- }
实现效果
想了解更多内容, 请访问:
51CTO 和华为官方战略合作共建的鸿蒙技术社区
https://harmonyos.51cto.com
来源: http://developer.51cto.com/art/202108/679113.htm