-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatchUrl.cpp
More file actions
210 lines (166 loc) · 4.86 KB
/
Copy pathmatchUrl.cpp
File metadata and controls
210 lines (166 loc) · 4.86 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "matchUrl.h"
// tokenize a URL
// normalize tokens (convert to lowercase and remove non-alphanumeric characters)
string normalize(const string &token) {
string normalized;
for (char c : token) {
if (std::isalnum(c)) {
// convert to lowercase
if (c >= 'A' && c <= 'Z') {
normalized.push_back(c + ('a' - 'A'));
}
else {
normalized.push_back(c);
}
}
else { // cut characters after non-alphanumeric
return normalized; // normalized = "": remain characters after non-alphanumeric
}
}
return normalized;
}
// split a string by a delimiter
vector<string> split(const string &str, char delimiter) {
vector<string> tokens;
string token = "";
for (char c : str) {
if (c == delimiter) {
if (token != "") {
// std::cout << "token: " << token << std::endl;
tokens.push_back(token);
token = "";
}
}
else {
token.push_back(c);
}
}
if (token != "") {
tokens.push_back(token);
}
return tokens;
}
// split a string by delimiter and push to tokens
void splitAdd(const string &str, char delimiter, vector<string> & tokens) {
string token = "";
for (char c : str) {
if (c == delimiter) {
if (token != "") {
// std::cout << "token: " << token << std::endl;
tokens.push_back(normalize(token));
token = "";
}
}
else {
token.push_back(c);
}
}
if (token != "") {
tokens.push_back(normalize(token));
}
}
// process domain
void processDomain(const string &domain, vector<string> &tokens) {
string cleanDomain = domain;
// remove "www."
if (cleanDomain.find("www.") != -1) {
cleanDomain = cleanDomain.substr(4);
}
// remove ".com"
string suffix = cleanDomain.substr(cleanDomain.size() - 4);
if (cleanDomain.size() > 4 && (suffix == ".com" || suffix == ".org" || suffix == ".net")) {
cleanDomain = cleanDomain.substr(0, cleanDomain.size() - 4);
}
// split by "."
splitAdd(cleanDomain, '.', tokens);
}
// parse fragments
void parseFragment(const string &str, vector<string> & tokens) {
vector<string> queries = split(str, '?');
for (const string &part : queries) {
if (part.find("+") != -1) {
splitAdd(part, '+', tokens);
}
else if (part.find("-") != -1) {
splitAdd(part, '-', tokens);
}
else if (part.find("_") != -1) {
splitAdd(part, '_', tokens);
}
else {
string normalized = normalize(part);
if (normalized != "")
tokens.push_back(normalized);
}
}
}
// TODO: remove this if too slow
// parse query parameters and push into tokens
void parsePath(const string &path, vector<string> & tokens) {
// parse path
vector<string> pairs = split(path, '&'); // whether there are queries
if (pairs.size() > 1) {
// query type
for (const string &pair : pairs) {
vector<string> kv = split(pair, '=');
for (const string &part : kv) {
parseFragment(part, tokens);
}
}
}
else {
// not query type
parseFragment(path, tokens);
}
}
// tokenize a URL
vector<string> tokenizeUrl(const string &url) {
vector<string> tokens;
// https://
size_t schemeEnd = url.find("://");
// if (schemeEnd != -1) {
// tokens.push_back(url.substr(0, schemeEnd)); // Scheme
// }
// the rest of the URL (host, path, query, fragment)
string remainder = url.substr(schemeEnd + 3); // After "://"
// domain and path split by '/'
vector<string> parts = split(remainder, '/');
// the first part of path is the domain
if (!parts.empty()) {
processDomain(parts[0], tokens);
}
// the rest of the path
for (size_t i = 1; i < parts.size(); ++i) {
// std::cout << "path: " << parts[i] << std::endl ;
parsePath(parts[i], tokens);
}
return tokens;
}
// find match in url
// count number of query matches in url
int matchCount(const vector<string> &tokens, const string &url) {
vector<string> urlTokens = tokenizeUrl(url);
std::cout << "url tokens: ";
for (int j = 0; j < urlTokens.size(); j ++) {
std::cout << urlTokens[j] << " ";
}
std::cout << "query tokens: " << tokens.size() << std::endl;;
int count = 0;
for (int i = 0; i < tokens.size(); i ++) {
for (int j = 0; j < urlTokens.size(); j ++) {
if (tokens[i] == urlTokens[j]) {
count ++;
}
}
}
return count;
}
// int main() {
// string url = "https://www.niche.com/colleges/search/best-colleges/s/michigan/";
// vector<string> tokens = tokenizeUrl(url);
// for (const string &token : tokens) {
// std::cout << "token: " << token << std::endl;
// }
// }
// match queries (remove stop words)
// score