-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest4.cpp
More file actions
186 lines (155 loc) · 4.22 KB
/
test4.cpp
File metadata and controls
186 lines (155 loc) · 4.22 KB
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
/**
* Map-reduce Word-Frequencies
*/
#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#include "MapReduceClient.h"
#include "MapReduceFramework.h"
#define NUM_THREADS 10
class Line : public K1 {
private:
const std::string line;
public:
Line (const std::string &line) : line (line)
{}
virtual bool operator< (const K1 &other) const
{
return this->line < ((Line &) other).line;
}
const std::string &getLine ()
{
return line;
}
};
class Word : public K2, public K3 {
private:
const std::string word;
public:
Word (const std::string &word) : word (word)
{}
Word (const Word &other) = default;
virtual bool operator< (const K2 &other) const
{
return this->word < ((Word &) other).word;
}
virtual bool operator< (const K3 &other) const
{
return this->word < ((Word &) other).word;
}
const std::string &getWord ()
{
return word;
}
};
class Integer : public V3 {
public:
int val;
Integer (int val) : val (val)
{}
};
class MapReduceWordFrequencies : public MapReduceClient {
virtual void Map (const K1 *key, const V1 *val, void *context) const
{
std::stringstream sstream (((Line *) key)->getLine ());
std::string word;
while (sstream >> word)
{
Word *k2 = new Word (word);
emit2 (k2, nullptr, context);
}
}
virtual void Reduce (const K2 *const key, const IntermediateVec *vals, void *context) const
{
Word *k3 = new Word (((Word &) *key));
auto *frequency = new Integer ((*vals).size ());
for (auto &val:*vals)
{
delete val.first;
delete val.second;
}
emit3 (k3, frequency, context);
}
virtual void map (const K1 *key, const V1 *value, void *context) const override
{
Map (key, value, context);
}
// gets a single K2 key and a vector of all its respective V2 values
// calls emit3(K3, V3, context) any number of times (usually once)
// to output (K3, V3) pairs.
virtual void reduce (const IntermediateVec *pairs, void *context) const override
{
if (!pairs->empty ())
{
Reduce ((*pairs)[0].first, pairs, context);
}
}
};
void writeByFrequency (OutputVec &frequencies, std::ofstream &ofs)
{
// get length of longest word (so we can write to the file in a nice format)
unsigned int maxLength = 0;
for (auto it = frequencies.begin (); it != frequencies.end (); ++it)
{
unsigned int length = (*(Word *) it->first).getWord ().length ();
maxLength = length > maxLength ? length : maxLength;
}
// sort by frequency is descending order
std::sort (
frequencies.begin (),
frequencies.end (),
[] (const OutputPair &o1, const OutputPair &o2)
{
if (((Integer *) o1.second)->val < ((Integer *) o2.second)->val)
return false;
return ((Integer *) o1.second)->val > ((Integer *) o2.second)->val
|| ((Word *) o1.first)->getWord () < ((Word *) o2.first)->getWord ();
}
);
// writing results to file
for (auto it = frequencies.begin (); it != frequencies.end (); ++it)
{
const std::string &word = (*(Word *) it->first).getWord ();
int frequency = ((Integer *) it->second)->val;
ofs << '{' << word << " , " << frequency << "}" << std::endl;
}
}
void findWordFrequencies (std::ifstream &ifs, std::ofstream &ofs)
{
InputVec k1v1Pairs;
// make the input for the framework
std::string line;
while (std::getline (ifs, line))
{
Line *k1 = new Line (line);
k1v1Pairs.push_back (InputPair (k1, nullptr));
}
MapReduceWordFrequencies mapReduceObj;
OutputVec frequencies;
auto job = startMapReduceJob (mapReduceObj, k1v1Pairs, frequencies, NUM_THREADS);
waitForJob (job);
closeJobHandle (job);
writeByFrequency (frequencies, ofs);
}
int main (int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "Usage: WordFrequencies <path to text_file>" << std::endl;
return 1;
}
std::string textPath (argv[1]);
std::ifstream ifs (textPath);
std::ofstream ofs (textPath + std::string ("_test_results"),
std::ofstream::out);
if (!ifs.is_open () || !ofs.is_open ())
{
std::cerr << "ERROR: can't open input or output file"
<< std::endl;
return 1;
}
findWordFrequencies (ifs, ofs);
ifs.close ();
ofs.close ();
}