-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordBag.cpp
77 lines (66 loc) · 1.48 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
#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> wordToCount;
};
WordBagImpl::WordBagImpl(const string& text)
{
// get words and counts then associate on MyMap
string copy = text;
strToLower(copy);
Tokenizer t(copy);
string w;
while (t.getNextToken(w))
{
int* check = wordToCount.find(w);
if (check != NULL) // found
{
*check = *check + 1;
continue;
}
wordToCount.associate(w, 1);
}
}
bool WordBagImpl::getFirstWord(string& word, int& count)
{
int* number = wordToCount.getFirst(word);
if (number == NULL)
return false;
count = *number;
return true;
}
bool WordBagImpl::getNextWord(string& word, int& count)
{
int* number = wordToCount.getNext(word);
if (number == NULL)
return false;
count = *number;
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);
}