lib/megahal/megahal.rb
def _generate(keyword_symbols)
    results = 
      if keyword = _select_keyword(keyword_symbols)
        # Use the @seed model to find two contexts that contain the keyword.
        contexts = [[2, keyword], [keyword, 2]]
        contexts.map! do |context|
          id = @brain[context.dup] ||= @brain.length
          count = @seed.count(id)
          if count > 0
            id = @brain[context.dup] ||= @brain.length
            limit = @seed.count(id)
            context[context.index(2)] = @seed.select(id, limit)
            context
          else
            nil
          end
        end
        # Select one of the contexts at random
        context = contexts.compact.shuffle.first
        return nil unless context
        # Here we glue the generations of the @back and @fore models together
        glue = context.select { |symbol| symbol != 1 }
        _random_walk(@back, context.reverse, keyword_symbols).reverse + glue + _random_walk(@fore, context, keyword_symbols)
      else
        # we weren't given any keywords, so do a normal markovian generation
        context = [1, 1]
        _random_walk(@fore, context, keyword_symbols)
      end
    results.length == 0 ? nil : results
  end
# Given an array of keyword symbols, generate an array of norms that hopefully
# contain at least one of the keywords. All the symbols given as keywords must
# have been observed in the past, othewise this will raise an exception.
Given an array of keyword symbols, generate an array of norms that hopefully
contain at least one of the keywords. All the symbols given as keywords must
have been observed in the past, othewise this will raise an exception.
Given an array of keyword symbols generate an array of norms that hopefully contain at least one of the keywords . All the symbols given as keywords must have been observed in the past othewise this will raise an exception .
def _generate(keyword_symbols)
    results = 
      if keyword = _select_keyword(keyword_symbols)
        contexts = [[2, keyword], [keyword, 2]]
        contexts.map! do |context|
          id = @brain[context.dup] ||= @brain.length
          count = @seed.count(id)
          if count > 0
            id = @brain[context.dup] ||= @brain.length
            limit = @seed.count(id)
            context[context.index(2)] = @seed.select(id, limit)
            context
          else
            nil
          end
        end
        context = contexts.compact.shuffle.first
        return nil unless context
        glue = context.select { |symbol| symbol != 1 }
        _random_walk(@back, context.reverse, keyword_symbols).reverse + glue + _random_walk(@fore, context, keyword_symbols)
      else
        context = [1, 1]
        _random_walk(@fore, context, keyword_symbols)
      end
    results.length == 0 ? nil : results
  end
def _generate ( keyword_symbols ) results = if keyword = _select_keyword ( keyword_symbols ) contexts = [ [ 2 , keyword ] , [ keyword , 2 ] ] contexts . map! do | context | id = @brain [ context . dup ] ||= @brain . length count = @seed . count ( id ) if count > 0 id = @brain [ context . dup ] ||= @brain . length limit = @seed . count ( id ) context [ context . index ( 2 ) ] = @seed . select ( id , limit ) context else nil end end context = contexts . compact . shuffle . first return nil unless context glue = context . select { | symbol | symbol != 1 } _random_walk ( @back , context . reverse , keyword_symbols ) . reverse + glue + _random_walk ( @fore , context , keyword_symbols ) else context = [ 1 , 1 ] _random_walk ( @fore , context , keyword_symbols ) end results . length == 0 ? nil : results end
Given an array of keyword symbols, generate an array of norms that hopefully
contain at least one of the keywords.
Given an array of keyword symbols generate an array of norms that hopefully contain at least one of the keywords .
# Use the @seed model to find two contexts that contain the keyword. # Select one of the contexts at random # Here we glue the generations of the @back and @fore models together # we weren't given any keywords, so do a normal markovian generation
keyword_symbols