Module: Arrolio::Harness::TextDiff

Defined in:
lib/arrolio/harness/text_diff.rb

Class Method Summary collapse

Class Method Details

.match_paragraphs(ours, theirs) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/arrolio/harness/text_diff.rb', line 25

def match_paragraphs(ours, theirs)
  ours_paras = paragraphs(ours)
  theirs_paras = paragraphs(theirs)
  theirs_used = Array.new(theirs_paras.length, false)

  ours_paras.map do |op|
    best_idx = nil
    best_sim = 0.0
    theirs_paras.each_with_index do |tp, i|
      next if theirs_used[i]

      s = similarity(op, tp)
      if s > best_sim
        best_sim = s
        best_idx = i
      end
    end

    if best_idx && best_sim >= 0.3
      theirs_used[best_idx] = true
      { ours: op, theirs: theirs_paras[best_idx], similarity: best_sim }
    else
      { ours: op, theirs: nil, similarity: 0.0 }
    end
  end
end

.paragraphs(text) ⇒ Object



21
22
23
# File 'lib/arrolio/harness/text_diff.rb', line 21

def paragraphs(text)
  text.to_s.split(/\n\s*\n/).map(&:strip).reject(&:empty?)
end

.similarity(a, b) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/arrolio/harness/text_diff.rb', line 12

def similarity(a, b)
  ta = tokens(a).to_set
  tb = tokens(b).to_set
  return 1.0 if ta.empty? && tb.empty?
  return 0.0 if ta.empty? || tb.empty?

  (ta & tb).size.to_f / (ta | tb).size
end

.tokens(text) ⇒ Object



8
9
10
# File 'lib/arrolio/harness/text_diff.rb', line 8

def tokens(text)
  text.to_s.downcase.gsub(/[^a-z0-9\s]/i, ' ').split
end