Class: TextAlignment::AnchorFinder
- Inherits:
-
Object
- Object
- TextAlignment::AnchorFinder
- Defined in:
- lib/text_alignment/anchor_finder.rb
Instance Method Summary collapse
- #get_next_anchor ⇒ Object
-
#initialize(source_str, target_str, _size_ngram = nil, _size_window = nil) ⇒ AnchorFinder
constructor
A new instance of AnchorFinder.
Constructor Details
#initialize(source_str, target_str, _size_ngram = nil, _size_window = nil) ⇒ AnchorFinder
Returns a new instance of AnchorFinder.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/text_alignment/anchor_finder.rb', line 12 def initialize(source_str, target_str, _size_ngram = nil, _size_window = nil) @size_ngram = _size_ngram || TextAlignment::SIZE_NGRAM @size_window = _size_window || TextAlignment::SIZE_WINDOW @reverse = (target_str.length < source_str.length) @s1, @s2 = if @reverse [target_str.downcase, source_str.downcase] else [source_str.downcase, target_str.downcase] end # current position in s1 @beg_s1 = 0 @end_s1_prev = 0 @end_s2_prev = 0 end |
Instance Method Details
#get_next_anchor ⇒ Object
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 |
# File 'lib/text_alignment/anchor_finder.rb', line 30 def get_next_anchor # find the position of an anchor ngram in s1 and s2 while @beg_s1 < (@s1.length - @size_ngram) anchor = @s1[@beg_s1, @size_ngram] # search_position = 0 search_position = @end_s2_prev while @beg_s2 = @s2.index(anchor, search_position) # if both the begining points are sufficiantly close to the end points of the last match break if @end_s1_prev && (@beg_s1 - @end_s1_prev < 5) && (@beg_s2 >= @end_s2_prev) && (@beg_s2 - @end_s2_prev < 5) left_window_s1, left_window_s2 = get_left_windows break if left_window_s1 && (text_similarity(left_window_s1, left_window_s2) > TextAlignment::TEXT_SIMILARITY_TRESHOLD) right_window_s1, right_window_s2 = get_right_windows break if right_window_s2 && (text_similarity(right_window_s1, right_window_s2) > TextAlignment::TEXT_SIMILARITY_TRESHOLD) search_position = @beg_s2 + 1 end break unless @beg_s2.nil? @beg_s1 += 1 end return nil if @beg_s1 >= (@s1.length - @size_ngram) # extend the block b1 = @beg_s1 b2 = @beg_s2 while b1 >= @end_s1_prev && b2 > -1 && @s1[b1] == @s2[b2] b1 -= 1; b2 -= 1 end b1 += 1; b2 += 1 e1 = @beg_s1 + @size_ngram e2 = @beg_s2 + @size_ngram while @s1[e1] && @s1[e1] == @s2[e2] e1 += 1; e2 += 1 end @end_s1_prev = e1 @end_s2_prev = e2 @beg_s1 = e1 if @reverse {source:{begin:b2 , end:e2}, target:{begin:b1, end:e1}} else {source:{begin:b1 , end:e1}, target:{begin:b2, end:e2}} end end |