-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
152 lines (123 loc) · 5.05 KB
/
main.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
using namespace std;
class SentimentAnalyzer {
public:
// Loading positive words from the uploaded file i.e positive words.txt
void loadPositiveWords(const string& filename) {
loadWords(filename, positiveWords_, 1); // Set the value for positive words as +1
}
// Loading negative words from the uploaded file i.e negative words.txt
void loadNegativeWords(const string& filename) {
loadWords(filename, negativeWords_, -1); // Set the value for negative words as -1
}
// In the below code snippet, we analyze the sentiment of user input.
int analyzeSentiment(const string& userInput) const {
// Extract words from user input
unordered_map<string, int> wordScores = extractWordsWithScores(userInput);
// Count positive and negative words
int positiveCount = countMatchingWords(wordScores, positiveWords_);
int negativeCount = countMatchingWords(wordScores, negativeWords_);
// Check for negation and adjust sentiment
if (containsNegation(wordScores, userInput)) {
swap(positiveCount, negativeCount);
}
// Adjust sentiment score based on overall counts
int totalScore = positiveCount - negativeCount;
return totalScore;
}
private:
// Maps to store positive and negative words with their scores
unordered_map<string, int> positiveWords_;
unordered_map<string, int> negativeWords_;
unordered_set<string> negationWords = {"not", "no", "never", "nor", "neither", "nothing"};
// Load words from a file into a specified unordered_map with scores
void loadWords(const string& filename, unordered_map<string, int>& wordMap, int score) const {
ifstream file(filename);
if (file.is_open()) {
string word;
while (file >> word) {
// Convert to lowercase before inserting
transform(word.begin(), word.end(), word.begin(), ::tolower);
wordMap[word] = score;
}
file.close();
} else {
// Print an error message if the file is not opening.
cerr << "Error opening file: " << filename << endl;
}
}
// Extract words from a text with their scores, removing punctuation and converting to lowercase
unordered_map<string, int> extractWordsWithScores(const string& text) const {
unordered_map<string, int> wordScores;
istringstream iss(text);
string word;
while (iss >> word) {
// Remove punctuations
word.erase(remove_if(word.begin(), word.end(), ::ispunct), word.end());
// Convert to lowercase before inserting
transform(word.begin(), word.end(), word.begin(), ::tolower);
// Check for negation and adjust the score
int score = 1; // Default score for non-negated words
if (containsNegation(wordScores, word)) {
score = -score; // Negate the score if the word is in a negation context
}
wordScores[word] = score;
}
return wordScores;
}
// Check if the given word is in a negation context
bool containsNegation(const unordered_map<string, int>& wordScores, const string& userInput) const {
// Check if the user input contains any negation words
for (const auto& negationWord : negationWords) {
if (userInput.find(negationWord) != string::npos) {
return true;
}
}
return false;
}
// Count the number of words in a set that match the words in another set
int countMatchingWords(const unordered_map<string, int>& wordScores, const unordered_map<string, int>& lexicon) const {
int count = 0;
for (const auto& pair : wordScores) {
if (lexicon.count(pair.first)) {
count += pair.second;
}
}
return count;
}
};
int main() {
SentimentAnalyzer analyzer;
// Loading positive and negative words from the uploaded files
analyzer.loadPositiveWords("positive words.txt");
analyzer.loadNegativeWords("negative words.txt");
// Take user input as a paragraph
cout << "Enter text for sentiment analysis (press Ctrl+D to end input): ";
stringstream inputParagraphStream;
string line;
while (getline(cin, line)) {
inputParagraphStream << line << endl;
}
string userInput = inputParagraphStream.str();
// Analyzing sentiment
int sentimentScore = analyzer.analyzeSentiment(userInput);
// Determine sentiment label
string sentimentLabel;
if (sentimentScore > 0) {
sentimentLabel = "Positive";
} else if (sentimentScore < 0) {
sentimentLabel = "Negative";
} else {
sentimentLabel = "Neutral";
}
// Output/result
cout << "\n Sentiment Score: " << sentimentScore << endl;
cout << "Sentiment Label: " << sentimentLabel << endl;
return 0;
}