Class: StringDiff::Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/string_diff.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string1, string2) ⇒ Diff

Returns a new instance of Diff.



8
9
10
11
# File 'lib/string_diff.rb', line 8

def initialize(string1, string2)
  @string1 = string1
  @string2 = string2
end

Instance Attribute Details

#string1Object (readonly)

Returns the value of attribute string1.



6
7
8
# File 'lib/string_diff.rb', line 6

def string1
  @string1
end

#string2Object (readonly)

Returns the value of attribute string2.



6
7
8
# File 'lib/string_diff.rb', line 6

def string2
  @string2
end

Instance Method Details

#annotate_deletions(deletions, array1) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/string_diff.rb', line 29

def annotate_deletions(deletions, array1)
  deletions.each do |v|
    index = array1.find_index(v)
    array1[index] = "<span class='deletion'>#{v}</span>"
  end
  #array1.join(" ")
end

#annotate_insertions(insertions, array1, array2) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/string_diff.rb', line 37

def annotate_insertions(insertions, array1, array2)
  insertions.each_with_index do |v, i|
    if array2.find_index(v) == 0
      index = 0
    else
      insertion_position = array2.find_index(v) + i - 1
      index = array1.find_index(array2[insertion_position])
    end

    if index.nil?
      array1.insert(-1, "<span class='insertion'>#{v}</span>")
    else
      array1.insert(index + 1, "<span class='insertion'>#{v}</span>")
    end
  end

  array1
end

#compare(array1, array2) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/string_diff.rb', line 20

def compare(array1, array2)
  deletions = array1 - array2
  insertions = array2 - array1

  process_duplicates(array1, array2)
  annotate_deletions(deletions, array1)
  annotate_insertions(insertions, array1, array2)
end

#construct_string(array1) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/string_diff.rb', line 81

def construct_string(array1)
  string = ""

  array1.each_with_index do |token, i|
    if i == 0
      string += token
    else
      if token.include?("<span")
        if token.scan(/(?<='>).*(?=<\/)/)[0] !~ /[[:punct:]]/ || string1.include?(" #{token.scan(/(?<='>).*(?=<\/)/)[0]}")
          string += " #{token}"
        else
          string += token
        end
      else
        if token !~ /[[:punct:]]/ || string1.include?(" #{token.scan(/(?<='>).*(?=<\/)/)[0]}")
          string += " #{token}"
        else
          string += token
        end
      end
    end
  end
  string
end

#diffObject



13
14
15
16
17
18
# File 'lib/string_diff.rb', line 13

def diff
  a1 = PragmaticTokenizer::Tokenizer.new(downcase: false).tokenize(string1)
  a2 = PragmaticTokenizer::Tokenizer.new(downcase: false).tokenize(string2)

  construct_string(compare(a1, a2))
end

#process_duplicates(array1, array2) ⇒ Object



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

def process_duplicates(array1, array2)
  dup1 = array1.find_all { |e| array1.count(e) > 1 }
  dup2 = array2.find_all { |e| array2.count(e) > 1 }

  missing_words = (dup1 - dup2).uniq

  duplicate_indexs_of_array1 = []
  duplicate_indexs_of_array2 = []

  missing_words.each do |word|
    array1.each_with_index do |v, i|
      duplicate_indexs_of_array1 << i if word == v
    end

    array2.each_with_index do |v, i|
      duplicate_indexs_of_array2 << i if word == v
    end

    missing_index = duplicate_indexs_of_array1 - duplicate_indexs_of_array2

    array1[missing_index[0]] = "<span class='deletion'>#{word}</span>"
  end
  #array1.join(" ")
end