Sha256: 0d534d8145db4e0917b068fd81ebb7849105753043c80b0b4fbc6b815ab215aa

Contents?: true

Size: 931 Bytes

Versions: 2

Compression:

Stored size: 931 Bytes

Contents

require "mrkv/version"

module Mrkv
  class Chain
    def initialize ngram=2
      @ngram = ngram
      @chain = Hash.new { |h,k| h[k] = [] }
    end

    def add lines
      lines.each do |line|
        line.downcase.capitalize.gsub(/[^a-zA-Z0-9\.\!\?\s]/, '').split.each_cons(@ngram + 1) do |link|
          next if link.nil?
          @chain[link.take(@ngram).join(" ")] << link.last
        end
      end
      @starters = @chain.keys.select{|k| k =~ /^[A-Z]/}
      true
    end

    def generate
      random_starter.split.tap do |str_arr|
        until str_arr.last.match /[\?\!\.]$/
          possibilities = @chain[str_arr[-@ngram..-1].join(" ")]
          if !possibilities.empty?
            str_arr << possibilities.sample
          else
            str_arr.last << ["?", "!", "."].sample
          end
        end
      end.join(" ")
    end

  private

    def random_starter
      @starters.sample
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mrkv-0.1.1 lib/mrkv.rb
mrkv-0.1.0 lib/mrkv.rb