Read the original article:Understanding Natural Language Kit on HarmonyOS NEXT
Introduction
HarmonyOS NEXT introduces a powerful tool for text understanding: the Natural Language Kit. Designed for developers who want to integrate intelligent language features into their applications, this kit provides core Natural Language Processing (NLP) capabilities that work natively within the Harmony ecosystem.
The Natural Language Kit is not just a text utility — it lays the foundation for building smart, context-aware apps. Whether you’re building a search engine, a chatbot, a news reader, or an intelligent assistant, this kit can help your app understand text semantically and structurally, in real time.
Currently, it offers two fundamental features:
- Word Segmentation Breaks down raw text into individual word units, crucial for further NLP tasks like keyword extraction, semantic analysis, and search input optimization.
- Entity Extraction Detects meaningful elements in the text such as names, locations, dates, emails, phone numbers, and more — enabling use cases like auto-filling forms, enhancing reading experiences, or assisting in customer service.
With these features, developers can:
- Extract structured insights from unstructured input.
- Build smarter user interfaces that understand what users write.
- Enhance app features such as autocomplete, text highlighting, data validation, and contextual responses.
And the best part? It’s all done using simple API calls, without needing external ML models or complex libraries.
Let’s now dive into each feature in more detail — how they work, how to use them, and where they shine.
🔍 Word Segmentation
What is it?
Word segmentation splits a sentence into meaningful units such as words or phrases. This is especially important in languages like Chinese, where words are not separated by spaces. It’s also useful in English for keyword extraction and semantic analysis.
Use Case Example:
When a user enters a sentence in a search bar, the system segments the sentence into words to extract relevant keywords.
Supported Languages: Simplified Chinese, Traditional Chinese, English
Max Text Length: 1000 characters
Note: Not supported on emulators.
Development Flow
- Import the kit
import { textProcessing } from '@kit.NaturalLanguageKit';
2.Capture input via a TextInput
.
3.Use textProcessing.getWordSegment(inputText) to get the segmented result.
let result = await textProcessing.getWordSegment(this.inputText);
4.Format the response to display:
Word[0]: HarmonyOS, Tag: Noun
Word[1]: is, Tag: Verb
Word[2]: awesome, Tag: Adjective
Tip: You can easily bind this functionality to a button for real-time processing in the UI.
🧠Entity Extraction
What is it?
Entity extraction automatically detects meaningful private information contained in text. These include assets such as contact names, email addresses, phone numbers, date and time. Thus, text content from the user can be converted to structured data.
📚 Types of Recognizable Entities:
- 📅 DATETIME : Date and time information (e.g. “June 18, 2024”)
- ✉️ EMAIL : Email address
- 📦 EXPRESS_NO : Shipping tracking number
- ✈️ FLIGHT_NO : Flight number
- 📍 LOCATION : Location / address
- 👤 NAME : Contact name
- 📞 PHONE_NO : Phone number
- 🌐 URL : Web address
- 🔐 VERIFICATION_CODE :Verification code
- 🆔 ID_NO : ID number
Use Case Example:
News reading apps: Highlight important person, place, and date information in the text
Customer service: Automatically extract information such as phone, shipping, or verification code from user-submitted text
Finance apps: Simplify transactions by detecting identity or verification codes
Supported Languages: Simplified Chinese, Traditional Chinese, English
Max Text Length: 1000 characters
Note: Not supported on emulators.
Development Flow:
1.Include the library in the project:
import { textProcessing, EntityType } from '@kit.NaturalLanguageKit';
2.Specify which entity types you want to detect:
entityTypes: [EntityType.NAME, EntityType.PHONE_NO]
3.When the button is pressed, call the getEntity() function:
let result = await textProcessing.getEntity(this.inputText, { entityTypes: [...] });
4.Process the results as follows:
Entity[0]:
oriText: John Smith
charOffset: 5
entityType: NAME
jsonObject: {...}
Conclusion
HarmonyOS NEXT’s Natural Language Kit provides developers with built-in and easy-to-use NLP tools. It allows you to perform basic operations, such as word segmentation and asset extraction, quickly and efficiently, on the device.
This kit is an important step in natural language processing in the HarmonyOS ecosystem, offering a robust infrastructure for intelligent applications ranging from search engines to chatbots, news apps to customer support systems.
Making sense of your application is now a few lines of code.
Top comments (0)