Class: Markov::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/markov/cli.rb

Instance Method Summary collapse

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 options['out_json'] # || options['out_markov']

  order = options.fetch('order',6).to_i
  new_words = options['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 = options['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 = options.fetch('max', 1_000).to_i
  start_text = options.fetch('start_text', "")

  say "Sampling #{max} characters."
  say "With drama!" if options['show']
  say "Using start text:\n#{start_text}" if start_text

  say "==== BEGIN SAMPLE ===="
  text = chain.generate!(max: max, show: options['show'], start_text: start_text)
  puts text unless options['show']
  say "==== END SAMPLE ===="
end

#serve(*analysis_files) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/markov/cli.rb', line 57

def serve(*analysis_files)
  Server.configure do |config|
    config.set :chains, analysis_files.map { |f| load_chain(f) }
    config.set :port, options['p'] if options['p']
  end

  Server.run!
end