init project
Browse files- rag_core/chunker.py +15 -2
rag_core/chunker.py
CHANGED
|
@@ -1,9 +1,22 @@
|
|
| 1 |
import re
|
| 2 |
from typing import List
|
| 3 |
from rag_core.utils import log_timed
|
|
|
|
| 4 |
|
| 5 |
@log_timed("chunking văn bản luật")
|
| 6 |
def chunk_legal_text(text: str) -> List[str]:
|
| 7 |
-
|
|
|
|
| 8 |
matches = re.findall(pattern, text, flags=re.DOTALL)
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
from typing import List
|
| 3 |
from rag_core.utils import log_timed
|
| 4 |
+
import logging
|
| 5 |
|
| 6 |
@log_timed("chunking văn bản luật")
|
| 7 |
def chunk_legal_text(text: str) -> List[str]:
|
| 8 |
+
# Chunk theo "Chương" và "Điều"
|
| 9 |
+
pattern = r"(Chương\s+[IVXLC]+\s+.*?|Điều\s+\d+\..*?)(?=(Chương\s+[IVXLC]+\s+|Điều\s+\d+\.|$))"
|
| 10 |
matches = re.findall(pattern, text, flags=re.DOTALL)
|
| 11 |
+
|
| 12 |
+
chunks = [m[0].strip() for m in matches if len(m[0].strip()) > 30]
|
| 13 |
+
|
| 14 |
+
if not chunks:
|
| 15 |
+
logging.warning("Không tìm thấy chunk theo Chương/Điều. Đang fallback sang chia theo đoạn văn.")
|
| 16 |
+
chunks = [p.strip() for p in text.split("\n\n") if len(p.strip()) > 100]
|
| 17 |
+
|
| 18 |
+
logging.info(f"Tổng số chunk sau khi xử lý: {len(chunks)}")
|
| 19 |
+
for i, c in enumerate(chunks[:2]):
|
| 20 |
+
logging.info(f"Mẫu chunk {i+1}:\n{c[:300]}...\n")
|
| 21 |
+
|
| 22 |
+
return chunks
|