Class: TextAlignment::CharMapping

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_text, char_mapping = nil, to_ignore_whitespaces = false) ⇒ CharMapping

Returns a new instance of CharMapping.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/text_alignment/char_mapping.rb', line 116

def initialize(_text, char_mapping = nil, to_ignore_whitespaces = false)
	if to_ignore_whitespaces
		@method_get_positions_squeeze_ws = method(:get_positions_squeeze_ws_0)
		@method_squeeze_ws = method(:squeeze_ws_0!)
	else
		@method_get_positions_squeeze_ws = method(:get_positions_squeeze_ws_1)
		@method_squeeze_ws = method(:squeeze_ws_1!)
	end

	@text  = _text

	# sort by the length of the spell-outs is important
	char_mapping ||= TextAlignment::CHAR_MAPPING.sort{|a, b| b[1].length <=> a[1].length}
	@mapped_text, offset_mapping = enmap_text(_text, char_mapping)
	@index_enmap = offset_mapping.to_h
	@index_demap = offset_mapping.map{|m| m.reverse}.to_h
end

Instance Attribute Details

#index_enmapObject (readonly)

Returns the value of attribute index_enmap.



114
115
116
# File 'lib/text_alignment/char_mapping.rb', line 114

def index_enmap
  @index_enmap
end

#mapped_textObject (readonly)

Returns the value of attribute mapped_text.



114
115
116
# File 'lib/text_alignment/char_mapping.rb', line 114

def mapped_text
  @mapped_text
end

Instance Method Details

#demap_position(position) ⇒ Object



138
139
140
# File 'lib/text_alignment/char_mapping.rb', line 138

def demap_position(position)
	@index_demap[position]
end

#enmap_denotations(denotations) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/text_alignment/char_mapping.rb', line 142

def enmap_denotations(denotations)
	return nil if denotations.nil?

	denotations.map do |d|
		d.dup.merge(span:{begin:enmap_position(d[:span][:begin]), end:enmap_position(d[:span][:end])})
	end
end

#enmap_position(position) ⇒ Object



134
135
136
# File 'lib/text_alignment/char_mapping.rb', line 134

def enmap_position(position)
	@index_enmap[position]
end