-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dictionary.cpp
68 lines (63 loc) · 1.85 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
64
65
66
67
68
#include <ostream>
#include <fstream>
#include <sstream>
#include "Dictionary.h"
// constructor
Dictionary::Dictionary(const string& filename) : filename(filename)
{
// create an input file stream
std::ifstream fin(filename);
if (!fin)
{
std::cout << "could not open input file: " << filename << std::endl;
exit(1);
}
int linenum = 0;
string line;
getline(fin, line); // very important first attempt to read
// this first attemot will get the i/o flags initialized
while (fin)
{
// std::cout << line << std::endl;
++linenum; // count the line
std::istringstream sin(line); // turn the line into an input string stream
string tokenStr;
while (sin >> tokenStr) // extract token strings
{
// std::cout << tokenStr << std::endl;
processToken(tokenStr, linenum);
}
getline(fin, line); // attempt to read the next line, if any
}
fin.close();
}
void Dictionary::processToken(const string& token, int linenum)
{
size_t index = bucketIndex(token);
this->tokenListBuckets[index].addSorted(token, linenum);
// char* cstr = (char *) &token;
// Token newToken = Token(cstr,linenum);
// this->tokenListBuckets[index].addSorted(newToken);
}
void Dictionary::print(std::ostream& output) const
{
char c = 'A';
for (const TokenList &t : this->tokenListBuckets)
{
std::cout << "<<<< " << c << " >>>>" << std::endl;
std::cout << std::endl;
c++;
t.print(output);
std::cout << std::endl;
}
}
size_t Dictionary::bucketIndex(const string& token) const
{
size_t index = 26; // bucket index for tokens not beginning with a letter
if (isalpha(token[0]))
{
if(isupper(token[0])) index = token[0] - 'A';
else index = token[0] - 'a';
}
return index;
}