-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordBag.cpp
83 lines (73 loc) · 1.57 KB
/
WordBag.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
#include "provided.h"
#include "MyMap.h"
#include <string>
using namespace std;
class WordBagImpl
{
public:
WordBagImpl(const string& text);
bool getFirstWord(string& word, int& count);
bool getNextWord(string& word, int& count);
private:
MyMap<string, int>* m_token;
};
WordBagImpl::WordBagImpl(const string& text)
{
m_token = new MyMap<string, int>;
Tokenizer t(text);
string w;
while(t.getNextToken(w))
{
strToLower(w);
int* c = m_token->find(w);
if (c == NULL)
m_token->associate(w, 1);
else
*c += 1;
}
}
bool WordBagImpl::getFirstWord(string& word, int& count)
{
string key;
int* c = m_token->getFirst(key);
if (c == NULL)
return false;
else
{
word = key;
count = *c;
return true;
}
}
bool WordBagImpl::getNextWord(string& word, int& count)
{
string key;
int* c = m_token->getNext(key);
if (c == NULL)
return false;
else
{
word = key;
count = *c;
return true;
}
}
//******************** WordBag functions *******************************
// These functions simply delegate to WordBagImpl's functions.
// You probably don't want to change any of this code.
WordBag::WordBag(const std::string& text)
{
m_impl = new WordBagImpl(text);
}
WordBag::~WordBag()
{
delete m_impl;
}
bool WordBag::getFirstWord(string& word, int& count)
{
return m_impl->getFirstWord(word, count);
}
bool WordBag::getNextWord(string& word, int& count)
{
return m_impl->getNextWord(word, count);
}