Class: Stenographer::Conversation
- Inherits:
-
Object
- Object
- Stenographer::Conversation
- Defined in:
- lib/stenographer/conversation.rb
Instance Attribute Summary collapse
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#id ⇒ Object
Returns the value of attribute id.
-
#members ⇒ Object
Returns the value of attribute members.
-
#messages ⇒ Object
Returns the value of attribute messages.
Instance Method Summary collapse
- #cuss_words(cache = word_distribution) ⇒ Object
- #header(range) ⇒ Object
- #include?(word) ⇒ Boolean
-
#initialize(id, noko) ⇒ Conversation
constructor
A new instance of Conversation.
- #messages_count ⇒ Object
- #range(opts = {}) ⇒ Object
-
#read_back(opts = {}) ⇒ Object
Print out a conversation.
- #report ⇒ Object
- #to_s ⇒ Object
- #top_words(limit = 10, cache = word_distribution) ⇒ Object
- #word_count ⇒ Object
- #word_distribution(opts = {}) ⇒ Object
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 |, i| name = (.attributes['alias'] || .attributes['sender']).value @created_at ||= .attributes['time'].value body = .children.inner_text @members << name if !@members.include?(name) @messages << Message.new(i, name, body) end end |
Instance Attribute Details
#created_at ⇒ Object
Returns the value of attribute created_at.
4 5 6 |
# File 'lib/stenographer/conversation.rb', line 4 def created_at @created_at end |
#id ⇒ Object
Returns the value of attribute id.
4 5 6 |
# File 'lib/stenographer/conversation.rb', line 4 def id @id end |
#members ⇒ Object
Returns the value of attribute members.
4 5 6 |
# File 'lib/stenographer/conversation.rb', line 4 def members @members end |
#messages ⇒ Object
Returns the value of attribute messages.
4 5 6 |
# File 'lib/stenographer/conversation.rb', line 4 def @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
100 101 102 103 104 |
# File 'lib/stenographer/conversation.rb', line 100 def include?(word) return true if word.nil? .collect{|m| m.include?(word)}.include?(true) end |
#messages_count ⇒ Object
110 111 112 |
# File 'lib/stenographer/conversation.rb', line 110 def .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 = [] .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 > .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) [id_range].each do || if query && .include?(query) .print(highlighted: true) else .print unless query && opts[:context].nil? end end puts end |
#report ⇒ Object
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: ', ) 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 ⇒ Object
162 163 164 |
# File 'lib/stenographer/conversation.rb', line 162 def to_s "Conversation <members: #{members.join(', ')}, 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_count ⇒ Object
114 115 116 |
# File 'lib/stenographer/conversation.rb', line 114 def word_count .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 = {} .each do || .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 |