Read the original article:Small Data, Smart Models: Sentiment Analysis in ArkTS with NLP
Introduction
In this article, I will walk you through how to implement a basic sentiment analysis tool using a small dataset. The idea is simple: the user writes how they feel after a workout in a TextInput component, and the app analyzes the words they used. Based on a predefined word list, the app responds with one of the following emotional categories:
✅ Positive
❌ Negative
🤷♂️ So-so
❓ What do you mean? (if no match is found)
Even with a small dataset, this functionality can offer a surprising amount of value, and it’s a great way to get started with natural language processing in ArkTS. By the end of this article, you’ll know how to build a simple, real-time sentiment analysis tool that reacts to user input in a meaningful way.
What is Sentiment Analysis?
Sentiment analysis is a branch of Natural Language Processing (NLP) that helps identify whether a given piece of text expresses a positive, negative, or neutral sentiment. This is especially useful in applications like:
- Customer feedback analysis
- Mood tracking
- Social media monitoring
- Personal journaling apps
In this example, we’re keeping it simple by using a hardcoded list of words categorized by sentiment, rather than relying on complex machine learning models.
Note: You can also explore Huawei’s NLP Kit for more use cases. However, keep in mind that NLP Kit is not yet supported on wearable devices as of now.
How does Sentiment Analysis work?
1.Define the Sentiment Word Lists Separate words into three arrays: positiveWords, negativeWords. These will serve as our reference data.
export default class WordModel {
static positiveWords: string[] = [
'good', 'great', 'excellent', 'relaxed', 'happy', 'energetic', 'enjoyable',
'pretty good', 'nice', 'effective', 'fantastic', 'awesome', 'refreshed',
'motivated', 'satisfied', 'strong', 'fine'
];
static negativeWords: string[] = [
'bad', 'terrible', 'tired', 'exhausted', 'hard', 'painful', 'stressed',
'devastating', 'devastated', 'weak', 'sore', 'uncomfortable', 'frustrated',
'angry', 'sad', 'drained'
];
}
2.Set Up User Input Provide a TextInput field for users to type in how they feel.
TextInput({ placeholder: 'How are you feeling?' })
.width('90%')
.height(40)
.margin({ top: 20 })
.onChange((value: string) => {
this.inputText = value;
});
3.Analyze the Text Split the input into individual words and compare them against the lists.
interface SentimentResult {
result: string;
exerciseNe: boolean;
}
function checkSentiment(text: string, exerciseNe: boolean): SentimentResult {
let lowerText = text.toLowerCase();
let positiveCount = WordModel.positiveWords.filter((word: string): boolean => {
return lowerText.includes(word);
}).length;
let negativeCount = WordModel.negativeWords.filter((word: string): boolean => {
return lowerText.includes(word);
}).length;
if (positiveCount === 0 && negativeCount === 0) {
return { result: 'What do you mean? 🤔', exerciseNe: false };
}
if (positiveCount > negativeCount) {
return { result: 'Positive 😊', exerciseNe: false };
} else if (negativeCount >
positiveCount) {
return { result: 'Negative 😞', exerciseNe: true };
} else {
return { result: 'So so 😐', exerciseNe: true };
}
}
4.Generate Feedback when button is clicked display the result based on which category has the most matches or show a fallback message if nothing matches. If sentiment analysis returns ‘Negative’ or ‘so so’ display breatheButton() for Breath exercise.
Button() {
Text('Analyze')
.fontColor($r('app.color.DefaultNavyBlueColor'))
}
.backgroundColor($r('app.color.DefaultGrayColor'))
.width('50%')
.margin(10)
.onClick(() => {
const result = checkSentiment(this.inputText, this.exerciseNeed);
this.sentimentResult = result.result;
this.exerciseNeed = result.exerciseNe;
});
Text(this.sentimentResult)
.textAlign(TextAlign.Center)
.width('100%')
.fontSize($r('app.float.page_title_font_size'))
if (this.exerciseNeed) {
breatheButton();
}
Output
- The user writes something like “I feel energized and happy today”.
- The app breaks this into words: [“I”, “feel”, “energized”, “and”, “happy”, “today”].
- It finds two positive matches: energized, happy.
- The result: Positive 🎉
If the user writes something with unrecognized words, the app gracefully responds with:
What do you mean? 🤔
Conclusion
In this project, we carried out sentiment analysis on a small dataset using basic NLP techniques. By examining each word in the user’s sentence, we identified whether the words carried positive or negative meaning. Depending on the results, the system responds with different outputs. Even with a limited amount of data, this simple approach shows how we can still extract meaningful insights and understand user sentiment effectively.
🎯 This approach gives you a real-time, responsive user experience without any need for external APIs or machine learning services.
Top comments (0)