This module of Natural Language Kit provides the capabilities of word segmentation and part-of-speech tagging for input text based on natural language processing technologies. It tags each word as a noun, verb, adjective, or another part-of-speech category.
It also extracts entities from the input text and classifies them into different types according to the entity type list supported by this kit. The output results contain the entity type, location of an entity in the input text, original text of the entity, and other fields obtained in entity parsing. For details about entity fields, please refer to EntityType. This capability is applicable to scenarios that require in-depth text parsing, such as intelligent customer service, content analysis, and information extraction.
Since: 5.0.0(12)
Word segmentation output, including words and parts of speech.
System capability: SystemCapability.AI.NaturalLanguage.TextProcessing
Model restriction: This API can be used only in the stage model.
Since: 5.0.0(12)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| word | string | No | No | Word in the result obtained through the getWordSegment method. |
| wordTag | string | No | No | Part of speech in the result obtained through the getWordSegment method. |
Entity type, which is optional.
System capability: SystemCapability.AI.NaturalLanguage.TextProcessing
Model restriction: This API can be used only in the stage model.
Since: 5.0.0(12)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| entityTypes | EntityType[] | No | Yes | Type of an entity. If not specified, all supported types of entities are identified by default. |
| timestamp | number | No | Yes | Reference timestamp, which is used to specify the time context for entity recognition. The value is an integer in milliseconds since January 1, 1970. If it is not specified, the current system time is used. Since: 26.0.0 |
Entity extraction result.
System capability: SystemCapability.AI.NaturalLanguage.TextProcessing
Model restriction: This API can be used only in the stage model.
Since: 5.0.0(12)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| text | string | No | No | Original text of an entity. |
| charOffset | number | No | No | Location of an entity in the original text. The location is measured in characters. |
| type | EntityType | No | No | Type of an entity. |
| jsonObject | string | No | No | Description of parameters in the Entity return value. For details, please refer to jsonObject. |
getWordSegment(text: string): Promise<Array<WordSegment>>
Creates a word segmentation instance and initializes the engine. This API uses a promise to return the result. It is applicable to scenarios such as word segmentation, keyword extraction, and text similarity calculation.
System capability: SystemCapability.AI.NaturalLanguage.TextProcessing
Model restriction: This API can be used only in the stage model.
Since: 5.0.0(12)
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| text | string | Yes | Text to be analyzed. For details, please refer to Constraints. |
Returns
| Type | Description |
|---|---|
| Promise<Array<WordSegment>> | Promise used to return a set of word segmentation results. |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
| ID | Error Message |
|---|---|
| 200 | Run timed out, please try again later. |
| 401 | The parameter check failed. |
| 1011200001 | Failed to run, please try again. |
| 1011200002 | The service is abnormal. |
Example:
import { textProcessing } from '@kit.NaturalLanguageKit';
import { BusinessError } from '@kit.BasicServicesKit';
// Function for formatting the word segmentation result.
// Format the word segmentation result array, which contains each word and its part-of-speech.
function 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;
}
// Function for testing the word segmentation capability.
// Perform word segmentation on the input text to identify words and their parts-of-speech.
function testWordSegment(inputText: string) {
textProcessing.getWordSegment(inputText)
.then(result => {
// Upon successful word segmentation, format the result and then output it.
let outputText = formatWordSegmentResult(result);
console.info('NLUDemo', `getWordSegment result:${outputText}`);
})
.catch((err: BusinessError) => {
console.error('NLUDemo', `getWordSegment errorCode: ${err.code} errorMessage: ${err.message}`);
});
}
@Entry
@Component
struct Page {
build() {
Column(){
Button('Start').onClick(() => {
// Enter the text and call the word segmentation function.
let inputText = 'test for nlp word segment';
testWordSegment(inputText);
})
}
}
} getEntity(text: string, entityConfig?: EntityConfig): Promise<Array<Entity>>
Creates an entity extraction instance and initializes the engine. This API uses a promise to return the result. It is applicable to scenarios such as sensitive information identification and content review.
System capability: SystemCapability.AI.NaturalLanguage.TextProcessing
Model restriction: This API can be used only in the stage model.
Since: 5.0.0(12)
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| text | string | Yes | Input string. For details, please refer to Constraints. |
| entityConfig | EntityConfig | No | Entity type. By default, all options are selected. You are advised to load entity types on demand to improve app performance. |
Returns
| Type | Description |
|---|---|
| Promise<Array<Entity>> | Promise used to return the entity set. |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
| ID | Error Message |
|---|---|
| 200 | Run timed out, please try again later. |
| 401 | The parameter check failed. |
| 1011200001 | Failed to run, please try again. |
| 1011200002 | The service is abnormal. |
Example:
import { textProcessing, EntityType } from '@kit.NaturalLanguageKit';
import { BusinessError } from '@kit.BasicServicesKit';
// Function for formatting the entity recognition result.
// Format the entity recognition result array, which contains the original text, position, type, and detailed parameters of each entity.
function 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`; // Original text of the entity.
output += ` charOffset: ${entity.charOffset}\n`; // Character offset of the entity in the original text.
output += ` entityType: ${entity.type}\n`; // Entity type.
output += ` jsonObject: ${entity.jsonObject}\n\n`; // Detailed parameters about the entity.
}
return output;
}
// Function for testing the entity recognition capability.
// Perform entity recognition on the input text to extract specific types of entities.
function testEntityRecognition(inputText: string) {
// Specify that only the name entities are recognized to improve processing efficiency and accuracy.
textProcessing.getEntity(inputText, {
entityTypes: [EntityType.NAME]
}).then(result => {
// Upon successful recognition, format the result and then output it.
let outputText = formatEntityResult(result);
console.info('NLUDemo', `getEntity result:${outputText}`);
}).catch((err: BusinessError) => {
console.error('NLUDemo', `getEntity errorCode: ${err.code} errorMessage: ${err.message}`);
});
}
@Entry
@Component
struct Page {
build() {
Column(){
Button('Start').onClick(() => {
// Enter the text and call the entity recognition function (recognizing names).
let inputText = 'test for nlp getEntity. Mary, Bob and Mike.';
testEntityRecognition(inputText);
})
}
}
} init(): Promise<boolean>
Initializes the natural language processing engine. This API uses a promise to return the result.
You can call this API to initialize the natural language processing engine in advance to reduce the delay when the text processing capabilities (such as word segmentation and entity recognition) are invoked for the first time. In actual use scenarios, if there are no special performance requirements, you do not need to call this API. The system will automatically initialize the engine when it is used for the first time.
System capability: SystemCapability.AI.NaturalLanguage.TextProcessing
Model restriction: This API can be used only in the stage model.
Since: 5.0.0(12)
Returns
| Type | Description |
|---|---|
| Promise<boolean> | Promise used to return the result. The value true indicates that the initialization is successful, and false indicates that the initialization fails. |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
| ID | Error Message |
|---|---|
| 200 | Run timed out, please try again later. |
| 1011200001 | Failed to run, please try again. |
| 1011200002 | The service is abnormal. |
Example:
import { textProcessing } from '@kit.NaturalLanguageKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Page {
build() {
Column(){
Button('init').onClick(() => {
textProcessing.init().then(result => {
console.info(`textProcess init result: ${result}`);
}).catch((err: BusinessError) => {
console.error(`textProcess init failed errorCode: ${err.code} errorMessage: ${err.message}`);
});
})
}
}
} release(): Promise<boolean>
Releases the engine. This API uses a promise to return the result. When natural language processing functions are no longer needed, you should call this API to release engine resources. It is recommended that this API be called to deallocate memory when the corresponding page is exited or the text processing capabilities are no longer needed. After release() is called, calling getWordSegment or getEntity again will trigger engine re-initialization, which may increase the call duration.
System capability: SystemCapability.AI.NaturalLanguage.TextProcessing
Model restriction: This API can be used only in the stage model.
Since: 5.0.0(12)
Returns
| Type | Description |
|---|---|
| Promise<boolean> | Promise used to return the result. The value true indicates that the engine is successfully released, and false indicates that the release fails. |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
| ID | Error Message |
|---|---|
| 200 | Run timed out, please try again later. |
| 1011200001 | Failed to run, please try again. |
| 1011200002 | The service is abnormal. |
Example:
import { textProcessing } from '@kit.NaturalLanguageKit';
async function runTextProcessing() {
// Initialize the engine.
await textProcessing.init();
console.info('Text processing initialized successfully');
// Release the engine resources after natural language processing functions are used.
try {
const result = await textProcessing.release();
console.info(`textProcess release result: ${result}`);
} catch (err) {
console.error(`textProcess release failed errorCode: ${err.code} errorMessage: ${err.message}`);
}
}
@Entry
@Component
struct Page {
build() {
Column(){
Button('Start').onClick(() => {
void runTextProcessing();
})
}
}
}