This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
335 lines (280 loc) · 10.4 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
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
/*
* This module provides the main functions that output .
* Copyright 2021 © Joey Malvinni
* License: Apache
*/
/**
* @license
* Copyright 2021 © Joey Malvinni. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
////////////////////////////////////////////////////////////
// //
// INDEX.js //
// //
// This file takes the classifier.json file //
// outputted by the generator and exports //
// three functions, the first, which generates //
// the acronyms and outputs them to a file, //
// the second, which generates the acronyms //
// with the classifier and returns them within //
// an array as a promise, and the last, //
// to which you input an array and get //
// an object back determining whether the //
// string is a good acronym or not. //
// //
////////////////////////////////////////////////////////////
var bayes = require('bayes')
let fs = require('fs')
const readline = require("readline");
let util = require('util');
var crypto = require("crypto");
let isBad = require('./src/bad_words')
if(process.env.npm_config_output && process.env.npm_config_output === 'file'){
outputToFile()
}
if(process.env.npm_config_output && process.env.npm_config_output === 'console'){
generate().then(data =>{
data.forEach(line => {
console.log(line)
})
})
}
if(process.env.npm_config_acronym){
isGoodAcronym(process.env.npm_config_acronym).then(console.log)
}
async function isGoodAcronym(acronym){
let { getClassifier, changeClassifier } = require('./src/classifier.js')
let classifier;
const readFile = util.promisify(fs.readFile)
if(fs.existsSync(`./default_data/${getClassifier()}.json`)) {
let stats = await readFile('./default_data/classifierJSON.json')
classifier = bayes.fromJson(stats)
let bad = fs.readFileSync('./default_data/bad.txt').toString().split('\n')
let good = fs.readFileSync('./default_data/good.txt').toString().split('\n')
for(let i = 0; i < bad.length; i++){
try {
await classifier.learn(bad[i], 'n')
await classifier.learn(bad[i], 'n')
await classifier.learn(bad[i], 'n')
} catch(err){
// :(
}
}
for(let i = 0; i < good.length; i++){
try {
await classifier.learn(good[i], 'y')
await classifier.learn(good[i], 'y')
await classifier.learn(good[i], 'y')
} catch(err){
// :(
}
}
let goodN = fs.readFileSync('./gen_good.txt').toString().split('\n')
for(let i = 0; i < good.length; i++){
try {
await classifier.learn(goodN[i], 'y')
await classifier.learn(goodN[i], 'y')
await classifier.learn(goodN[i], 'y')
} catch(err){
}
}
} else {
firstPrompt('Classifier file does not exist. Please try again ')
}
function end(){
return new Promise(async (resolve, reject)=>{
let result = await classifier.categorize(acronym)
let classified_data = result === 'y' ? true : false;
let data = await isBad(acronym);
let acronymLetters = acronym.split(' ').map(i => i.charAt(0)).join('')
let isCorrectAcronym;
if(acronymLetters === 'NPM'){
isCorrectAcronym = true;
} else {
isCorrectAcronym = false;
}
resolve({
inappropriate: data,
good: classified_data,
NPM: isCorrectAcronym
})
})
}
return end()
}
function generate(){
return new Promise(async (resolve, reject)=>{
let generate = require('./src/index')
let classifier;
const readFile = util.promisify(fs.readFile)
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function ask(question){
return new Promise((resolve, reject)=>{
rl.question(question, function(answer) {
resolve(answer)
})
})
}
async function firstPrompt(question){
let data = await ask(`${question}> `)
checkFile(data)
}
let { getClassifier } = require('./src/classifier.js')
checkFile(getClassifier())
async function checkFile(file){
if(fs.existsSync(`./default_data/${file}.json`)) {
let stats = await readFile('./default_data/classifierJSON.json')
classifier = bayes.fromJson(stats)
let bad = fs.readFileSync('./default_data/bad.txt').toString().split('\n')
let good = fs.readFileSync('./default_data/good.txt').toString().split('\n')
for(let i = 0; i < bad.length; i++){
try {
await classifier.learn(bad[i], 'n')
} catch(err){
// :(
}
}
for(let i = 0; i < good.length; i++){
try {
await classifier.learn(good[i], 'y')
} catch(err){
// :(
}
}
let goodN = fs.readFileSync('./gen_good.txt').toString().split('\n')
for(let i = 0; i < good.length; i++){
try {
await classifier.learn(goodN[i], 'y')
} catch(err){
}
}
end()
} else {
firstPrompt('Classifier file does not exist. Please try again ')
}
}
async function end(){
let data = generate()
let array = [];
for(let i = 0; i <= data.length; i++){
if(i !== data.length){
let result = await classifier.categorize(data[i])
if(result === 'y'){
array.push(data[i])
}
} else {
resolve(array)
}
}
}
})
}
async function outputToFile(){
let generate = require('./src/index')
let classifier;
const readFile = util.promisify(fs.readFile)
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function ask(question){
return new Promise((resolve, reject)=>{
rl.question(question, function(answer) {
resolve(answer)
})
})
}
async function firstPrompt(question){
let data = await ask(`${question}> `)
checkFile(data)
}
let { getClassifier } = require('./src/classifier.js')
checkFile(getClassifier())
async function checkFile(file){
if(fs.existsSync(`./default_data/${file}.json`)) {
let stats = await readFile('./default_data/classifierJSON.json')
classifier = bayes.fromJson(stats)
let bad = fs.readFileSync('./default_data/bad.txt').toString().split('\n')
let good = fs.readFileSync('./default_data/good.txt').toString().split('\n')
for(let i = 0; i < bad.length; i++){
try {
await classifier.learn(bad[i], 'n')
} catch(err){
// :(
}
}
for(let i = 0; i < good.length; i++){
try {
await classifier.learn(good[i], 'y')
} catch(err){
// :(
}
}
let goodN = fs.readFileSync('./gen_good.txt').toString().split('\n')
for(let i = 0; i < good.length; i++){
try {
await classifier.learn(goodN[i], 'y')
} catch(err){
}
}
end()
} else {
firstPrompt('Classifier file does not exist. Please try again ')
}
}
async function end(){
let data = generate()
var id = crypto.randomBytes(7).toString('hex');
let file = fs.readdirSync(__dirname + `\\output\\`)[0]
if(fs.existsSync(__dirname + '\\output\\' + file)){
let rename = util.promisify(fs.rename);
await rename(__dirname + '\\output\\' + file, __dirname + '\\archived_outputs\\' + file)
}
let stream = fs.createWriteStream(__dirname + `\\output\\${id}-output.txt`)
for(let i = 0; i < data.length; i++){
let result = await classifier.categorize(data[i])
if(result === 'y'){
stream.write(data[i] + '\n')
}
}
stream.on('error', function(err) {
console.log(err);
stream.end();
});
stream.end()
setTimeout(() => {
let exists = fs.existsSync(__dirname + `\\output\\${id}-output.txt`)
if(exists){
console.log('Generated ./output/' + id + '-output.txt')
} else {
throw `Couldn't generate file.`
}
process.exit()
}, 1000);
}
}
function isAcronym(word){
return Object.assign(
isGoodAcronym(word), {
generate,
outputToFile,
isGoodAcronym
}
)
};
module.exports = {isGoodAcronym, generate}