HarmonyOS Sample 之AI能力之NLU引擎服务

仰天大笑出门去,我辈岂是蓬蒿人。这篇文章主要讲述HarmonyOS Sample 之AI能力之NLU引擎服务相关的知识,希望能为你提供帮助。
@toc
HarmonyOS Sample 之 AI能力之NLU引擎服务 1.介绍NLU(the natural language understanding)自然语言理解引擎服务。
该引擎服务提供了 分词、词性标注、实体识别、意图识别、关键词提取等接口,有同步和异步两种方法。
这一期的内容包括:分词能力、词性标注、关键字提取、实体识别。相关代码都增加了注释说明和调试日志,方便理解和学习。
2.效果展示

HarmonyOS Sample 之AI能力之NLU引擎服务

文章图片

3.搭建环境安装DevEco Studio,详情请参考DevEco Studio下载。
设置DevEco Studio开发环境,DevEco Studio开发环境需要依赖于网络环境,需要连接上网络才能确保工具的正常使用,可以根据如下两种情况来配置开发环境:
如果可以直接访问Internet,只需进行下载HarmonyOS SDK操作。
如果网络不能直接访问Internet,需要通过代理服务器才可以访问,请参考配置开发环境。
下载源码后,使用DevEco Studio 打开项目,模拟器运行即可。
真机运行需要将config.json中的buddleName修改为自己的,如果没有请到AGC上进行配置,参见 使用模拟器进行调试 。
4.项目结构 5.代码讲解 5.1 分词能力(getWordSegment)
//2.异步接口
NluClient.getInstance()
.getWordSegment(requestData,NluRequestType.REQUEST_TYPE_LOCAL,
asyncResult -> {
//发送分词结果
sendResult(asyncResult.getResponseResult(), 0);
release();
});
`requestData:`JSON格式,参数名包括{text,type,callPkg,callType,callVersion,callState},其中, `text:`待分析的文本,必填项; `type:`分词的粒度,枚举值,0:基本词 ;1:在基本词的基础上,做实体合并;223372036854775807:在type为1的基础上,把实体时间、地点等整体结构合并,出现符号隔开不合并,并把一些常用短语合并,默认为0。 `requestType:` 枚举值,NluRequestType.REQUEST_TYPE_LOCAL 表示调用本地引擎。**3.解析返回结果** ResponseResult responseResult 返回JSON格式字符串,JSON格式,参数名包括:{code,message,words} ```java //{"code":0,"message":"success","words":["我","明天","下午","三点", //"要","去","江宁万达广场","看","速度","与","激情"]}// 将分词结果转换成list if (result.contains("\\"message\\":\\"success\\"")) { switch (operateType) { //分词 case 0: String words = result.substring(result.indexOf(WORDS) + STEP, result.lastIndexOf("]")).replaceAll("\\"", ""); if ((words == null) || ("".equals(words))) { // 未识别到分词结果,返回"no keywords" lists = new ArrayList< > (1); lists.add("no keywords"); } else { lists = Arrays.asList(words.split(",")); } //构建事件 event = InnerEvent.get(TWO, ZERO, lists); } }

4.释放资源
NluClient.getInstance().destroy(slice);

5.1.3 分词粒度测试type=0
requestData:{" text" :我明天下午三点要去江宁万达广场看速度与激情," type" :0}
分词结果:
{" code" :0," message" :" success" ," words" :
[" 我" ," 明天" ," 下午" ," 三点" ," 要" ," 去" ," 江宁万达广场" ," 看" ,==" 速度" ," 与" ," 激情" ==]}
type=1
requestData:{" text" :我明天下午三点要去江宁万达广场看速度与激情," type" :1}
分词结果:
{" code" :0," message" :" success" ," words" :
[" 我" ," 明天" ,==" 下午" ," 三点" ," 要" ," 去" ==," 江宁万达广场" ," 看" ,==" 速度与激情" ==]}
type=9223372036854775807
requestData:{" text" :我明天下午三点要去江宁万达广场看速度与激情," type" :9223372036854775807}
分词结果:
{" code" :0," message" :" success" ," words" :
[" 我" ,==" 明天下午三点" ==,==" 要去" ==," 江宁万达广场" ," 看" ," 速度与激情" ]}
5.2 词性标注(getWordPos)
5.2.2 词性标注结果requestData:
{"text":"我明天下午三点要去江宁万达广场看速度与激情","type":0}

responseResult:
{"code":0,"message":"success","pos":[ {"word":"我","tag":"rr"}, {"word":"明天","tag":"t"}, {"word":"下午","tag":"t"},{"word":"三点","tag":"t"},{"word":"要","tag":"v"},{"word":"去","tag":"vf"}, {"word":"江宁万达广场","tag":"n"},{"word":"看","tag":"v"},{"word":"速度","tag":"n"},{"word":"与","tag":"cc"},{"word":"激情","tag":"n"} ]}

==词性:rr:人称代词,t:时间词,v:动词,vf:趋向动词,n:名词,cc:并列连词==
tag词性值就不一一列举了,详情查看
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ai-pos-tagging-guidelines-0000001050732512
5.3 提取关键词(getKeywords)
responseResult:
{ "entity": { "movie": [{ "oriText": "速度与激情", "sequence": 1, "origin": "1637301307158", "heat": 0, "standardName": "速度与激情", "charOffset": 16, "normalValue": "速度与激情", "user.extend": false, "isCorrected": false }], "location": [{ "sequence": 1, "origin": "1637301307158", "oriText": "江宁万达广场", "key": "江宁万达广场", "type": "nspHB", "coreLocation": { "value": "江宁万达广场", "location": { "value": "江宁万达广场" } }, "isAbstract": "0", "cost": "29", "charOffset": 9, "normalValue": "江宁万达广场", "user.extend": false, "isCorrected": false }], "time": [{ "normalTime": { "start": { "timestamp": 1637391600000, "section": "P", "standardTime": "2021年11月20日15时00分00秒" } }, "oriText": "明天下午三点", "sequence": 1, "origin": "1637301307158", "charOffset": 1, "normalValue": "明天下午三点", "user.extend": false, "isCorrected": false }], "varietyshow": [{ "oriText": "速度与激情", "sequence": 1, "origin": "1637301307158", "heat": 0, "standardName": "速度与激情", "charOffset": 16, "normalValue": "速度与激" }] } }

6.思考总结1.上述的AI能力不需要申请权限
2.这些AI能力使用起来还是非常方便的,开箱即用,可以灵活运用到应用开发中。
7.完整代码附件链接:https://harmonyos.51cto.com/resource/1514
想了解更多关于鸿蒙的内容,请访问:
【HarmonyOS Sample 之AI能力之NLU引擎服务】51CTO和华为官方战略合作共建的鸿蒙技术社区
https://harmonyos.51cto.com/#bkwz
::: hljs-center
HarmonyOS Sample 之AI能力之NLU引擎服务

文章图片

:::

    推荐阅读