-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
79 lines (66 loc) · 2.54 KB
/
main.cpp
File metadata and controls
79 lines (66 loc) · 2.54 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
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <unordered_map>
#include "Buffer.h"
#include "Scorer.h"
#include "Utils.h"
std::unordered_map<std::string, int> loadDictionary(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) {
throw FileException("Error: Dictionary file '" + filename + "' not found.");
}
std::unordered_map<std::string, int> dict;
std::string word;
int score;
while (file >> word >> score) {
dict[word] = score;
}
return dict;
}
int main() {
try {
std::unordered_map<std::string, int> sentimentDict = loadDictionary("dictionary.txt");
std::string userInput;
std::cout << "Enter the text you want to analyze: ";
std::getline(std::cin, userInput);
if (userInput.empty()) {
throw EmptyTextException("Error: Input text is empty.");
}
std::ofstream outFile("input.txt");
if (!outFile.is_open()) {
throw FileException("Error: Could not write to input.txt.");
}
outFile << userInput;
outFile.close();
Buffer textBuffer(std::move(userInput));
WordCountScorer wcScorer(sentimentDict);
WeightedScorer wScorer(sentimentDict);
double scoreWC = wcScorer.calculateScore(textBuffer);
double scoreW = wScorer.calculateScore(textBuffer);
std::vector<double> results = { scoreWC, scoreW };
double avg = calculateAverage<double>(results);
std::cout << "\n--- SentimentStream Report ---" << std::endl;
std::cout << "Word Count Score: " << scoreWC << std::endl;
std::cout << "Weighted Score: " << scoreW << std::endl;
std::cout << "Average Sentiment: " << avg << std::endl;
std::cout << "Overall Tone: ";
if (avg > 5) std::cout << "Strongly Positive" << std::endl;
else if (avg > 0) std::cout << "Positive" << std::endl;
else if (avg < -5) std::cout << "Strongly Negative" << std::endl;
else if (avg < 0) std::cout << "Negative" << std::endl;
else std::cout << "Neutral" << std::endl;
std::cout << "------------------------------" << std::endl;
} catch (const FileException& e) {
std::cerr << e.what() << std::endl;
return 1;
} catch (const EmptyTextException& e) {
std::cerr << e.what() << std::endl;
return 1;
} catch (const std::exception& e) {
std::cerr << "Unexpected error: " << e.what() << std::endl;
return 1;
}
return 0;
}