-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dictionary.cpp
63 lines (56 loc) · 1.72 KB
/
Dictionary.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
#include "Dictionary.h"
#include <iostream>
#include <fstream>
#include <cstdio>
#include<ctype.h>
#include <string.h>
using namespace std;
Dictionary::~Dictionary() {}
Dictionary::Dictionary() {
ifstream input("stanfordSentimentTreebank/dictionary.txt");
string line;
bool is_number = false;
long long number = 0;
string word = "";
while(getline( input, line ) ) {
is_number = false;
word.clear();
for(char & c : line) {
if (c == '|') {
is_number = true;
number = 0;
} else {
if (!is_number) {
char aux = tolower(c);
word +=aux;
}
else number = number*10 + c -'0';
}
}
dictionary.insert(make_pair(word, number));
}
}
string addPhysicalBrackets(string phrase) {
int leftBracket = phrase.find("-LRB-");
while(leftBracket != string::npos) {
phrase.replace(leftBracket, 5, "(");
leftBracket = phrase.find("-LRB-");
}
int rightBracket = phrase.find("-RRB-");
while(rightBracket != string::npos) {
phrase.replace(rightBracket, 5, ")");
rightBracket = phrase.find("-RRB-");
}
return phrase;
}
long long Dictionary::getPhraseIndex(string phrase2) {
string phrase = addPhysicalBrackets(phrase2);
unordered_map<string, long long>::const_iterator found_iter = dictionary.find(phrase);
if (found_iter == dictionary.end()) {
ofstream outputFile;
outputFile.open("debug.txt", std::ios_base::app);
outputFile<<"The given phrase: "<<phrase<<" was not found in the dictionary."<<endl;
return -1;
}
return found_iter->second;
}