赞
踩
本示例展示了使用hiai引擎框架提供的基于自然语言处理服务的分词、实体抽取功能。
本示例模拟了在应用里,输入一段文字,调用分词、实体抽取能力后得到的结果。
需要使用hiai引擎框架通用文字识别接口@hms.ai.nlp.textProcessing.d.ts。
使用说明:
本示例展示的控件在@hms.ai.nlp.textProcessing.d.ts定义了分词和实体抽取的API:
输入一段文本后点击分词、实体结果按钮,接收处理返回的结果(文字信息)。参考:
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; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。