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, cultivation_map, to_ignore_whitespaces = false, to_ignore_text_order = false) ⇒ AnchorFinder
constructor
A new instance of AnchorFinder.
Constructor Details
#initialize(source_str, target_str, cultivation_map, to_ignore_whitespaces = false, to_ignore_text_order = false) ⇒ AnchorFinder
Returns a new instance of AnchorFinder.
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 |
# File 'lib/text_alignment/anchor_finder.rb', line 9 def initialize(source_str, target_str, cultivation_map, to_ignore_whitespaces = false, to_ignore_text_order = false) @method_get_left_windows, @method_get_right_windows = if to_ignore_whitespaces [method(:get_left_windows_no_squeeze_ws), method(:get_right_windows_no_squeeze_ws)] else [method(:get_left_windows), method(:get_right_windows)] end @s1 = source_str.downcase @s2 = target_str.downcase @cultivation_map = cultivation_map @to_ignore_text_order = to_ignore_text_order @size_ngram = TextAlignment::SIZE_NGRAM @size_window = TextAlignment::SIZE_WINDOW @sim_threshold = TextAlignment::TEXT_SIMILARITY_THRESHOLD @pos_s1_final_possible_begin = @s1.length - @size_ngram - 1 @pos_s2_final_possible_end = @s2.length # positions of last match @pos_s1_last_match = 0 @pos_s2_last_match = 0 # Performance: cache for character classification @half_ngram = @size_ngram / 2 end |
Instance Method Details
#get_next_anchor ⇒ Object
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 |
# File 'lib/text_alignment/anchor_finder.rb', line 36 def get_next_anchor # To find the beginning positions of an anchor ngram in s1 and s2, beginning from the last positions matched iterations = 0 beg_s2 = for beg_s1 in @pos_s1_last_match .. @pos_s1_final_possible_begin iterations += 1 char = @s1[beg_s1] # To skip whitespace letters next if char == ' ' || char == "\n" || char == "\t" # Skip positions that start with punctuation or numbers (likely poor anchors) next if char < 'A' || (char > 'Z' && char < 'a') || char > 'z' # Performance optimization: skip if we've had too many failed attempts recently if iterations > 50 && @recent_failures && @recent_failures > 10 step_size = [@recent_failures / 5, 3].min beg_s1 += step_size next if beg_s1 > @pos_s1_final_possible_begin end _beg_s2 = get_beg_s2(beg_s1) if _beg_s2.nil? @recent_failures = (@recent_failures || 0) + 1 else @recent_failures = 0 # Reset on success end break _beg_s2 unless _beg_s2.nil? end # To return nil when it fails to find an anchor return nil if beg_s2.class == Range # To extend the block to the left b1 = beg_s1 b2 = beg_s2 left_boundary_b2 = [@pos_s2_last_match, (@cultivation_map.last_cultivated_position(b2) || 0)].max while b1 > @pos_s1_last_match && b2 > left_boundary_b2 && @s1[b1 - 1] == @s2[b2 - 1] b1 -= 1; b2 -= 1 end # To extend the block to the right e1 = beg_s1 + @size_ngram e2 = beg_s2 + @size_ngram right_boundary_b2 = @cultivation_map.next_cultivated_position(e2) || @pos_s2_final_possible_end while @s1[e1] && e2 < right_boundary_b2 && @s1[e1] == @s2[e2] e1 += 1; e2 += 1 end @pos_s1_last_match = e1 @pos_s2_last_match = e2 {source:{begin:b1 , end:e1}, target:{begin:b2, end:e2}} end |