鸿蒙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;
  }
}
相关推荐
DisonTangor5 分钟前
上海AI气象大模型提前6天预测“贝碧嘉”台风登陆浦东 今年已多次精准预测
人工智能
人工智能培训咨询叶梓22 分钟前
生成式人工智能在无人机群中的应用、挑战和机遇
人工智能·语言模型·自然语言处理·aigc·无人机·多模态·生成式人工智能
羊小猪~~33 分钟前
深度学习基础案例5--VGG16人脸识别(体验学习的痛苦与乐趣)
人工智能·python·深度学习·学习·算法·机器学习·cnn
Zhangci]33 分钟前
OpenCv(一)
人工智能·opencv·计算机视觉
钡铼技术35 分钟前
通过iFIX在ARMxy边缘计算网关上实现维护管理
人工智能·物联网·边缘计算·钡铼技术·armxy边缘计算网关
m0_609000422 小时前
向日葵好用吗?4款稳定的远程控制软件推荐。
运维·服务器·网络·人工智能·远程工作
开MINI的工科男2 小时前
深蓝学院-- 量产自动驾驶中的规划控制算法 小鹏
人工智能·机器学习·自动驾驶
AI大模型知识分享4 小时前
Prompt最佳实践|如何用参考文本让ChatGPT答案更精准?
人工智能·深度学习·机器学习·chatgpt·prompt·gpt-3
张人玉5 小时前
人工智能——猴子摘香蕉问题
人工智能
草莓屁屁我不吃6 小时前
Siri因ChatGPT-4o升级:我们的个人信息还安全吗?
人工智能·安全·chatgpt·chatgpt-4o