Repository navigation
Expand file tree
/
Copy pathtest_diffing.py
More file actions
115 lines (85 loc) · 4.27 KB
/
Copy pathtest_diffing.py
File metadata and controls
115 lines (85 loc) · 4.27 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
import math
from backend.app.diffing import (
compute_analyst_metrics,
cosine_similarity_score,
diff_paragraphs,
fog_index,
section_breakdown,
)
def test_diff_paragraphs_classifies_unchanged_added_removed():
a = ["Alpha paragraph.", "Beta paragraph.", "Gamma paragraph."]
b = ["Alpha paragraph.", "Gamma paragraph.", "Delta paragraph."]
chunks, summary = diff_paragraphs(a, b)
types = {c["text"]: c["type"] for c in chunks}
assert types["Alpha paragraph."] == "unchanged"
assert types["Beta paragraph."] == "removed"
assert types["Delta paragraph."] == "added"
assert summary["added_chunks"] == 1
assert summary["removed_chunks"] == 1
assert summary["unchanged_chunks"] == 2 # Alpha + Gamma
def test_diff_paragraphs_replace_renders_as_remove_then_insert():
a = ["We face intense competition."]
b = ["We face intense competition from new market entrants."]
chunks, summary = diff_paragraphs(a, b)
assert [c["type"] for c in chunks] == ["removed", "added"]
assert summary["removed_chunks"] == 1
assert summary["added_chunks"] == 1
def test_diff_paragraphs_identical_input_is_all_unchanged():
a = ["One.", "Two.", "Three."]
chunks, summary = diff_paragraphs(a, list(a))
assert all(c["type"] == "unchanged" for c in chunks)
assert summary["added_chunks"] == 0
assert summary["removed_chunks"] == 0
assert summary["unchanged_chunks"] == 3
def test_cosine_similarity_identical_text_is_100():
text = "Our business depends on continued demand for our products and services."
assert cosine_similarity_score(text, text) == 100.0
def test_cosine_similarity_disjoint_text_is_zero():
assert cosine_similarity_score("apple banana cherry", "xylophone zeppelin quokka") == 0.0
def test_cosine_similarity_empty_text_is_zero():
assert cosine_similarity_score("", "some text here") == 0.0
assert cosine_similarity_score("some text here", "") == 0.0
def test_fog_index_higher_for_longer_more_complex_sentences():
simple = "We are a company. We sell goods. We make money."
complex_text = (
"Our multinational organization systematically evaluates macroeconomic "
"considerations affecting operational profitability across jurisdictions."
)
simple_fog = fog_index(simple)
complex_fog = fog_index(complex_text)
assert simple_fog is not None and complex_fog is not None
assert complex_fog > simple_fog
def test_fog_index_empty_text_returns_none():
assert fog_index("") is None
def test_compute_analyst_metrics_word_count_change_pct():
a = "one two three four"
b = "one two three four five six"
metrics = compute_analyst_metrics(a, b)
assert metrics["word_count_from"] == 4
assert metrics["word_count_to"] == 6
assert math.isclose(metrics["word_count_change_pct"], 50.0, abs_tol=0.1)
def test_section_breakdown_groups_under_real_headings():
chunks = [
{"type": "unchanged", "text": "Cybersecurity Risks", "heading": True},
{"type": "unchanged", "text": "We face risks from cyber attacks on our systems.", "heading": False},
{"type": "added", "text": "New disclosure about a recent security incident response.", "heading": False},
{"type": "unchanged", "text": "Regulatory Risks", "heading": True},
{"type": "removed", "text": "Old language about a since-resolved regulatory matter.", "heading": False},
]
sections = section_breakdown(chunks)
titles = [s["title"] for s in sections]
assert "Cybersecurity Risks" in titles
assert "Regulatory Risks" in titles
cyber = next(s for s in sections if s["title"] == "Cybersecurity Risks")
assert cyber["added_words"] > 0
assert cyber["removed_words"] == 0
reg = next(s for s in sections if s["title"] == "Regulatory Risks")
assert reg["removed_words"] > 0
assert math.isclose(sum(s["share_pct"] for s in sections), 100.0, abs_tol=0.5)
def test_section_breakdown_no_headings_falls_under_general():
chunks = [{"type": "unchanged", "text": "Just a plain paragraph with no heading before it.", "heading": False}]
sections = section_breakdown(chunks)
assert len(sections) == 1
assert sections[0]["title"] == "General disclosures"
def test_section_breakdown_empty_chunks_returns_empty():
assert section_breakdown([]) == []