class Plasper::Runner

Public Class Methods

new(argv) click to toggle source
# File lib/Plasper/runner.rb, line 6
def initialize(argv)
  @plasper = Plasper.new
  @options = Options.new(argv)
end

Public Instance Methods

run() click to toggle source
# File lib/Plasper/runner.rb, line 11
def run
  import_weights @options.weights_file unless @options.weights_file.nil?
  import_text @options.text_file unless @options.text_file.nil?
  action = @options.action.to_sym
  send action if respond_to? action, true
  export_weights @options.output_file unless @options.output_file.nil?
end

Private Instance Methods

chat() click to toggle source
# File lib/Plasper/runner.rb, line 29
def chat
  puts 'Use ^D to exit.'
  print 'Ego: '
  while (input = STDIN.gets)
    @plasper << input
    if input.index(/\s+/).nil?
      output = @plasper.word
    elsif input.scan(Plasper::SENTENCE_DELIMITER).length < 2
      output = @plasper.sentence
    else
      output = @plasper.passage
    end

    puts " Id: #{output}"
    print 'Ego: '
  end
  puts
end
dump() click to toggle source
# File lib/Plasper/runner.rb, line 21
def dump
  dump_weights STDOUT
end
dump_weights(output) click to toggle source
# File lib/Plasper/runner.rb, line 77
def dump_weights(output)
  @plasper.weights.each do |type, type_data|
    output.puts "#{type}:"
    type_data.each do |category, item_data|
      output.puts "  #{category}:"
      item_data.each { |item, data| output.puts "    #{item}: #{data}" }
    end
  end
end
export_weights(path) click to toggle source
# File lib/Plasper/runner.rb, line 73
def export_weights(path)
  File.open(path, 'w') { |file| dump_weights file }
end
import_text(path) click to toggle source
# File lib/Plasper/runner.rb, line 67
def import_text(path)
  if File.exists? path
    File.open(path).read.each_line { |line| @plasper << line }
  end
end
import_weights(path) click to toggle source
# File lib/Plasper/runner.rb, line 48
def import_weights(path)
  if File.exists? path
    type, category = nil, nil
    File.open(path).read.each_line do |line|
      if line =~ /\A\S+:\s*\z/
        type = line.gsub(/[:\s]/, '').to_sym
      elsif line =~ /\A  \S+:\s*\z/
        category = line.gsub(/[\s:]/, '')
        category = category.to_sym if %w(word sentence letter).include? category
      elsif line =~ /\A    \S*?:\s+\d+\s*\z/
        if type.is_a?(Symbol) && !category.nil?
          item, weight = line.strip.split(':')
          @plasper.add_weight type, category, (item == '' ? nil : item), Integer(weight)
        end
      end
    end
  end
end
talk() click to toggle source
# File lib/Plasper/runner.rb, line 25
def talk
  puts @plasper.passage
end