-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspellchecker.js
executable file
·61 lines (48 loc) · 1.81 KB
/
spellchecker.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
#! /usr/bin/env node
import fs from 'fs'
import chalk from 'chalk'
import { createSpinner } from 'nanospinner'
import boxen from 'boxen'
import { createSpellChecker } from './lib/spellingUtils.js'
import figlet from 'figlet'
const dictionaryPath = process.argv[2]
const textFilePath = process.argv[3]
if (!dictionaryPath || !textFilePath) {
console.log(chalk.red('Please provide a dictionary and a text file'))
process.exit(1)
}
const wait = (ms = 1000) => new Promise((resolve) => setTimeout(resolve, ms))
const spellChecker = createSpellChecker(dictionaryPath)
const dictionaryProgress = createSpinner(`Loading ${spellChecker.dictionaryWordCount} words from dictionary`)
dictionaryProgress.start()
await wait(1000)
dictionaryProgress.success()
const textFileProgress = createSpinner('Reading text file')
const text = fs.readFileSync(textFilePath, 'utf8')
textFileProgress.start()
await wait(1000)
textFileProgress.success()
const checkSpellingProgress = createSpinner('Checking spelling')
checkSpellingProgress.start()
await wait(1000)
const errors = spellChecker.checkSpelling(text)
checkSpellingProgress.success()
if (errors.length > 0) {
console.log(chalk.red(`Found ${errors.length} errors`))
console.log()
errors.forEach((error) => {
console.log(boxen(`${chalk.dim(error.context.left)} ${error.context.word} ${chalk.dim(error.context.right)}`, {padding: 1, title: error.word, borderColor: 'yellow'}))
console.log(chalk.blue(`Found at line ${error.line}, column ${error.column}`))
console.log(`Suggestions: ${chalk.cyan(error.suggestions.join(', '))}`)
console.log()
})
process.exit(0)
}
console.log(chalk.green('No spelling errors found'))
figlet('Nice job!', (err, data) => {
if (err) {
console.log(chalk.red('Error rendering ASCII art'))
process.exit(1)
}
console.log(chalk.green(data))
})