DEV Community

ShuGang Zhou
ShuGang Zhou

Posted on • Edited on

09-TextProcessing-WordSegment-Case

Summary

This article introduces how to use @kit.NaturalLanguageKit in HarmonyOS for text word segmentation and implement a simple sentiment analysis function. By creating a TextProcessingWordSegment component, users can input evaluation text, click a button to perform sentiment analysis, and finally display the sentiment tendency (positive, negative, or neutral) of the evaluation.

Implementation Steps

  1. Import the textProcessing module from @kit.NaturalLanguageKit.
  2. Create a TextProcessingWordSegment component, which includes a text input box, a text area to display results, and an analysis button.
  3. Implement the event handler for the button click, and call textProcessing.getWordSegment to perform word segmentation.
  4. Create a SentimentAnalysisService class to perform sentiment analysis based on the word segmentation results.
  5. Display the final sentiment label according to the sentiment score.

Implementation Code

1. Import Modules and Define Components

import { textProcessing } from '@kit.NaturalLanguageKit'; 

@Entry 
@Component 
struct TextProcessingWordSegment { 
  @State comment: string = 'The logistics is fast, the packaging is good, and I am particularly satisfied with the product'; 
  @State label: string = '' 

  build() { 
    Column({ space: 20 }) { 
      TextArea({ text: this.comment }) 
        .height(200) 
      Text('Evaluation Nature: ' + this.label) 
      Button('Sentiment Analysis') 
        .onClick(async () => { 
          const results = await textProcessing.getWordSegment(this.comment) 
          const words = results.map(item => item.word) 
          const service = new SentimentAnalysisService() 
          this.label = service.analyze(words) 
        }) 
    } 
    .padding(15) 
    .height('100%') 
    .width('100%') 
  } 
} 
Enter fullscreen mode Exit fullscreen mode

2. Sentiment Analysis Service Class

// Sentiment Analysis Service Class 
class SentimentAnalysisService { 
  private positiveWords = ['good', 'satisfied', 'great', 'excellent', 'fast']; 
  private negativeWords = ['bad', 'slow', 'expensive', 'awful', 'unsatisfied']; 

  analyze(words: string[]) { 
    let score = 0.5; 
    words.forEach(word => { 
      if (this.positiveWords.includes(word)) { 
        score += 0.1; 
      } 
      if (this.negativeWords.includes(word)) { 
        score -= 0.1; 
      } 
    }); 
    score = Math.max(0, Math.min(1, score)); 
    const label = score > 0.6 ? 'Positive' : score < 0.4 ? 'Negative' : 'Neutral'; 
    return label; 
  } 
} 
Enter fullscreen mode Exit fullscreen mode

Conclusion

This case demonstrates the method of using @kit.NaturalLanguageKit for text processing and implementing simple sentiment analysis in HarmonyOS. By creating components and a service class, we have implemented the functions of text input, word segmentation, and sentiment analysis. The key knowledge points include the creation of HarmonyOS components, state management, the use of asynchronous functions, and simple sentiment analysis algorithms. This method can serve as a foundation to expand more complex natural language processing functions.

Kit Introduction

Natural Language Kit (Natural Language Understanding Service) provides several basic capabilities related to text semantic understanding, helping developers better process and analyze text data. Specifically, it includes the following aspects:

Word Segmentation: It can split a piece of text into independent word units and identify each word in a sentence. This is a fundamental step in natural language processing, laying the foundation for subsequent tasks such as semantic analysis and information extraction.
Entity Extraction: It can identify entities with specific meanings from text, such as person names, place names, dates, numbers, phone numbers, email addresses, etc. This entity information plays an important role in many scenarios, such as information retrieval, knowledge graph construction, and intelligent Q&A.
Developers can leverage the above - mentioned capabilities, combine them with their own business scenarios, and develop various intelligent applications to achieve their business goals.

Constraints

Supported Languages: Simplified Chinese, English, Traditional Chinese.
Text Length: No more than 1000 characters.

Top comments (0)