Uploading dataset files from the local data folder.
Browse files- googleadkcyber_no_venv.tar.gz.gpg +3 -0
- merge_span.py +308 -0
googleadkcyber_no_venv.tar.gz.gpg
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fcd0e3c4d6c7997f5bdb47714101ad5b36c8b0132b7d4b7b85547b0b09e755e9
|
| 3 |
+
size 215455381
|
merge_span.py
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Span merger: given a list of valid line numbers and a list of possible spans,
|
| 3 |
+
produce a list of output spans that accounts for every line number.
|
| 4 |
+
|
| 5 |
+
Assignment rules
|
| 6 |
+
----------------
|
| 7 |
+
Each line number is assigned to the span(s) closest to it:
|
| 8 |
+
|
| 9 |
+
distance(line l, span [a, b]) = max(0, a β l, l β b)
|
| 10 |
+
= 0 if the line is inside the span,
|
| 11 |
+
a β l if the line is left of the span,
|
| 12 |
+
l β b if the line is right of the span.
|
| 13 |
+
|
| 14 |
+
A line is assigned to *every* span that achieves the minimum distance.
|
| 15 |
+
When a line is equidistant between two adjacent spans both spans claim it
|
| 16 |
+
at assignment time.
|
| 17 |
+
|
| 18 |
+
Tie-breaking at span boundaries
|
| 19 |
+
-------------------------------
|
| 20 |
+
For a given span's intermediate output [start, end]:
|
| 21 |
+
β’ start (1st element) β when multiple assigned lines are tied as closest
|
| 22 |
+
to the span's nominal left boundary, the *smallest* is chosen (biased left).
|
| 23 |
+
β’ end (2nd element) β when multiple assigned lines are tied as closest
|
| 24 |
+
to the span's nominal right boundary, the *largest* is chosen (biased right).
|
| 25 |
+
|
| 26 |
+
Because the intermediate boundaries are simply min / max of the assigned
|
| 27 |
+
lines these tie-breaking rules are satisfied automatically.
|
| 28 |
+
|
| 29 |
+
Output normalization
|
| 30 |
+
--------------------
|
| 31 |
+
The returned spans are normalized after assignment:
|
| 32 |
+
|
| 33 |
+
β’ If two consecutive outputs touch at exactly one duplicated boundary
|
| 34 |
+
point, the earlier span is shrunk so the final output stays disjoint.
|
| 35 |
+
β’ If candidate spans overlap enough to create a genuine overlap in the
|
| 36 |
+
output, those overlapping outputs are merged.
|
| 37 |
+
|
| 38 |
+
Special cases
|
| 39 |
+
-------------
|
| 40 |
+
β’ Empty line list β empty output.
|
| 41 |
+
β’ No possible spans β all lines emitted as one ``missing`` span.
|
| 42 |
+
β’ Spans with no lines assigned to them are omitted from the output.
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
from __future__ import annotations
|
| 46 |
+
|
| 47 |
+
from typing import TypedDict
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class SpanResult(TypedDict):
|
| 51 |
+
start: int
|
| 52 |
+
end: int
|
| 53 |
+
missing: bool
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def _distance_to_span(line: int, start: int, end: int) -> int:
|
| 57 |
+
return max(0, start - line, line - end)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def _normalize_spans(possible_spans: list[list[int]]) -> list[tuple[int, int]]:
|
| 61 |
+
normalized: list[tuple[int, int]] = []
|
| 62 |
+
|
| 63 |
+
for idx, span in enumerate(possible_spans):
|
| 64 |
+
if len(span) != 2:
|
| 65 |
+
raise ValueError(f"span at index {idx} must contain exactly two integers")
|
| 66 |
+
|
| 67 |
+
start, end = span
|
| 68 |
+
if not isinstance(start, int) or not isinstance(end, int):
|
| 69 |
+
raise TypeError(f"span at index {idx} must contain integers")
|
| 70 |
+
if start > end:
|
| 71 |
+
raise ValueError(
|
| 72 |
+
f"span at index {idx} has start > end: [{start}, {end}]"
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
normalized.append((start, end))
|
| 76 |
+
|
| 77 |
+
return sorted(normalized, key=lambda span: (span[0], span[1]))
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def merge_spans(
|
| 81 |
+
line_numbers: list[int],
|
| 82 |
+
possible_spans: list[list[int]],
|
| 83 |
+
) -> list[SpanResult]:
|
| 84 |
+
"""
|
| 85 |
+
Parameters
|
| 86 |
+
----------
|
| 87 |
+
line_numbers:
|
| 88 |
+
Unsorted / duplicate-containing list of valid line numbers.
|
| 89 |
+
possible_spans:
|
| 90 |
+
List of [start, end] inclusive span boundaries.
|
| 91 |
+
|
| 92 |
+
Returns
|
| 93 |
+
-------
|
| 94 |
+
List of dicts ``{'start': int, 'end': int, 'missing': bool}``,
|
| 95 |
+
ordered by start position.
|
| 96 |
+
|
| 97 |
+
Each line is assigned to the span(s) with the smallest
|
| 98 |
+
``max(0, a β l, l β b)`` distance. Lines equidistant between two
|
| 99 |
+
adjacent spans are claimed by *both* at assignment time. The final
|
| 100 |
+
returned spans are normalized so the output does not contain a
|
| 101 |
+
duplicated boundary point, and any genuine overlap caused by
|
| 102 |
+
overlapping candidate spans is merged.
|
| 103 |
+
|
| 104 |
+
The output start / end of each span is the min / max of its assigned
|
| 105 |
+
lines, which automatically implements the tie-breaking rule:
|
| 106 |
+
start biased toward smaller values, end toward larger values.
|
| 107 |
+
|
| 108 |
+
Raises
|
| 109 |
+
------
|
| 110 |
+
ValueError
|
| 111 |
+
If any span does not contain exactly two integers or if ``start > end``.
|
| 112 |
+
TypeError
|
| 113 |
+
If any span boundary is not an integer.
|
| 114 |
+
"""
|
| 115 |
+
sorted_lines = sorted(set(line_numbers))
|
| 116 |
+
spans = _normalize_spans(possible_spans)
|
| 117 |
+
|
| 118 |
+
if not sorted_lines:
|
| 119 |
+
return []
|
| 120 |
+
|
| 121 |
+
if not spans:
|
| 122 |
+
return [{"start": sorted_lines[0], "end": sorted_lines[-1], "missing": True}]
|
| 123 |
+
|
| 124 |
+
assignments: list[list[int]] = [[] for _ in spans]
|
| 125 |
+
|
| 126 |
+
for line in sorted_lines:
|
| 127 |
+
min_dist = min(_distance_to_span(line, start, end) for start, end in spans)
|
| 128 |
+
for idx, (start, end) in enumerate(spans):
|
| 129 |
+
if _distance_to_span(line, start, end) == min_dist:
|
| 130 |
+
assignments[idx].append(line)
|
| 131 |
+
|
| 132 |
+
output: list[SpanResult] = []
|
| 133 |
+
out_assignments: list[list[int]] = []
|
| 134 |
+
for assigned in assignments:
|
| 135 |
+
if assigned:
|
| 136 |
+
output.append({"start": assigned[0], "end": assigned[-1], "missing": False})
|
| 137 |
+
out_assignments.append(assigned)
|
| 138 |
+
|
| 139 |
+
i = 0
|
| 140 |
+
while i < len(output) - 1:
|
| 141 |
+
if output[i]["end"] == output[i + 1]["start"]:
|
| 142 |
+
overlap = output[i]["end"]
|
| 143 |
+
smaller = [line for line in out_assignments[i] if line < overlap]
|
| 144 |
+
if smaller:
|
| 145 |
+
output[i]["end"] = smaller[-1]
|
| 146 |
+
i += 1
|
| 147 |
+
else:
|
| 148 |
+
output.pop(i)
|
| 149 |
+
out_assignments.pop(i)
|
| 150 |
+
else:
|
| 151 |
+
i += 1
|
| 152 |
+
|
| 153 |
+
i = 0
|
| 154 |
+
while i < len(output) - 1:
|
| 155 |
+
if output[i]["end"] >= output[i + 1]["start"]:
|
| 156 |
+
output[i] = {
|
| 157 |
+
"start": output[i]["start"],
|
| 158 |
+
"end": max(output[i]["end"], output[i + 1]["end"]),
|
| 159 |
+
"missing": output[i]["missing"] or output[i + 1]["missing"],
|
| 160 |
+
}
|
| 161 |
+
output.pop(i + 1)
|
| 162 |
+
else:
|
| 163 |
+
i += 1
|
| 164 |
+
|
| 165 |
+
return output
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
# --------------------------------------------------------------------------- #
|
| 169 |
+
# Formatting helper #
|
| 170 |
+
# --------------------------------------------------------------------------- #
|
| 171 |
+
|
| 172 |
+
def format_spans(spans: list[SpanResult]) -> str:
|
| 173 |
+
parts = []
|
| 174 |
+
for s in spans:
|
| 175 |
+
tag = f"[{s['start']},{s['end']}]"
|
| 176 |
+
parts.append(tag)
|
| 177 |
+
return "[" + ", ".join(parts) + "]"
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
# --------------------------------------------------------------------------- #
|
| 181 |
+
# Smoke-tests #
|
| 182 |
+
# --------------------------------------------------------------------------- #
|
| 183 |
+
|
| 184 |
+
if __name__ == "__main__":
|
| 185 |
+
cases = [
|
| 186 |
+
# ββ Original examples ββββββββββββββββββββββββββββββββββββββββββββ #
|
| 187 |
+
{
|
| 188 |
+
"label": "Example 1 β pre-line absorbed into nearest span; empty span dropped",
|
| 189 |
+
"lines": [1, 3, 5, 6, 7, 8, 9, 10, 12, 15],
|
| 190 |
+
"spans": [[2, 5], [6, 15], [16, 20]],
|
| 191 |
+
"expected": "[[1,5], [6,15]]",
|
| 192 |
+
},
|
| 193 |
+
{
|
| 194 |
+
"label": "Example 2 β line 5 equidistant β prev span end shrunk from 5β3",
|
| 195 |
+
"lines": [1, 3, 5, 6, 7, 8, 9, 10, 12, 18],
|
| 196 |
+
"spans": [[2, 4], [6, 14], [16, 20]],
|
| 197 |
+
"expected": "[[1,3], [5,12], [18,18]]",
|
| 198 |
+
},
|
| 199 |
+
# ββ Edge cases βββββββββββββββββββββββββββββββββββββββββββββββββββ #
|
| 200 |
+
{
|
| 201 |
+
"label": "EC-01 empty line list β empty output",
|
| 202 |
+
"lines": [],
|
| 203 |
+
"spans": [[1, 5], [6, 10]],
|
| 204 |
+
"expected": "[]",
|
| 205 |
+
},
|
| 206 |
+
{
|
| 207 |
+
"label": "EC-02 no spans β all lines become missing groups",
|
| 208 |
+
# Bug: the old else-branch would double-emit everything.
|
| 209 |
+
"lines": [2, 3, 7, 8],
|
| 210 |
+
"spans": [],
|
| 211 |
+
"expected": "[[2,8]]",
|
| 212 |
+
},
|
| 213 |
+
{
|
| 214 |
+
"label": "EC-03 all lines left of only span β absorbed as nearest",
|
| 215 |
+
"lines": [1, 2, 3],
|
| 216 |
+
"spans": [[10, 20]],
|
| 217 |
+
"expected": "[[1,3]]",
|
| 218 |
+
},
|
| 219 |
+
{
|
| 220 |
+
"label": "EC-04 all lines right of only span β absorbed as nearest",
|
| 221 |
+
"lines": [15, 16, 20],
|
| 222 |
+
"spans": [[1, 10]],
|
| 223 |
+
"expected": "[[15,20]]",
|
| 224 |
+
},
|
| 225 |
+
{
|
| 226 |
+
"label": "EC-05 adjacent spans β each line goes to the span it is inside",
|
| 227 |
+
"lines": [1, 3, 5, 8],
|
| 228 |
+
"spans": [[1, 4], [5, 10]],
|
| 229 |
+
"expected": "[[1,3], [5,8]]",
|
| 230 |
+
},
|
| 231 |
+
{
|
| 232 |
+
"label": "EC-06 duplicate line numbers β deduplicated before processing",
|
| 233 |
+
"lines": [3, 3, 5, 5, 7],
|
| 234 |
+
"spans": [[1, 10]],
|
| 235 |
+
"expected": "[[3,7]]",
|
| 236 |
+
},
|
| 237 |
+
{
|
| 238 |
+
"label": "EC-07 line 7 equidistant β prev span end shrunk from 7β3",
|
| 239 |
+
"lines": [3, 7, 11],
|
| 240 |
+
"spans": [[1, 5], [9, 15]],
|
| 241 |
+
"expected": "[[3,3], [7,11]]",
|
| 242 |
+
},
|
| 243 |
+
{
|
| 244 |
+
"label": "EC-08 empty gap β each line assigned to the span it is inside, no overlap",
|
| 245 |
+
"lines": [2, 4, 8, 12],
|
| 246 |
+
"spans": [[1, 5], [7, 15]],
|
| 247 |
+
"expected": "[[2,4], [8,12]]",
|
| 248 |
+
},
|
| 249 |
+
{
|
| 250 |
+
"label": "EC-09 single line, span wider than needed β clipped at line",
|
| 251 |
+
"lines": [5],
|
| 252 |
+
"spans": [[3, 7]],
|
| 253 |
+
"expected": "[[5,5]]",
|
| 254 |
+
},
|
| 255 |
+
{
|
| 256 |
+
"label": "EC-10 line 6 equidistant β prev span end shrunk from 6β2",
|
| 257 |
+
"lines": [2, 6, 7, 11, 15],
|
| 258 |
+
"spans": [[1, 4], [8, 12], [14, 20]],
|
| 259 |
+
"expected": "[[2,2], [6,11], [15,15]]",
|
| 260 |
+
},
|
| 261 |
+
{
|
| 262 |
+
"label": "EC-11 single span β all lines (pre and inside) absorbed as nearest",
|
| 263 |
+
"lines": [1, 2, 5, 6, 15],
|
| 264 |
+
"spans": [[10, 20]],
|
| 265 |
+
"expected": "[[1,15]]",
|
| 266 |
+
},
|
| 267 |
+
{
|
| 268 |
+
"label": "EC-12 first span gets no lines; subsequent spans absorb orphans",
|
| 269 |
+
"lines": [3, 8],
|
| 270 |
+
"spans": [[1, 2], [5, 7], [10, 15]],
|
| 271 |
+
"expected": "[[3,3], [8,8]]",
|
| 272 |
+
},
|
| 273 |
+
{
|
| 274 |
+
"label": "EC-13 line 10 equidistant β prev span end shrunk from 10β5",
|
| 275 |
+
"lines": [1, 5, 10, 15, 20],
|
| 276 |
+
"spans": [[3, 8], [12, 18]],
|
| 277 |
+
"expected": "[[1,5], [10,20]]",
|
| 278 |
+
},
|
| 279 |
+
{
|
| 280 |
+
"label": "EC-14 overlap where prev span has no smaller line β prev span dropped",
|
| 281 |
+
# line 7 equidistant between [1,5] and [9,15]: both claim it.
|
| 282 |
+
# span 0 assigned=[7] only; no line < 7 exists β span 0 is dropped.
|
| 283 |
+
"lines": [7, 11],
|
| 284 |
+
"spans": [[1, 5], [9, 15]],
|
| 285 |
+
"expected": "[[7,11]]",
|
| 286 |
+
},
|
| 287 |
+
{
|
| 288 |
+
"label": "EC-15 overlapping possible spans β genuine overlap merged in pass 2",
|
| 289 |
+
# spans [1,8] and [3,10] overlap; lines 4 and 6 are inside both β
|
| 290 |
+
# both spans claim 4 and 6, so output before pass 2 = [[2,6],[4,9]].
|
| 291 |
+
# pass 1 sees 6 != 4 (strict >), skips; pass 2 merges β [[2,9]].
|
| 292 |
+
"lines": [2, 4, 6, 9],
|
| 293 |
+
"spans": [[1, 8], [3, 10]],
|
| 294 |
+
"expected": "[[2,9]]",
|
| 295 |
+
},
|
| 296 |
+
]
|
| 297 |
+
|
| 298 |
+
for case in cases:
|
| 299 |
+
result = merge_spans(case["lines"], case["spans"])
|
| 300 |
+
formatted = format_spans(result)
|
| 301 |
+
status = "β" if formatted == case["expected"] else "β"
|
| 302 |
+
print(f"{status} {case['label']}")
|
| 303 |
+
print(f" lines: {case['lines']}")
|
| 304 |
+
print(f" spans: {case['spans']}")
|
| 305 |
+
print(f" output: {formatted}")
|
| 306 |
+
print(f" expected: {case['expected']}")
|
| 307 |
+
print()
|
| 308 |
+
|