File size: 15,625 Bytes
7a0d33c 8f05c9e 7a0d33c | 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 | """ุตูุญุฉ ุงูู
ุฑุฌุนุ ููุงุฆู
ุฉ ุงูู
ุฑุงุฌุน โ ุชูุฑูุฑ ู
ุนุงููุฑ ุงูุชูุซูู.
ูููุณุช ุนูู ุชุดุบููุงุช ุญููููุฉ ูุจู ุงูุนูุงุฌ:
ูจูข ุงุณุชุดูุงุฏูุง ู
ู ูจูฃ ุนูู ุณุช ุญูุงุฆุจ ูุจุฏุฃ ู
ู ุงูุตูุญุฉ ูกุ ูุฃู ูู ู
ุตุฏุฑ ูุงู ูููุฑุฃ
ู
ู ุฃูู ุญุฑู ูููุ ูุงูุตูุญุฉ ุงูู
ุฐููุฑุฉ ุตูุญุฉู ุบูุงู ุงูู
ุณุชูุฏ ูุง ุตูุญุฉู ุงูููุฑุฉ.
ุญููุจุฉ ู
ู ููู
ูู ุฎุฑุฌุช ุจู ูกูขูฅ ู
ุฑุฌุนูุงุ ูกูขู ู
ููุง ูู
ููุณุชุดูุฏ ุจู ูู ุงูู
ุชูุ
ููงูกูช ู
ููุง ู
ูุชูุจ ููู ยซ(ุฏ.ุช)ยปุ ููููุง ยซู
ุดุฑูุน ุงููุงุฌู ูู ู
ุตุฑยป ูยซุจูุงููุฑููุณ
ุงูุฌููููุฌูุง ุงูุนุงู
ุฉยป ูู ุญููุจุฉ ุนู ุงูุชุทููุฑ ุงูุนูุงุฑู โ ูุฃู ุงููุงุฆู
ุฉ ูุงูุช ุชูุจูู
ู
ู ูู ุฑุงุจุท ูุฒููู ุงูุจุญุซ ูุง ู
ู ุงูู
ุตุงุฏุฑ ุงูุชู ุงุณุชุนู
ููุง ุงููุต.
"""
from __future__ import annotations
import json
import pytest
from app.agents.arabic_bag.citations import citation_block, in_text, pages_for
from app.agents.arabic_bag.references import (
_work_key,
cited_urls,
drop_stale,
is_in_text_marker,
orphan_citations,
)
from app.agents.arabic_bag.search import _best_window
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ ุตูุญุฉ ุงูููุฑุฉ ูุง ุตูุญุฉ ุงูุบูุงู
def _pages(*spec):
return [(number, text) for number, text in spec]
def test_the_window_is_the_pages_that_discuss_the_cover():
pages = _pages(
(1, "ุตูุญุฉ ุงูุบูุงู ูุงุณู
ุงูุฌุงู
ุนุฉ ูุงูู
ุดุฑู. " * 10),
(2, "ููุฑุณ ุงูู
ุญุชููุงุช ููุงุฆู
ุฉ ุงูุฌุฏุงูู. " * 10),
(18, "ุงูู
ุฎุงุทุฑุฉ ุงูู
ุญุณูุจุฉ ูู ุงูุชุทููุฑ ุงูุนูุงุฑู. " * 10),
(19, "ุฃุฏูุงุช ุฅุฏุงุฑุฉ ุงูู
ุฎุงุทุฑุฉ ุงูู
ุญุณูุจุฉ ุนู
ูููุง. " * 10),
)
excerpt, first, last = _best_window(pages, "ุงูู
ุฎุงุทุฑุฉ ุงูู
ุญุณูุจุฉ ุงูุชุทููุฑ ุงูุนูุงุฑู")
assert (first, last) == (18, 19)
assert "ููุฑุณ ุงูู
ุญุชููุงุช" not in excerpt
def test_a_narrower_window_wins_over_a_wider_one_of_the_same_score():
"""ูุงูุฐุฉ ุชุญู
ู ุงูุฏุฑุฌุฉ ููุณูุง ูู ุตูุญุชูู ุฃุฏูู ูู ุงูุฅุญุงูุฉ ู
ู ูุงูุฐุฉ ุชุจูุบูุง ูู
ุฎู
ุณุ ูุงุจุชูุงุน ุตูุญุงุช ูุง ุชุฎุตู ุงูู
ูุถูุน ูุนูุฏ ุงูู
ุดููุฉ ููุณูุง."""
pages = _pages(
(1, "ู
ูุฏู
ุฉ ุนุงู
ุฉ ูุง ุชุฎุต ุงูู
ูุถูุน. " * 8),
(2, "ุชู
ููุฏ ุนุงู
ุขุฎุฑ. " * 8),
(3, "ุงูุชุณููู ุงูุนูุงุฑู ููููุงุช ุงูุชุฑููุฌ ุงูุฑูู
ูุฉ. " * 8),
)
_, first, last = _best_window(pages, "ุงูุชุณููู ุงูุนูุงุฑู ูููุงุช ุงูุชุฑููุฌ")
assert (first, last) == (3, 3)
def test_a_document_that_never_mentions_the_cover_yields_its_opening():
"""ููู ู
ุง ูุงู ููุนุงุฏ ูุจู ูุฐุงุ ููู ุงูุตุฏู ูู ูุตู ุงูู
ูุชุทู."""
pages = _pages((4, "ูุต ูุง ุนูุงูุฉ ูู. " * 8), (5, "ููุง ูุฐุง. " * 8))
excerpt, first, last = _best_window(pages, "ุงูุฐูุงุก ุงูุงุตุทูุงุนู ุงูุชูููุฏู")
assert first == 4 and excerpt
assert last in (4, 5)
def test_the_document_s_own_page_number_is_used():
"""ุตูุญุฉู ุฑูู
ูุง ูกูง ูุง ุชูุณู
ูู ุงูุฃููู ูุฃู ู
ุง ูุจููุง ูู
ููุณุชุฎุฑุฌ."""
pages = _pages((17, "ุงูุฃู
ู ุงูููุฑู ูุฏู ุงูุทูุงุจ ุงูุฌุงู
ุนููู. " * 10))
_, first, last = _best_window(pages, "ุงูุฃู
ู ุงูููุฑู ุงูุทูุงุจ")
assert (first, last) == (17, 17)
def test_the_citation_reads_the_recorded_range_not_the_arithmetic():
source = {"pages": [19, 21], "page_lengths": [1200, 1200, 1200, 1200]}
assert pages_for(source, 2500) == (19, 21)
def test_a_source_read_before_the_window_existed_still_works():
"""ุชุดุบููุฉ ูุฏูู
ุฉ ูุง ุชุญู
ู `pages`ุ ูููุญุณุจ ุงูู
ุฏู ู
ู ุฃุทูุงู ุงูุตูุญุงุช ูู
ุง ูุงู."""
assert pages_for({"page_lengths": [1200, 1100, 900]}, 2500) == (1, 3)
assert pages_for({"page_lengths": []}, 2500) is None
def test_the_page_marker_carries_its_dot():
assert in_text("ุงูุนุชูุจู", "2023", (19, 19)) == "(ุงูุนุชูุจูุ 2023ุ ุต. 19)"
assert in_text("ุงูุนุชูุจู", "2023", (19, 21)) == "(ุงูุนุชูุจูุ 2023ุ ุต ุต. 19-21)"
assert in_text("Smith", "2023", (19, 21)) == "(Smith, 2023, pp. 19-21)"
def test_two_covers_of_one_document_can_cite_two_different_pages():
"""ยซูู
ูุน ุชูุฑุงุฑ ุฑูู
ุงูุตูุญุฉ ููุณู ูู ุนุฏุฉ ู
ูุงุถุน ู
ุฎุชููุฉ ุฏูู ุชุญููยป."""
pages = _pages(
(5, "ุงูุชู
ููู ุงูุฐุงุชู ูุงูุดุฑุงูุงุช ุงูุงุณุชุซู
ุงุฑูุฉ ูู ุงูู
ุดุฑูุนุงุช. " * 10),
(31, "ุฅุฏุงุฑุฉ ุงูุฃุฒู
ุงุช ูุชูููุน ุงูุงุณุชุซู
ุงุฑุงุช ูู
ูุงุฌูุฉ ุงูู
ุฎุงุทุฑ. " * 10),
)
_, a, _ = _best_window(pages, "ุงูุชู
ููู ุงูุฐุงุชู ุงูุดุฑุงูุงุช ุงูุงุณุชุซู
ุงุฑูุฉ")
_, b, _ = _best_window(pages, "ุฅุฏุงุฑุฉ ุงูุฃุฒู
ุงุช ุชูููุน ุงูุงุณุชุซู
ุงุฑุงุช")
assert a == 5 and b == 31
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ ุงููุงุฆู
ุฉ = ู
ุง ุงุณุชุดูุฏ ุจู ุงูู
ุชู
CREDITS = {
"https://a.org/x.pdf": {"author": "ุงูุนูุฒู", "year": "2023"},
"https://b.org/y.pdf": {"author": "ุงูุบุงู
ุฏู", "year": "2022"},
"https://c.org/z.pdf": {"author": "ุงููุญุทุงูู", "year": "2024"},
}
CONTENT = [{"cover": "ู
ุญูุฑ", "blocks": [
{"block_type": "paragraph",
"paragraphs": ["ูุต ูุณุชุดูุฏ ุจู (ุงูุนูุฒูุ 2023ุ ุต. 19) ุซู
ููุงุตู.",
"ูููุง (ุงูุบุงู
ุฏูุ 2022ุ ุต ุต. 4-6) ูู ู
ูุถุน ุขุฎุฑ."]}]}]
def test_only_the_sources_the_text_cites_reach_the_list():
used = cited_urls(CONTENT, CREDITS)
assert used == {"https://a.org/x.pdf", "https://b.org/y.pdf"}
assert "https://c.org/z.pdf" not in used
def test_a_source_is_matched_whatever_page_the_citation_names():
"""ุฑูู
ุงูุตูุญุฉ ูุฎุชูู ู
ู ู
ูุถุน ูุขุฎุฑุ ูุงูุงุณู
ูุงูุณูุฉ ูู
ุง ู
ุง ูู
ููุฒ ุงูู
ุตุฏุฑ."""
content = [{"blocks": [{"paragraphs": ["(ุงูุนูุฒูุ 2023)"]}]}]
assert cited_urls(content, CREDITS) == {"https://a.org/x.pdf"}
def test_a_citation_with_no_source_in_this_run_is_named_not_hidden():
content = [{"blocks": [{"paragraphs": ["ูุต ููู (ุงูุญุฑุจูุ 2021ุ ุต. 3) ููุท."]}]}]
assert orphan_citations(content, CREDITS) == ["ุงูุญุฑุจู"]
assert orphan_citations(CONTENT, CREDITS) == []
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ ุดูู ุงูู
ุฏุฎู
def test_an_in_text_marker_never_enters_the_list():
"""ยซูุง ูุฌูุฒ ุฃู ุชูุชูู ุตูุบุฉ ุงูุชูุซูู ุฏุงุฎู ุงูู
ุชู ุฅูู ูุงุฆู
ุฉ ุงูู
ุฑุงุฌุนยป."""
assert is_in_text_marker("ุฃุญู
ุฏุ 2020ุ ุต. 3")
assert is_in_text_marker("(ุฃุญู
ุฏุ 2020ุ ุต. 3)")
assert is_in_text_marker("OECD, 2024, p. 55")
assert is_in_text_marker("ุจูุจูุฑ ุฏูุงุจูุ 2012ุ ุต. 7")
def test_a_real_entry_is_not_mistaken_for_a_marker():
assert not is_in_text_marker("ุฃุญู
ุฏุ ู
ุญู
ุฏ. (2020). ุนููุงู ุงููุชุงุจ. ุงููุงูุฑุฉ: ุฏุงุฑ ุงููุดุฑ.")
assert not is_in_text_marker("IBM. (n.d.). What is artificial intelligence? IBM.")
assert not is_in_text_marker("ูุฒุงุฑุฉ ุงูุชุนููู
. (ุฏ.ุช). ุงูุฏููู ุงูุชูุธูู
ู.")
def test_two_shapes_of_one_work_share_a_key():
"""ููุณ ุงููุฑูุฉ ุจุนููุงูููุ ุฃุญุฏูู
ุง ู
ุจุชูุฑ ูุฃุญุฏูู
ุง ุจูุง ุณูุฉ โ ู
ุฏุฎู ูุงุญุฏ."""
a = "ุฃุซุฑ ุงูู
ุฌุงูุงุช ุงูู
ุนุฑููุฉ ูุฅุฏุงุฑุฉ ุงูู
ุดุงุฑูุน ุนูู ุฌูุฏุฉ ุงูู
ุฎุฑุฌุงุช. (2022). https://x/1"
b = "ุฃุซุฑ ุงูู
ุฌุงูุงุช ุงูู
ุนุฑููุฉ ูุฅุฏุงุฑุฉ ุงูู
ุดุงุฑูุน ุนูู ุฌูุฏุฉ ุงูู
ุฎุฑุฌุงุช. (ุฏ.ุช). https://x/2"
assert _work_key(a) != _work_key(b) # ุงูุณูุฉ ุชู
ููุฒ
c = "ุฃุซุฑ ุงูู
ุฌุงูุงุช ุงูู
ุนุฑููุฉ ูุฅุฏุงุฑุฉ ุงูู
ุดุงุฑูุน ุนูู ุฌูุฏุฉ ุงูู
ุฎุฑุฌุงุช. (2022). https://x/2"
assert _work_key(a) == _work_key(c) # ุงูุฑุงุจุท ูุญุฏู ูุง ูู
ููุฒ
def test_an_entry_that_is_only_a_link_keeps_its_own_identity():
"""ุตูุญุฉ ู
ุคุณุณูุฉ ุจูุง ุนููุงู ุญูููู ุชูู
ูููุฒ ุจุฑุงุจุทูุงุ ูุฅูุง ุงูุทูุช ูููุง ูู ู
ุฏุฎู."""
assert _work_key("ู
ุฑุฌุน https://x.org/0") != _work_key("ู
ุฑุฌุน https://x.org/1")
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ ุงูุญุฏุงุซุฉ ูุง ุชูุณุฑ ุงูู
ุทุงุจูุฉ
def _ref(text, language="ar"):
return {"reference": text, "language": language, "kind": "ูุชุงุจ"}
def test_the_recency_floor_is_not_applied_to_a_cited_list():
"""ยซูู ู
ุตุฏุฑ ุชู
ุงูุงุณุชุดูุงุฏ ุจู ูู ุงูู
ุชู ูุฌุจ ุฃู ูุธูุฑ ูู ูุงุฆู
ุฉ ุงูู
ุฑุงุฌุนยป โ
ูุญุฐู ุงููุฏูู
ู
ู ุงููุงุฆู
ุฉ ูุตูุน ุจูุฏู ุงูุฎูู ุงูุฐู ูู
ูุนู."""
import inspect
from app.agents.arabic_bag import references
# ุงูุตูุงุบุฉ ุจูููู ุตุงุฑุช ุขุฎุฑ ู
ูุงุฐุ ูุงูุฃุฑุถูุฉ ุจููุช ู
ุนูุง ูุญุฏูุง.
assert "drop_stale" in inspect.getsource(references._refs_from_agent)
assert "drop_stale" not in inspect.getsource(references.run_references)
def test_the_floor_still_applies_when_nothing_was_cited():
floor_refs = [_ref(f"ู
ุคูู{i}ุ ุฃ. (2024). ุนููุงู.") for i in range(9)]
floor_refs.append(_ref("ูุฏูู
ุ ุจ. (2009). ุนููุงู."))
assert len(drop_stale(floor_refs)) == 9
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ ุงูุฎุทูุฉ ููุณูุง
def test_the_references_step_can_no_longer_be_skipped():
"""ุญููุจุฉ ุดูุบูููุช ุจูุง ูุฐู ุงูุฎุทูุฉ ุฎุฑุฌุช ุจุนุดุฑุฉ ุงุณุชุดูุงุฏุงุช ูุตูุญุฉ ู
ุฑุงุฌุน ูุงุฑุบุฉ."""
from app.pipelines.registry import get, load_all
load_all()
step = next(s for s in get("arabic_training_bag").steps
if s.key == "bag_ar.references")
assert step.optional is False
assert "bag_ar.content" in step.depends_on
assert "bag_ar.citations" in step.depends_on
def test_the_references_step_runs_after_the_content_that_feeds_it():
from app.pipelines.registry import get, load_all
load_all()
keys = [s.key for s in get("arabic_training_bag").steps]
assert keys.index("bag_ar.content") < keys.index("bag_ar.references")
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ ู
ุง ููุนุฑุถ ุนูู ุงููุงุชุจ
def test_a_stale_source_is_still_not_offered_for_citation():
"""ุงูุญุฏุงุซุฉ ู
ูุถุนูุง ุงูุงุฎุชูุงุฑุ ูุจู ุงูุงุณุชุดูุงุฏุ ูุง ุงููุงุฆู
ุฉ ุจุนุฏู."""
scraped = [{"cover": "ู
", "sources": [
{"title": "ุญุฏูุซ", "url": "https://a.org/x.pdf", "pages": [19, 20]},
{"title": "ูุฏูู
", "url": "https://b.org/y.pdf", "pages": [3, 4]}]}]
credits = {"https://a.org/x.pdf": {"author": "ุงูุนูุฒู", "year": "2024"},
"https://b.org/y.pdf": {"author": "ุงููุฏูู
ู", "year": "2011"}}
block = citation_block("ู
", scraped, credits)
assert "ุงูุนูุฒู" in block and "ุงููุฏูู
ู" not in block
# ูุงูุตูุญุฉ ุงูู
ุนุฑูุถุฉ ูู ุตูุญุฉ ุงูู
ูุทุน.
assert "ุต ุต. 19-20" in block
@pytest.mark.parametrize("cover_pages,expected", [
([19, 19], "ุต. 19"),
([19, 21], "ุต ุต. 19-21"),
])
def test_the_offered_citation_names_the_excerpt_s_pages(cover_pages, expected):
scraped = [{"cover": "ู
", "sources": [
{"title": "ุช", "url": "https://a.org/x.pdf", "pages": cover_pages}]}]
credits = {"https://a.org/x.pdf": {"author": "ุงูุนูุฒู", "year": "2024"}}
assert expected in citation_block("ู
", scraped, credits)
def test_the_head_of_the_document_reaches_the_extractor_not_the_excerpt():
"""ุงูู
ุคูู ูุงูุณูุฉ ุนูู ุตูุญุฉ ุงูุนููุงูุ ูุงูู
ูุชุทู ุตุงุฑ ูุจุฏุฃ ู
ู ูุณุท ุงูู
ูู."""
import inspect
from app.agents.arabic_bag import citations
src = inspect.getsource(citations._extract)
assert 's.get("head")' in src
def test_the_source_record_carries_the_window_and_the_head():
import inspect
from app.agents.arabic_bag import sources
src = inspect.getsource(sources.run_source_finder)
assert "focus=focus" in src
assert '"pages": list(pages) if pages else []' in src
assert '"head": head' in src
def test_nothing_in_the_new_path_invents_a_page():
"""ยซูุง ูุฌูุฒ ููุฃุฏุงุฉ ุงุฎุชูุงู ุฑูู
ุตูุญุฉยป โ ูู ุฑูู
ููุทุจุน ุฌุงุก ู
ู ุงูู
ุณุชูุฏ.
ุตูุญุฉ ุงูููุจ ูุง ุตูุญุงุช ููุงุ ููุง ุฑูู
ููุงุ ูุงูู
ุณุชูุฏ ุงูุฐู ูู
ููุณุชุฎุฑุฌ ู
ูู ุดูุก
ูุง ูุงูุฐุฉ ูู.
"""
assert pages_for({"pages": [], "page_lengths": []}, 2500) is None
assert _best_window([], "ุฃู ู
ูุถูุน") == ("", None, None)
web = [{"cover": "ู
", "sources": [
{"title": "ุตูุญุฉ", "url": "https://w.org/a", "pages": [], "page_lengths": []}]}]
block = citation_block("ู
", web, {"https://w.org/a": {"author": "ุงูู
ุงููู",
"year": "2024"}})
assert "โ (ุงูู
ุงูููุ 2024)" in block
assert "ุต." not in block.rsplit("โ", 1)[-1]
def test_the_cited_set_survives_a_json_round_trip():
"""ุงูู
ุญุชูู ูุตู ู
ู ู
ุฎุฑูุฌ ุฎุทูุฉ ู
ุญููุธ ูู ูุงุนุฏุฉ ุงูุจูุงูุงุชุ ูุง ู
ู ุงูุฐุงูุฑุฉ."""
revived = json.loads(json.dumps(CONTENT, ensure_ascii=False))
assert cited_urls(revived, CREDITS) == {"https://a.org/x.pdf",
"https://b.org/y.pdf"}
def test_a_cover_offered_sources_must_cite_at_least_one():
"""ยซุทุงูู
ุง ุงูู
ุตุฏุฑ ููููุง ููู
ุนุงููุฑ ูุจูู ูุณุชุดูุฏ ุจู ูู ุงูู
ุญูุฑ ุฏูยป."""
from app.agents.arabic_bag.writer import validate_written_cover
plan = {"blocks": [{"block_type": "paragraph", "heading": "",
"needs_lead_in": False, "needs_closing_line": False}]}
silent = {"blocks": [{"block_type": "paragraph", "heading": "",
"paragraphs": ["ููุฑุฉ ุจูุง ุฃู ุชูุซูู ูููุง ุฅุทูุงููุง."]}]}
errs = validate_written_cover(silent, plan, sourced=True)
assert any("ุจูุง ุชูุซูู ูุงุญุฏ" in e for e in errs)
voiced = {"blocks": [{"block_type": "paragraph", "heading": "",
"paragraphs": ["ููุฑุฉ ูููุง (ุงูุนูุฒูุ 2023ุ ุต. 19)."]}]}
assert not any("ุจูุง ุชูุซูู ูุงุญุฏ" in e
for e in validate_written_cover(voiced, plan, sourced=True))
def test_a_cover_with_no_sources_is_not_asked_to_cite():
from app.agents.arabic_bag.writer import validate_written_cover
plan = {"blocks": [{"block_type": "paragraph", "heading": "",
"needs_lead_in": False, "needs_closing_line": False}]}
silent = {"blocks": [{"block_type": "paragraph", "heading": "",
"paragraphs": ["ููุฑุฉ ุจูุง ุฃู ุชูุซูู ูููุง ุฅุทูุงููุง."]}]}
assert not any("ุจูุง ุชูุซูู ูุงุญุฏ" in e
for e in validate_written_cover(silent, plan, sourced=False))
|