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

介绍

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

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

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

效果预览

使用说明:

  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;
  }
}
相关推荐
物联网软硬件开发-轨物科技26 分钟前
【轨物方案】新能源的下半场:构筑光伏场站全生命周期智慧运维新范式
大数据·人工智能·物联网
2501_9361460430 分钟前
【目标检测】钙钛矿晶体YOLO11-GhostDynamicConv模型改进与实现_1
人工智能·目标检测·计算机视觉
Deepoch33 分钟前
Deepoc具身模型:电厂巡检机械狗的智能核心
人工智能·科技·机器狗·具身模型·deepoc·机械狗·巡检机械狗
好奇龙猫5 小时前
【人工智能学习-AI入试相关题目练习-第七次】
人工智能·学习
Mao.O8 小时前
开源项目“AI思维圆桌”的介绍和对于当前AI编程的思考
人工智能
jake don8 小时前
AI 深度学习路线
人工智能·深度学习
寒季6668 小时前
Electron 实战:构建跨平台桌面端 Markdown 编辑器(含实时预览、文件操作、快捷键)
华为·electron·harmonyos
信创天地8 小时前
信创场景软件兼容性测试实战:适配国产软硬件生态,破解运行故障难题
人工智能·开源·dubbo·运维开发·risc-v
幻云20108 小时前
Python深度学习:从筑基到登仙
前端·javascript·vue.js·人工智能·python
无风听海8 小时前
CBOW 模型中的输出层
人工智能·机器学习