DEV Community

ShuGang Zhou
ShuGang Zhou

Posted 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.

Top comments (0)