Skip to content

Commit

Permalink
Create sentimentAnalysis.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 4, 2024
1 parent 8a1c324 commit 690c72e
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/analytics/sentimentAnalysis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// sentimentAnalysis.js

class SentimentAnalysis {
constructor() {
this.positiveWords = ['good', 'great', 'excellent', 'happy', 'love', 'fantastic', 'amazing'];
this.negativeWords = ['bad', 'terrible', 'hate', 'sad', 'awful', 'poor', 'disappointing'];
}

// Analyze sentiment of a given text
analyzeSentiment(text) {
const words = text.toLowerCase().split(/\s+/);
let positiveCount = 0;
let negativeCount = 0;

words.forEach(word => {
if (this.positiveWords.includes(word)) {
positiveCount++;
} else if (this.negativeWords.includes(word)) {
negativeCount++;
}
});

const totalWords = positiveCount + negativeCount;
let sentimentScore = 0;

if (totalWords > 0) {
sentimentScore = (positiveCount - negativeCount) / totalWords;
}

return {
sentimentScore,
sentiment: sentimentScore > 0 ? 'Positive' : sentimentScore < 0 ? 'Negative' : 'Neutral',
positiveCount,
negativeCount,
};
}

// Analyze multiple feedbacks
analyzeMultipleFeedbacks(feedbacks) {
return feedbacks.map(feedback => ({
feedback,
analysis: this.analyzeSentiment(feedback),
}));
}
}

// Example usage
const sentimentAnalyzer = new SentimentAnalysis();
const feedbacks = [
"I love this product! It's fantastic.",
"This is the worst experience I've ever had.",
"The service was okay, nothing special.",
];

const results = sentimentAnalyzer.analyzeMultipleFeedbacks(feedbacks);
console.log('Sentiment Analysis Results:', results);

0 comments on commit 690c72e

Please sign in to comment.