-
Notifications
You must be signed in to change notification settings - Fork 0
/
enron.cpp
145 lines (113 loc) · 3.64 KB
/
enron.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
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
#include "enron.h"
#include <fstream>
#include <iostream>
#include <dirent.h>
#include <cstdio>
#include <unistd.h>
#include <cstring>
enron::enron() {
mails = new array<set<int>, NB_MAILS>;
setup_enron_data();
}
enron* enron::singleton;
// Enron is a singleton class. Its entity is accessed by this static function.
enron *enron::get() {
if (!enron::singleton) enron::singleton = new enron();
return enron::singleton;
}
// Associates each word in enron inboxes with a different number and returns them as a map
void enron::recursiveParse(const string &path) {
DIR *dir;
struct dirent *dirent;
string child_path;
// 1 - Use path as a folder
if ((dir = opendir(path.c_str())) == nullptr) {
// 2 - If it doesn't work, use it as a file
if (!read_file_at(path, dictionary)) {
// 3 - If it doesn't work, there's an error
cout << "Error(" << errno << ") opening " << path << endl;
}
} else {
// Repeat these 3 steps on everything this folder contains
while ((dirent = readdir(dir)) != nullptr) {
// Avoid infinite recursion
if (strcmp(dirent->d_name, "..") && strcmp(dirent->d_name, ".")) {
child_path = path;
child_path.append("/");
child_path.append(dirent->d_name);
recursiveParse(child_path);
}
}
}
closedir(dir);
}
// Opens the file and calls the necessary methods to build
bool enron::read_file_at(const string &file_path, map<string, int>* dictionary) {
ifstream input(file_path);
if (input.is_open()) {
extract_data_from(input, dictionary);
input.close();
return true;
} else {
return false;
}
}
// Adds to enron::words the words that haven't been added yet, and associates them with a number
void enron::extract_data_from(ifstream &input, map<string, int>* dictionary) {
static int i = 0;
set<int> words_to_int;
string word;
while (input >> word) {
if ((*dictionary).find(word) == (*dictionary).end()) {
(*dictionary).emplace(word, (*dictionary).size());
}
words_to_int.emplace((*dictionary)[word]);
}
mails->at(i++) = words_to_int;
}
// Outputs data in files on disk
void enron::save() {
ofstream mails_stream(get_enron_path().append("/mails.txt"), ofstream::out);
for (const set<int> &set : (*mails)) {
for (int id : set) {
mails_stream << id << " ";
}
mails_stream << "\n";
}
}
void enron::setup_enron_data() {
ifstream mails_file(get_enron_path().append("/mails.txt"), ifstream::in);
if (mails_file.is_open()) {
restore_mails(mails_file);
mails_file.close();
} else {
cout << "Data not found at " << get_enron_path() << ", now parsing enron database...\n";
dictionary = new map<string, int>;
recursiveParse(get_enron_path());
delete dictionary;
save();
}
}
void enron::restore_mails(ifstream &input_file) {
string str_line;
set<int> mail;
static int i = 0;
while (getline(input_file, str_line)) {
size_t pos = str_line.find(' ');
size_t initialPos = 0;
mail.clear();
// Decompose statement
while (pos != std::string::npos) {
mail.emplace(stoi(str_line.substr(initialPos, pos - initialPos)));
initialPos = pos + 1;
pos = str_line.find(' ', initialPos);
}
mails->at(i++) = mail;
}
}
void enron::log() {
cout << "Treating " << mails->size() << " mails\n";
}
array<set<int>, NB_MAILS>* enron::get_mails() {
return mails;
}