Class: TextAlignment::TextAlignment
- Inherits:
-
Object
- Object
- TextAlignment::TextAlignment
- Defined in:
- lib/text_alignment/text_alignment.rb
Instance Attribute Summary collapse
-
#block_alignments ⇒ Object
readonly
Returns the value of attribute block_alignments.
-
#lost_annotations ⇒ Object
readonly
Returns the value of attribute lost_annotations.
-
#similarity ⇒ Object
readonly
Returns the value of attribute similarity.
Instance Method Summary collapse
-
#initialize(str1, str2, mappings = nil) ⇒ TextAlignment
constructor
A new instance of TextAlignment.
- #transform_a_span(span) ⇒ Object
- #transform_begin_position(begin_position) ⇒ Object
- #transform_denotations!(denotations) ⇒ Object
- #transform_end_position(end_position) ⇒ Object
- #transform_hdenotations(hdenotations) ⇒ Object
- #transform_spans(spans) ⇒ Object
Constructor Details
#initialize(str1, str2, mappings = nil) ⇒ TextAlignment
Returns a new instance of TextAlignment.
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 161 162 163 164 165 |
# File 'lib/text_alignment/text_alignment.rb', line 17 def initialize(str1, str2, mappings = nil) raise ArgumentError, "nil string" if str1.nil? || str2.nil? mappings ||= TextAlignment::MAPPINGS # try exact match block_begin = str2.index(str1) unless block_begin.nil? @block_alignments = [{source:{begin:0, end:str1.length}, target:{begin:block_begin, end:block_begin + str1.length}, delta:block_begin}] return @block_alignments end anchor_finder = TextAlignment::AnchorFinder.new(str1, str2) # To collect matched blocks mblocks = [] while anchor = anchor_finder.get_next_anchor last = mblocks.last if last && (anchor[:source][:begin] == last[:source][:end] + 1) && (anchor[:target][:begin] == last[:target][:end] + 1) last[:source][:end] = anchor[:source][:end] last[:target][:end] = anchor[:target][:end] else mblocks << anchor end end pp mblocks puts "-----" puts # mblocks.each do |b| # p [b[:source], b[:target]] # puts "---" # puts str1[b[:source][:begin] ... b[:source][:end]] # puts "---" # puts str2[b[:target][:begin] ... b[:target][:end]] # puts "=====" # puts # end # puts "-=-=-=-=-" # puts ## To find block alignments @block_alignments = [] return if mblocks.empty? # Initial step if mblocks[0][:source][:begin] > 0 e1 = mblocks[0][:source][:begin] e2 = mblocks[0][:target][:begin] if mblocks[0][:target][:begin] == 0 @block_alignments << {source:{begin:0, end:e1}, target:{begin:0, end:0}, alignment: :empty} else _str1 = str1[0 ... e1] _str2 = str2[0 ... e2] unless _str1.strip.empty? if _str2.strip.empty? @block_alignments << {source:{begin:0, end:e1}, target:{begin:0, end:e2}, alignment: :empty} else len_min = [_str1.length, _str2.length].min len_buffer = (len_min * (1 + TextAlignment::BUFFER_RATE)).to_i + TextAlignment::BUFFER_MIN b1 = _str1.length < len_buffer ? 0 : e1 - len_buffer b2 = _str2.length < len_buffer ? 0 : e2 - len_buffer @block_alignments << {source:{begin:0, end:b1}, target:{begin:0, end:b2}, alignment: :empty} if b1 > 0 alignment = TextAlignment::MixedAlignment.new(_str1.downcase, _str2.downcase, mappings) if alignment.similarity < 0.6 @block_alignments << {source:{begin:b1, end:e1}, target:{begin:0, end:e2}, alignment: :empty} else @block_alignments << {source:{begin:b1, end:e1}, target:{begin:0, end:e2}, alignment:alignment} end end end end end @block_alignments << mblocks[0] (1 ... mblocks.length).each do |i| b1 = mblocks[i - 1][:source][:end] b2 = mblocks[i - 1][:target][:end] e1 = mblocks[i][:source][:begin] e2 = mblocks[i][:target][:begin] _str1 = str1[b1 ... e1] _str2 = str2[b2 ... e2] unless _str1.strip.empty? if _str2.strip.empty? @block_alignments << {source:{begin:b1, end:e1}, target:{begin:b2, end:e2}, alignment: :empty} else alignment = TextAlignment::MixedAlignment.new(_str1.downcase, _str2.downcase, mappings) if alignment.similarity < 0.6 @block_alignments << {source:{begin:b1, end:e1}, target:{begin:b2, end:e2}, alignment: :empty} else @block_alignments << {source:{begin:b1, end:e1}, target:{begin:b2, end:e2}, alignment:alignment} end end end @block_alignments << mblocks[i] end # Final step if mblocks[-1][:source][:end] < str1.length b1 = mblocks[-1][:source][:end] b2 = mblocks[-1][:target][:end] if mblocks[-1][:target][:end] < str2.length else e1 = str1.length e2 = str2.length @block_alignments << {source:{begin:b1, end:e1}, target:{begin:b2, end:e2}, alignment: :empty} end end if mblocks[-1][:source][:end] < str1.length && mblocks[-1][:target][:end] < str2.length b1 = mblocks[-1][:source][:end] b2 = mblocks[-1][:target][:end] _str1 = str1[b1 ... -1] _str2 = str2[b2 ... -1] unless _str1.strip.empty? if _str2.strip.empty? @block_alignments << {source:{begin:b1, end:str1.length}, target:{begin:b2, end:str2.length}, alignment: :empty} else len_min = [_str1.length, _str2.length].min len_buffer = (len_min * (1 + TextAlignment::BUFFER_RATE)).to_i + TextAlignment::BUFFER_MIN e1 = _str1.length < len_buffer ? str1.length : b1 + len_buffer e2 = _str2.length < len_buffer ? str2.length : b2 + len_buffer _str1 = str1[b1 ... e1] _str2 = str2[b2 ... e2] alignment = TextAlignment::MixedAlignment.new(_str1.downcase, _str2.downcase, mappings) if alignment.similarity < 0.6 @block_alignments << {source:{begin:b1, end:e1}, target:{begin:b2, end:e2}, alignment: :empty} else @block_alignments << {source:{begin:b1, end:e1}, target:{begin:b2, end:e2}, alignment:alignment} end @block_alignments << {source:{begin:e1, end:-1}, target:{begin:e2, end:-1}, alignment: :empty} if e1 < str1.length end end end @block_alignments.each do |a| a[:delta] = a[:target][:begin] - a[:source][:begin] end end |
Instance Attribute Details
#block_alignments ⇒ Object (readonly)
Returns the value of attribute block_alignments.
13 14 15 |
# File 'lib/text_alignment/text_alignment.rb', line 13 def block_alignments @block_alignments end |
#lost_annotations ⇒ Object (readonly)
Returns the value of attribute lost_annotations.
15 16 17 |
# File 'lib/text_alignment/text_alignment.rb', line 15 def lost_annotations @lost_annotations end |
#similarity ⇒ Object (readonly)
Returns the value of attribute similarity.
14 15 16 |
# File 'lib/text_alignment/text_alignment.rb', line 14 def similarity @similarity end |
Instance Method Details
#transform_a_span(span) ⇒ Object
205 206 207 |
# File 'lib/text_alignment/text_alignment.rb', line 205 def transform_a_span(span) {begin: transform_begin_position(span[:begin]), end: transform_end_position(span[:end])} end |
#transform_begin_position(begin_position) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/text_alignment/text_alignment.rb', line 167 def transform_begin_position(begin_position) i = @block_alignments.index{|b| b[:source][:end] > begin_position} block_alignment = @block_alignments[i] b = if block_alignment[:alignment].nil? begin_position + block_alignment[:delta] elsif block_alignment[:alignment] == :empty if begin_position == block_alignment[:source][:begin] block_alignment[:target][:begin] else # raise "lost annotation" nil end else r = block_alignment[:alignment].transform_begin_position(begin_position - block_alignment[:source][:begin]) r.nil? ? nil : r + block_alignment[:target][:begin] end end |
#transform_denotations!(denotations) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/text_alignment/text_alignment.rb', line 213 def transform_denotations!(denotations) return nil if denotations.nil? @lost_annotations = [] denotations.each do |d| begin d.begin = transform_begin_position(d.begin); d.end = transform_end_position(d.end); rescue @lost_annotations << d d.begin = nil d.end = nil end end @lost_annotations end |
#transform_end_position(end_position) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/text_alignment/text_alignment.rb', line 186 def transform_end_position(end_position) i = @block_alignments.index{|b| b[:source][:end] >= end_position} block_alignment = @block_alignments[i] e = if block_alignment[:alignment].nil? end_position + block_alignment[:delta] elsif block_alignment[:alignment] == :empty if end_position == block_alignment[:source][:end] block_alignment[:target][:end] else # raise "lost annotation" nil end else r = block_alignment[:alignment].transform_end_position(end_position - block_alignment[:source][:begin]) r.nil? ? nil : r + block_alignment[:target][:begin] end end |
#transform_hdenotations(hdenotations) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/text_alignment/text_alignment.rb', line 231 def transform_hdenotations(hdenotations) return nil if hdenotations.nil? @lost_annotations = [] r = hdenotations.collect do |d| new_d = begin d.dup.merge({span:transform_a_span(d[:span])}) rescue @lost_annotations << d nil end end.compact r end |
#transform_spans(spans) ⇒ Object
209 210 211 |
# File 'lib/text_alignment/text_alignment.rb', line 209 def transform_spans(spans) spans.map{|span| transform_a_span(span)} end |