-
Notifications
You must be signed in to change notification settings - Fork 13
/
thesis-services.js
382 lines (293 loc) · 15.1 KB
/
thesis-services.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
const natural = require("natural");
//const bClassifier = new natural.BayesClassifier();
const bClassifierThesis = new natural.BayesClassifier(null,0.1);
const thesesClassifier = require("./bn.js");
const tokenizer = new natural.CaseTokenizer();
const sw = require('stopword');
const ba_logger = require('../../log/ba_logger');
const threshold = 0.5;
var htmlparser = require("htmlparser2");
const fs = require('fs'),
path = require('path'),
filePath = path.join(__dirname, "../../files/tTrad.html");
const util = require('util');
var HashMap = require('hashmap');
let ThesisTypes = ["Projecto", "Dissertação", "Empresa"];
class ThesisServices {
constructor() {
this.processThesis = processThesis;
this.trainSaveClassifierUsingLink = trainSaveClassifierUsingLink;
this.trainSaveClassifier = trainSaveClassifier;
this.parseThesis = parseThesis;
this.printThesesArray = printThesesArray;
}
}
let thesis_services = module.exports = exports = new ThesisServices();
function parseThesis(callback) {
let parsedTheses = [];
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data) {
let handler = new htmlparser.DomHandler(function (error, dom) {
if (error) {
callback(error,null,null);
}
else {
//Path to table responsive is children[3].children[7].children[1].children[3].children[9].children[7].children[3].children[3]
//Path to table is dom[4].children[3].children[7].children[1].children[3].children[9].children[7].children[3].children[3].children[1]
//Path to table head is dom[4].children[3].children[7].children[1].children[3].children[9].children[7].children[3].children[3].children[1].children[1].name);
//Path to table body is dom[4].children[3].children[7].children[1].children[3].children[9].children[7].children[3].children[3].children[1].children[3].name);
//Table body holds tr. Each tr corresponds to one master thesis
let tableBody = dom[4].children[3].children[7].children[1].children[3].children[9].children[7].children[3].children[3].children[1].children[3];
tableBody.children.forEach((element)=> {
if (element.type === "tag" && element.name === "tr") {
let oneThesis = {
id: "",
title: "",
supervisors: "",
vacancies: "",
location: "",
courses: "",
observations: "",
objectives: "",
status: "",
requirements: "",
areas: "",
type: ""
};
//contém os td's com info
let trChild = element.children;
//We have td's in iterations 1,3,5,7,9,11
let i = 0;
//Entre texto (espaços) e tds, trChild tem 13 elementos
//há 6 td's, alguns com info nested
trChild.forEach((subelement)=> {
if (subelement.type === "tag" && subelement.name === "td") {
//ID
if (i === 1) {
//console.log("Case 1, ID");
let id = subelement.children[0].data;
if (id === null) {
callback("ID of thesis is not defined", null, null);
}
oneThesis.id = id;
}
//Title
if (i === 3) {
//console.log("Case 3, Title");
let title = subelement.children[0].data;
oneThesis.title = title;
}
//Supervisors
if (i === 5) {
//console.log("Case 5, Superv");
let elementsNumber = subelement.children.length;
let arraySupervisors = [];
//let supervisorsNumber = (elementsNumber - 1) / 2;
for (let j = 1; j < elementsNumber; j = j+2) {
let supervisorDiv = subelement.children[j];
let supervisor = supervisorDiv.children[0].data;
arraySupervisors.push(supervisor);
}
oneThesis.supervisors = arraySupervisors;
}
//Vacancies
if (i === 7) {
//console.log("Caso 7, Vac");
let vacancies = subelement.children[0].data;
oneThesis.vacancies = vacancies;
}
if (i === 9) {
//console.log("Caso 9, status");
let status = subelement.children[1].children[0].data;
if (status === "Not assigned") {
status = "Unassigned";
}
oneThesis.status = status;
}
if (i === 11) {
//TODO: Impact of \n and \t at FE. Remove?
//console.log("Caso 11, General");
let info = subelement.children[1].children[3].children[5];
let observations = info.attribs["data-observations"];
if(observations) {
observations = observations.replace("\t",": ");
}
oneThesis.observations = observations;
let requirements = info.attribs["data-requirements"];
if (requirements) {
requirements = requirements.replace("\t",": ");
requirements = requirements.replace("\n","");
}
oneThesis.requirements = requirements;
let objectives = info.attribs["data-goals"];
oneThesis.objectives = objectives;
let location = info.attribs["data-localization"];
oneThesis.location = location;
let courses = info.attribs["data-degrees"];
oneThesis.courses = courses;
}
}
//Last iteration, push thesis to array.
if (i === 12) {
parsedTheses.push(oneThesis);
}
i++;
});
//console.log("Processed Thesis. ID: "+ oneThesis.id);
//console.log("Processed Thesis Number: "+ parsedTheses.length);
}
//For each thesis
});
//Here one can print after geral computation
//printThesesArray(parsedTheses, "");
//console.log("Number of total parsed thesis is: "+ parsedTheses.length);
//TODO Just save sometimes
//saveFile(parsedTheses,"ParsedTheses");
callback(null, parsedTheses, parsedTheses.length)
}
//End of DOM parser
}, {normalizeWhitespace: true, withStartIndices: true});
let parser = new htmlparser.Parser(handler);
parser.write(data);
parser.end();
//End of reading file callback
});
}
function trainSaveClassifierUsingLink(link, callback) {
//Loads data from link and trains the current classifier. Saves it
}
function trainSaveClassifier(type, callback) {
//TODO use loaded thesis instead of hardcoded solution
//TODO It seems that if several requests are made without restarting the server, classifier.save will
//TODO increment info on the file
let classifier = thesesClassifier.train(type);
if (fs.existsSync(path.join(__dirname, "../../files/Thesis/bClassifier.json"))) {
fs.unlinkSync(path.join(__dirname, "../../files/Thesis/bClassifier.json"));
}
classifier.save(path.join(__dirname, "../../files/Thesis/bClassifier.json"), function(err, classifierS) {
if (err) {
console.log(err);
callback(err,null);
} else {
ba_logger.ba("BA|"+ "TRAIN|" + "CLASSIFIER_SAVED|" + new Date());
}
});
callback(null,classifier);
}
//Parses thesis from HTML document generated by Fenix at:
//https://fenix.tecnico.ulisboa.pt/student/finalists/studentCandidacies
//Receives a raw thesis and returns a categorized thesis
function processThesis (theses, callback) {
// Categorias
//load classifier
//stem, remove stop words
//classify thesis based on req, obj? etc
let classifiedTheses = {};
classifiedTheses.theses = [];
classifiedTheses.projectsNumber = 0;
classifiedTheses.companyNumber = 0;
let thesisNumber = theses.length;
let projectsNumber = 0;
let dissertationNumber = 0;
let companyNumber = 0;
//Receives theses array, and asks the user for classification.
//Sends the classifier
let titlesTheses = [];
//Using Naive Bayes to determine if the thesis is a project or dissertation, and if there is a company evolved
let classifier = thesesClassifier.train(2);
//Build array of objects to store.
for (let i = 0; i < thesisNumber; i++) {
if ((theses[i].title.toLowerCase().includes("projeto")||theses[i].title.toLowerCase().includes("project") ||
theses[i].title.toLowerCase().includes("projecto")) &&
!(theses[i].title.toLowerCase().includes("thesis") || theses[i].title.toLowerCase().includes("dissertação") || theses[i].title.toLowerCase().includes("dissertation") )) {
projectsNumber ++;
theses[i].type = "Project";
theses[i].title = theses[i].title.replace('(PROJECT)','');
theses[i].title = theses[i].title.replace('(Project)','');
theses[i].title = theses[i].title.replace('Project -','');
theses[i].title = theses[i].title.replace('Project:','');
theses[i].title = theses[i].title.replace('PROJECT:','');
theses[i].title = theses[i].title.replace('Projecto:','');
theses[i].title = theses[i].title.replace('PROJECTO:','');
theses[i].title = theses[i].title.replace('Projeto:','');
theses[i].title = theses[i].title.replace('PROJETO:','');
} else {
//Default case
theses[i].title = theses[i].title.replace('Dissertação:','');
theses[i].title = theses[i].title.replace('DISSERTAÇÃO:','');
theses[i].title = theses[i].title.replace('Dissertation:','');
theses[i].title = theses[i].title.replace('Dissertation -','');
theses[i].title = theses[i].title.replace('DISSERTATION:','');
theses[i].title = theses[i].title.replace('(DISSERTATION)','');
theses[i].title = theses[i].title.replace('[Dissertation]','');
theses[i].title = theses[i].title.replace('[Thesis]:','');
theses[i].title = theses[i].title.replace('Thesis:','');
theses[i].title = theses[i].title.replace('[Thesis]','');
theses[i].title = theses[i].title.replace('Thesis -','');
dissertationNumber ++;
theses[i].type = "Dissertation";
}
theses[i].areas = thesesClassifier.getFirstTwoLabels(theses[i],classifier);
//classify project or disseration
}
return callback(null,theses, projectsNumber, dissertationNumber);
}
function addThesis () {
//recebe info de tese, processa-a e adiciona-a a BD
}
//Classifier: An algorithm that maps the input data to a specific category.
//Classification model: A classification model tries to draw some conclusion from the input values given for training. It will predict the class labels/categories for the new data.
//Multi label classification: Classification task where each sample is mapped to a set of target labels (more than one class).
function saveFile(data, name) {
fs.writeFile(path.join(__dirname,"../../files/" + name + ".txt"), util.inspect(data), function(err) {
if(err) {
return console.log(err);
}
ba_logger.ba("BA|"+ "FILE_SAVED|" + name + "|" + new Date());
});
}
function printThesesArray(array, id) {
if (id) {
for (let i = 0; i < array.length; i++) {
if (array[i].id = id) {
console.log("===================");
console.log(array[i]);
console.log("===================");
console.log();
return;
}
}
} else {
for (let i = 0; i < array.length; i++) {
console.log("===================");
console.log(array[i]);
console.log("===================");
console.log();
}
}
}
function printThesesArraySimplified(array, id) {
if (id) {
for (let i = 0; i < array.length; i++) {
if (array[i].id = id) {
console.log("===================");
console.log("ID| " + array[i].id);
console.log("TITLE| " + array[i].title);
console.log("REQ| " + array[i].requirements);
console.log("AREAS| " + array[i].areas);
console.log("===================");
console.log();
return;
}
}
} else {
for (let i = 0; i < array.length; i++) {
console.log("===================");
console.log("ID| " + array[i].id);
console.log("TITLE| " + array[i].title);
console.log("REQ| " + array[i].requirements);
console.log("AREAS| " + array[i].areas);
console.log("===================");
console.log();
}
}
}