-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.cpp
193 lines (178 loc) · 4.45 KB
/
util.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
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
#include <iostream>
#include <sstream>
#include "util.hpp"
using std::istream;
using std::endl;
using std::cout;
using std::istringstream;
using std::string;
using std::vector;
/**
* Starts the timer. Saves the current time.
*/
void Timer::begin_timer()
{
start = std::chrono::high_resolution_clock::now();
}
/**
* Ends the timer. Compares end time with the start time and returns number of nanoseconds
*/
long long Timer::end_timer()
{
std::chrono::time_point<std::chrono::high_resolution_clock> end;
end = std::chrono::high_resolution_clock::now();
return (long long)std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
}
/**
* Parse a line by taking the number at the beginning of a file.
* This might be helpful if you want make your own test files of
* the following format:
*
* ----
* 7 word
* 8 words
* 9 wordlike
* ----
*
* For example, you could use the following snippet:
*
* getline(words, line);
* if (words.eof())
* break;
* unsigned int freq = Utils::stripFrequency(line);
* dict.insert(line, freq);
*
* Input: a line from a dictionary file. This line will be modified.
*/
unsigned int Utils::stripFrequency(string& line)
{
// Count the number of characters past the first space
int count = 0;
string::iterator last = line.end();
for (string::iterator it = line.begin(); it != last; ++it) {
count++;
if (*it == ' ') {
break;
}
}
unsigned int freq = std::stoi(line.substr(0, count));
line.erase(0, count);
return freq;
}
/**
* Parses all the tokens on a given line, returning them
* in a vector.
*/
vector<string> Utils::getWordsFromLine(string& line)
{
vector<string> words;
std::stringstream ss(line);
string word;
while (!ss.eof())
{
ss >> word;
words.push_back(word);
}
return words;
}
/*
* Load the words in the file into the dictionary trie
*/
void Utils::load_dict(DictionaryTrie& dict, istream& words)
{
unsigned int freq;
string data = "";
string temp_word = "";
string word = "";
vector<string> word_string;
unsigned int i = 0;
while(getline(words, data))
{
if(words.eof()) break;
temp_word = "";
word = "";
data = data + " .";
istringstream iss(data);
iss >> freq;
while(1)
{
iss >> temp_word;
if(temp_word == ".") break;
if(temp_word.length() > 0) word_string.push_back(temp_word);
}
for(i = 0; i < word_string.size(); i++)
{
if(i > 0) word = word + " ";
word = word + word_string[i];
}
dict.insert(word, freq);
word_string.clear();
}
}
/*
* Load num_words from words stream into the dictionary trie
*/
void Utils::load_dict(DictionaryTrie& dict, istream& words, unsigned int num_words)
{
unsigned int freq;
string data = "";
string temp_word = "";
string word = "";
vector<string> word_string;
unsigned int i = 0;
unsigned int j = 0;
for(; j < num_words; j++)
{
getline(words, data);
if(words.eof()) break;
temp_word = "";
word = "";
data = data + " .";
istringstream iss(data);
iss >> freq;
while(1)
{
iss >> temp_word;
if(temp_word == ".") break;
if(temp_word.length() > 0) word_string.push_back(temp_word);
}
for(i = 0; i < word_string.size(); i++)
{
if(i > 0) word = word + " ";
word = word + word_string[i];
}
dict.insert(word, freq);
word_string.clear();
}
}
void Utils::load_dict(vector<string>& dict, istream& words)
{
unsigned int junk;
string data = "";
string temp_word = "";
string word = "";
vector<string> word_string;
unsigned int i = 0;
while(getline(words, data))
{
if(words.eof()) break;
temp_word = "";
word = "";
data = data + " .";
istringstream iss(data);
iss >> junk;
while(1)
{
iss >> temp_word;
if(temp_word == ".") break;
if(temp_word.length() > 0) word_string.push_back(temp_word);
}
for(i = 0; i < word_string.size(); i++)
{
if(i > 0) word = word + " ";
word = word + word_string[i];
}
dict.push_back(word);
word_string.clear();
}
}