-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.js
80 lines (56 loc) · 2.09 KB
/
code.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
var isStoppword = require('./stopwords.json');
let expression = require('./expression')
var fs = require('fs'),
path = require('path')
filePath = path.join(__dirname, 'documentos/');
let scanf = require('scanf');
let remover_stoppwords = (tokens) => {
var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
let indices = [];
tokens.forEach(element => {
if (!isStoppword[element] && !format.test(element))
indices.push(element.replace(',;.', ''))
});
console.log(indices)
return indices;
}
const testFolder = './documentos/';
console.log("Digite a query de busca: exemplo: Javascript AND bananas AND(tomates AND morangos)")
let query = scanf("%S");
async function decidirArquivo(file){
return new Promise(function (resolveArquivo, rejectArquivo) {
fs.readFile(filePath + file, {
encoding: 'utf-8'
}, function (err, data) {
if (!err) {
let tokens = data.match(/\S+/g)
let indices = remover_stoppwords(tokens);
let string_de_indices = "";
indices.forEach((el) => {
string_de_indices += "" + el + " "
})
var parsed = new expression(query);
let relevante = parsed.test(string_de_indices);
resolveArquivo(relevante ? file : null)
} else {
console.log(err);
}
});
})
}
async function lerDiretorio(){
return new Promise(function (resolveDiretorio, rejectDiretorio) {
let arquivosRelevantes = []
fs.readdir(testFolder,async (err, files) => {
for(let i = 0 ;i<files.length;i++){
let leitura = await decidirArquivo(files[i])
if(leitura!=null)
arquivosRelevantes.push(leitura)
}
resolveDiretorio(arquivosRelevantes)
})
})
}
lerDiretorio().then(arquivosRelevantes=>{
console.log('arquivosRelevantes: ',arquivosRelevantes)
})