-
Notifications
You must be signed in to change notification settings - Fork 1
/
dict.js
executable file
·275 lines (222 loc) · 6.4 KB
/
dict.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
#!/usr/bin/env node
require('dotenv').config()
const axios = require('axios')
const readline = require('readline')
const key = require('./key/key.js')
const type = process.argv[2] || 'wotd'
const queryWord = process.argv[3]
// Command definitions
var commands = {}
commands.syn = async (parsedWord) => {
var url = `/v4/word.json/${parsedWord}/relatedWords?relationshipTypes=synonym&api_key=${key}`
var txt = `Synonyms for '${parsedWord}' are :\n`
await getData(url)
.then(response => processData(response.data, 'syn'))
.then(response => print(response, txt, 'syn'))
.catch(err => console.log(err))
return Promise.resolve()
}
commands.ant = async (parsedWord) => {
var url = `/v4/word.json/${parsedWord}/relatedWords?relationshipTypes=antonym&api_key=${key}`
var txt = `Antonyms for '${parsedWord}' are :\n`
await getData(url)
.then(response => processData(response.data, 'ant'))
.then(response => print(response, txt, 'ant'))
.catch(err => console.log(err))
return Promise.resolve()
}
commands.def = async (parsedWord) => {
var url = `/v4/word.json/${parsedWord}/definitions?limit=5&api_key=${key}`
var txt = `Definitions for '${parsedWord}' are :\n`
await getData(url)
.then(response => processData(response.data, 'def'))
.then(response => print(response, txt, 'def'))
.catch(err => console.log(err))
return Promise.resolve()
}
commands.ex = async (parsedWord) => {
var url = `/v4/word.json/${parsedWord}/examples?limit=5&api_key=${key}`
var txt = `Examples for '${parsedWord}' are :\n`
await getData(url)
.then(response => processData(response.data, 'ex'))
.then(response => print(response, txt, 'ex'))
.catch(err => console.log(err))
return Promise.resolve()
}
commands.wotd = async () => {
var date = new Date()
date = `${date.getUTCFullYear()}-${(date.getMonth() + 1)}-${(date.getDate())}`
var url = `/v4/words.json/wordOfTheDay?date=${date}&api_key=${key}`
var txt = 'Word of the day is : '
var parsedWord
await getData(url)
.then(response => {
parsedWord = response.data.word
console.log(`${txt} ${parsedWord}\n`)
})
.catch(err => console.log(err))
commands['dict'](parsedWord)
}
commands.dict = async (parsedWord) => {
await commands['def'](parsedWord)
await commands['syn'](parsedWord)
await commands['ant'](parsedWord)
await commands['ex'](parsedWord)
}
commands.play = () => {
var url = `/v4/words.json/randomWord?hasDictionaryDef=true&minCorpusCount=0&maxCorpusCount=-1&minDictionaryCount=1&maxDictionaryCount=-1&minLength=2&maxLength=8&api_key=${key}`
console.log('\nNew Game!')
getData(url)
.then(response => playFunction(response.data.word))
.catch(err => console.log(err))
}
// play functions
class HintStorage {
constructor () {
this.def = []
this.syn = []
this.ant = []
}
clean () {
this.def = []
this.syn = []
this.ant = []
}
}
var hintStore = new HintStorage()
async function playFunction (randomWord) {
hintStore.clean()
await Promise.all([
commands['def'](randomWord),
commands['syn'](randomWord),
commands['ant'](randomWord)
])
gameOn(true, randomWord)
}
// main game
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
function gameOn (showHint, randomWord) {
var clue = showHint ? hinter(randomWord) : ''
rl.question(`\n${clue} \nGuess the word: `, answer => {
if (answer === randomWord || hintStore['syn'].includes(answer)) {
rl.question(`You guessed right! Play again? (Y/N) : `, subAnswer => {
if (subAnswer.toUpperCase() === 'Y') {
commands.play()
} else {
rl.close()
}
})
} else {
console.log(`You guessed wrong :/
1: Guess again
2: New hint
3: Show word and quit
4: Show word and play again
`)
wrongGuess(randomWord)
}
})
}
// options displayed on wrong guess
function wrongGuess (randomWord) {
rl.question('Enter your option\n', function (answer) {
if (answer === '1') {
console.log('Guess again')
gameOn(false, randomWord)
} else if (answer === '2') {
console.log('New hint')
gameOn(true, randomWord)
} else if (answer === '3') {
console.log('Show word and quit')
console.log(randomWord)
rl.close()
} else if (answer === '4') {
console.log('Show word and play again')
console.log(randomWord)
commands.play()
} else {
console.log('Invalid option')
wrongGuess(randomWord)
}
})
}
// hint generator
function hinter (parsedWord) {
var hints = Object.keys(hintStore)
var coinFlip = Math.floor(Math.random() * 2)
if (coinFlip) {
do {
var hintType = Math.floor(Math.random() * 3)
var row = hintStore[hints[hintType]]
} while (!row.length)
var hintIndex = Math.floor(Math.random() * row.length)
return `${expand(hints[hintType])} : ${row[hintIndex]}`
} else {
return `Jumbled word : ${jumble(parsedWord)}`
}
}
// expand hint type
function expand (inType) {
if (inType === 'syn') {
return 'Synonym'
} else if (inType === '') {
return 'Antonym'
} else {
return 'Definition'
}
}
// word jumble
function jumble (word) {
word = word.split('')
for (var i = word.length - 1; i >= 0; i--) {
var rand = Math.floor(Math.random() * i)
var temp = word[i]
word[i] = word[rand]
word[rand] = temp
}
word = word.join('')
return word
}
// GET request
function getData (url) {
return axios({
url: url,
baseURL: 'http://api.wordnik.com',
method: 'get'
})
}
// data formatting
function processData (response, id) {
if (id === 'ex') {
response = response.examples.map(example => example.text)
} else if (id === 'def') {
response = response.map(definition => definition.text)
} else if (response.length) {
response = response[0].words
}
if (type !== 'play') {
response = response.reduce((acc, cur, index) => acc + `${index + 1}) ${cur}\n`, '\n')
rl.close()
}
return Promise.resolve(response)
}
// print function
function print (response, txt, id) {
if (type !== 'play') {
console.log(txt + response)
} else {
hintStore[id] = response
}
}
// execute command
if (key === 'api key value') {
console.log('Invalid API key. Add a valid key in /key/key.js')
} else if (typeof commands[type] === 'function') {
commands[type](queryWord)
} else {
console.log('Not a valid option')
rl.close()
}