summary_judgement 1.2.4 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.4
1
+ 1.3.0
@@ -42,8 +42,12 @@ module SummaryJudgement
42
42
 
43
43
  def summarize_as_branch(options = {})
44
44
  result = ''
45
+ leaves = canopy
46
+ options[:verbs] ||= :canopy if leaves.map(&:class).uniq.all? { |k| k.summary.predicate }
47
+ options[:verbs] ||= :branches
48
+
45
49
  if conjugation = options.delete(:conjugate)
46
- options.reverse_merge! :tense => :present, :plurality => :singular
50
+ options.reverse_merge! :plurality => :singular
47
51
  case conjugation
48
52
  when String
49
53
  options[:person] ||= :third
@@ -53,25 +57,28 @@ module SummaryJudgement
53
57
  when TrueClass
54
58
  options[:person] ||= :third
55
59
  end
56
- result << Verbs::Conjugator.conjugate(self.class.summary.predicate, options.slice(:person, :subject, :tense, :plurality)).to_s
57
- result = result.humanize unless options[:subject]
58
- result << ' '
60
+ if options[:verbs] == :canopy and options[:subject]
61
+ result << case options[:subject]
62
+ when String
63
+ options[:subject]
64
+ when Symbol, TrueClass
65
+ Verbs::Conjugator.subject(options.slice(:person, :plurality)).humanize
66
+ end
67
+ elsif options[:verbs] == :branches
68
+ result << Verbs::Conjugator.conjugate(self.class.summary.predicate, options.slice(:person, :subject, :tense, :plurality, :aspect)).to_s
69
+ result = result.humanize unless options[:subject]
70
+ end
71
+ result << ' ' if options[:subject]
59
72
  end
60
73
 
74
+
61
75
  verbosity = options.delete(:verbose)
62
76
  verbosity = self.class.summary.class.render verbosity, self
63
77
  verbosity = canopy.length <= verbosity if verbosity.is_a? Fixnum
64
78
 
65
- if verbosity
66
- leaves = canopy
67
- first_leaf = leaves.shift
68
- result << leaves.map { |leaf| leaf.summary :capitalize_indefinite_article => false }.unshift(first_leaf.summary(:capitalize_indefinite_article => !conjugation)).to_sentence
69
- else
70
- result << canopy.map {|c| c.class}.uniq.map do |k|
71
- siblings = canopy.select {|c| c.is_a? k}
72
- siblings.length.to_s + ' ' + k.to_s.underscore.humanize.downcase.pluralize_on(siblings.length)
73
- end.to_sentence
74
- end
79
+ tufts = organize_leaves(*leaves)
80
+ first_tuft = tufts.shift
81
+ result << tufts.map { |tuft| summarize_tuft(tuft, conjugation, verbosity, options) }.unshift(summarize_tuft(first_tuft, conjugation, verbosity, options.merge(:capitalize_anonymous => true))).to_sentence(:words_connector => ';', :last_word_connector => '; and ')
75
82
  end
76
83
 
77
84
  def summarize_as_bare_branch(options = {})
@@ -92,6 +99,35 @@ module SummaryJudgement
92
99
  end
93
100
  result << self.class.summary.class.render(self.class.summary.default, self).with_indefinite_article
94
101
  end
102
+
103
+ def organize_leaves(*leaves)
104
+ leaves.inject(::ActiveSupport::OrderedHash.new) do |memo, leaf|
105
+ memo[leaf.class] ||= []
106
+ memo[leaf.class] << leaf
107
+ memo
108
+ end
109
+ end
110
+
111
+ def summarize_tuft(tuft, conjugation, verbosity, options)
112
+ klass, siblings = tuft
113
+ tuft_summary = ''
114
+ if options[:verbs] == :canopy and conjugation
115
+ tuft_summary << Verbs::Conjugator.conjugate(klass.summary.predicate, options.slice(:person, :tense, :aspect, :plurality).reverse_merge(klass.summary.options_for_conjugation)).to_s
116
+ tuft_summary = tuft_summary.humanize unless options[:subject]
117
+ tuft_summary << ' '
118
+ end
119
+ if verbosity
120
+ if conjugation
121
+ tuft_summary << siblings.map { |leaf| leaf.summary :capitalize_indefinite_article => false }.to_sentence
122
+ else
123
+ first_sibling = siblings.shift
124
+ tuft_summary << siblings.map { |leaf| leaf.summary :capitalize_indefinite_article => false }.unshift(first_sibling.summary(:capitalize_indefinite_article => !options[:subject])).to_sentence
125
+ end
126
+ else
127
+ tuft_summary << siblings.length.to_s + ' ' + klass.to_s.underscore.humanize.downcase.pluralize_on(siblings.length)
128
+ end
129
+ tuft_summary
130
+ end
95
131
 
96
132
  end
97
133
  end
@@ -9,6 +9,8 @@ module SummaryJudgement
9
9
  @modifiers = options[:modifiers] || []
10
10
  @subordinates = options[:subordinates] || []
11
11
  @verb = options[:verb]
12
+ @aspect = options[:aspect]
13
+ @tense = options[:tense]
12
14
  @placeholder = options[:placeholder]
13
15
  end
14
16
 
@@ -47,6 +49,18 @@ module SummaryJudgement
47
49
  @verb
48
50
  end
49
51
 
52
+ def tense(t)
53
+ @tense = t
54
+ end
55
+
56
+ def aspect(a)
57
+ @aspect = a
58
+ end
59
+
60
+ def options_for_conjugation
61
+ { :tense => @tense, :aspect => @aspect }
62
+ end
63
+
50
64
  def placeholder(p)
51
65
  @placeholder = p
52
66
  end
@@ -56,7 +70,7 @@ module SummaryJudgement
56
70
  end
57
71
 
58
72
  def to_hash
59
- [:adjectives, :modifiers, :subordinates, :terms, :verb, :placeholder].inject({}) do |properties, property|
73
+ [:adjectives, :modifiers, :subordinates, :terms, :verb, :placeholder, :aspect, :tense].inject({}) do |properties, property|
60
74
  val = instance_variable_get :"@#{property}"
61
75
  case val
62
76
  when Symbol, NilClass, TrueClass, FalseClass, Fixnum
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{summary_judgement}
8
- s.version = "1.2.4"
8
+ s.version = "1.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andy Rossmeissl"]
12
- s.date = %q{2009-12-07}
12
+ s.date = %q{2009-12-10}
13
13
  s.description = %q{Constructs adaptive summaries of object hierarchies based on ActiveRecord associations and other simple relationship structures}
14
14
  s.email = %q{andy@rossmeissl.net}
15
15
  s.extra_rdoc_files = [
data/test/helper.rb CHANGED
@@ -26,6 +26,9 @@ class Book
26
26
  has.adjective lambda { |book| "#{book.length_in_pages}pp" }, :if => :length_in_pages
27
27
  has.identity lambda { |book| book.book_type }
28
28
  has.modifier lambda { |book| "by #{book.author}" }, :if => :author
29
+ has.verb :read
30
+ has.aspect :perfect
31
+ has.tense :present
29
32
  end
30
33
  end
31
34
 
@@ -52,6 +55,9 @@ class Magazine
52
55
  has.adjective lambda { |magazine| "#{magazine.length_in_pages}pp" }, :if => :length_in_pages
53
56
  has.identity 'magazine issue'
54
57
  has.modifier lambda { |magazine| "from #{magazine.date}" }, :if => :date
58
+ has.verb :skim
59
+ has.aspect :perfect
60
+ has.tense :present
55
61
  end
56
62
  end
57
63
 
@@ -19,7 +19,7 @@ class TestSummaryJudgement < Test::Unit::TestCase
19
19
  assert_equal Book, @neuromancer.class
20
20
  assert_equal Library, @bookshelf.class
21
21
  assert_equal 2, @bookshelf.books.length
22
- assert_equal :accept, Verbs::Conjugator.conjugate(:accept, :tense => :present, :person => :first, :plurality => :singular)
22
+ assert_equal 'accept', Verbs::Conjugator.conjugate(:accept, :tense => :present, :person => :first, :plurality => :singular)
23
23
  assert_equal 'a', 'one'.indefinite_article
24
24
  end
25
25
 
@@ -52,9 +52,9 @@ class TestSummaryJudgement < Test::Unit::TestCase
52
52
  end
53
53
 
54
54
  def test_conjugated_summary
55
- assert_equal 'Has 2 books', @bookshelf.summary(:conjugate => true, :subject => false)
56
- assert_equal 'You have 2 books', @bookshelf.summary(:conjugate => :second, :subject => true)
57
- assert_equal 'The catalog contains 2 books and 1 magazine', @catalog.summary(:conjugate => :third, :subject => 'The catalog')
55
+ assert_equal 'Has read 2 books', @bookshelf.summary(:conjugate => true, :subject => false)
56
+ assert_equal 'You have 2 books', @bookshelf.summary(:conjugate => :second, :subject => true, :verbs => :branches)
57
+ assert_equal 'The catalog contains 2 books and 1 magazine', @catalog.summary(:conjugate => :third, :subject => 'The catalog', :verbs => :branches)
58
58
  end
59
59
 
60
60
  def test_verbose_summaries
@@ -62,7 +62,7 @@ class TestSummaryJudgement < Test::Unit::TestCase
62
62
  end
63
63
 
64
64
  def test_conjugated_verbose_summaries
65
- assert_equal 'I have a novel by William Gibson and an illustrated childrens book by Maurice Sendak', @bookshelf.summary(:verbose => true, :conjugate => :first, :subject => true)
65
+ assert_equal 'I have read a novel by William Gibson and an illustrated childrens book by Maurice Sendak', @bookshelf.summary(:verbose => true, :conjugate => :first, :subject => true)
66
66
  end
67
67
 
68
68
  def test_summaries_with_adaptive_verbosity
@@ -71,8 +71,8 @@ class TestSummaryJudgement < Test::Unit::TestCase
71
71
  end
72
72
 
73
73
  def test_conjugated_summaries_with_adaptive_verbosity
74
- assert_equal 'They have 2 books', @bookshelf.summary(:verbose => :small?, :conjugate => :third, :plurality => :plural, :subject => true)
75
- assert_equal 'They have a novel by William Gibson', @bedstand.summary(:verbose => :small?, :conjugate => :third, :plurality => :plural, :subject => true)
74
+ assert_equal 'They have read 2 books', @bookshelf.summary(:verbose => :small?, :conjugate => :third, :plurality => :plural, :subject => true)
75
+ assert_equal 'They have read a novel by William Gibson', @bedstand.summary(:verbose => :small?, :conjugate => :third, :plurality => :plural, :subject => true)
76
76
  end
77
77
 
78
78
  def test_inheritance
@@ -91,4 +91,9 @@ class TestSummaryJudgement < Test::Unit::TestCase
91
91
  def test_empty_state
92
92
  assert_equal 'You have an empty shelf', @new_releases.summary(:conjugate => :second, :subject => true)
93
93
  end
94
+
95
+ def test_conjugated_summary_with_multiple_verbs
96
+ assert_equal 'I have read 2 books and have skimmed 1 magazine', @catalog.summary(:conjugate => :first, :subject => true, :aspect => :perfect)
97
+ assert_equal 'I have read a novel by William Gibson and an illustrated childrens book by Maurice Sendak and have skimmed a magazine issue from December 2009', @catalog.summary(:conjugate => :first, :subject => true, :aspect => :perfect, :verbose => true)
98
+ end
94
99
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: summary_judgement
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.4
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Rossmeissl
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-07 00:00:00 -05:00
12
+ date: 2009-12-10 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15