Class: Stenographer::Conversation

Inherits:
Object
  • Object
show all
Defined in:
lib/stenographer/conversation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, noko) ⇒ Conversation

Returns a new instance of Conversation.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



4
5
6
# File 'lib/stenographer/conversation.rb', line 4

def created_at
  @created_at
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/stenographer/conversation.rb', line 4

def id
  @id
end

#membersObject

Returns the value of attribute members.



4
5
6
# File 'lib/stenographer/conversation.rb', line 4

def members
  @members
end

#messagesObject

Returns the value of attribute messages.



4
5
6
# File 'lib/stenographer/conversation.rb', line 4

def messages
  @messages
end

Instance Method Details

#cuss_words(cache = word_distribution) ⇒ Object



134
135
136
# File 'lib/stenographer/conversation.rb', line 134

def cuss_words(cache=word_distribution)
  cache.reject!{|key| Word::SWEARWORDS.include?(key) }
end

#header(range) ⇒ Object



106
107
108
# File 'lib/stenographer/conversation.rb', line 106

def header(range)
  "\n=> #{members.join(', ')} @ #{created_at} Lines: #{range}"
end

#include?(word) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
104
# 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_countObject



110
111
112
# File 'lib/stenographer/conversation.rb', line 110

def messages_count
  messages.length
end

#range(opts = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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 = {}) ⇒ Object

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


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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

#reportObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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_sObject



162
163
164
# 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) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# 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_countObject



114
115
116
# File 'lib/stenographer/conversation.rb', line 114

def word_count
  messages.collect{|m| m.words.length}.inject(0, :+)
end

#word_distribution(opts = {}) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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