-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
73 lines (60 loc) · 2.32 KB
/
main.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
const TimePrecision = require('./boolean_search_modules/TimePrecision/time-precision');
const TextDocument = require('./boolean_search_modules/TextDocument/text-document');
const TextCollection = require('./boolean_search_modules/TextCollection/text-collection');
const InvertedIndex = require('./boolean_search_modules/InvertedIndex/inverted-index');
const Config = require('./main-cli-config.js');
const readline = require('readline');
const fse = require('fs-extra');
const events = require('events');
var eventEmitter = new events.EventEmitter();
console.log(Config.intro);
// get list of text files to index
const textFilePaths = Config.textFiles;
// get text transformation options
const textTransformOptions = Config.textCleaningFlags;
// define a TextCollection object
let textCollection = new TextCollection();
// define an InvertedIndex objet
let invertedIndex = new InvertedIndex();
// build collection of tokenized documents
let lastPathIdx = textFilePaths.length - 1;
textFilePaths.forEach((path, idx) => {
let textDoc = new TextDocument(path);
textDoc.tokenizeAndCountDocument(textTransformOptions, function () {
textCollection.addDocument(textDoc);
if (idx === lastPathIdx) eventEmitter.emit('tokenization:complete');
});
});
// index TextDocuments once tokenization is complete
eventEmitter.on('tokenization:complete', function () {
console.log('\nTokenization complete');
const documentsToIndex = textCollection.getDocumentList();
const documentIDs = Object.keys(documentsToIndex);
documentIDs.forEach(id => invertedIndex.indexDocument(documentsToIndex[id]));
invertedIndex.sortPostingsByFrequency();
this.emit('indexing:complete');
});
// prompt user for search word once indexing is complete
eventEmitter.on('indexing:complete', function () {
console.log('\nIndexing Complete');
promptUser();
});
// create readline cli and define listeners
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: '\nEnter a word to search for, or enter blank string to quit:\n'
});
rl.on('line', function (response) {
if (response === '') this.close();
else {
console.log(invertedIndex.searchTerm(response));
rl.prompt();
}
}).on('close', function () {
console.log('\n\nGoodbye\n\n');
});
// prompt user to enter a search word
function promptUser() {
rl.prompt();
}