-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassifier.cpp
More file actions
198 lines (163 loc) · 6.1 KB
/
Copy pathClassifier.cpp
File metadata and controls
198 lines (163 loc) · 6.1 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "Classifier.h"
Classifier::Classifier() {}
void Classifier::train(char* fileName) {
auto start = chrono::high_resolution_clock::now();
readStopWords();
bool isPositive;
ifstream inFS(fileName);
char line[1000];
// read training file
if (!inFS.is_open())
cerr << "Error reading input file\n";
inFS.getline(line,1000); // ignore header text in data
while(inFS.getline(line, 1000, ',')) {;
// store sentiments in isPositive
DSString sentiment(line);
if(sentiment[0] == '0')
isPositive = 0;
else
isPositive = 1;
// read tweets
for (size_t i = 0; i < 4; i++)
inFS.getline(line, 1000, ',');
inFS.getline(line, 1000);
DSString tweet(line);
// clean and tokenize tweet
tweet = tweet.toLower();
tweet = tweet.removeSymbols();
vector<DSString> tempWords = tokenize(tweet);
// increments or decrements word's sentiment value based on tweet's sentiment value
for (size_t i = 0; i < tempWords.size(); i++) {
tempWords.at(i) = tempWords.at(i).stem();
if (findWord(tempWords.at(i),stopWords) == -1) {
if (findWord(tempWords.at(i), words) == -1) {
words.push_back(tempWords.at(i));
if (isPositive)
sentiments.push_back(1);
else
sentiments.push_back(-1);
}
else {
int index = findWord(tempWords.at(i), words);
if (isPositive)
(sentiments.at(index))++;
else
(sentiments.at(index))--;
}
}
}
}
inFS.close();
auto end = chrono::high_resolution_clock::now();
cout << "Training execution time: " << chrono::duration_cast<chrono::seconds>(end - start).count() << " seconds\n";
}
void Classifier::predict(char* fileName) {
auto start = chrono::high_resolution_clock::now();
ifstream inFS(fileName);
char line[1000];
if (!inFS.is_open())
cerr << "Error reading input file\n";
inFS.getline(line,1000); // ignore header text in data
while(inFS.getline(line, 1000, ',')) {
// stores testing IDs in vector
DSString id(line);
testingID.push_back(id);
// stores tweet in DSString
for (size_t i = 0; i < 3; i++)
inFS.getline(line, 1000, ',');
inFS.getline(line, 1000);
DSString tweet(line);
// cleans and tokenizes tweet
tweet = tweet.toLower();
tweet = tweet.removeSymbols();
vector<DSString> tempWords = tokenize(tweet);
// predicts sentiment values of tweet based on sentiments of words in training data
int prediction = 0;
for (size_t i = 0; i < tempWords.size(); i++) {
tempWords.at(i) = tempWords.at(i).stem();
int index = findWord(tempWords.at(i), words);
if (index != -1) {
if (sentiments.at(index) > 0)
prediction++;
else if (sentiments.at(index) < 0)
prediction--;
}
}
// adds prediction to vector based on total prediction number
if (prediction >= 0)
predictions.push_back("4");
else if (prediction < 0)
predictions.push_back("0");
else
predictions.push_back("Error");
}
inFS.close();
auto end = chrono::high_resolution_clock::now();
cout << "Classification execution time: " << chrono::duration_cast<chrono::seconds>(end - start).count() << " seconds\n";
}
void Classifier::evaluatePredictions(char* testingSentimentFile, char* resultsFileName, char* accuracyFileName) {
vector<DSString> sentimentsAnswers;
double correctCount;
size_t i = 0;
char line[1000];
ifstream inFS(testingSentimentFile);
ofstream resultsFile(resultsFileName);
if (!inFS.is_open())
cerr << "Error reading input file\n";
inFS.getline(line,1000); // ignore header text in data
while(inFS.getline(line, 1000, ',')) {
// assign first char of each line to sentimentAnswers
DSString sentiment(line);
sentimentsAnswers.push_back(sentiment);
// compare predictions and sentimentAnswers
if (sentimentsAnswers.at(i) == predictions.at(i))
correctCount++;
// writes to results file
resultsFile << predictions.at(i) << "," << testingID.at(i) << "\n";
inFS.getline(line, 1000);
i++;
}
inFS.close();
resultsFile.close();
double accuracy = correctCount / sentimentsAnswers.size();
// write to accuracyFile
ofstream accuracyFile(accuracyFileName);
accuracyFile << fixed << setprecision(3) << accuracy << "\n";
for (size_t i = 0; i < predictions.size(); i++) {
if (!(predictions.at(i) == sentimentsAnswers.at(i)))
accuracyFile << predictions.at(i) << "," << sentimentsAnswers.at(i) << "," << testingID.at(i) << "\n";
}
accuracyFile.close();
}
vector<DSString> Classifier::tokenize(DSString& tweet) { // splits tweet into tokens and returns vector of tokens
size_t start = 0;
vector<DSString> words;
DSString token;
for (size_t i = 0; i < tweet.length(); i++) {
if (tweet[i] == ' ' || i == tweet.length()-1) {
token = tweet.substring(start, i-start);
start = i+1;
words.push_back(token);
}
}
return words;
}
// searches vector for word and returns index if found or -1 if not found
int Classifier::findWord(DSString word, vector<DSString> vector) {
for (size_t i = 0; i < vector.size(); i++) {
if (word == vector.at(i))
return i;
}
return -1;
}
void Classifier::readStopWords() { // reads txt file of stop words
ifstream inFS("../data/stopwords.txt");
char line[200];
if (!inFS.is_open())
cerr << "Error reading input file\n";
while(inFS.getline(line,200)) {
DSString temp(line);
stopWords.push_back(temp);
}
inFS.close();
}