Class: TextAlignment::AnchorFinder

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

Instance Method Summary collapse

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
# 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
end

Instance Method Details

#get_next_anchorObject



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
# File 'lib/text_alignment/anchor_finder.rb', line 33

def get_next_anchor
	# To find the beginning positions of an anchor ngram in s1 and s2, beginning from the last positions matched
	beg_s2 = for beg_s1 in @pos_s1_last_match .. @pos_s1_final_possible_begin

		# To skip whitespace letters
		next if [' ', "\n", "\t"].include? @s1[beg_s1]

		_beg_s2 = get_beg_s2(beg_s1)
		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