1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#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; } |
Explanation
- Function
toLowerCase(const std::string& str)
:- Purpose: Converts a string to lowercase to ensure case-insensitive comparison.
- Implementation: Uses the
std::transform
function to convert all characters in the string to lowercase.
- Function
analyzeSentiment(const std::string& text)
:- Purpose: Analyzes the sentiment of the input text based on predefined keywords.
- Keywords:
- Positive Keywords: Words associated with positive sentiment.
- Negative Keywords: Words associated with negative sentiment.
- Score Calculation:
- Counts occurrences of positive and negative keywords in the text.
- Uses
std::string::find
to check if a keyword is present in the lowercase text.
- Sentiment Determination:
- Compares positive and negative scores to classify the sentiment as “Positive”, “Negative”, or “Neutral”.
- Main Function:
- User Input: Prompts the user to enter a sentence for sentiment analysis.
- Sentiment Analysis: Calls
analyzeSentiment
to determine the sentiment of the input text. - Result Display: Outputs the result of the sentiment analysis.
Usage
- Sentiment Analysis: Provides a basic simulation of sentiment analysis using keyword matching.
- Text Classification: Classifies input text into positive, negative, or neutral categories based on predefined keywords.