Class: LLT::Review::Common::Comparison

Inherits:
Object
  • Object
show all
Includes:
Core::Structures::HashContainable
Defined in:
lib/llt/review/common/comparison.rb

Direct Known Subclasses

Alignment::Comparison, Treebank::Comparison

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gold, reviewable) ⇒ Comparison

Returns a new instance of Comparison.



8
9
10
11
12
13
# File 'lib/llt/review/common/comparison.rb', line 8

def initialize(gold, reviewable)
  @gold       = gold
  @reviewable = reviewable
  @container  = {}
  @unique_differences = Set.new # set suffices for now, we only need the diff_id's
end

Instance Attribute Details

#goldObject (readonly)

Returns the value of attribute gold.



6
7
8
# File 'lib/llt/review/common/comparison.rb', line 6

def gold
  @gold
end

#reviewableObject (readonly)

Returns the value of attribute reviewable.



6
7
8
# File 'lib/llt/review/common/comparison.rb', line 6

def reviewable
  @reviewable
end

#unique_differencesObject (readonly)

Returns the value of attribute unique_differences.



6
7
8
# File 'lib/llt/review/common/comparison.rb', line 6

def unique_differences
  @unique_differences
end

Instance Method Details

#all_differencesObject



29
30
31
32
33
# File 'lib/llt/review/common/comparison.rb', line 29

def all_differences
  map do |_, element|
    element.all_differences rescue nil # container might contain other stuff too
  end.flatten.compact
end

#compare(comparables = nil) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/llt/review/common/comparison.rb', line 35

def compare(comparables = nil)
  a = @gold.sentences
  b = @reviewable.sentences
  a.each do |sentence_id, sentence|
    difference = sentence.compare(b[sentence_id], comparables)
    add(difference) if difference.any?
  end
end

#idObject



15
16
17
# File 'lib/llt/review/common/comparison.rb', line 15

def id
  "#{@gold.id}---#{@reviewable.id}"
end

#report(clone_gold = false) ⇒ Object

The option to clone gold is here for for performance reasons only. The report stats are calculated right inside the Gold report, which holds all baseline figures. When there is more than one Reviewable present, we need to clone gold, otherwise we would break the calculated values. The easy way out would be to clone gold in any event, but quite often there will be only one item we need to review - a clone is unnecessary then and comes at a cost then - when the treebank file is large it’s not only time-expensive, but also adds memory load, which we have enough already of already. Of course this causes pollution of the original Gold instance - for now there’s no use case in sight where it would be after this side-effect has been caused.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/llt/review/common/comparison.rb', line 56

def report(clone_gold = false)
  @report ||= begin
    @gold = @gold.clone if clone_gold
    # container includes SentenceReviews, which contain WordsReviews
    r = @gold.report
    r.each_value(&:init_diff)
    each_value do  |d|
      d.report_diff(r, @unique_differences)
    end
    r.each_value(&:count_rights)

    # This looks a bit catastrophic, the reasoning though is:
    # The container includes the sentence diffs - the next element we
    # like to display eventually are the contents of @report, wrapped
    # in an report tag.
    # It might be better to do this otherwise, but this a workaround
    # on short notice.
    # We just add a generic HashContainable structure to the container -
    # other services just as the Assessor can then add their results
    # to the container and we get a nice output in return without
    # coupling llt-diff to llt-assessor.
    report_container = placeholder_container(:report)
    report_container.container.merge!(r)
    add(report_container)
    r
  end
end

#xml_attributesObject



25
26
27
# File 'lib/llt/review/common/comparison.rb', line 25

def xml_attributes
  { gold_id: @gold.id, review_id: @reviewable.id }
end

#xml_tagObject

can’t use the class method that sets the same, as we have subclasses using this value as well



21
22
23
# File 'lib/llt/review/common/comparison.rb', line 21

def xml_tag
  :comparison
end