update content_summary in chunking
Browse files- app/law_document_chunker.py +27 -25
app/law_document_chunker.py
CHANGED
|
@@ -133,7 +133,7 @@ class LawDocumentChunker:
|
|
| 133 |
|
| 134 |
def _create_chunk_metadata(self, content: str, level: str, level_value: Optional[str],
|
| 135 |
parent_id: Optional[str], vanbanid: int,
|
| 136 |
-
document_title: str, chunk_stack: List[Tuple[str, str, Optional[str]]] = []) -> ChunkMetadata:
|
| 137 |
"""Tạo metadata cho chunk."""
|
| 138 |
chunk_id = str(uuid.uuid4())
|
| 139 |
|
|
@@ -160,36 +160,38 @@ class LawDocumentChunker:
|
|
| 160 |
|
| 161 |
return metadata
|
| 162 |
|
| 163 |
-
def _fill_metadata_from_parents(self, metadata: ChunkMetadata, chunk_stack: List[Tuple[str, str, Optional[str]]], parent_id: str):
|
| 164 |
"""
|
| 165 |
Điền metadata từ parent chunks (Điều, Khoản) nếu chunk hiện tại có cha hoặc ông là Điều/Khoản.
|
| 166 |
"""
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
| 169 |
current_parent = parent_id
|
| 170 |
|
| 171 |
-
# Tìm tất cả
|
| 172 |
-
for chunk_id, level, level_value in reversed(chunk_stack):
|
| 173 |
if chunk_id == current_parent:
|
| 174 |
-
|
| 175 |
-
# Tìm parent của
|
| 176 |
-
for
|
| 177 |
-
if
|
| 178 |
-
current_parent =
|
| 179 |
break
|
|
|
|
|
|
|
| 180 |
|
| 181 |
-
# Điền metadata từ
|
| 182 |
-
for level, level_value in
|
| 183 |
if level == "DIEU" and level_value:
|
| 184 |
if not metadata.article_number: # Chỉ điền nếu chưa có
|
| 185 |
metadata.article_number = int(level_value) if level_value.isdigit() else None
|
| 186 |
if not metadata.article_title: # Chỉ điền nếu chưa có
|
| 187 |
-
# Lấy
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
# Tìm content của parent chunk này
|
| 191 |
-
# (Cần truyền content của parent vào đây)
|
| 192 |
-
break
|
| 193 |
elif level == "KHOAN" and level_value:
|
| 194 |
if not metadata.clause_number: # Chỉ điền nếu chưa có
|
| 195 |
metadata.clause_number = level_value
|
|
@@ -286,7 +288,7 @@ class LawDocumentChunker:
|
|
| 286 |
chunks = []
|
| 287 |
|
| 288 |
# Stack để theo dõi các chunks theo thứ tự xuất hiện
|
| 289 |
-
# Mỗi item là (chunk_id, level, level_value)
|
| 290 |
chunk_stack = []
|
| 291 |
|
| 292 |
current_chunk_content = ""
|
|
@@ -329,7 +331,7 @@ class LawDocumentChunker:
|
|
| 329 |
chunks.append(metadata)
|
| 330 |
|
| 331 |
# Thêm vào stack NGAY LẬP TỨC
|
| 332 |
-
chunk_stack.append((metadata.id, current_level, current_level_value))
|
| 333 |
logger.debug(f"[CHUNKER] Created chunk: {metadata.id[:8]}... Level: {current_level}, Parent: {current_parent}")
|
| 334 |
|
| 335 |
# Tìm parent cho level mới TRƯỚC KHI tạo chunk mới
|
|
@@ -362,7 +364,7 @@ class LawDocumentChunker:
|
|
| 362 |
chunks.append(metadata)
|
| 363 |
|
| 364 |
# Thêm vào stack NGAY LẬP TỨC
|
| 365 |
-
chunk_stack.append((metadata.id, current_level, current_level_value))
|
| 366 |
logger.debug(f"[CHUNKER] Created sub-chunk: {metadata.id[:8]}... Level: {current_level}, Parent: {current_parent}")
|
| 367 |
|
| 368 |
current_chunk_content = ""
|
|
@@ -381,7 +383,7 @@ class LawDocumentChunker:
|
|
| 381 |
chunks.append(metadata)
|
| 382 |
|
| 383 |
# Thêm vào stack NGAY LẬP TỨC
|
| 384 |
-
chunk_stack.append((metadata.id, current_level, current_level_value))
|
| 385 |
logger.debug(f"[CHUNKER] Created final chunk: {metadata.id[:8]}... Level: {current_level}, Parent: {current_parent}")
|
| 386 |
|
| 387 |
# Debug: Kiểm tra kết quả
|
|
@@ -397,7 +399,7 @@ class LawDocumentChunker:
|
|
| 397 |
|
| 398 |
return chunks
|
| 399 |
|
| 400 |
-
def _find_parent_for_level(self, chunk_stack: List[Tuple[str, str, Optional[str]]],
|
| 401 |
current_level: str, level_priority: Dict[str, int]) -> Optional[str]:
|
| 402 |
"""
|
| 403 |
Tìm parent gần nhất có level cao hơn (priority thấp hơn) cho level hiện tại.
|
|
@@ -405,7 +407,7 @@ class LawDocumentChunker:
|
|
| 405 |
current_priority = level_priority.get(current_level, 999)
|
| 406 |
|
| 407 |
# Tìm từ cuối stack (gần nhất) đến đầu stack
|
| 408 |
-
for chunk_id, level, level_value in reversed(chunk_stack):
|
| 409 |
if level_priority.get(level, 999) < current_priority:
|
| 410 |
return chunk_id
|
| 411 |
|
|
|
|
| 133 |
|
| 134 |
def _create_chunk_metadata(self, content: str, level: str, level_value: Optional[str],
|
| 135 |
parent_id: Optional[str], vanbanid: int,
|
| 136 |
+
document_title: str, chunk_stack: List[Tuple[str, str, Optional[str], str]] = []) -> ChunkMetadata:
|
| 137 |
"""Tạo metadata cho chunk."""
|
| 138 |
chunk_id = str(uuid.uuid4())
|
| 139 |
|
|
|
|
| 160 |
|
| 161 |
return metadata
|
| 162 |
|
| 163 |
+
def _fill_metadata_from_parents(self, metadata: ChunkMetadata, chunk_stack: List[Tuple[str, str, Optional[str], str]], parent_id: str):
|
| 164 |
"""
|
| 165 |
Điền metadata từ parent chunks (Điều, Khoản) nếu chunk hiện tại có cha hoặc ông là Điều/Khoản.
|
| 166 |
"""
|
| 167 |
+
if not parent_id:
|
| 168 |
+
return
|
| 169 |
+
|
| 170 |
+
# Tìm tất cả ancestors trong stack
|
| 171 |
+
ancestors = []
|
| 172 |
current_parent = parent_id
|
| 173 |
|
| 174 |
+
# Tìm tất cả ancestors từ parent_id trở lên
|
| 175 |
+
for chunk_id, level, level_value, content in reversed(chunk_stack):
|
| 176 |
if chunk_id == current_parent:
|
| 177 |
+
ancestors.append((level, level_value, content))
|
| 178 |
+
# Tìm parent của chunk này
|
| 179 |
+
for ancestor_id, ancestor_level, ancestor_level_value, ancestor_content in reversed(chunk_stack):
|
| 180 |
+
if ancestor_id == chunk_id:
|
| 181 |
+
current_parent = ancestor_id
|
| 182 |
break
|
| 183 |
+
if current_parent == chunk_id: # Không tìm thấy parent
|
| 184 |
+
break
|
| 185 |
|
| 186 |
+
# Điền metadata từ ancestors (từ gần nhất đến xa nhất)
|
| 187 |
+
for level, level_value, content in ancestors:
|
| 188 |
if level == "DIEU" and level_value:
|
| 189 |
if not metadata.article_number: # Chỉ điền nếu chưa có
|
| 190 |
metadata.article_number = int(level_value) if level_value.isdigit() else None
|
| 191 |
if not metadata.article_title: # Chỉ điền nếu chưa có
|
| 192 |
+
# Lấy dòng đầu tiên làm title
|
| 193 |
+
first_line = content.split('\n')[0].strip() if content else ""
|
| 194 |
+
metadata.article_title = first_line
|
|
|
|
|
|
|
|
|
|
| 195 |
elif level == "KHOAN" and level_value:
|
| 196 |
if not metadata.clause_number: # Chỉ điền nếu chưa có
|
| 197 |
metadata.clause_number = level_value
|
|
|
|
| 288 |
chunks = []
|
| 289 |
|
| 290 |
# Stack để theo dõi các chunks theo thứ tự xuất hiện
|
| 291 |
+
# Mỗi item là (chunk_id, level, level_value, content)
|
| 292 |
chunk_stack = []
|
| 293 |
|
| 294 |
current_chunk_content = ""
|
|
|
|
| 331 |
chunks.append(metadata)
|
| 332 |
|
| 333 |
# Thêm vào stack NGAY LẬP TỨC
|
| 334 |
+
chunk_stack.append((metadata.id, current_level, current_level_value, current_chunk_content.strip()))
|
| 335 |
logger.debug(f"[CHUNKER] Created chunk: {metadata.id[:8]}... Level: {current_level}, Parent: {current_parent}")
|
| 336 |
|
| 337 |
# Tìm parent cho level mới TRƯỚC KHI tạo chunk mới
|
|
|
|
| 364 |
chunks.append(metadata)
|
| 365 |
|
| 366 |
# Thêm vào stack NGAY LẬP TỨC
|
| 367 |
+
chunk_stack.append((metadata.id, current_level, current_level_value, sub_chunk.strip()))
|
| 368 |
logger.debug(f"[CHUNKER] Created sub-chunk: {metadata.id[:8]}... Level: {current_level}, Parent: {current_parent}")
|
| 369 |
|
| 370 |
current_chunk_content = ""
|
|
|
|
| 383 |
chunks.append(metadata)
|
| 384 |
|
| 385 |
# Thêm vào stack NGAY LẬP TỨC
|
| 386 |
+
chunk_stack.append((metadata.id, current_level, current_level_value, current_chunk_content.strip()))
|
| 387 |
logger.debug(f"[CHUNKER] Created final chunk: {metadata.id[:8]}... Level: {current_level}, Parent: {current_parent}")
|
| 388 |
|
| 389 |
# Debug: Kiểm tra kết quả
|
|
|
|
| 399 |
|
| 400 |
return chunks
|
| 401 |
|
| 402 |
+
def _find_parent_for_level(self, chunk_stack: List[Tuple[str, str, Optional[str], str]],
|
| 403 |
current_level: str, level_priority: Dict[str, int]) -> Optional[str]:
|
| 404 |
"""
|
| 405 |
Tìm parent gần nhất có level cao hơn (priority thấp hơn) cho level hiện tại.
|
|
|
|
| 407 |
current_priority = level_priority.get(current_level, 999)
|
| 408 |
|
| 409 |
# Tìm từ cuối stack (gần nhất) đến đầu stack
|
| 410 |
+
for chunk_id, level, level_value, content in reversed(chunk_stack):
|
| 411 |
if level_priority.get(level, 999) < current_priority:
|
| 412 |
return chunk_id
|
| 413 |
|