class Stenographer::Conversation

Attributes

created_at[RW]
id[RW]
members[RW]
messages[RW]

Public Class Methods

new(id, noko) click to toggle source
# File lib/stenographer/conversation.rb, line 6
def initialize(id, noko)
  @id       = id
  @messages = []
  @members  = []
  @history  = []
  @lines_to_print = 0
  @created_at = nil

  noko.css('message').each_with_index do |message, i|
    name = (message.attributes['alias'] || message.attributes['sender']).value
    @created_at ||= message.attributes['time'].value
    body = message.children.inner_text

    @members << name if !@members.include?(name)
    @messages << Message.new(i, name, body)
  end
end

Public Instance Methods

cuss_words(cache=word_distribution) click to toggle source
# File lib/stenographer/conversation.rb, line 134
def cuss_words(cache=word_distribution)
  cache.reject!{|key| Word::SWEARWORDS.include?(key) }
end
header(range) click to toggle source
# File lib/stenographer/conversation.rb, line 106
def header(range)
  "\n=> #{members.join(', ')} @ #{created_at} Lines: #{range}"
end
include?(word) click to toggle source
# File lib/stenographer/conversation.rb, line 100
def include?(word)
  return true if word.nil?

  messages.collect{|m| m.include?(word)}.include?(true)
end
messages_count() click to toggle source
# File lib/stenographer/conversation.rb, line 110
def messages_count
  messages.length
end
range(opts={}) click to toggle source
# File lib/stenographer/conversation.rb, line 74
def range(opts={})
  min_id  = 0
  max_id  = -1
  context = opts[:context].nil? ? 0 : 5

  if opts[:query]
    occurrences = []
    messages.each do |m|
      occurrences << m.id if m.include?(opts[:query])
    end
    
    if occurrences.any?
      min_id = occurrences.min - (context)
      max_id = occurrences.max + (context)
    else
      min_id = -1
      max_id = 0
    end
  end

  min_id = 0  if min_id < 0
  max_id = -1 if max_id > messages.length

  return (opts[:min_id] || min_id)..(opts[:max_id] || max_id)
end
read_back(opts={}) click to toggle source

Print out a conversation

Example:

>> conversation.read_back
=> Jack Johnson, John Jackson @ 2012-11-05T20:48:50-08:00 Lines: 0..-1
John Jackson   It's time someone had the courage to stand up and say: I'm against those things that everybody hates.
Jack Johnson   Now, I respect my opponent. I think he's a good man. But quite frankly, I agree with everything he just said.
John Jackson   I say your three cent titanium tax goes too far.
Jack Johnson   And I say your three cent titanium tax doesn't go too far enough.

Arguments:

opts: (Hash)
[query]   only return lines that match query
[context] used with query, returns surrounding lines to match
# File lib/stenographer/conversation.rb, line 38
def read_back(opts={})
  raise "read_back only accepts an options hash" if !opts.is_a?(Hash)
  query = opts[:query]
  return if query && !include?(query)

  id_range = range(opts)
  puts header(id_range)
  messages[id_range].each do |message|
    if query && message.include?(query)
      message.print(highlighted: true)
    else
      message.print unless query && opts[:context].nil?
    end
  end
  puts
end
report() click to toggle source
# File lib/stenographer/conversation.rb, line 55
def report
  distribution = word_distribution(top: 10)
  max_length   = distribution.keys.map(&:length).max + 2
  max_length = 15 if max_length < 15
  code = "%-#{max_length}s %0s"

  puts "\n=> Conversation Report <" + ('=' * 21)
  puts printf(code, 'Members: ', members.join(', '))
  puts printf(code, 'Messages: ', messages_count)
  puts printf(code, 'Words: ', word_count)
  puts printf(code, 'Created at: ', created_at)
  puts "\n=> Word Usage"
  distribution.each_pair do |word, count|
    puts printf(code, "#{word}:", count)
  end

  puts ('=' * 45) + "\n"
end
to_s() click to toggle source
# File lib/stenographer/conversation.rb, line 162
def to_s
  "Conversation <members: #{members.join(', ')}, messages: #{messages.length}>"
end
top_words(limit=10, cache=word_distribution) click to toggle source
# File lib/stenographer/conversation.rb, line 138
def top_words(limit=10, cache=word_distribution)
  limit = limit == true ? (+1.0/0.0) : limit
  
  sorted = {}
  cache.each_pair do |word, count|
    sorted[count] = [] if sorted[count].nil?

    sorted[count] << word
  end

  top = {}; i = 0
  sorted.keys.sort{|x,y| y <=> x }.each do |key|
    break if i > limit

    sorted[key].each do |word|
      top[word] = key; i += 1
      
      break if i > limit
    end
  end

  return top
end
word_count() click to toggle source
# File lib/stenographer/conversation.rb, line 114
def word_count
  messages.collect{|m| m.words.length}.inject(0, :+)
end
word_distribution(opts={}) click to toggle source
# File lib/stenographer/conversation.rb, line 118
def word_distribution(opts={})
  cache = {}
  messages.each do |message|
    message.explode.each do |word|
      cache[word.text] = 0 if cache[word.text].nil?

      cache[word.text] += 1
    end
  end

  cache = cuss_words(cache) unless opts[:cuss].nil?
  cache = top_words(opts[:top], cache)   unless opts[:top].nil?

  cache
end