Module: Plain::Merge

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

Overview

StructuredMerge plain text fallback behavior.

Defined Under Namespace

Modules: Backend, Version

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



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

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



85
86
87
88
89
90
91
92
# File 'lib/plain/merge.rb', line 85

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



94
95
96
97
98
99
100
101
102
103
104
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
# File 'lib/plain/merge.rb', line 94

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_text(template_source, destination_source) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/plain/merge.rb', line 153

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



30
31
32
33
34
35
36
37
38
# File 'lib/plain/merge.rb', line 30

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



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/plain/merge.rb', line 173

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

.similarity_score(left_source, right_source) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/plain/merge.rb', line 67

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



18
19
20
21
22
23
24
# File 'lib/plain/merge.rb', line 18

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

.text_parse_request(source) ⇒ Object



26
27
28
# File 'lib/plain/merge.rb', line 26

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