欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 鸿蒙AI功能开发【hiai引擎框架-分词、实体抽取】 自然语言理解服务

鸿蒙AI功能开发【hiai引擎框架-分词、实体抽取】 自然语言理解服务

2024/10/24 5:21:44 来源:https://blog.csdn.net/2301_76813281/article/details/141031774  浏览:    关键词:鸿蒙AI功能开发【hiai引擎框架-分词、实体抽取】 自然语言理解服务

介绍

本示例展示了使用hiai引擎框架提供的基于自然语言处理服务的分词、实体抽取功能。

本示例模拟了在应用里,输入一段文字,调用分词、实体抽取能力后得到的结果。

需要使用hiai引擎框架通用文字识别接口@hms.ai.nlp.textProcessing.d.ts。

效果预览

1

使用说明:

  1. 在手机的主屏幕,点击”nlpDemo“,启动应用。
  2. 输入一段文字。
  3. 点击“获取分词结果”按钮,展示出分词结果,点击“获取实体结果”,展示出实体抽取结果。

具体实现

本示例展示的控件在@hms.ai.nlp.textProcessing.d.ts定义了分词和实体抽取的API:

  • function getWordSegment(text: string): Promise;
  • function getEntity(text: string, entityConfig?: EntityConfig): Promise;

输入一段文本后点击分词、实体结果按钮,接收处理返回的结果(文字信息)。参考:

import hilog from '@ohos.hilog';
import { textProcessing } from '@kit.NaturalLanguageKit';@Entry
@Component
struct Index {private inputText: string = '';@State outputText: string = '';build() {Column() {TextInput({ placeholder: '请输入文本' }).height(40).fontSize(16).width('90%').margin(10).onChange((value: string) => {this.inputText = value;})Scroll() {Text(this.outputText).fontSize(16).width('90%').margin(10)}.height('40%')Row() {Button('获取分词结果').type(ButtonType.Capsule).fontColor(Color.White).width('45%').margin(10).onClick(async () => {try {let result: textProcessing.WordSegment[] = await textProcessing.getWordSegment(this.inputText);this.outputText = this.formatWordSegmentResult(result);} catch (err) {hilog.error(0x0000, 'testTag', `getWordSegment error: code: ${err.code}, message: ${err.message}`);}})Button('获取实体结果').type(ButtonType.Capsule).fontColor(Color.White).width('45%').margin(10).onClick(async () => {try {let result: textProcessing.Entity[] = await textProcessing.getEntity(this.inputText);this.outputText = this.formatEntityResult(result);} catch (err) {hilog.error(0x0000, 'testTag', `getEntity error: code: ${err.code}, message: ${err.message}`);}})}}.width('100%').height('100%').justifyContent(FlexAlign.Center)}private formatWordSegmentResult(segments: textProcessing.WordSegment[]): string {let output = 'Word Segments:\n';segments.forEach((segment, index) => {output += `Word[${index}]: ${segment.word}, Tag: ${segment.wordTag}\n`;});return output;}private formatEntityResult(entities: textProcessing.Entity[]): string {if (!entities || !entities.length) {return 'No entities found.';}let output = 'Entities:\n';for (let i = 0; i < entities.length; i++) {let entity = entities[i];output += `Entity[${i}]:\n`;output += `  oriText: ${entity.text}\n`;output += `  charOffset: ${entity.charOffset}\n`;output += `  entityType: ${entity.type}\n`;output += `  jsonObject: ${entity.jsonObject}\n\n`;}return output;}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com