-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringDiffer.rb
160 lines (125 loc) · 3.13 KB
/
StringDiffer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
47
48
49
50
51
52
53
54
55
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# encoding: UTF-8
#
# Contains very basic string diffing classes.
#
module StringDiffer
#
# Finds the difference between two sentences
# on a word basis
#
class WordDiff
# A string array of the words in sentence A
attr_accessor :a_words
# A string array of the words in sentence B
attr_accessor :b_words
# An integer array with the "word" indices
# where "a_words" and "b_words" are different
attr_accessor :differences
# Takes two sentences as parameters
# and performs the comparison.
#
# The result of the diff is available
# in the "differences" property
def initialize(a, b)
@a_words = []
@b_words = []
@differences = []
run_diff(a, b)
end
# Run word-based diff between the sentences
# given as parameters
def run_diff(a, b)
a = "" if a == nil
b = "" if b == nil
@a_words = a.split(" ")
@b_words = b.split(" ")
max = @a_words.length
max = @b_words.length if @b_words.length > @a_words.length
for i in 0..max-1
@differences.push(i) if @a_words[i] != @b_words[i]
end
# return void
return
end
end
#
# Find differences between two sentences.
# The diff is able to find a difference
# in something like the following:
#
# A: "Short sentence"
# B: "Short, sentence"
#
# In that case, only the comma after "Short"
# would be considered a difference
#
class SentenceDiff
# Sentence A
attr_accessor :a
# Sentence B
attr_accessor :b
# An integer array with the character
# positions that are different
attr_accessor :differences
# The underlying "WordDiff" instance
attr_accessor :worddiff
# Constructor taking the two sentences
# to diff. The diff is run automatically
def initialize(a, b)
@a = ""
@b = ""
@b_linenumber = -1
@differences = []
@worddiff = WordDiff.new("", "")
run_diff(a, b)
end
# Run diff on the two given sentences
def run_diff(a, b)
a = "" if a == nil
b = "" if b == nil
@a = a
@b = b
# run the initial word-based diff
@worddiff = WordDiff.new(a, b)
awords = @worddiff.a_words
bwords = @worddiff.b_words
wdiff = @worddiff.differences
#
# now run through all the words in the longest
# of the sentences, and for words that differ
# run a character based diff and calculate the differing
# characters actual position within the sentences
#
max = awords.length
max = bwords.length if bwords.length > awords.length
str = ""
for i in 0..max-1
if wdiff.include?(i)
diff_by_char(awords[i], bwords[i]).each do |chardiff|
@differences.push(chardiff + str.length)
end
end
str << " #{bwords[i]}"
end
# return void
return
end
# Run a character-by-character diff
# between two given sentences
#
# The method returns an integer array
# with the positions of the characters
# that do not match
def diff_by_char(a, b)
differences = []
a = "" if a == nil
b = "" if b == nil
max = a.length
max = b.length if b.length > a.length
for pos in 0..max-1
differences.push(pos+1) if a[pos] != b[pos]
end
return differences
end
end
end