Class: TextAlignment::MixedAlignment
- Inherits:
-
Object
- Object
- TextAlignment::MixedAlignment
- Defined in:
- lib/text_alignment/mixed_alignment.rb
Instance Attribute Summary collapse
-
#common_elements ⇒ Object
readonly
Returns the value of attribute common_elements.
-
#mapped_elements ⇒ Object
readonly
Returns the value of attribute mapped_elements.
-
#position_map_begin ⇒ Object
readonly
Returns the value of attribute position_map_begin.
-
#position_map_end ⇒ Object
readonly
Returns the value of attribute position_map_end.
-
#sdiff ⇒ Object
readonly
Returns the value of attribute sdiff.
-
#similarity ⇒ Object
readonly
Returns the value of attribute similarity.
-
#str1_match_final ⇒ Object
readonly
Returns the value of attribute str1_match_final.
-
#str1_match_initial ⇒ Object
readonly
Returns the value of attribute str1_match_initial.
-
#str2_match_final ⇒ Object
readonly
Returns the value of attribute str2_match_final.
-
#str2_match_initial ⇒ Object
readonly
Returns the value of attribute str2_match_initial.
Instance Method Summary collapse
-
#initialize(str1, str2, mappings = []) ⇒ MixedAlignment
constructor
A new instance of MixedAlignment.
- #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 = []) ⇒ MixedAlignment
Returns a new instance of MixedAlignment.
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 |
# File 'lib/text_alignment/mixed_alignment.rb', line 22 def initialize(str1, str2, mappings = []) raise ArgumentError, "nil string" if str1.nil? || str2.nil? raise ArgumentError, "nil mappings" if mappings.nil? ## preprocessing str1 = str1.dup str2 = str2.dup mappings = mappings.dup ## find the first nomatch character TextAlignment::NOMATCH_CHARS.each_char do |c| if str2.index(c).nil? @nomatch_char1 = c break end end raise RuntimeError, "Cannot find nomatch character" if @nomatch_char1.nil? ## find the first nomatch character TextAlignment::NOMATCH_CHARS.each_char do |c| if c != @nomatch_char1 && str1.index(c).nil? @nomatch_char2 = c break end end raise RuntimeError, "Cannot find nomatch character" if @nomatch_char2.nil? # single character mappings character_mappings = mappings.select{|m| m[0].length == 1 && m[1].length == 1} characters_from = character_mappings.collect{|m| m[0]}.join characters_to = character_mappings.collect{|m| m[1]}.join characters_to.gsub!(/-/, '\-') str1.tr!(characters_from, characters_to) str2.tr!(characters_from, characters_to) mappings.delete_if{|m| m[0].length == 1 && m[1].length == 1} # ASCII foldings ascii_foldings = mappings.select{|m| m[0].length == 1 && m[1].length > 1} ascii_foldings.each do |f| from = f[1] if str2.index(f[0]) to = f[0] + (@nomatch_char1 * (f[1].length - 1)) str1.gsub!(from, to) end if str1.index(f[0]) to = f[0] + (@nomatch_char2 * (f[1].length - 1)) str2.gsub!(from, to) end end mappings.delete_if{|m| m[0].length == 1 && m[1].length > 1} _compute_mixed_alignment(str1, str2, mappings) end |
Instance Attribute Details
#common_elements ⇒ Object (readonly)
Returns the value of attribute common_elements.
18 19 20 |
# File 'lib/text_alignment/mixed_alignment.rb', line 18 def common_elements @common_elements end |
#mapped_elements ⇒ Object (readonly)
Returns the value of attribute mapped_elements.
18 19 20 |
# File 'lib/text_alignment/mixed_alignment.rb', line 18 def mapped_elements @mapped_elements end |
#position_map_begin ⇒ Object (readonly)
Returns the value of attribute position_map_begin.
17 18 19 |
# File 'lib/text_alignment/mixed_alignment.rb', line 17 def position_map_begin @position_map_begin end |
#position_map_end ⇒ Object (readonly)
Returns the value of attribute position_map_end.
17 18 19 |
# File 'lib/text_alignment/mixed_alignment.rb', line 17 def position_map_end @position_map_end end |
#sdiff ⇒ Object (readonly)
Returns the value of attribute sdiff.
16 17 18 |
# File 'lib/text_alignment/mixed_alignment.rb', line 16 def sdiff @sdiff end |
#similarity ⇒ Object (readonly)
Returns the value of attribute similarity.
19 20 21 |
# File 'lib/text_alignment/mixed_alignment.rb', line 19 def similarity @similarity end |
#str1_match_final ⇒ Object (readonly)
Returns the value of attribute str1_match_final.
20 21 22 |
# File 'lib/text_alignment/mixed_alignment.rb', line 20 def str1_match_final @str1_match_final end |
#str1_match_initial ⇒ Object (readonly)
Returns the value of attribute str1_match_initial.
20 21 22 |
# File 'lib/text_alignment/mixed_alignment.rb', line 20 def str1_match_initial @str1_match_initial end |
#str2_match_final ⇒ Object (readonly)
Returns the value of attribute str2_match_final.
20 21 22 |
# File 'lib/text_alignment/mixed_alignment.rb', line 20 def str2_match_final @str2_match_final end |
#str2_match_initial ⇒ Object (readonly)
Returns the value of attribute str2_match_initial.
20 21 22 |
# File 'lib/text_alignment/mixed_alignment.rb', line 20 def str2_match_initial @str2_match_initial end |
Instance Method Details
#transform_a_span(span) ⇒ Object
88 89 90 |
# File 'lib/text_alignment/mixed_alignment.rb', line 88 def transform_a_span(span) {begin: @position_map_begin[span[:begin]], end: @position_map_end[span[:end]]} end |
#transform_begin_position(begin_position) ⇒ Object
80 81 82 |
# File 'lib/text_alignment/mixed_alignment.rb', line 80 def transform_begin_position(begin_position) @position_map_begin[begin_position] end |
#transform_denotations!(denotations) ⇒ Object
96 97 98 |
# File 'lib/text_alignment/mixed_alignment.rb', line 96 def transform_denotations!(denotations) denotations.map!{|d| d.begin = @position_map_begin[d.begin]; d.end = @position_map_end[d.end]; d} unless denotations.nil? end |
#transform_end_position(end_position) ⇒ Object
84 85 86 |
# File 'lib/text_alignment/mixed_alignment.rb', line 84 def transform_end_position(end_position) @position_map_end[end_position] end |
#transform_hdenotations(hdenotations) ⇒ Object
100 101 102 103 |
# File 'lib/text_alignment/mixed_alignment.rb', line 100 def transform_hdenotations(hdenotations) return nil if hdenotations.nil? hdenotations.collect{|d| d.dup.merge({span:transform_a_span(d[:span])})} end |
#transform_spans(spans) ⇒ Object
92 93 94 |
# File 'lib/text_alignment/mixed_alignment.rb', line 92 def transform_spans(spans) spans.map{|span| transform_a_span(span)} end |