forge
Explore
Log in

Don't you dare go Hollow

hezarfen_rag

Clone
★ 0

0 watching0 forksPython2.4 MiB184b84dAhmet Kaan Gümüş2 months ago189 commits

test_pdf_classify.py94 lines, 4.0 KiBHistoryRawDownload
1"""Faz 0.3b birim testleri — blok sınıflandırma + sütun-farkında okuma sırası.
2Sentetik bloklarla (PDF gerekmez) → deterministik edge case'ler."""
3import unittest
4
5from src.ingest.pdf_parse import (Block, classify, order_blocks,
6 HEADING, PARAGRAPH, LIST, CAPTION,
7 HEADER, FOOTER, LABEL)
8
9H = 800.0 # sayfa yüksekliği
10BODY = 10.0 # gövde font medyanı
11
12
13def c(text, size, bbox):
14 return classify(text, size, bbox, H, BODY)
15
16
17class ClassifyTests(unittest.TestCase):
18 def test_heading_by_big_font(self):
19 self.assertEqual(c("1.2.2. Genetik Mühendisliği", 13, (57, 53, 309, 66)), HEADING)
20
21 def test_heading_by_section_marker_small_font(self):
22 # "A) ..." font gövdeyle eşit ama bölüm işareti → heading
23 self.assertEqual(c("A) Deoksiribonükleik Asit (DNA)", 11, (57, 54, 211, 68)), HEADING)
24
25 def test_paragraph(self):
26 t = "DNA; kalıtsal bilgiyi taşıyan, kalıtsal bilgiyi bir sonraki nesle aktaran moleküldür."
27 self.assertEqual(c(t, 10, (57, 131, 397, 250)), PARAGRAPH)
28
29 def test_list_bullets(self):
30 self.assertEqual(c("• Birinci madde\n• İkinci madde", 10, (57, 300, 300, 340)), LIST)
31
32 def test_list_numbered(self):
33 self.assertEqual(c("1. Adım\n2. Adım\n3. Adım", 10, (57, 300, 300, 350)), LIST)
34
35 def test_caption_gorsel(self):
36 self.assertEqual(c("Görsel 1.9: Fosfodiester bağı", 8.5, (405, 281, 501, 295)), CAPTION)
37
38 def test_caption_with_leading_marker(self):
39 # kitap "W Görsel..." öncü işaretini temizler
40 self.assertEqual(c("W Görsel 1.10: İkili sarmal", 8.5, (58, 679, 223, 692)), CAPTION)
41
42 def test_header_top(self):
43 self.assertEqual(c("1. BÖLÜM ● Nükleik Asitlerin Keşfi", 8, (334, 23, 508, 32)), HEADER)
44
45 def test_footer_pageno(self):
46 self.assertEqual(c("21", 10, (487, 747, 497, 760)), FOOTER)
47
48 def test_label_short_diagram_noise(self):
49 # diyagram etiketi: kısa + gövde-üstü değil
50 self.assertEqual(c("3,4 nm", 11, (98, 474, 111, 486)), LABEL)
51
52 def test_short_big_font_is_heading_not_label(self):
53 # kısa ama büyük font → başlık (label değil); sıralama doğru mu
54 self.assertEqual(c("Giriş", 15, (57, 60, 120, 78)), HEADING)
55
56
57class OrderTests(unittest.TestCase):
58 def _blk(self, x0, y0, x1, y1, kind=PARAGRAPH, text="x"):
59 return Block(page=1, bbox=(x0, y0, x1, y1), text=text, block_no=0,
60 font_size=10.0, kind=kind)
61
62 def test_single_column_y_order(self):
63 b = [self._blk(50, 300, 200, 320), self._blk(50, 100, 200, 120),
64 self._blk(50, 200, 200, 220)]
65 out = order_blocks(b, page_width=300)
66 ys = [x.bbox[1] for x in out]
67 self.assertEqual(ys, [100, 200, 300])
68
69 def test_two_column_left_before_right(self):
70 # sol: y=100,300 ; sağ: y=50,200 → sol tümü sağdan önce (y küçük olsa bile)
71 left = [self._blk(40, 100, 200, 120), self._blk(40, 300, 200, 320)]
72 right = [self._blk(300, 50, 460, 70), self._blk(300, 200, 460, 220)]
73 out = order_blocks(left + right, page_width=500)
74 centers = [(x.bbox[0] + x.bbox[2]) / 2 for x in out]
75 # ilk iki blok sol (<250), son iki blok sağ (>250)
76 self.assertTrue(all(cx < 250 for cx in centers[:2]))
77 self.assertTrue(all(cx > 250 for cx in centers[2:]))
78 # sol kendi içinde y sıralı
79 self.assertEqual([x.bbox[1] for x in out[:2]], [100, 300])
80
81 def test_header_first_footer_last(self):
82 b = [self._blk(50, 400, 200, 420, kind=FOOTER, text="12"),
83 self._blk(50, 200, 200, 220, kind=PARAGRAPH),
84 self._blk(50, 20, 200, 32, kind=HEADER, text="BÖLÜM")]
85 out = order_blocks(b, page_width=300)
86 self.assertEqual(out[0].kind, HEADER)
87 self.assertEqual(out[-1].kind, FOOTER)
88
89 def test_empty_no_crash(self):
90 self.assertEqual(order_blocks([], page_width=300), [])
91
92
93if __name__ == "__main__":
94 unittest.main()