#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
// Function to convert a string to lowercase
std::string toLowerCase(const std::string& str) {
std::string lowerStr = str;
std::transform(lowerStr.begin(), lowerStr.end(), lowerStr.begin(), ::tolower);
return lowerStr;
}
// Function to analyze sentiment based on keywords
std::string analyzeSentiment(const std::string& text) {
// Define positive and negative keywords
std::vector<std::string> positiveKeywords = {"happy", "joy", "love", "excellent", "good", "great"};
std::vector<std::string> negativeKeywords = {"sad", "hate", "terrible", "poor", "bad", "horrible"};
int positiveScore = 0;
int negativeScore = 0;
// Convert text to lowercase for case-insensitive comparison
std::string lowerText = toLowerCase(text);
// Count occurrences of positive and negative keywords
for (const auto& word : positiveKeywords) {
if (lowerText.find(word) != std::string::npos) {
positiveScore++;
}
}
for (const auto& word : negativeKeywords) {
if (lowerText.find(word) != std::string::npos) {
negativeScore++;
}
}
// Determine sentiment based on scores
if (positiveScore > negativeScore) {
return "Positive";
} else if (negativeScore > positiveScore) {
return "Negative";
} else {
return "Neutral";
}
}
int main() {
std::string inputText;
// Get user input
std::cout << "Enter a sentence for sentiment analysis: ";
std::getline(std::cin, inputText);
// Analyze sentiment
std::string sentiment = analyzeSentiment(inputText);
// Display result
std::cout << "Sentiment Analysis Result: " << sentiment << std::endl;
return 0;
}