鸿蒙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;
  }
}
相关推荐
2501_919749033 小时前
华为鸿蒙免费PDF工具—小羊免费PDF
华为·pdf·harmonyos·鸿蒙
甲维斯3 小时前
浪费时间!DeepSeek 4.1 Flash
人工智能
tianxuanjg3 小时前
工业/协作机器人研发采购指南:如何适配新品迭代的CNC加工合作
人工智能·经验分享·机器人·无人机·材质
醍醐实验室3 小时前
视觉投影层(Projector)的几何变换:从 Linear 到 C-Abstractor 的信息压缩
人工智能·c-abstractor
举个栗子。3 小时前
Generative AI for Beginners:微软官方 21 课生成式 AI 入门教程,从零掌握 AI 应用开发
人工智能·microsoft
科技苑3 小时前
日常用的 prompt词汇集指南
人工智能·prompt
SamChan903 小时前
PDF多语言翻译的格式还原技术拆解:从版面分析到内容重排的工程实现
python·ai·pdf·机器翻译
AIwenIPgeolocation3 小时前
告别“数据孤岛”:可信数据空间如何打通产业链、城市群?
大数据·人工智能
七夜zippoe3 小时前
AI Agent 工程化落地实战(01):从 ChatBot 到自主智能体的范式跃迁
人工智能·ai·agent·chatbot·自主智能体