/
marky_markov
executable file
·91 lines (80 loc) · 2.85 KB
/
marky_markov
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
#!/usr/bin/env ruby -i
#A Markov Chain generator.
require 'optparse'
require 'marky_markov'
options = {}
opt_parser = OptionParser.new do |opts|
opts.banner = "Usage: marky-markov COMMAND [OPTIONS]"
opts.separator ""
opts.separator "Commands:"
opts.separator " speak: Generate Markov Chain sentence (default wordcount of 200)"
opts.separator " listen [sentence]: Generate Markov Chain sentence from supplied string."
opts.separator " read [file]: Add words to dictionary from supplied text file"
opts.separator ""
opts.separator "Options"
options[:dictionary] = "#{ENV["HOME"]}/.marky_markov_dictionary"
opts.on('-l', '--location DICTIONARY', 'Use custom dictionary location') do |file|
options[:dictionary] = file
end
options[:wordcount] = 200
opts.on('-w', '--wordcount NUMBER', 'Set number of words generated') do |number|
options[:wordcount] = number.to_i
end
options[:source] = nil
opts.on('-s', '--source FILE',
'Generate and use temporary dictionary from source text') do |file|
options[:source] = file
end
options[:depth] = 2
opts.on('-d', '--depth 1-9', 'Set dictionary depth. The higher the number
, the less random the sentence will be. Must be between 1 and 9.') do |number|
options[:depth] = number.to_i
end
options[:resetdictionary] = false
opts.on('--reset', "WARNING: Deletes default dictionary." ) do
options[:resetdictionary] = true
end
opts.on('-h', '--help', 'Display this screen') do
STDOUT.puts opt_parser
exit
end
end
opt_parser.parse!
if options[:resetdictionary]
STDOUT.puts MarkyMarkov::Dictionary.delete_dictionary!(options[:dictionary])
exit
end
case ARGV[0]
when "speak"
if options[:source]
markov = MarkyMarkov::TemporaryDictionary.new(options[:depth])
markov.parse_file(options[:source])
else
unless File.exists?("#{options[:dictionary]}.mmd")
STDERR.puts "Dictionary file #{options[:dictionary]}}.mmd does not exist. Cannot generate sentence."
STDERR.puts "Please build a dictionary with read or use the --source option with speak."
exit(false)
end
markov = MarkyMarkov::Dictionary.new(options[:dictionary])
end
STDOUT.puts markov.generate_n_words(options[:wordcount])
when "read"
source = ARGV[1] || options[:source]
markov = MarkyMarkov::Dictionary.new(options[:dictionary], options[:depth])
markov.parse_file(source)
markov.save_dictionary!
STDOUT.puts "Added #{source} to dictionary."
when "listen"
markov = MarkyMarkov::TemporaryDictionary.new
markov.parse_string(STDIN.tty? ? ARGV[1] : STDIN.read)
STDOUT.puts markov.generate_n_words(options[:wordcount])
else
unless STDIN.tty?
markov = MarkyMarkov::TemporaryDictionary.new(options[:depth])
markov.parse_string(STDIN.read)
STDOUT.puts markov.generate_n_words(options[:wordcount])
else
STDOUT.puts opt_parser
end
end
#end