forked from ls1248659692/python_guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtf-05.cpp
More file actions
180 lines (147 loc) · 3.48 KB
/
tf-05.cpp
File metadata and controls
180 lines (147 loc) · 3.48 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <regex>
#include <map>
using namespace std;
struct Freq {
string word;
int freq;
Freq(string w, int f) : word(w), freq(f) {}
};
//
// Helpers
//
static int tospace(int c)
{
if (!isalpha(c))
return ' ';
else
return c;
}
static vector<string> get_stop_words()
{
string word;
vector<string> stop_words;
ifstream is("../stop_words.txt");
while (getline(is, word, ',')) {
stop_words.push_back(word);
}
char w[2];
w[1] = '\0';
for (char c : "abcdefghijklmopqrstuvwxyz") {
w[0] = c;
stop_words.push_back(string(w));
}
sort(stop_words.begin(), stop_words.end());
return stop_words;
}
static bool sort_by_freq(Freq x, Freq y)
{
return y.freq < x.freq;
}
//
// The functions
//
/** Takes a path to a file and returns the entire
contents of the file as a string
*/
string read_file(const char* path_to_file)
{
string data;
ifstream is(path_to_file, std::ifstream::binary);
if (is) {
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
char* buffer = new char [length + 1];
is.read(buffer,length);
buffer[length] = '\0';
data = buffer;
delete [] buffer;
}
return data;
}
/** Takes a string and returns a copy with all nonalphanumeric
chars replaced by white space
*/
string filter_chars(string str_data)
{
std::transform(str_data.begin(), str_data.end(), str_data.begin(), ::tospace);
return str_data;
}
/** Takes a string and returns a copy with all chars in lower case
*/
string normalize(string str_data)
{
std::transform(str_data.begin(), str_data.end(), str_data.begin(), ::tolower);
return str_data;
}
/** Takes a string and scans for words, returning
a list of words.
*/
vector<string> scan(string str_data)
{
string word;
vector<string> words;
istringstream is(str_data);
while (is >> word) {
words.push_back(word);
}
return words;
}
/** Takes a list of words and returns a copy with all stop
words removed
*/
vector<string> remove_stop_words(vector<string> words)
{
vector<string> stop_words = get_stop_words();
vector<string> filtered_list;
filtered_list.reserve(words.size());
for (string w : words) {
if (!binary_search(stop_words.begin(), stop_words.end(), w))
filtered_list.push_back(w);
}
return filtered_list;
}
/** Takes a list of words and returns a dictionary associating
words with frequencies of occurrence
*/
map<string,int> frequencies(vector<string> words)
{
map<string,int> freq;
for (string w : words) {
map<string,int>::iterator it = freq.find(w);
if (it != freq.end()) {
it->second++;
}
else {
freq.insert(pair<string,int>(w,1));
}
}
return freq;
}
/** Takes a dictionary of words and their frequencies
and returns a list of pairs where the entries are
sorted by frequency
*/
vector<Freq> sort(map<string,int> word_freq)
{
vector<Freq> out_list;
out_list.reserve(word_freq.size());
for (pair<string,int> p : word_freq) {
out_list.push_back(Freq(p.first, p.second));
}
sort(out_list.begin(), out_list.end(), sort_by_freq);
return out_list;
}
//
// The main function
//
int main(int argc, char* argv[])
{
vector<Freq> word_freqs = sort(frequencies(remove_stop_words(scan(normalize(filter_chars(read_file(argv[1])))))));
for (vector<Freq>::iterator it = word_freqs.begin(); it != word_freqs.begin()+25; it++)
cout << it->word << " - " << it->freq << endl;
return 0;
}