-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSentimentLabels.cpp
53 lines (49 loc) · 1.55 KB
/
SentimentLabels.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
#include "SentimentLabels.h"
#include <iostream>
#include <fstream>
#include<ctype.h>
#include <cstdio>
#include <string.h>
using namespace std;
/*
* Maps phrase indexes with their sentiment score.
*/
SentimentLabels::SentimentLabels() {
ifstream input("stanfordSentimentTreebank/sentiment_labels.txt");
string line;
bool character = false;
bool first_line = true;
long long first_number = 0;
double second_number = 0;
string word = "";
while(getline( input, line ) ) {
character = false;
first_number = 0;
second_number = 0;
word.clear();
if (!first_line) {
for(char & c : line) {
if (c == '|') {
character = true;
} else {
if (!character) first_number = first_number*10 + c -'0';
else {
char aux = tolower(c);
word +=aux;
}
}
}
second_number = atof(word.c_str());
sentimentLabelsMap.insert(make_pair(first_number, second_number));
}
first_line = false;
}
}
double SentimentLabels::getSentimentScore(long long phraseIndex) {
unordered_map<long long, double>::const_iterator found_iter = sentimentLabelsMap.find(phraseIndex);
if (found_iter == sentimentLabelsMap.end()) {
cout<<"The given index: "<<phraseIndex<<" was not found as an index in the sentiment labels map."<<endl;
return -1;
}
return found_iter->second;
}