|
1 | 1 | import fs from 'fs' |
2 | 2 | import path from 'path' |
3 | 3 | import { fileURLToPath } from 'url' |
| 4 | +import { exit } from 'process' |
4 | 5 | import chalk from 'chalk' |
5 | 6 | import readlineSync from 'readline-sync' |
6 | 7 |
|
7 | 8 |
|
8 | 9 |
|
9 | 10 |
|
| 11 | +// Simplify std out from `console.log` into `print` |
| 12 | +const print = console.log |
| 13 | + |
| 14 | + |
10 | 15 | /** |
11 | 16 | * TODO: |
12 | 17 | * |
13 | | - * - Just complete the program flow based on some console.log below [0/7] |
| 18 | + * - Just complete the program flow based on some print below [0/7] |
14 | 19 | */ |
15 | 20 |
|
16 | 21 |
|
17 | 22 | const dataDir = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'data') |
18 | 23 |
|
| 24 | +const showGuide = () => { |
| 25 | + print('This is a guide for this program!'); |
| 26 | + for (let i = 1; i <= 5; i++) { |
| 27 | + print('Data: ', i) |
| 28 | + } |
19 | 29 |
|
20 | | -let confirmation = 0 |
21 | | -do { |
22 | | - console.log(` |
23 | | -========================== |
24 | | - WELCOME! |
25 | | -========================== |
26 | | -SIMPLE LINEAR REGRESSION DEMO |
27 | | - `) |
28 | | - |
29 | | - // 1 |
30 | | - console.log('Show menu') |
31 | 30 |
|
32 | | - console.log(` |
33 | | -========================== |
34 | | -1. Guide |
35 | | -2. Run demo |
36 | | -========================== |
37 | | -`) |
| 31 | + if (!readlineSync.keyInYNStrict(chalk.green('Back?'))) { |
| 32 | + exit(0) |
| 33 | + } |
| 34 | +} |
38 | 35 |
|
| 36 | +const runDemo = () => { |
39 | 37 | let files |
40 | 38 | try { |
41 | | - console.log('Scanning data input file ...') |
| 39 | + print('Scanning data input file ...') |
42 | 40 | files = fs.readdirSync(dataDir) |
43 | 41 |
|
44 | | - console.log('Choose file to do simple demo linear regression: ') |
| 42 | + print('Choose file to do simple demo linear regression: ') |
45 | 43 | files.forEach(file => { |
46 | | - console.log(chalk.blue('- ', file)) |
| 44 | + print(chalk.blue('- ', file)) |
47 | 45 | }) |
48 | 46 | } catch (error) { |
49 | | - console.error(chalk.red('Error reading directory: ', error.message)) |
| 47 | + print(chalk.red('Error reading directory: ', error.message)) |
| 48 | + return; |
50 | 49 | } |
51 | 50 |
|
| 51 | + if (readlineSync.keyInYNStrict(chalk.green('Preview data?'))) { |
| 52 | + previewData('data passed') |
| 53 | + } |
52 | 54 |
|
53 | | - // 2 |
54 | | - console.log('Preview data') |
| 55 | +} |
| 56 | + |
| 57 | +const previewData = (data) => { |
| 58 | + print('Preview data: ', data) |
| 59 | + |
| 60 | + if (readlineSync.keyInYNStrict(chalk.green('Continue next process?'))) { |
| 61 | + dataIdentification() |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +const dataIdentification = () => { |
| 69 | + print('Data identification!') |
| 70 | + const feature = readlineSync.question('Choose feature: ') |
| 71 | + const label = readlineSync.question('Choose label: ') |
| 72 | + |
| 73 | + if (readlineSync.keyInYNStrict(chalk.green('Continue process data?'))) { |
| 74 | + processing() |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +const processing = () => { |
| 82 | + print('Model processing!') |
| 83 | + for (let i = 1; i <= 20; i++) { |
| 84 | + print('iteration: ', i) |
| 85 | + } |
| 86 | + |
| 87 | + if (readlineSync.keyInYNStrict(chalk.green('Show visualization: '))) { |
| 88 | + visualization() |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | + |
55 | 93 |
|
56 | | - // 3 |
57 | | - console.log('Choose feature') |
58 | 94 |
|
59 | | - // 4 |
60 | | - console.log('Choose target') |
61 | 95 |
|
62 | | - // 5 |
63 | | - console.log('Processing...') |
| 96 | +const visualization = () => { |
| 97 | + print('Visualization here!') |
| 98 | + print('Graph, chart, or whatever...') |
64 | 99 |
|
65 | | - // 6 |
66 | | - console.log('Show the linear equation result') |
67 | 100 |
|
68 | 101 | // 7 |
69 | | - console.log('Show the prove of linear equation is actually match with user data') |
| 102 | + print('Show the prove of linear equation is actually match with user data') |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +///////////////////////////////////////////////////////////////////// |
| 112 | +//// 6MMMMb\ MMMMMMMMMM dM. `MMMMMMMb. MMMMMMMMMM ///// |
| 113 | +//// 6M' ` / MM \ ,MMb MM `Mb / MM \ ///// |
| 114 | +//// MM MM d'YM. MM MM MM ///// |
| 115 | +//// YM. MM ,P `Mb MM MM MM ///// |
| 116 | +//// YMMMMb MM d' YM. MM .M9 MM ///// |
| 117 | +//// `Mb MM ,P `Mb MMMMMMM9' MM ///// |
| 118 | +//// MM MM d' YM. MM \M\ MM ///// |
| 119 | +//// MM MM ,MMMMMMMMb MM \M\ MM ///// |
| 120 | +//// L ,M9 MM d' YM. MM \M\ MM ///// |
| 121 | +//// MYMMMM9 _MM_ _dM_ _dMM__MM_ \M\_ _MM_ ///// |
| 122 | +///////////////////////////////////////////////////////////////////// |
70 | 123 |
|
71 | | - confirmation = readlineSync.questionInt('Continue? ') |
72 | | -} while (confirmation == 0); |
| 124 | +do { |
| 125 | + console.clear() |
| 126 | + print(` |
| 127 | +========================== |
| 128 | + WELCOME! |
| 129 | +========================== |
| 130 | +SIMPLE LINEAR REGRESSION DEMO |
| 131 | +========================== |
| 132 | +-----------MENU----------- |
| 133 | +========================== |
| 134 | +1. Guide |
| 135 | +2. Run demo |
| 136 | +3. Exit |
| 137 | +========================== |
| 138 | + `) |
| 139 | + |
| 140 | + const selectedMenu = readlineSync.questionInt(chalk.green('Choose menu: ')) |
| 141 | + console.clear() |
| 142 | + |
| 143 | + switch (selectedMenu) { |
| 144 | + case 1: |
| 145 | + showGuide() |
| 146 | + break; |
| 147 | + case 2: |
| 148 | + runDemo() |
| 149 | + break; |
| 150 | + case 3: |
| 151 | + print('Bye!') |
| 152 | + exit(0); |
| 153 | + break; |
| 154 | + default: |
| 155 | + print('Invalid input! Menu can not be found!') |
| 156 | + break; |
| 157 | + } |
73 | 158 |
|
| 159 | +} while (readlineSync.keyInYNStrict(chalk.green('\nContinue program? '))) |
0 commit comments