Class: Arrolio::Harness::PdfDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/arrolio/harness/pdf_diff.rb

Defined Under Namespace

Classes: PageResult, Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ours_path, theirs_path) ⇒ PdfDiff

Returns a new instance of PdfDiff.



16
17
18
19
# File 'lib/arrolio/harness/pdf_diff.rb', line 16

def initialize(ours_path, theirs_path)
  @ours_path = ours_path
  @theirs_path = theirs_path
end

Instance Attribute Details

#ours_pathObject (readonly)

Returns the value of attribute ours_path.



14
15
16
# File 'lib/arrolio/harness/pdf_diff.rb', line 14

def ours_path
  @ours_path
end

#theirs_pathObject (readonly)

Returns the value of attribute theirs_path.



14
15
16
# File 'lib/arrolio/harness/pdf_diff.rb', line 14

def theirs_path
  @theirs_path
end

Instance Method Details

#callObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/arrolio/harness/pdf_diff.rb', line 21

def call
  ours = pages_of(@ours_path)
  theirs = pages_of(@theirs_path)
  per_page = []
  max_len = [ours.length, theirs.length].max
  (1..max_len).each do |n|
    ot = ours[n - 1] || ''
    tt = theirs[n - 1] || ''
    matches = TextDiff.match_paragraphs(ot, tt)
    sim = if ot.empty? && tt.empty?
            1.0
          elsif ot.empty? || tt.empty?
            0.0
          else
            matches.sum { |m| m[:similarity] } / [matches.length, 1].max
          end
    missing = matches.select { |m| m[:theirs].nil? }.map { |m| m[:ours] }
    extra = TextDiff.paragraphs(tt).reject { |p| matches.any? { |m| m[:theirs] == p } }
    per_page << PageResult.new(number: n, ours_text: ot, theirs_text: tt,
                               similarity: sim, missing: missing, extra: extra)
  end
  overall = per_page.empty? ? 0.0 : per_page.sum(&:similarity) / per_page.length
  Result.new(ours_pages: ours.length, theirs_pages: theirs.length,
             page_count_delta: ours.length - theirs.length,
             per_page: per_page, overall_similarity: overall)
end

#report(max_per_page: 3) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/arrolio/harness/pdf_diff.rb', line 48

def report(max_per_page: 3)
  r = call
  lines = []
  lines << format('ours: %d pages, theirs: %d pages (delta %+d)',
                  r.ours_pages, r.theirs_pages, r.page_count_delta)
  lines << format('overall similarity: %.1f%%', r.overall_similarity * 100)
  lines << ''
  r.per_page.each do |page|
    lines << format('Page %d: similarity %.1f%%  (missing %d, extra %d)',
                    page.number, page.similarity * 100,
                    page.missing.length, page.extra.length)
    next unless page.similarity < 0.95

    page.missing.first(max_per_page).each { |t| lines << "  MISSING: #{truncate(t, 100)}" }
    page.extra.first(max_per_page).each { |t| lines << "  EXTRA:   #{truncate(t, 100)}" }
  end
  lines.join("\n") + "\n"
end