import pytest from grounding_eval.rouge import rouge_all, rouge_l, rouge_n def test_rouge1_counts_clipped_overlap(): # 후보 a a b c / 정답 a b b d → 공통 a(min 2,1)=1, b(min 1,2)=1 → 2 score = rouge_n(["a", "a", "b", "c"], ["a", "b", "b", "d"], 1) assert score.precision == pytest.approx(2 / 4) assert score.recall == pytest.approx(2 / 4) assert score.f1 == pytest.approx(0.5) def test_rouge2_uses_bigrams(): # 후보 바이그램 (a,b)(b,c) / 정답 (a,b)(b,d)(d,c) → 공통 1 score = rouge_n(["a", "b", "c"], ["a", "b", "d", "c"], 2) assert score.precision == pytest.approx(1 / 2) assert score.recall == pytest.approx(1 / 3) assert score.f1 == pytest.approx(2 * (1 / 2) * (1 / 3) / (1 / 2 + 1 / 3)) def test_rouge_l_uses_longest_common_subsequence(): # LCS(a b c d e, a c e x) = a c e → 3 score = rouge_l(["a", "b", "c", "d", "e"], ["a", "c", "e", "x"]) assert score.precision == pytest.approx(3 / 5) assert score.recall == pytest.approx(3 / 4) def test_identical_sequences_score_one(): tokens = ["연간", "400", "개", "거래"] assert all(s.f1 == pytest.approx(1.0) for s in rouge_all(tokens, tokens).values()) @pytest.mark.parametrize("cand, ref", [([], ["a"]), (["a"], []), ([], []), (["a"], ["b"])]) def test_empty_or_disjoint_scores_zero(cand, ref): assert all(s.f1 == 0.0 for s in rouge_all(cand, ref).values()) def test_bigram_on_single_token_is_zero(): assert rouge_n(["a"], ["a"], 2).f1 == 0.0