VietCat commited on
Commit
bd809bf
·
1 Parent(s): 81cf5ad

add data viewer

Browse files
Files changed (1) hide show
  1. app/main.py +22 -60
app/main.py CHANGED
@@ -760,62 +760,24 @@ async def view_all_document_chunks():
760
  def build_chunk_tree(chunks: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
761
  """
762
  Xây dựng cấu trúc cây từ danh sách chunks phẳng.
763
- Đảm bảo thứ tự phân cấp đúng: PHAN -> PHU_LUC -> CHUONG -> MUC -> DIEU -> KHOAN -> DIEM
764
  """
765
  if not chunks:
766
  return []
767
 
768
- # Sắp xếp chunks theo thứ tự xuất hiện (dựa vào content để đoán thứ tự)
769
- # Chunks có level cao hơn thường xuất hiện trước
770
- level_priority = {
771
- "PHAN": 1,
772
- "PHU_LUC": 1,
773
- "CHUONG": 2,
774
- "MUC": 3,
775
- "DIEU": 4,
776
- "KHOAN": 5,
777
- "DIEM": 6,
778
- "CONTENT": 7
779
- }
780
-
781
- def get_chunk_level(chunk):
782
- """Đoán level của chunk dựa vào content"""
783
- content = chunk.get('content', '').upper()
784
- if any(keyword in content for keyword in ['PHẦN', 'PHẦN THỨ']):
785
- return 1
786
- elif 'PHỤ LỤC' in content:
787
- return 1
788
- elif 'CHƯƠNG' in content:
789
- return 2
790
- elif 'MỤC' in content:
791
- return 3
792
- elif 'ĐIỀU' in content:
793
- return 4
794
- elif any(keyword in content for keyword in ['KHOẢN', 'ĐIỂM']):
795
- return 5
796
- else:
797
- return 7
798
-
799
- # Sắp xếp chunks theo level (thấp hơn = cao hơn trong phân cấp)
800
- sorted_chunks = sorted(chunks, key=lambda x: get_chunk_level(x))
801
-
802
  # Tạo dictionary để truy cập nhanh
803
- chunks_dict = {chunk['id']: chunk for chunk in sorted_chunks}
804
 
805
- # Tạo cấu trúc cây
806
- root_chunks = []
807
- processed_nodes = {} # Để theo dõi các node đã được xử lý
808
-
809
- for chunk in sorted_chunks:
810
- chunk_id = chunk['id']
811
- parent_id = chunk.get('cha')
812
 
813
- # Tạo node mới với cấu trúc cây
814
- tree_node = {
815
  "id": chunk_id,
816
  "content": chunk.get('content', ''),
817
  "vanbanid": chunk.get('vanbanid'),
818
- "cha": parent_id,
819
  "document_title": chunk.get('document_title', ''),
820
  "article_number": chunk.get('article_number'),
821
  "article_title": chunk.get('article_title', ''),
@@ -826,22 +788,22 @@ def build_chunk_tree(chunks: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
826
  "children": []
827
  }
828
 
829
- # Lưu node vào processed_nodes
830
- processed_nodes[chunk_id] = tree_node
 
 
 
831
 
832
- if parent_id is None:
833
- # Đây là root node
834
- root_chunks.append(tree_node)
835
- else:
836
- # Tìm parent và thêm vào children
837
- parent_node = processed_nodes.get(parent_id)
838
- if parent_node:
839
- # Parent đã được xử lý trước đó
840
- parent_node["children"].append(tree_node)
841
- else:
842
- # Parent chưa được xử lý, coi như root
843
- root_chunks.append(tree_node)
844
 
 
845
  return root_chunks
846
 
847
  @app.get("/api/document-chunks/status")
 
760
  def build_chunk_tree(chunks: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
761
  """
762
  Xây dựng cấu trúc cây từ danh sách chunks phẳng.
763
+ Cách đơn giản: tìm root nodes (cha=None) trước, sau đó tìm children.
764
  """
765
  if not chunks:
766
  return []
767
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
768
  # Tạo dictionary để truy cập nhanh
769
+ chunks_dict = {chunk['id']: chunk for chunk in chunks}
770
 
771
+ def build_node(chunk_id: str) -> Dict[str, Any]:
772
+ """Tạo node và tìm tất cả children của nó."""
773
+ chunk = chunks_dict[chunk_id]
 
 
 
 
774
 
775
+ # Tạo node
776
+ node = {
777
  "id": chunk_id,
778
  "content": chunk.get('content', ''),
779
  "vanbanid": chunk.get('vanbanid'),
780
+ "cha": chunk.get('cha'),
781
  "document_title": chunk.get('document_title', ''),
782
  "article_number": chunk.get('article_number'),
783
  "article_title": chunk.get('article_title', ''),
 
788
  "children": []
789
  }
790
 
791
+ # Tìm tất cả children của node này
792
+ for other_chunk in chunks:
793
+ if other_chunk.get('cha') == chunk_id:
794
+ child_node = build_node(other_chunk['id'])
795
+ node["children"].append(child_node)
796
 
797
+ return node
798
+
799
+ # Tìm tất cả root nodes (cha=None)
800
+ root_chunks = []
801
+ for chunk in chunks:
802
+ if chunk.get('cha') is None:
803
+ root_node = build_node(chunk['id'])
804
+ root_chunks.append(root_node)
 
 
 
 
805
 
806
+ logger.info(f"[TREE] Built tree with {len(root_chunks)} root nodes from {len(chunks)} total chunks")
807
  return root_chunks
808
 
809
  @app.get("/api/document-chunks/status")