Spaces:
Running on Zero
Running on Zero
File size: 1,862 Bytes
3d9ba5b | 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 | import unittest
from evaluation.rouge import RougeScorer
from evaluation.bleu import BleuScorer
from evaluation.metrics_manager import MetricsManager
class TestEvaluation(unittest.TestCase):
def setUp(self):
self.rouge = RougeScorer()
self.bleu = BleuScorer()
self.manager = MetricsManager()
def test_rouge_calculation(self):
candidate = "natural language processing enables automatic text summarization"
reference = "natural language processing creates automatic text summaries"
scores = self.rouge.evaluate(candidate, reference)
self.assertIn("rouge-1", scores)
self.assertIn("rouge-2", scores)
self.assertIn("rouge-l", scores)
# ROUGE-1 should have positive precision, recall, and f1
self.assertGreater(scores["rouge-1"]["f1"], 0.4)
self.assertGreater(scores["rouge-l"]["f1"], 0.4)
def test_bleu_calculation(self):
candidate = "the cat is sitting on the mat"
reference = "the cat sits on the mat"
scores = self.bleu.evaluate(candidate, reference)
self.assertIn("bleu-1", scores)
self.assertIn("bleu_cumulative", scores)
self.assertGreater(scores["bleu-1"], 0.5)
def test_metrics_manager(self):
orig = "Natural language processing is an essential field. It enables machines to comprehend documents and produce concise summaries."
summary = "NLP enables machines to produce summaries."
ref = "NLP helps computers produce summaries."
results = self.manager.evaluate_summary(orig, summary, reference_summary=ref, lang="en")
self.assertIn("compression_ratio", results)
self.assertIn("rouge", results)
self.assertIn("bleu", results)
self.assertLess(results["compression_ratio"], 1.0)
if __name__ == "__main__":
unittest.main()
|