forked from AgoraDMV/DeltaTrack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_bill.py
More file actions
executable file
·823 lines (687 loc) · 28.7 KB
/
Copy pathdiff_bill.py
File metadata and controls
executable file
·823 lines (687 loc) · 28.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
#!/usr/bin/env python3
"""Compare two bill versions and produce a structured diff."""
import argparse
import difflib
import json
import re
import sys
from collections import Counter, defaultdict
from dataclasses import dataclass
from pathlib import Path
from bill_tree import BillNode, BillTree, normalize_bill, normalize_division_title
from shared.version_stems import version_number_from_stem
# --- Financial amount extraction ---
# A comma-grouped amount must use groups of exactly three digits, so a trailing
# run of digits (e.g. a percentage abutting with no space: "$17,40022%") falls
# outside the match instead of merging into it (#34). The no-comma alternative
# preserves amounts written without thousands separators ("$5000000").
_DOLLAR_RE = re.compile(r"\$\d{1,3}(?:,\d{3})+|\$\d+")
_AMENDMENT_RE = re.compile(r"\((?:increased|reduced|decreased) by\s+\$[\d,]+\)")
def extract_amounts(text: str) -> tuple[int, ...]:
"""Find all dollar amounts in text.
Returns tuple of integer values in document order. $0 is kept: it is real
budget data (e.g. a rescinded or zeroed line), and an unchanged $0 produces
no diff noise (multiset equality), so keeping it only surfaces $0 when it
actually changes (#60). Strips floor amendment annotations like
(increased by $X) before scanning.
"""
text = _AMENDMENT_RE.sub("", text)
results = []
for match in _DOLLAR_RE.finditer(text):
value = int(match.group().replace("$", "").replace(",", ""))
results.append(value)
return tuple(results)
def _extract_word_amounts(words: list[str]) -> list[tuple[int, int]]:
"""Find dollar amounts in a word list, returning (word_index, value) pairs.
Keeps $0 (see extract_amounts, #60). Assumes amendment annotations already stripped.
"""
results = []
for i, word in enumerate(words):
m = _DOLLAR_RE.search(word)
if m:
value = int(m.group().replace("$", "").replace(",", ""))
results.append((i, value))
return results
def match_amounts(
old_text: str | None,
new_text: str | None,
) -> list[tuple[int | None, int | None]]:
"""Pair dollar amounts across old/new text using word-level diff alignment.
Returns list of (old_value, new_value) pairs where:
- (old, new): matched pair (same or changed amount in same context)
- (old, None): removed amount
- (None, new): added amount
Uses SequenceMatcher to align old/new words, then traces dollar amounts
through the diff opcodes to determine pairing.
"""
old_clean = _AMENDMENT_RE.sub("", old_text) if old_text else ""
new_clean = _AMENDMENT_RE.sub("", new_text) if new_text else ""
old_words = old_clean.split()
new_words = new_clean.split()
old_amounts = _extract_word_amounts(old_words)
new_amounts = _extract_word_amounts(new_words)
if not old_amounts and not new_amounts:
return []
# Handle one side empty (added/removed sections)
if not old_words:
return [(None, val) for _, val in new_amounts]
if not new_words:
return [(val, None) for _, val in old_amounts]
sm = difflib.SequenceMatcher(None, old_words, new_words, autojunk=False)
pairs: list[tuple[int | None, int | None]] = []
for op, i1, i2, j1, j2 in sm.get_opcodes():
old_in_range = [(idx, val) for idx, val in old_amounts if i1 <= idx < i2]
new_in_range = [(idx, val) for idx, val in new_amounts if j1 <= idx < j2]
if op == "equal":
# Equal blocks: amounts should match 1:1
for (_, ov), (_, nv) in zip(old_in_range, new_in_range):
pairs.append((ov, nv))
elif op == "delete":
for _, ov in old_in_range:
pairs.append((ov, None))
elif op == "insert":
for _, nv in new_in_range:
pairs.append((None, nv))
elif op == "replace":
# Positional pairing is only trustworthy when both sides hold the same
# number of amounts (a clean value swap). When counts differ, an amount
# was inserted or removed inside the block and position no longer tracks
# meaning, so pairing positionally fabricates a plausible-but-wrong delta
# (e.g. old [$100,$200] vs new [$150,<inserted>,$250] mispairs $200->$250).
# We have no trustworthy correspondence, so report each amount as an
# explicit add/remove rather than guess (#60).
if len(old_in_range) == len(new_in_range):
for (_, ov), (_, nv) in zip(old_in_range, new_in_range):
pairs.append((ov, nv))
else:
for _, ov in old_in_range:
pairs.append((ov, None))
for _, nv in new_in_range:
pairs.append((None, nv))
return pairs
@dataclass(frozen=True)
class FinancialChange:
"""Financial analysis of a single NodeDiff."""
old_amounts: tuple[int, ...]
new_amounts: tuple[int, ...]
amounts_changed: bool
paired_amounts: tuple[tuple[int | None, int | None], ...]
has_amendment_annotations: bool = False
def compute_financial_change(
old_text: str | None,
new_text: str | None,
) -> FinancialChange | None:
"""Compare dollar amounts between old and new text.
Returns None if no amounts on either side (non-financial section).
"""
has_annotations = bool(
(old_text and _AMENDMENT_RE.search(old_text)) or (new_text and _AMENDMENT_RE.search(new_text))
)
old_amounts = extract_amounts(old_text) if old_text else ()
new_amounts = extract_amounts(new_text) if new_text else ()
if not old_amounts and not new_amounts:
return None
paired = match_amounts(old_text, new_text)
return FinancialChange(
old_amounts=old_amounts,
new_amounts=new_amounts,
amounts_changed=Counter(old_amounts) != Counter(new_amounts),
paired_amounts=tuple(paired),
has_amendment_annotations=has_annotations,
)
def financial_change_to_dict(fc: FinancialChange) -> dict:
"""Serialize a FinancialChange for JSON output."""
return {
"old_amounts": list(fc.old_amounts),
"new_amounts": list(fc.new_amounts),
"amounts_changed": fc.amounts_changed,
"paired_amounts": [list(pair) for pair in fc.paired_amounts],
"has_amendment_annotations": fc.has_amendment_annotations,
}
def _similarity_pair(
old_nodes: list[BillNode],
new_nodes: list[BillNode],
) -> list[tuple[BillNode | None, BillNode | None]]:
"""Greedy best-match pairing by text similarity within a group."""
if not old_nodes and not new_nodes:
return []
if not old_nodes:
return [(None, n) for n in new_nodes]
if not new_nodes:
return [(o, None) for o in old_nodes]
if len(old_nodes) == 1 and len(new_nodes) == 1:
return [(old_nodes[0], new_nodes[0])]
# Compute all pairwise similarities
candidates: list[tuple[float, int, int]] = []
for oi, o in enumerate(old_nodes):
o_norm = _normalize_text(o.body_text)
for ni, n in enumerate(new_nodes):
n_norm = _normalize_text(n.body_text)
sim = _text_similarity(o_norm, n_norm)
candidates.append((sim, oi, ni))
# Greedy: highest similarity first
candidates.sort(reverse=True)
claimed_old: set[int] = set()
claimed_new: set[int] = set()
pairs: list[tuple[BillNode | None, BillNode | None]] = []
for _sim, oi, ni in candidates:
if oi in claimed_old or ni in claimed_new:
continue
claimed_old.add(oi)
claimed_new.add(ni)
pairs.append((old_nodes[oi], new_nodes[ni]))
# Leftovers
for oi, o in enumerate(old_nodes):
if oi not in claimed_old:
pairs.append((o, None))
for ni, n in enumerate(new_nodes):
if ni not in claimed_new:
pairs.append((None, n))
return pairs
def _match_collision_group(
old_nodes: list[BillNode],
new_nodes: list[BillNode],
) -> list[tuple[BillNode | None, BillNode | None]]:
"""Resolve a collision group (multiple nodes sharing one match_path).
Uses division titles to sub-group, then text similarity as fallback.
"""
# Step 1: Sub-group by normalized division title
old_by_div: dict[str, list[BillNode]] = defaultdict(list)
new_by_div: dict[str, list[BillNode]] = defaultdict(list)
for node in old_nodes:
old_by_div[normalize_division_title(node.division_label)].append(node)
for node in new_nodes:
new_by_div[normalize_division_title(node.division_label)].append(node)
pairs: list[tuple[BillNode | None, BillNode | None]] = []
unmatched_old: list[BillNode] = []
unmatched_new: list[BillNode] = []
all_divs = dict.fromkeys(list(old_by_div.keys()) + list(new_by_div.keys()))
# Step 2: Pair within each division sub-group
for div_title in all_divs:
div_old = old_by_div.get(div_title, [])
div_new = new_by_div.get(div_title, [])
if not div_old:
unmatched_new.extend(div_new)
elif not div_new:
unmatched_old.extend(div_old)
else:
sub_pairs = _similarity_pair(div_old, div_new)
for o, n in sub_pairs:
if o is None:
unmatched_new.append(n)
elif n is None:
unmatched_old.append(o)
else:
pairs.append((o, n))
# Step 3-4: Cross-division similarity fallback for leftovers
if unmatched_old and unmatched_new:
cross_pairs = _similarity_pair(unmatched_old, unmatched_new)
leftover_old = []
leftover_new = []
for o, n in cross_pairs:
if o is None:
leftover_new.append(n)
elif n is None:
leftover_old.append(o)
else:
pairs.append((o, n))
unmatched_old = leftover_old
unmatched_new = leftover_new
# Step 5: True leftovers
for o in unmatched_old:
pairs.append((o, None))
for n in unmatched_new:
pairs.append((None, n))
return pairs
def match_nodes(
old: BillTree,
new: BillTree,
) -> list[tuple[BillNode | None, BillNode | None]]:
"""Match nodes across two bill versions by match_path.
Returns list of (old_node, new_node) tuples where one side may be None:
- (old, new): matched pair
- (old, None): removed (only in old)
- (None, new): added (only in new)
For unique match_paths, pairs directly (fast path). For collision groups
(multiple nodes sharing one match_path), uses division-aware sub-grouping
with text similarity fallback.
"""
# Group nodes by match_path
old_groups: dict[tuple[str, ...], list[BillNode]] = defaultdict(list)
new_groups: dict[tuple[str, ...], list[BillNode]] = defaultdict(list)
for node in old.nodes:
old_groups[node.match_path].append(node)
for node in new.nodes:
new_groups[node.match_path].append(node)
all_paths = dict.fromkeys(list(old_groups.keys()) + list(new_groups.keys()))
pairs: list[tuple[BillNode | None, BillNode | None]] = []
for path in all_paths:
old_nodes = old_groups.get(path, [])
new_nodes = new_groups.get(path, [])
if len(old_nodes) <= 1 and len(new_nodes) <= 1:
# Fast path: no collision, preserve current behavior
pairs.append(
(
old_nodes[0] if old_nodes else None,
new_nodes[0] if new_nodes else None,
)
)
else:
pairs.extend(_match_collision_group(old_nodes, new_nodes))
return pairs
def diff_text(old_text: str, new_text: str) -> list[str]:
"""Produce unified diff lines between two text blocks.
Returns empty list if texts are identical.
"""
if old_text == new_text:
return []
old_lines = old_text.splitlines(keepends=True)
new_lines = new_text.splitlines(keepends=True)
diff_lines = list(
difflib.unified_diff(
old_lines,
new_lines,
fromfile="old",
tofile="new",
lineterm="",
)
)
# Strip trailing whitespace from each line
return [line.rstrip() for line in diff_lines]
@dataclass(frozen=True)
class NodeDiff:
"""Diff result for a single node."""
display_path_old: tuple[str, ...] | None
display_path_new: tuple[str, ...] | None
match_path: tuple[str, ...]
change_type: str # "added" | "removed" | "modified" | "unchanged"
old_text: str | None
new_text: str | None
text_diff: list[str] | None
section_number: str
element_id_old: str
element_id_new: str
@dataclass(frozen=True)
class BillDiff:
"""Complete diff between two bill versions."""
old_version: str
new_version: str
congress: int
bill_type: str
bill_number: int
summary: dict
changes: list[NodeDiff]
_SIMILARITY_THRESHOLD = 0.4
def _normalize_text(text: str) -> str:
"""Normalize whitespace for comparison: collapse runs, strip."""
return " ".join(text.split())
def _text_similarity(a: str, b: str) -> float:
"""Compute word-level similarity ratio between two texts (0.0 to 1.0)."""
return difflib.SequenceMatcher(None, a.split(), b.split()).ratio()
def _text_similarity_at_least(a: str, b: str, threshold: float) -> float:
"""Word-level similarity, but skip the full computation when it provably
can't reach `threshold`.
`difflib.SequenceMatcher.real_quick_ratio()` (length-based) and
`quick_ratio()` (multiset-based) are documented upper bounds on `ratio()`,
so if either falls below `threshold` the true ratio does too. Returns the
exact ratio when it is >= `threshold`, else `0.0`. Result-preserving for any
caller that compares the result against `threshold` (and uses the exact
value only when it clears it). Matches `_text_similarity` (default autojunk)
when the full ratio is computed.
"""
sm = difflib.SequenceMatcher(None, a.split(), b.split())
if sm.real_quick_ratio() < threshold or sm.quick_ratio() < threshold:
return 0.0
ratio = sm.ratio()
return ratio if ratio >= threshold else 0.0
def _move_candidates(
removed_texts: list[str],
added_texts: list[str],
threshold: float,
) -> list[tuple[float, int, int]]:
"""All `(sim, removed_idx, added_idx)` whose word-level ratio >= `threshold`.
Two behavior-preserving speedups over the naive removed×added double loop:
1. One `SequenceMatcher` is reused with `set_seq2` called once per added text
(difflib's documented "compare one sequence against many" pattern), so the
expensive seq2 index (`__chain_b`) is built once per added text instead of
once per pair.
2. `real_quick_ratio()`/`quick_ratio()` (upper bounds on `ratio()`) gate the
full `ratio()` so impossible pairs are skipped.
The returned tuples are identical to computing `_text_similarity` for every
pair: same seq1/seq2 and autojunk, and the indices are local positions in
`removed_texts`/`added_texts`. Callers sort by the full tuple, so iteration
order does not affect the result.
"""
removed_words = [t.split() for t in removed_texts]
candidates: list[tuple[float, int, int]] = []
sm = difflib.SequenceMatcher() # autojunk=True, matching _text_similarity
for ai, added in enumerate(added_texts):
sm.set_seq2(added.split())
for ri, words in enumerate(removed_words):
sm.set_seq1(words)
if sm.real_quick_ratio() < threshold or sm.quick_ratio() < threshold:
continue
sim = sm.ratio()
if sim >= threshold:
candidates.append((sim, ri, ai))
return candidates
_MOVE_THRESHOLD = 0.6
def reconcile_moves(
changes: list[NodeDiff],
threshold: float = _MOVE_THRESHOLD,
) -> list[NodeDiff]:
"""Re-link removed+added pairs that are actually moved sections.
Computes pairwise text similarity between removed and added entries.
Pairs above threshold are greedily matched (highest similarity first)
and converted to change_type="moved".
"""
removed = [(i, c) for i, c in enumerate(changes) if c.change_type == "removed"]
added = [(i, c) for i, c in enumerate(changes) if c.change_type == "added"]
if not removed or not added:
return changes
# Pairwise similarities (gated + matcher-reused; identical result to the
# naive removed×added double loop, since callers sort by the full tuple).
candidates = _move_candidates(
[_normalize_text(rc.old_text or "") for _, rc in removed],
[_normalize_text(ac.new_text or "") for _, ac in added],
threshold,
)
if not candidates:
return changes
# Greedy: highest similarity first
candidates.sort(reverse=True)
claimed_removed: set[int] = set()
claimed_added: set[int] = set()
moved_indices: set[int] = set() # original indices to remove
moved_entries: list[NodeDiff] = []
for sim, ri, ai in candidates:
if ri in claimed_removed or ai in claimed_added:
continue
claimed_removed.add(ri)
claimed_added.add(ai)
orig_ri, rc = removed[ri]
orig_ai, ac = added[ai]
moved_indices.add(orig_ri)
moved_indices.add(orig_ai)
# Compute text_diff if texts differ
old_norm = _normalize_text(rc.old_text or "")
new_norm = _normalize_text(ac.new_text or "")
text_changes = diff_text(old_norm, new_norm) if old_norm != new_norm else None
moved_entries.append(
NodeDiff(
display_path_old=rc.display_path_old,
display_path_new=ac.display_path_new,
match_path=rc.match_path,
change_type="moved",
old_text=rc.old_text,
new_text=ac.new_text,
text_diff=text_changes,
section_number=ac.section_number or rc.section_number,
element_id_old=rc.element_id_old,
element_id_new=ac.element_id_new,
)
)
# Rebuild: keep non-moved entries in original order, append moved at end
result = [c for i, c in enumerate(changes) if i not in moved_indices]
result.extend(moved_entries)
return result
def _count_changes(changes: list[NodeDiff]) -> dict:
"""Compute summary counts from the final changes list."""
counts = Counter(c.change_type for c in changes)
return {t: counts.get(t, 0) for t in ("added", "removed", "modified", "unchanged", "moved")}
def diff_bills(old: BillTree, new: BillTree) -> BillDiff:
"""Compare two bill versions and produce a structured diff."""
pairs = match_nodes(old, new)
changes: list[NodeDiff] = []
for old_node, new_node in pairs:
if old_node is None and new_node is not None:
changes.append(
NodeDiff(
display_path_old=None,
display_path_new=new_node.display_path,
match_path=new_node.match_path,
change_type="added",
old_text=None,
new_text=new_node.body_text,
text_diff=None,
section_number=new_node.section_number,
element_id_old="",
element_id_new=new_node.element_id,
)
)
elif old_node is not None and new_node is None:
changes.append(
NodeDiff(
display_path_old=old_node.display_path,
display_path_new=None,
match_path=old_node.match_path,
change_type="removed",
old_text=old_node.body_text,
new_text=None,
text_diff=None,
section_number=old_node.section_number,
element_id_old=old_node.element_id,
element_id_new="",
)
)
elif old_node is not None and new_node is not None:
old_normalized = _normalize_text(old_node.body_text)
new_normalized = _normalize_text(new_node.body_text)
text_changes = diff_text(old_normalized, new_normalized)
if not text_changes:
changes.append(
NodeDiff(
display_path_old=old_node.display_path,
display_path_new=new_node.display_path,
match_path=old_node.match_path,
change_type="unchanged",
old_text=old_node.body_text,
new_text=new_node.body_text,
text_diff=None,
section_number=new_node.section_number or old_node.section_number,
element_id_old=old_node.element_id,
element_id_new=new_node.element_id,
)
)
elif _text_similarity(old_normalized, new_normalized) < _SIMILARITY_THRESHOLD:
# Texts too different: false match (e.g., reused section number).
changes.append(
NodeDiff(
display_path_old=old_node.display_path,
display_path_new=None,
match_path=old_node.match_path,
change_type="removed",
old_text=old_node.body_text,
new_text=None,
text_diff=None,
section_number=old_node.section_number,
element_id_old=old_node.element_id,
element_id_new="",
)
)
changes.append(
NodeDiff(
display_path_old=None,
display_path_new=new_node.display_path,
match_path=new_node.match_path,
change_type="added",
old_text=None,
new_text=new_node.body_text,
text_diff=None,
section_number=new_node.section_number,
element_id_old="",
element_id_new=new_node.element_id,
)
)
else:
changes.append(
NodeDiff(
display_path_old=old_node.display_path,
display_path_new=new_node.display_path,
match_path=old_node.match_path,
change_type="modified",
old_text=old_node.body_text,
new_text=new_node.body_text,
text_diff=text_changes,
section_number=new_node.section_number or old_node.section_number,
element_id_old=old_node.element_id,
element_id_new=new_node.element_id,
)
)
changes = reconcile_moves(changes)
return BillDiff(
old_version=old.version,
new_version=new.version,
congress=old.congress,
bill_type=old.bill_type,
bill_number=old.bill_number,
summary=_count_changes(changes),
changes=changes,
)
def bill_diff_to_dict(diff: BillDiff, *, financial: bool = False) -> dict:
"""Serialize a BillDiff to a JSON-compatible dict."""
changes_list = []
financial_change_count = 0
for c in diff.changes:
entry = {
"display_path_old": list(c.display_path_old) if c.display_path_old else None,
"display_path_new": list(c.display_path_new) if c.display_path_new else None,
"match_path": list(c.match_path),
"change_type": c.change_type,
"old_text": c.old_text,
"new_text": c.new_text,
"text_diff": c.text_diff,
"section_number": c.section_number,
"element_id_old": c.element_id_old,
"element_id_new": c.element_id_new,
}
if financial:
fc = compute_financial_change(c.old_text, c.new_text)
if fc is not None:
entry["financial"] = financial_change_to_dict(fc)
if fc.amounts_changed:
financial_change_count += 1
changes_list.append(entry)
result = {
"old_version": diff.old_version,
"new_version": diff.new_version,
"congress": diff.congress,
"bill_type": diff.bill_type,
"bill_number": diff.bill_number,
"summary": diff.summary,
"changes": changes_list,
}
if financial:
result["financial_summary"] = {
"sections_with_financial_changes": financial_change_count,
}
return result
# --- CLI ---
def filter_diff(
diff: BillDiff,
*,
include_unchanged: bool = False,
filter_text: str | None = None,
financial_only: bool = False,
) -> BillDiff:
"""Apply filters to a BillDiff, returning a new BillDiff with filtered changes."""
changes = list(diff.changes)
if not include_unchanged:
changes = [c for c in changes if c.change_type != "unchanged"]
if filter_text:
filter_lower = filter_text.lower()
changes = [c for c in changes if filter_lower in " ".join(c.match_path)]
if financial_only:
changes = [
c
for c in changes
if (fc := compute_financial_change(c.old_text, c.new_text)) is not None and fc.amounts_changed
]
return BillDiff(
old_version=diff.old_version,
new_version=diff.new_version,
congress=diff.congress,
bill_type=diff.bill_type,
bill_number=diff.bill_number,
summary=_count_changes(changes),
changes=changes,
)
def cmd_compare(args: argparse.Namespace) -> None:
old_tree = normalize_bill(Path(args.old_xml))
new_tree = normalize_bill(Path(args.new_xml))
result = diff_bills(old_tree, new_tree)
result = filter_diff(
result,
include_unchanged=args.include_unchanged,
filter_text=args.filter,
financial_only=args.financial,
)
fmt = getattr(args, "format", "json")
# HTML always gets financial enrichment; JSON only when --financial is passed
include_financial = args.financial or fmt == "html"
diff_dict = bill_diff_to_dict(result, financial=include_financial)
# Extract version numbers from filenames (e.g., "1_reported-in-house.xml" -> 1)
for key, xml_arg in (("old_version_number", args.old_xml), ("new_version_number", args.new_xml)):
num = version_number_from_stem(Path(xml_arg).stem)
if num is not None:
diff_dict[key] = num
if fmt == "html":
from bill_tree import bill_title
from formatters.canonical import view_from_canonical, xml_diff_to_canonical
from formatters.diff_html import format_diff_html
from formatters.text_serializer import build_xml_full_text
full_text, full_text_spans, sections, tree = build_xml_full_text(old_tree, new_tree)
canonical = xml_diff_to_canonical(diff_dict, full_text=full_text, full_text_spans=full_text_spans, tree=tree)
output = format_diff_html(
view_from_canonical(canonical),
canonical=canonical,
title=bill_title(new_tree),
sections=sections,
)
else:
output = json.dumps(diff_dict, indent=2)
if args.output:
with open(args.output, "w") as f:
f.write(output)
else:
print(output)
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="Compare two bill XML versions and produce a structured diff.",
)
subparsers = parser.add_subparsers(dest="command")
compare = subparsers.add_parser("compare", help="Compare two bill versions")
compare.add_argument("old_xml", help="Path to older bill XML")
compare.add_argument("new_xml", help="Path to newer bill XML")
compare.add_argument("-o", "--output", help="Output JSON file (default: stdout)")
compare.add_argument(
"--include-unchanged",
action="store_true",
help="Include unchanged nodes in output",
)
compare.add_argument(
"--filter",
help="Only include nodes whose match_path contains this substring",
)
compare.add_argument(
"--financial",
action="store_true",
help="Only show sections with financial changes; add amount details to output",
)
compare.add_argument(
"--format",
choices=["json", "html"],
default="html",
help="Output format (default: html)",
)
return parser
def main() -> None:
parser = build_parser()
args = parser.parse_args()
if args.command == "compare":
cmd_compare(args)
else:
parser.print_help()
sys.exit(1)
if __name__ == "__main__":
main()