-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentimentAnalysisExpertSystem.cpp
More file actions
47 lines (41 loc) · 1.16 KB
/
Copy pathSentimentAnalysisExpertSystem.cpp
File metadata and controls
47 lines (41 loc) · 1.16 KB
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
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
set<string> positiveWords = {"good", "great", "happy", "excellent", "love"};
set<string> negativeWords = {"bad", "sad", "terrible", "hate", "angry"};
string classifySentiment(string sentence) {
transform(sentence.begin(), sentence.end(), sentence.begin(), [](unsigned char ch) {
return static_cast<char>(tolower(ch));
});
for (char& ch : sentence) {
if (!isalnum(static_cast<unsigned char>(ch))) ch = ' ';
}
stringstream ss(sentence);
string token;
int score = 0;
while (ss >> token) {
if (positiveWords.count(token)) ++score;
if (negativeWords.count(token)) --score;
}
if (score > 0) return "positive";
if (score < 0) return "negative";
return "neutral";
}
int main() {
string text = "I love this excellent product";
cout << "Sentiment: " << classifySentiment(text) << '\n';
return 0;
}