-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
299 lines (285 loc) · 9.14 KB
/
index.js
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//Modules and all
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
var natural = require('natural');
natural.PorterStemmer.attach();
var fs = require('fs');
const port = process.env.PORT || 3000;
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
// Delarations
let keyWordsAll = [];
let tf_idf = [];
let problemStatement = [];
let problemLinksArray = [];
let problemTitlesArray = [];
let tf_idf_mod = [];
// extract problemLinksArray - from clicktype/cfproblemlinks.txt - links array storing all links of the problems in order
function makeLinksArray(clicktype){
return new Promise(function(resolve, reject) {
let myReadstream = fs.createReadStream('public/'+clicktype+'/cfproblem_links.txt', 'utf8');
myReadstream.on('data', function(chunk){
// Replacing \n or \r\n or \r by a space
let problemLinks=chunk.replace(/(\r\n|\n|\r)/gm, " ");
let problemLinksChunkArray = problemLinks.split(" ");
for (let str in problemLinksChunkArray) {
problemLinksArray.push(problemLinksChunkArray[str]);
}
});
myReadstream.on('end', ()=>{
resolve();
});
});
}
// extract problemTitlesArray - from clicktype/cfproblemtitles.txt - problem Titles array storing problem titles in order
function makeTitlesArray(clicktype){
return new Promise(function(resolve, reject){
let myReadstream = fs.createReadStream('public/'+clicktype+'/cfproblem_titles.txt', 'utf8');
myReadstream.on('data', function(chunk){
// Removing \r tags(seen in codechef problems) before splitting by \n
let problemTitles=chunk.replace(/(\r)/gm, "");;
let problemTitlesChunkArray = problemTitles.split("\n");
for (let str in problemTitlesChunkArray) {
problemTitlesArray.push(problemTitlesChunkArray[str]);
}
});
myReadstream.on('end', ()=>{
resolve();
});
});
}
// extract problemStatement - from clicktype/problem_statement_all.txt - paragraph to be displayed of problems
function makeProblemStatement(clicktype){
return new Promise(function(resolve, reject) {
let myReadstream = fs.createReadStream('public/'+clicktype+'/problem_statement_all.txt', 'utf8');
myReadstream.on('data', function(chunk){
let problemStatementStringArray = chunk.split('\n');
for(i in problemStatementStringArray){
problemStatement.push(problemStatementStringArray[i]);
}
});
myReadstream.on('end',()=>{
resolve();
});
});
}
// extract keyWordsAll - from clicktype/keywordsall.txt - all keyWords in corpus
function makeKeywordsAll(clicktype){
return new Promise(function(resolve, reject) {
let myReadstream = fs.createReadStream('public/'+clicktype+'/keywordsall.txt', 'utf8');
myReadstream.on('data', function(chunk){
let keywords=chunk;
let keywordsChunkArray = keywords.split(" ");
for (let str in keywordsChunkArray) {
keyWordsAll.push(keywordsChunkArray[str]);
}
});
myReadstream.on('end', ()=>{
resolve();
});
});
}
// extract tf_idf - from clicktype/tf_idf/(i).txt - tfidf from txt files using file stream
function makeTfidf(filelocation, i){
return new Promise(function(resolve, reject) {
tf_idf[i] =[];
// initialisation to zero
for(let l = 0; l<keyWordsAll.length; l++){
tf_idf[i].push(0);
}
let myReadstream = fs.createReadStream(filelocation, 'utf8');
myReadstream.on('data',function(chunk){
let tf_idf_array_string = chunk.split('\n');
for (let j = 0; j<tf_idf_array_string.length;j++){
// splitting tf_idf (j, tf_idf) to j and its tf_idf
let arr = tf_idf_array_string[j].split(' ');
let k = Number(arr[0]);
let val = Number(arr[1])
// update non zero values
tf_idf[i][k]= val;
}
});
myReadstream.on('end',()=>{
resolve();
});
});
}
// extract tf_idf_mod - from clicktype/tf_idf_mod.txt - precomputed mod of tf_idf arrays
function makeTfidfMod(clicktype){
return new Promise(function(resolve, reject) {
let myReadstream = fs.createReadStream('public/'+clicktype+'/tf_idf_mod.txt', 'utf8');
myReadstream.on('data', function(chunk){
let modValues = chunk.split('\n');
for(i in modValues){
tf_idf_mod.push(Number(modValues[i]));
}
});
myReadstream.on('end',()=>{
resolve();
});
});
}
// Functions for query by user
function getKeyWordsArray(data){
// Filtering only English letters ignoring rest all
let stringWithoutLineBreaks = data.replace(/[^a-zA-Z ]/g, "");
// Adding space to two squeezed words : RedBlue -- Red Blue
stringWithoutLineBreaks = stringWithoutLineBreaks.replace(/([a-z])([A-Z])/g, '$1 $2');
// Removing Extra spaces
stringWithoutLineBreaks = stringWithoutLineBreaks.replace(/\s+/g,' ').trim();
// Everything to lowercase
stringWithoutLineBreaks = stringWithoutLineBreaks.toLowerCase();
// Using Natural to remove stopwords and stemming
return stringWithoutLineBreaks.tokenizeAndStem();
}
function getFrequencyMap(array){
let arrayMap = new Map();
for(let i = 0; i<array.length; i++){
if(!isNaN(arrayMap[array[i]])){
if(arrayMap[array[i]]){
arrayMap[array[i]]++;
}
else{
arrayMap[array[i]]=1;
}
}
else{
arrayMap[array[i]]=1;
}
}
return arrayMap;
}
function modOfVector(vector){
var mod = 0;
for (let i = 0; i<vector.length; i++){
mod += (vector[i]*vector[i]);
}
return Math.sqrt(mod);
}
function tf_idf_for_post(map){
let tf_idf_query = [];
for (let k =0; k<keyWordsAll.length;k++) {
let foo = map[keyWordsAll[k]]
if(!isNaN(foo)){
if(foo){
let termFrequencyKeyWord = foo/(keyWordsAll.length);
tf_idf_query.push(termFrequencyKeyWord);
}
else{
tf_idf_query.push(0);
}
}
else{
tf_idf_query.push(0);
}
}
return tf_idf_query;
}
// Cosine Similarity Function
function cosine_similarity(query_tf_idf){
let map_results = new Map();
let mod_query = modOfVector(query_tf_idf);
for (let i = 0; i<tf_idf.length; i++){
//tf_idf[i]
let mod_tf_idf_element = tf_idf_mod[i];
// Dot Product
let dot_product = 0;
for (let j = 0; j<query_tf_idf.length; j++){
dot_product += (query_tf_idf[j]*(tf_idf[i])[j]);
}
let modproduct = mod_query*mod_tf_idf_element;
let cosine_value;
if(modproduct!=0){
cosine_value = dot_product/(modproduct);
}
else{
cosine_value = 0;
}
map_results.set(cosine_value, i);
}
// sorting map_results by cosine_value
map_results = new Map([...map_results.entries()].sort((a, b) => b[0] - a[0]));
return map_results;
}
// extraction
async function extraction(clicktype){
// reset everything to erase earlier clicktype data for each clicktype
problemLinksArray = [];
problemTitlesArray = [];
keyWordsAll = [];
tf_idf = [];
tf_idf_mod = [];
problemStatement = [];
// extraction begins
await makeLinksArray(clicktype);
await makeTitlesArray(clicktype);
await makeKeywordsAll(clicktype);
for(let i =0; i<problemTitlesArray.length; i++){
let filelocation = 'public/'+clicktype+'/tf_idf/'+(i+1)+'.txt'
await makeTfidf(filelocation,i);
cnt =0;
}
await makeTfidfMod(clicktype);
await makeProblemStatement(clicktype);
// extraction ends
}
app.get("/", function(req,res){
res.sendFile(__dirname+"/index.html");
});
app.post("/", function(request, response){
async function run(clicktype){
await extraction(clicktype);
let query_keywords = getKeyWordsArray(queryString);
let query_string_map = getFrequencyMap(query_keywords);
let query_tf_idf = tf_idf_for_post(query_string_map);
let query_cosine_similarity = cosine_similarity(query_tf_idf);
// things to be printed as results
let queryResultsLinks = [];
let queryResultsTitles = [];
let queryResultsProblemStatement = [];
// save results into them
for (let [key, value] of query_cosine_similarity) {
let index = value;
queryResultsTitles.push(problemTitlesArray[index]);
queryResultsLinks.push(problemLinksArray[index]);
queryResultsProblemStatement.push(problemStatement[index]);
}
// no of queries to be printed
if(queryResultsLinks.length>=10)l=queryResultsLinks.length;
else l = queryResultsLinks.length;
//Passing results to .ejs file
response.render("results", {queryString: queryString,queryResultsLinks: queryResultsLinks, queryResultsTitles: queryResultsTitles, l:l, queryResultsProblemStatement: queryResultsProblemStatement, btn:btn});
}
let queryString ='';
let s = '';
let btn;
if(request.body.codeChef){
s = 'codechef_only_query';
run(s);
queryString = request.body.codeChef;
btn = 1;
}
else if(request.body.codeforces){
s = 'codeforces_only_query';
run(s);
queryString = request.body.codeforces;
btn = 2;
}
else if(request.body.all){
queryString = request.body.all;
s = 'all_search_query';
run(s);
btn = 0;
}
else{
queryString = request.body.query;
btn = 0;
s = 'all_search_query';
run(s);
}
});
app.listen(port, function(){
console.log("Server Started at 3000");
});