Class: Markov::CLI
- Inherits:
-
Thor
- Object
- Thor
- Markov::CLI
- Defined in:
- lib/markov/cli.rb
Instance Method Summary collapse
- #analyze(*source_file_paths) ⇒ Object
- #sample(analysis_file = "data/shakespeare.json") ⇒ Object
- #serve(*analysis_files) ⇒ Object
Instance Method Details
#analyze(*source_file_paths) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/markov/cli.rb', line 12 def analyze(*source_file_paths) raise "Please provide an --out_json options!" unless ['out_json'] # || options['out_markov'] order = .fetch('order',6).to_i new_words = ['new_words'] say "Constructing chain of order #{order}" chain = Chain.new(name: source_file_paths.map { |f| File.basename(f,'.txt') }.join('/'), order: order, new_words: new_words) source_file_paths.each do |source_file_path| say "Reading #{source_file_path} into memory..." text = File.read(source_file_path) say "Parsing #{source_file_path}..." chain.parse(text) end outfile = ['out_json'] say "Writing JSON data to #{outfile}..." File.write(outfile, Oj.dump(chain)) end |
#sample(analysis_file = "data/shakespeare.json") ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/markov/cli.rb', line 38 def sample(analysis_file="data/shakespeare.json") say "Reading analysis files..." chain = load_chain(analysis_file) max = .fetch('max', 1_000).to_i start_text = .fetch('start_text', "") say "Sampling #{max} characters." say "With drama!" if ['show'] say "Using start text:\n#{start_text}" if start_text say "==== BEGIN SAMPLE ====" text = chain.generate!(max: max, show: ['show'], start_text: start_text) puts text unless ['show'] say "==== END SAMPLE ====" end |