Module: Plain::Merge

Defined in:
lib/plain/merge/backend.rb,
lib/plain/merge.rb,
lib/plain/merge/version.rb,
lib/plain/merge/provider.rb,
sig/plain/merge.rbs

Overview

StructuredMerge plain text fallback behavior.

Defined Under Namespace

Modules: Backend, Version Classes: Provider

Constant Summary collapse

PACKAGE_NAME =
'plain-merge'
DEFAULT_TEXT_REFINEMENT_THRESHOLD =
0.7
DEFAULT_TEXT_REFINEMENT_WEIGHTS =
{
  content: 0.7,
  length: 0.15,
  position: 0.15
}.freeze
BACKEND_REFERENCE =
TreeHaver::BackendReference.new(id: 'plain-line', family: 'line')
VERSION =

Current gem version exposed at the traditional constant location.

Returns:

  • (String)
Version::VERSION

Class Method Summary collapse

Class Method Details

.analyze_text(source) ⇒ Object



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
# File 'lib/plain/merge.rb', line 51

def analyze_text(source)
  normalized_source = normalize_text(source)
  parts = normalized_source.empty? ? [] : normalized_source.split("\n\n")
  cursor = 0

  blocks = parts.each_with_index.map do |normalized, index|
    start_offset = cursor
    end_offset = start_offset + normalized.length
    cursor = end_offset + 2

    {
      index: index,
      normalized: normalized,
      span: {
        start: start_offset,
        end: end_offset
      }
    }
  end

  {
    kind: 'text',
    normalized_source: normalized_source,
    blocks: blocks
  }
end

.is_similar(left_source, right_source, threshold) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/plain/merge.rb', line 96

def is_similar(left_source, right_source, threshold)
  score = similarity_score(left_source, right_source)
  {
    score: score,
    threshold: threshold,
    matched: score >= threshold
  }
end

.match_text_blocks(template_source, destination_source) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/plain/merge.rb', line 105

def match_text_blocks(template_source, destination_source)
  template = analyze_text(template_source)
  destination = analyze_text(destination_source)
  matched_template = {}
  matched_destination = {}
  matched = []

  destination[:blocks].each_with_index do |destination_block, destination_index|
    template_index = template[:blocks].find_index.with_index do |template_block, candidate_index|
      !matched_template[candidate_index] && template_block[:normalized] == destination_block[:normalized]
    end
    if template_index
      matched_template[template_index] = true
      matched_destination[destination_index] = true
      matched << {
        template_index: template_index,
        destination_index: destination_index,
        phase: 'exact',
        score: 1.0
      }
      next
    end

    best_template_index = nil
    best_score = 0.0
    template[:blocks].each_with_index do |template_block, template_index|
      next if matched_template[template_index]

      score = refined_text_similarity(
        template_block,
        destination_block,
        template[:blocks].length,
        destination[:blocks].length
      )
      next unless score >= DEFAULT_TEXT_REFINEMENT_THRESHOLD && score > best_score

      best_score = score
      best_template_index = template_index
    end

    next unless best_template_index

    matched_template[best_template_index] = true
    matched_destination[destination_index] = true
    matched << {
      template_index: best_template_index,
      destination_index: destination_index,
      phase: 'refined',
      score: best_score
    }
  end

  {
    matched: matched,
    unmatched_template: template[:blocks].each_index.reject { |index| matched_template[index] },
    unmatched_destination: destination[:blocks].each_index.reject { |index| matched_destination[index] }
  }
end

.merge_providerProvider

Returns:



29
30
31
# File 'lib/plain/merge.rb', line 29

def merge_provider
  @merge_provider ||= Provider.new
end

.merge_text(template_source, destination_source) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/plain/merge.rb', line 164

def merge_text(template_source, destination_source)
  template = analyze_text(template_source)
  destination = analyze_text(destination_source)
  matches = match_text_blocks(template_source, destination_source)
  matched_template = matches[:matched].each_with_object({}) { |match, memo| memo[match[:template_index]] = true }
  merged_blocks = destination[:blocks].map { |block| block[:normalized] }

  template[:blocks].each_with_index do |block, index|
    next if matched_template[index]

    merged_blocks << block[:normalized]
  end

  {
    ok: true,
    diagnostics: [],
    output: merged_blocks.join("\n\n")
  }
end

.normalize_text(source) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/plain/merge.rb', line 41

def normalize_text(source)
  source
    .gsub(/\r\n?/, "\n")
    .strip
    .split(/\n\s*\n+/)
    .map { |block| block.strip.gsub(/\s+/, ' ') }
    .reject(&:empty?)
    .join("\n\n")
end

.refined_text_similarity(template_block, destination_block, template_total, destination_total, weights = DEFAULT_TEXT_REFINEMENT_WEIGHTS) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/plain/merge.rb', line 184

def refined_text_similarity(template_block, destination_block, template_total, destination_total,
                            weights = DEFAULT_TEXT_REFINEMENT_WEIGHTS)
  content = string_similarity(template_block[:normalized], destination_block[:normalized])
  length = length_similarity(template_block[:normalized], destination_block[:normalized])
  position = position_similarity(
    template_block[:index],
    destination_block[:index],
    template_total,
    destination_total
  )

  (weights[:content] * content) + (weights[:length] * length) + (weights[:position] * position)
end

.register_backend!Object



40
41
42
43
44
45
46
# File 'lib/plain/merge/backend.rb', line 40

def self.register_backend!
  TreeHaver::BackendRegistry.register(BACKEND_REFERENCE)
  %i[text plain].each do |language_name|
    register_language!(language_name)
  end
  nil
end

.register_language!(language_name) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/plain/merge/backend.rb', line 48

def self.register_language!(language_name)
  TreeHaver.register_language(
    language_name,
    backend_module: Backend,
    backend_type: :line,
    gem_name: 'plain-merge',
    contract: :line
  )
end

.register_provider!(replace: false) ⇒ Ast::Merge::_MergeProvider

Parameters:

  • replace: (Boolean) (defaults to: false)

Returns:

  • (Ast::Merge::_MergeProvider)


33
34
35
# File 'lib/plain/merge.rb', line 33

def register_provider!(replace: false)
  Ast::Merge.register_provider(merge_provider, replace: replace)
end

.similarity_score(left_source, right_source) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/plain/merge.rb', line 78

def similarity_score(left_source, right_source)
  left = analyze_text(left_source)
  right = analyze_text(right_source)
  total = [left[:blocks].length, right[:blocks].length].max
  return 1.0 if total.zero?

  sum = 0.0
  total.times do |index|
    left_block = left[:blocks][index]
    right_block = right[:blocks][index]
    next unless left_block && right_block

    sum += jaccard(left_block[:normalized], right_block[:normalized])
  end

  sum / total
end

.text_feature_profileObject



21
22
23
24
25
26
27
# File 'lib/plain/merge.rb', line 21

def text_feature_profile
  {
    family: 'text',
    supported_dialects: [],
    supported_policies: []
  }
end

.text_parse_request(source) ⇒ Object



37
38
39
# File 'lib/plain/merge.rb', line 37

def text_parse_request(source)
  TreeHaver::ParserRequest.new(source: source, language: 'text')
end