VietCat commited on
Commit
9d82430
·
1 Parent(s): 122bfc9

update content_summary in chunking

Browse files
Files changed (1) hide show
  1. app/law_document_chunker.py +49 -12
app/law_document_chunker.py CHANGED
@@ -167,37 +167,74 @@ class LawDocumentChunker:
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
 
198
  elif level == "DIEM" and level_value:
199
  if not metadata.sub_clause_letter: # Chỉ điền nếu chưa có
200
  metadata.sub_clause_letter = level_value
 
201
 
202
  async def _create_context_summary_with_llm(self, content: str, metadata: ChunkMetadata) -> str:
203
  """
 
167
  if not parent_id:
168
  return
169
 
170
+ # Debug logging
171
+ logger.debug(f"[CHUNKER] Filling metadata for chunk with parent_id: {parent_id}")
172
+ logger.debug(f"[CHUNKER] Chunk stack has {len(chunk_stack)} items")
173
+
174
+ # Tìm tất cả chunks có level cao hơn (Điều, Khoản) trong stack
175
+ # và xuất hiện trước chunk hiện tại
176
  ancestors = []
 
177
 
178
+ # Tìm index của parent chunk
179
+ parent_index = -1
180
+ for i, (chunk_id, _, _, _) in enumerate(chunk_stack):
181
+ if chunk_id == parent_id:
182
+ parent_index = i
183
+ break
184
+
185
+ if parent_index == -1:
186
+ logger.warning(f"[CHUNKER] Parent chunk {parent_id} not found in stack")
187
+ return
188
+
189
+ # Tìm tất cả chunks có level cao hơn từ index trước đó
190
+ level_priority = {
191
+ "PHAN": 1, "PHU_LUC": 1, "CHUONG": 2, "MUC": 3,
192
+ "DIEU": 4, "KHOAN": 5, "DIEM": 6, "CONTENT": 7
193
+ }
194
+
195
+ # Tìm parent level của chunk hiện tại
196
+ current_level = None
197
+ for chunk_id, level, _, _ in chunk_stack:
198
+ if chunk_id == parent_id:
199
+ current_level = level
200
+ break
201
+
202
+ if not current_level:
203
+ return
204
+
205
+ current_priority = level_priority.get(current_level, 999)
206
+
207
+ # Tìm tất cả ancestors từ index trước đó
208
+ for i in range(parent_index - 1, -1, -1):
209
+ chunk_id, level, level_value, content = chunk_stack[i]
210
+ ancestor_priority = level_priority.get(level, 999)
211
+
212
+ # Nếu level này cao hơn (priority thấp hơn) và là Điều/Khoản
213
+ if ancestor_priority < current_priority and level in ["DIEU", "KHOAN"]:
214
  ancestors.append((level, level_value, content))
215
+ logger.debug(f"[CHUNKER] Found ancestor: {level} {level_value}")
216
+
217
+ logger.debug(f"[CHUNKER] Found {len(ancestors)} ancestors: {[(level, value) for level, value, content in ancestors]}")
 
 
 
 
218
 
219
  # Điền metadata từ ancestors (từ gần nhất đến xa nhất)
220
  for level, level_value, content in ancestors:
221
  if level == "DIEU" and level_value:
222
  if not metadata.article_number: # Chỉ điền nếu chưa có
223
  metadata.article_number = int(level_value) if level_value.isdigit() else None
224
+ logger.debug(f"[CHUNKER] Set article_number: {metadata.article_number}")
225
  if not metadata.article_title: # Chỉ điền nếu chưa có
226
  # Lấy dòng đầu tiên làm title
227
  first_line = content.split('\n')[0].strip() if content else ""
228
  metadata.article_title = first_line
229
+ logger.debug(f"[CHUNKER] Set article_title: {metadata.article_title}")
230
  elif level == "KHOAN" and level_value:
231
  if not metadata.clause_number: # Chỉ điền nếu chưa có
232
  metadata.clause_number = level_value
233
+ logger.debug(f"[CHUNKER] Set clause_number: {metadata.clause_number}")
234
  elif level == "DIEM" and level_value:
235
  if not metadata.sub_clause_letter: # Chỉ điền nếu chưa có
236
  metadata.sub_clause_letter = level_value
237
+ logger.debug(f"[CHUNKER] Set sub_clause_letter: {metadata.sub_clause_letter}")
238
 
239
  async def _create_context_summary_with_llm(self, content: str, metadata: ChunkMetadata) -> str:
240
  """