-
Notifications
You must be signed in to change notification settings - Fork 0
/
JokeBot.coffee
36 lines (29 loc) · 984 Bytes
/
JokeBot.coffee
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
fs = require "fs"
getRandom = (arr) ->
arr[Math.floor(Math.random() * arr.length)]
class fortuneGenerator
constructor: (@filename) ->
generateFortune: ->
followMap = JSON.parse(fs.readFileSync("#{@filename}.json", 'utf8'));
starters = JSON.parse(fs.readFileSync("#{@filename}.starters.json", 'utf8'));
output = ""
prevPair= getRandom(starters.data)
current = prevPair.split(" ")[1]
prevWord = prevPair.split(" ")[0]
while current isnt "--EOJ--"
output = @handleOutput output, prevWord
prevWord = current
current = getRandom followMap["#{prevPair.toLowerCase()}"]
prevPair = "#{prevWord} #{current}"
output = @handleOutput output, prevWord
return output
handleOutput: (prev, current) ->
out = ""
if prev is ""
out = "#{current}"
else if current.match /^\s+$/
out = "#{prev}#{current}"
else
out = "#{prev} #{current}"
out
module.exports = new fortuneGenerator("openbsd")