varar-core 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6b3afe2eac41dbbd5e500dd3e0e845c77a8bdf3c8d6fd80dd61b63c95abbbf03
4
- data.tar.gz: 9e683dc8f095c659aeea657a9dc93b35b6ab561bf5b08feab08f242d41423ead
3
+ metadata.gz: 15bbbdf4e7eb1cd0ebee2f2610452575277fdcd1462d4f0cc8a2942a1aab2521
4
+ data.tar.gz: cc75918159b4d329eb13a2b7891c49db912abf7280b249e7f0be71514c26b843
5
5
  SHA512:
6
- metadata.gz: 1635a2d5f71a1714286f4bd167fbca6792a8097673e652d0979ae939fc44fe8a279ed0ef1cea3ed54f0cf7325fe8a367ecc5cc989e5b9fd15053d27c78d96ba3
7
- data.tar.gz: 387588fbc0526a7c99fe8281f2993c1f6097e1edeca095aaf5816e36906a79722d8753d10b7cde65b4cba176f8a1067af2f3d0bd949d78c2d6e2c99de8aa0ede
6
+ metadata.gz: 8fa2bb71451e46fae4824c65e1484543a38f54ef6dd05e07db7d4c409a18e7fd4ac95381b3851738f22522023b347b40f1f31b8313ed3c1bff81d1ec29c2f8ea
7
+ data.tar.gz: 311f3b2ae2f5d09844c333518ac31a49ab786863c2b6120c30aea684c0c38b536065fc9fc274fcd068cb18a644daecac093a6c40da1605636c54dcd704b50527
@@ -46,20 +46,28 @@ module Varar
46
46
 
47
47
  module_function
48
48
 
49
- # Do the two spans overlap at all (offset ranges intersect)? A candidate
50
- # paragraph relates to its planned example either way round: a header-bound
51
- # row sits inside its binding paragraph, while a merged example's span
52
- # covers each candidate it absorbed (ADR 0012). Overlap catches both.
53
- def overlaps?(span_a, span_b)
54
- span_a.start_offset < span_b.end_offset && span_b.start_offset < span_a.end_offset
55
- end
56
-
57
49
  # A candidate is "live" (still an example) if it overlaps at least one
58
- # planned example. A now-prose paragraph — one whose step def was renamed
59
- # or deleted — overlaps none (it became a delimiter, splitting any example
60
- # it was part of), so drift catches it.
61
- def live?(candidate_span, plan)
62
- plan.examples.any? { |pe| overlaps?(pe.span, candidate_span) }
50
+ # planned example (their offset ranges intersect). A now-prose paragraph —
51
+ # one whose step def was renamed or deleted — overlaps none (it became a
52
+ # delimiter, splitting any example it was part of), so drift catches it.
53
+ # Overlap, not containment: a header-bound row sits inside its binding
54
+ # paragraph, while a merged example's span covers each candidate it
55
+ # absorbed (ADR 0012).
56
+ #
57
+ # Computed for all candidates at once rather than by scanning every
58
+ # planned example per candidate (#168): sort the planned spans by start
59
+ # and keep the running maximum end. The planned spans that start before a
60
+ # candidate ends are a prefix of that order (found by binary search), and
61
+ # one of them overlaps the candidate iff the largest end among them lies
62
+ # past the candidate's start.
63
+ def liveness(spans, plan)
64
+ planned = plan.examples.map(&:span).sort_by(&:start_offset)
65
+ max_end = []
66
+ planned.each { |p| max_end << [p.end_offset, max_end.last || 0].max }
67
+ spans.map do |span|
68
+ n = planned.bsearch_index { |p| p.start_offset >= span.end_offset } || planned.length
69
+ n.positive? && max_end[n - 1] > span.start_offset
70
+ end
63
71
  end
64
72
 
65
73
  # Lower-cased word tokens (letters/digits) — the unit of similarity.
@@ -76,10 +84,27 @@ module Varar
76
84
  union.zero? ? 0.0 : intersection.to_f / union
77
85
  end
78
86
 
87
+ # A token set as a lookup key: equal sets, equal keys (tokens hold no
88
+ # spaces).
89
+ def token_key(tokens)
90
+ tokens.sort.join(' ')
91
+ end
92
+
93
+ # The candidates' indexes grouped by token set, each group in document
94
+ # order. Similarity is 1 exactly when two token sets are equal, and
95
+ # nothing scores higher, so a baseline paragraph whose set is here has
96
+ # this group as its equally best candidates, found without scoring
97
+ # anything (#168). Only a reworded or removed paragraph falls back to
98
+ # scoring every candidate.
99
+ def index_by_tokens(tokens)
100
+ tokens.each_index.group_by { |i| token_key(tokens[i]) }
101
+ end
102
+
79
103
  # The current example-producing paragraphs, in document order — what a
80
104
  # clean run records as the new baseline for an oath.
81
105
  def live_examples(doc, plan)
82
- doc.examples.select { |candidate| live?(candidate.span, plan) }
106
+ live = liveness(doc.examples.map(&:span), plan)
107
+ doc.examples.select.with_index { |_, i| live[i] }
83
108
  .map { |candidate| Plan.derive_example_key(candidate.body) }
84
109
  end
85
110
 
@@ -108,29 +133,35 @@ module Varar
108
133
 
109
134
  candidates = doc.examples
110
135
  tokens = candidates.map { |c| tokenize(Plan.derive_example_key(c.body)) }
111
- live = candidates.map { |c| live?(c.span, plan) }
112
- consumed = candidates.map do |c|
113
- plan.consumed.any? { |s| s.start_offset == c.span.start_offset && s.end_offset == c.span.end_offset }
114
- end
136
+ live = liveness(candidates.map(&:span), plan)
137
+ consumed = plan.consumed.to_set { |s| [s.start_offset, s.end_offset] }
138
+ exact = index_by_tokens(tokens)
115
139
  occurrences = Hash.new(0)
116
140
 
117
141
  baseline.paragraphs.filter_map do |text|
118
142
  occurrences[text] += 1
119
- best = best_candidates(tokenize(text), tokens)
143
+ best = best_candidates(tokenize(text), tokens, exact)
120
144
  # No recognizable paragraph — a rewrite/removal, not drift.
121
145
  next if best.empty?
122
146
 
123
147
  best_idx = best[[occurrences[text], best.length].min - 1]
124
148
  next if live[best_idx] # still an example — not drift
125
- next if consumed[best_idx] # runs where it is referenced — not drift (#117)
126
149
 
127
- Drift.new(name: text, line: candidates[best_idx].span.start_line, span: candidates[best_idx].span)
150
+ span = candidates[best_idx].span
151
+ # Runs where it is referenced — not drift (#117).
152
+ next if consumed.include?([span.start_offset, span.end_offset])
153
+
154
+ Drift.new(name: text, line: span.start_line, span: span)
128
155
  end
129
156
  end
130
157
 
131
158
  # Indexes of every candidate sharing the best similarity at or above the
132
- # threshold, in document order.
133
- def best_candidates(b_tokens, tokens)
159
+ # threshold, in document order: the exact matches if there are any, else
160
+ # a full scan.
161
+ def best_candidates(b_tokens, tokens, exact)
162
+ hit = exact[token_key(b_tokens)]
163
+ return hit if hit
164
+
134
165
  best_score = 0.0
135
166
  best = []
136
167
  tokens.each_with_index do |candidate_tokens, i|
@@ -159,9 +190,12 @@ module Varar
159
190
  return [] if baseline.nil?
160
191
 
161
192
  known = baseline.paragraphs.map { |text| tokenize(text) }
162
- doc.examples.select { |candidate| live?(candidate.span, plan) }.filter_map do |candidate|
193
+ known_keys = known.to_set { |k| token_key(k) }
194
+ live = liveness(doc.examples.map(&:span), plan)
195
+ doc.examples.select.with_index { |_, i| live[i] }.filter_map do |candidate|
163
196
  text = Plan.derive_example_key(candidate.body)
164
197
  tokens = tokenize(text)
198
+ next if known_keys.include?(token_key(tokens)) # exact match, no scan (#168)
165
199
  next if known.any? { |k| similarity(k, tokens) >= SIMILARITY_THRESHOLD }
166
200
 
167
201
  NewExample.new(text: text, line: candidate.span.start_line)
data/lib/varar/core.rb CHANGED
@@ -4,7 +4,7 @@ module Varar
4
4
  # The pure functional core: parse, match, plan, execute, diffs, drift, and
5
5
  # the conformance projections. No filesystem, network, globals, or time.
6
6
  module Core
7
- VERSION = '0.9.0'
7
+ VERSION = '0.9.1'
8
8
  end
9
9
  end
10
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: varar-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aslak Hellesøy