-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki.cpp
139 lines (121 loc) · 3.19 KB
/
wiki.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
#include "wiki.h"
using namespace std;
float maxfloat = static_cast<float>(INT32_MAX);
void wiki::getgrid() {
stringstream s;
s << '[';
for (auto row : grid) {
s << '[';
for (auto entry : row) {
s << entry << ",";
}
s << "]\n";
}
s << ']';
cout << s.str() << endl;
}
map<string, string> wiki::genmap(string path) {
map<string, string> nounmap;
ifstream words(path);
string curr;
// vector<string> out;
if (words.is_open()) {
while (getline(words, curr, '\n')) {
nounmap[curr] = "";
}
}
cout << "finish nounmap" << endl;
return nounmap;
}
void wiki::sortnoun(string pathh, string changeto) {
string path = pathh;
map<string, string> nounmap = genmap("nounlist.txt");
std::filesystem::create_directories("./onlynoun");
for (auto &entry : filesystem::directory_iterator(path)) {
string temp = static_cast<string>(entry.path());
string name;
for (int k = pathh.size() + 1; k < temp.size() - 4; k++) {
name.push_back(tolower(temp.at(k)));
}
// cout << name << endl;
if (nounmap.find(name) != nounmap.end()) {
cout << entry.file_size() << endl;
// cout << entry.path() << endl;
try {
std::ifstream src(entry.path(), std::ios::binary);
std::ofstream dst("./onlynoun/" + name + ".txt", std::ios::binary);
// std::cout<< " moved "<< name<<endl;
dst << src.rdbuf();
// src.close();
// dst.close();
} catch (std::filesystem::filesystem_error &e) {
std::cout << e.what() << '\n';
}
}
}
}
string wiki::remove_punct(const string &str) {
string ret;
remove_copy_if(str.begin(), str.end(), std::back_inserter(ret),
[](int c) { return std::ispunct(c); });
return ret;
}
vector<string> wiki::getpage(const string &pagename) {
ifstream words(pagename);
vector<string> out;
if (words.is_open()) {
std::istream_iterator<string> word_iter(words);
while (!words.eof()) {
out.push_back(remove_punct(*word_iter));
++word_iter;
}
} else {
cout << "file " << pagename << " failed to open" << endl;
}
return out;
}
string wiki::extract_word(string page_name) {
string name;
for (int m = 11; m < page_name.size() - 4; m++) {
name.push_back(tolower(page_name.at(m)));
}
return name;
}
float wiki::sumvec(vector<float> v,
vector<int> added) {
float toreturn;
for (auto i : added) {
if (v[i] == static_cast<float>(INT32_MAX)) {
return static_cast<float>(INT32_MAX);
}
//cout<<v[i]<<endl;
toreturn += v[i];
}
return toreturn;
}
vector<float> wiki::addvecf(vector<float> fir, vector<float> sec) {
vector<float> toreturn;
int f = 0;
int s = 0;
for (int i = 0; i < max(fir.size(), sec.size()); i++) {
if (fir[i] == maxfloat || sec[i] == maxfloat) {
toreturn.push_back(maxfloat);
} else{
toreturn.push_back(fir[i] + sec[i]);
}
}
return toreturn;
}
wiki::~wiki() {
for (auto *pg : allpages) {
delete pg;
}
}
void wiki::printpath(vector<int> currpath) {
stringstream s;
s << "path: ";
for (auto i : currpath) {
s << allpages[i]->noun << '(' << i << ')' << " -> ";
}
cout << s.str() << endl;
}