lib/megahal/megahal.rb
def _calculate_score(utterance, keyword_symbols)
    score = 0

    context = [1, 1]
    utterance.each do |norm|
      if keyword_symbols.include?(norm)
        id = @brain[context.dup] ||= @brain.length
        surprise = @fore.surprise(id, norm)
        score += surprise unless surprise.nil?
      end
      context << norm
      context.shift
    end

    context = [1, 1]
    utterance.reverse.each do |norm|
      if keyword_symbols.include?(norm)
        id = @brain[context.dup] ||= @brain.length
        surprise = @back.surprise(id, norm)
        score += surprise unless surprise.nil?
      end
      context << norm
      context.shift
    end

    if utterance.length >= 8
      score /= Math.sqrt(utterance.length - 1)
    end

    if utterance.length >= 16
      score /= utterance.length
    end

    score
  end
# Calculate the score of a particular utterance
Calculate the score of a particular utterance
Calculate the score of a particular utterance
def _calculate_score(utterance, keyword_symbols)
    score = 0
    context = [1, 1]
    utterance.each do |norm|
      if keyword_symbols.include?(norm)
        id = @brain[context.dup] ||= @brain.length
        surprise = @fore.surprise(id, norm)
        score += surprise unless surprise.nil?
      end
      context << norm
      context.shift
    end
    context = [1, 1]
    utterance.reverse.each do |norm|
      if keyword_symbols.include?(norm)
        id = @brain[context.dup] ||= @brain.length
        surprise = @back.surprise(id, norm)
        score += surprise unless surprise.nil?
      end
      context << norm
      context.shift
    end
    if utterance.length >= 8
      score /= Math.sqrt(utterance.length - 1)
    end
    if utterance.length >= 16
      score /= utterance.length
    end
    score
  end
def _calculate_score ( utterance , keyword_symbols ) score = 0 context = [ 1 , 1 ] utterance . each do | norm | if keyword_symbols . include? ( norm ) id = @brain [ context . dup ] ||= @brain . length surprise = @fore . surprise ( id , norm ) score += surprise unless surprise . nil? end context << norm context . shift end context = [ 1 , 1 ] utterance . reverse . each do | norm | if keyword_symbols . include? ( norm ) id = @brain [ context . dup ] ||= @brain . length surprise = @back . surprise ( id , norm ) score += surprise unless surprise . nil? end context << norm context . shift end if utterance . length >= 8 score /= Math . sqrt ( utterance . length - 1 ) end if utterance . length >= 16 score /= utterance . length end score end
Calculate the score of a particular utterance
Calculate the score of a particular utterance
utterance
keyword_symbols