Spaces:
Sleeping
Sleeping
File size: 8,219 Bytes
06671aa | 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 | from __future__ import annotations
import sys
import os
import pytest
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from comment_target_resolver import (
_build_char_map,
_normalize_for_match,
_find_all,
_resolve_target_in_paragraph,
resolve_single_target,
ResolveResult,
)
from models import ParagraphText
class TestCharMap:
def test_basic(self):
cm = _build_char_map("Hello World")
assert cm.normalized == "HelloWorld"
assert cm.orig_offsets == [0, 1, 2, 3, 4, 6, 7, 8, 9, 10]
def test_nbsp_stripped(self):
cm = _build_char_map("Hello\u00a0World")
assert cm.normalized == "HelloWorld"
def test_nbsp_offsets(self):
cm = _build_char_map("A\u00a0B")
assert cm.normalized == "AB"
assert cm.orig_offsets == [0, 2]
def test_nbsp(self):
cm = _build_char_map("Hello\u00a0World")
assert cm.normalized == "HelloWorld"
def test_smart_quotes(self):
cm = _build_char_map("\u201cHello\u201d")
assert cm.normalized == '"Hello"'
def test_em_dash(self):
cm = _build_char_map("A\u2014B")
assert cm.normalized == "A-B"
class TestNormalize:
def test_missing_spaces(self):
norm = _normalize_for_match("the Sellershall")
assert norm == "theSellershall"
def test_nbsp_normalized(self):
norm = _normalize_for_match("Hello\u00a0World")
assert norm == "HelloWorld"
def test_spaces_stripped(self):
norm = _normalize_for_match("The Seller shall")
assert norm == "TheSellershall"
class TestFindAll:
def test_single(self):
assert _find_all("abc", "b") == [1]
def test_multiple(self):
assert _find_all("ababa", "a") == [0, 2, 4]
def test_none(self):
assert _find_all("abc", "z") == []
class TestResolveTargetInParagraph:
def test_exact_match(self):
result = _resolve_target_in_paragraph(
"The Seller shall deliver the goods promptly.",
"shall deliver the goods",
"", "",
)
assert result is not None
assert result.strategy == "exact"
assert result.confidence == 1.0
assert result.target_text == "shall deliver the goods"
def test_exact_match_with_anchors(self):
result = _resolve_target_in_paragraph(
"The Seller shall deliver the goods promptly.",
"shall deliver the goods",
"Seller ",
" promptly",
)
assert result is not None
assert result.strategy == "exact"
assert result.was_resolved is False
assert result.before_anchor == "Seller "
assert result.after_anchor == " promptly"
def test_missing_spaces(self):
result = _resolve_target_in_paragraph(
"The Seller shall not be liable for any loss of revenue.",
"the Sellershall not be liable for anyloss of revenue",
"", "",
)
assert result is not None
assert result.strategy == "whitespace_normalized"
assert "Seller shall not be liable for any loss of revenue" in result.target_text
def test_missing_spaces_around_parens(self):
result = _resolve_target_in_paragraph(
"ABC Construction Ltd (The Buyer)",
"ABC Construction Ltd(TheBuyer)",
"", "",
)
assert result is not None
assert result.strategy == "whitespace_normalized"
assert result.target_text == "ABC Construction Ltd (The Buyer)"
def test_missing_space_after_punctuation(self):
result = _resolve_target_in_paragraph(
"from the factory. This warranty extends only to",
"factory.This warrantyextendsonly to",
"", "",
)
assert result is not None
assert result.strategy == "whitespace_normalized"
assert "factory. This warranty extends only" in result.target_text
def test_nbsp_vs_space(self):
result = _resolve_target_in_paragraph(
"The\u00a0materials remain the sole property of the Seller",
"The materials remain the sole property ofthe Seller",
"", "",
)
assert result is not None
assert result.strategy == "whitespace_normalized"
def test_smart_quotes(self):
result = _resolve_target_in_paragraph(
'He said \u201cno liability\u201d for damages.',
'He said "no liability" for damages',
"", "",
)
assert result is not None
assert result.strategy == "whitespace_normalized"
def test_em_dash(self):
result = _resolve_target_in_paragraph(
"Payment \u2014 if delayed \u2014 incurs interest.",
"Payment - if delayed - incurs interest.",
"", "",
)
assert result is not None
def test_duplicate_with_anchors(self):
para = "The Seller shall deliver. The Seller shall deliver promptly."
result = _resolve_target_in_paragraph(
para,
"The Sellershall deliver",
"The Seller", "promptly",
)
assert result is not None
assert result.strategy in ("anchor_normalized", "whitespace_normalized")
def test_no_match_returns_none(self):
result = _resolve_target_in_paragraph(
"Completely unrelated text here.",
"The Seller shall not be liable",
"", "",
)
assert result is None
def test_sentence_fallback(self):
para = "The Seller reserves all rights under this clause. The Buyer shall pay within 30 days. Force majeure does not apply."
result = _resolve_target_in_paragraph(
para,
"The Sellerreserves all rightsunder this clause",
"", "",
)
assert result is not None
assert result.strategy in ("whitespace_normalized", "sentence_fallback")
assert "The Seller reserves all rights under this clause" in result.target_text
def test_sentence_fallback_low_confidence_fails(self):
para = "The Seller reserves all rights. The Buyer shall pay. Force majeure does not apply."
result = _resolve_target_in_paragraph(
para,
"completely different and unrelated gibberish text",
"", "",
)
assert result is None
class TestResolveSingleTarget:
def test_with_paragraph_list(self):
paragraphs = [
ParagraphText(id="P_0001", text="The Seller shall deliver the goods."),
ParagraphText(id="P_0002", text="The Buyer shall pay the price."),
]
result = resolve_single_target(
paragraphs, "P_0001",
"the Sellershall deliver the goods",
"", "",
)
assert result.strategy == "whitespace_normalized"
assert result.paragraph_id == "P_0001"
assert "The Seller shall deliver the goods" in result.target_text
def test_missing_paragraph(self):
paragraphs = [
ParagraphText(id="P_0001", text="Some text."),
]
result = resolve_single_target(
paragraphs, "P_9999",
"Some text",
"", "",
)
assert result.strategy == "not_resolved"
assert result.confidence == 0.0
def test_paragraph_fallback(self):
paragraphs = [
ParagraphText(id="P_0001", text="Short paragraph here."),
]
result = resolve_single_target(
paragraphs, "P_0001",
"totally unrelated",
"", "",
allow_paragraph_fallback=True,
)
assert result.strategy == "paragraph_fallback"
def test_paragraph_fallback_disabled(self):
paragraphs = [
ParagraphText(id="P_0001", text="Short paragraph here."),
]
result = resolve_single_target(
paragraphs, "P_0001",
"totally unrelated",
"", "",
allow_paragraph_fallback=False,
)
assert result.strategy == "not_resolved"
assert result.confidence == 0.0
if __name__ == "__main__":
pytest.main([__file__, "-v"])
|