HarmonyOS AI基础技术赋能之关键字获取
想了解更多内容,础技请访问:
和华为官方合作共建的术赋鸿蒙技术社区
https://harmonyos.51cto.com
引言
在实际应用开发中,时不时的关键会遇到AI领域相关的一些技术,本节旨在详细讲述关键字获取技术,字获关键字获取涉及各个领域中,础技如:游记摘要、术赋新闻标签、关键杂志文刊等。字获所以对于HarmonyOS开发者而言,础技了解和掌握HarmonyOS AI领域相关技术是术赋至关重要的,也是亿华云计算关键每一个HarmonyOS开发者的一项必不可少的专业技能。
功能介绍
关键字提取主要用于从新闻和邮件里提取出关键字,字获便于用户快速获取新闻和邮件的础技主题。关键字可以为有意义的术赋实体,比如,关键人名、电影,也可以为非实体的关键词汇,如,上课、服务器租用考研。
开发指南
1、使用NluClient静态类进行初始化,通过异步方式获取服务的连接
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静态类初始化,通过异步方式获取服务连接 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); } }实现效果


想了解更多内容,请访问:
和华为官方合作共建的源码下载鸿蒙技术社区
https://harmonyos.51cto.com