-
Notifications
You must be signed in to change notification settings - Fork 0
/
spellchecker.js
85 lines (57 loc) · 1.61 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
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
'use strict';
const argv = require('yargs').argv;
const _ = require('lodash');
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const readline = require('readline');
const Dictionary = require('./dictionary.js');
class SpellChecker {
constructor(wordlist) {
this.dictionary = new Dictionary(wordlist);
this.dictionaryLoaded = false;
this.dictionary.load()
.then(() => {
this.dictionaryLoaded = true;
});
}
load() {
return new Promise((resolve) => {
let interval = setInterval(() => {
if (this.dictionaryLoaded) {
console.log('Dictionary loaded...');
clearInterval(interval);
return resolve();
}
})
});
}
check(search) {
return this.dictionary.has(search);
}
static run() {
let wordlist = argv.wordlist || 'wordlist.txt';
let spck = new SpellChecker(wordlist);
const rl = Promise.promisifyAll(readline.createInterface({
input: process.stdin,
output: process.stdout
}));
spck.load()
.then(() => {
promptForSearch();
function promptForSearch(dictionary) {
rl.question('Enter a word [CTRL+C to exit]: ', (search) => {
if (spck.check(search)) {
console.log(`${search} is spelled correctly`);
} else {
console.log(`${search} is misspelled`);
}
return promptForSearch();
});
}
});
}
}
module.exports = SpellChecker;
if (require.main === module) {
SpellChecker.run();
}