Add tests/test_corpus.py
Browse files- tests/test_corpus.py +100 -0
tests/test_corpus.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Article cleaning and the balanced selection policy."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import pytest
|
| 7 |
+
|
| 8 |
+
from ehekim.corpus import clean_articles, parent_id_for, select_articles
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def make_frame(counts: dict[str, int], body_len: int = 800) -> pd.DataFrame:
|
| 12 |
+
rows = []
|
| 13 |
+
for source, n in counts.items():
|
| 14 |
+
for i in range(n):
|
| 15 |
+
rows.append(
|
| 16 |
+
{
|
| 17 |
+
"url": f"https://{source}.example/makale-{i}",
|
| 18 |
+
"title": f"{source} makale {i}",
|
| 19 |
+
# Unique bodies so the text-dedup step does not remove them.
|
| 20 |
+
"text": f"{source}-{i} " + ("kelime " * body_len),
|
| 21 |
+
"source": source,
|
| 22 |
+
}
|
| 23 |
+
)
|
| 24 |
+
return pd.DataFrame(rows)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class TestCleanArticles:
|
| 28 |
+
def test_drops_null_and_short_and_nonhttp(self):
|
| 29 |
+
df = pd.DataFrame(
|
| 30 |
+
[
|
| 31 |
+
{"url": "https://a.test/1", "title": "t", "text": "x" * 900, "source": "acibadem"},
|
| 32 |
+
{"url": "https://a.test/2", "title": "t", "text": None, "source": "acibadem"},
|
| 33 |
+
{"url": "https://a.test/3", "title": "t", "text": "kısa", "source": "acibadem"},
|
| 34 |
+
{"url": "ftp://a.test/4", "title": "t", "text": "y" * 900, "source": "acibadem"},
|
| 35 |
+
]
|
| 36 |
+
)
|
| 37 |
+
out = clean_articles(df, min_chars=400)
|
| 38 |
+
assert out["url"].tolist() == ["https://a.test/1"]
|
| 39 |
+
|
| 40 |
+
def test_removes_duplicate_urls_and_duplicate_bodies(self):
|
| 41 |
+
body = "z" * 900
|
| 42 |
+
df = pd.DataFrame(
|
| 43 |
+
[
|
| 44 |
+
{"url": "https://a.test/1", "title": "t", "text": body, "source": "acibadem"},
|
| 45 |
+
{"url": "https://a.test/1", "title": "t", "text": body, "source": "acibadem"},
|
| 46 |
+
{"url": "https://a.test/2", "title": "t", "text": body, "source": "liv"},
|
| 47 |
+
]
|
| 48 |
+
)
|
| 49 |
+
assert len(clean_articles(df, min_chars=400)) == 1
|
| 50 |
+
|
| 51 |
+
def test_drops_boilerplate_pages(self):
|
| 52 |
+
df = pd.DataFrame(
|
| 53 |
+
[
|
| 54 |
+
{
|
| 55 |
+
"url": "https://a.test/cookie",
|
| 56 |
+
"title": "Çerez",
|
| 57 |
+
"text": "Çerez politikası hakkında bilgilendirme. " + ("metin " * 200),
|
| 58 |
+
"source": "acibadem",
|
| 59 |
+
},
|
| 60 |
+
{"url": "https://a.test/ok", "title": "t", "text": "q" * 900, "source": "acibadem"},
|
| 61 |
+
]
|
| 62 |
+
)
|
| 63 |
+
assert clean_articles(df, min_chars=400)["url"].tolist() == ["https://a.test/ok"]
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
class TestSelectArticles:
|
| 67 |
+
def test_returns_exactly_the_target_count(self):
|
| 68 |
+
df = clean_articles(make_frame({"acibadem": 500, "liv": 300, "atlas": 200}))
|
| 69 |
+
assert len(select_articles(df, target=300)) == 300
|
| 70 |
+
|
| 71 |
+
def test_balances_across_sources_instead_of_following_raw_proportions(self):
|
| 72 |
+
# Acıbadem outnumbers Atlas 10:1 in the raw data.
|
| 73 |
+
df = clean_articles(make_frame({"acibadem": 1000, "liv": 500, "atlas": 100}))
|
| 74 |
+
counts = select_articles(df, target=150).groupby("source").size().to_dict()
|
| 75 |
+
assert set(counts) == {"acibadem", "liv", "atlas"}
|
| 76 |
+
# Equal quota, not proportional: each source contributes ~50.
|
| 77 |
+
assert max(counts.values()) - min(counts.values()) <= 1
|
| 78 |
+
|
| 79 |
+
def test_redistributes_when_a_source_cannot_fill_its_quota(self):
|
| 80 |
+
df = clean_articles(make_frame({"acibadem": 500, "liv": 500, "atlas": 10}))
|
| 81 |
+
counts = select_articles(df, target=300).groupby("source").size().to_dict()
|
| 82 |
+
assert counts["atlas"] == 10
|
| 83 |
+
assert sum(counts.values()) == 300
|
| 84 |
+
|
| 85 |
+
def test_is_deterministic_for_a_fixed_seed(self):
|
| 86 |
+
df = clean_articles(make_frame({"acibadem": 300, "liv": 300}))
|
| 87 |
+
first = select_articles(df, target=100, seed=42)["url"].tolist()
|
| 88 |
+
second = select_articles(df, target=100, seed=42)["url"].tolist()
|
| 89 |
+
assert first == second
|
| 90 |
+
|
| 91 |
+
def test_caps_at_available_when_target_exceeds_corpus(self):
|
| 92 |
+
df = clean_articles(make_frame({"acibadem": 20, "liv": 20}))
|
| 93 |
+
assert len(select_articles(df, target=1000)) == 40
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
class TestParentId:
|
| 97 |
+
def test_is_stable_and_url_specific(self):
|
| 98 |
+
assert parent_id_for("https://a.test/1") == parent_id_for("https://a.test/1")
|
| 99 |
+
assert parent_id_for("https://a.test/1") != parent_id_for("https://a.test/2")
|
| 100 |
+
assert len(parent_id_for("https://a.test/1")) == 16
|