-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
163 lines (117 loc) 路 3.96 KB
/
Copy pathmain.js
File metadata and controls
163 lines (117 loc) 路 3.96 KB
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
import fs from 'fs'
import { readdir } from 'fs/promises'
import path from 'path'
import { fileURLToPath } from 'url'
import { exit } from 'process'
import chalk from 'chalk'
import { confirm, select } from '@inquirer/prompts'
import csv from 'csv-parser'
import Table from 'cli-table3'
import singleFeatureProcess from './single-feature.js'
import multiFeatureProcess from './multi-feature.js'
// Simplify std out from `console.log` into `print`
const print = console.log;
const DATA_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', '..', 'data');
/////////////////////////////////////////////////////////////////////
//// 6MMMMb\ MMMMMMMMMM dM. `MMMMMMMb. MMMMMMMMMM /////
//// 6M' ` / MM \ ,MMb MM `Mb / MM \ /////
//// MM MM d'YM. MM MM MM /////
//// YM. MM ,P `Mb MM MM MM /////
//// YMMMMb MM d' YM. MM .M9 MM /////
//// `Mb MM ,P `Mb MMMMMMM9' MM /////
//// MM MM d' YM. MM \M\ MM /////
//// MM MM ,MMMMMMMMb MM \M\ MM /////
//// L ,M9 MM d' YM. MM \M\ MM /////
//// MYMMMM9 _MM_ _dM_ _dMM__MM_ \M\_ _MM_ /////
/////////////////////////////////////////////////////////////////////
do {
console.clear()
print(
`==========================
WELCOME!
==========================
SIMPLE LINEAR REGRESSION DEMO
`)
const answers = await select({
message: 'Choose menu',
choices: [
'1. Guide',
'2. Run Demo',
'3. Exit'
]
})
console.clear()
switch (answers) {
case '1. Guide': await showGuide(); break;
case '2. Run Demo': await runDemo(); break;
case '3. Exit': print('Thanks, bye!'); exit(0);
}
} while (await confirm({ message: 'Continue program?' }))
async function showGuide() {
print('This is a guide for this program!');
for (let i = 1; i <= 5; i++) {
print('Data: ', i)
}
if (!await confirm({ message: 'Back?'})) {
exit(0)
}
}
async function runDemo() {
let files
try {
print('Scanning data input file ...')
files = await readdir(DATA_DIR)
} catch (error) {
print(chalk.red('Error reading directory: ', error.message))
return;
}
const answers = await select({
message: 'Choose data',
choices: files
})
if (await confirm({ message: 'Preview data?'})) {
await previewData(answers)
}
}
function readFileAsync(filePath) {
return new Promise((resolve, reject) => {
const data = []
fs.createReadStream(filePath)
.pipe(csv())
.on('data', (row) => data.push(row))
.on('end', () => resolve(data))
.on('error', (error) => reject(error))
})
}
async function previewData(filePath) {
print('\n-----------------------------------------------------\n')
print('Preview data: ', filePath)
const data = await readFileAsync(path.join(DATA_DIR, filePath))
const table = new Table({
head: Object.keys(data[0])
})
data.forEach(row => {
table.push(Object.values(row))
})
print(table.toString())
if (await confirm({ message: 'Continue next process' })) {
await dataIdentification(data)
}
}
async function dataIdentification(data) {
console.clear()
print('-----------------------------------------------------\n')
print('Data identification')
const answers = await select({
message: 'Choose model type',
choices: [
'1. Single feature',
'2. Multi feature'
]
})
console.clear()
switch (answers) {
case '1. Single feature': await singleFeatureProcess(data); break;
case '2. Multi feature': await multiFeatureProcess(data); break;
}
}