10-HarmonyOS5-TextProcessingEntity-Case
1 zhousg 0 6/17/2025, 7:21:22 AM
This article introduces how to use @kit.NaturalLanguageKit in HarmonyOS to extract email addresses from text. By creating a TextProcessingEntity component, users can input text containing email addresses, click a button to extract the email addresses, and finally display the extracted email addresses.
import { EntityType, textProcessing } from '@kit.NaturalLanguageKit';
@Entry @Component struct TextProcessingEntity { @State article: string = '电子邮件(EMAIL):识别文本中的电子邮件地址,如“example@abc.com”'; @State email: string = ''
build() {
Column({ space: 20 }) {
TextArea({ text: this.article })
.height(200)
Text('电子邮箱:' + this.email)
Button('提取邮箱')
.onClick(async () => {
const results = await textProcessing.getEntity(this.article)
results.forEach(item => {
if (item.type === EntityType.EMAIL) {
this.email = item.text
}
})
})
}
.padding(15)
.height('100%')
.width('100%')
}
}
No comments yet