Spaces:
				
			
			
	
			
			
		Paused
		
	
	
	
			
			
	
	
	
	
		
		
		Paused
		
	Update data_processor.py
Browse files- data_processor.py +57 -24
    	
        data_processor.py
    CHANGED
    
    | @@ -1,25 +1,20 @@ | |
| 1 | 
             
            # file: data_processor.py
         | 
| 2 | 
             
            import json
         | 
|  | |
| 3 |  | 
| 4 | 
             
            def process_law_data_to_chunks(structured_data_input):
         | 
| 5 | 
            -
                """
         | 
| 6 | 
            -
                Xử lý dữ liệu luật từ cấu trúc JSON lồng nhau thành một danh sách phẳng các chunks.
         | 
| 7 | 
            -
                Mỗi chunk chứa text và metadata tương ứng.
         | 
| 8 | 
            -
                """
         | 
| 9 | 
             
                flat_list = []
         | 
| 10 |  | 
| 11 | 
            -
                # Đảm bảo đầu vào là một danh sách các điều luật (articles)
         | 
| 12 | 
             
                if isinstance(structured_data_input, dict) and "article" in structured_data_input:
         | 
| 13 | 
             
                    articles_list = [structured_data_input]
         | 
| 14 | 
             
                elif isinstance(structured_data_input, list):
         | 
| 15 | 
             
                    articles_list = structured_data_input
         | 
| 16 | 
             
                else:
         | 
| 17 | 
            -
                    print("Lỗi: Dữ liệu đầu vào không  | 
| 18 | 
             
                    return flat_list
         | 
| 19 |  | 
| 20 | 
             
                for article_data in articles_list:
         | 
| 21 | 
             
                    if not isinstance(article_data, dict):
         | 
| 22 | 
            -
                        print(f"Cảnh báo: Bỏ qua một mục trong danh sách điều luật vì không phải là dictionary: {article_data}")
         | 
| 23 | 
             
                        continue
         | 
| 24 |  | 
| 25 | 
             
                    article_metadata_base = {
         | 
| @@ -30,12 +25,10 @@ def process_law_data_to_chunks(structured_data_input): | |
| 30 |  | 
| 31 | 
             
                    clauses = article_data.get("clauses", [])
         | 
| 32 | 
             
                    if not isinstance(clauses, list):
         | 
| 33 | 
            -
                        print(f"Cảnh báo: 'clauses' trong điều {article_metadata_base.get('article')} không phải là danh sách. Bỏ qua.")
         | 
| 34 | 
             
                        continue
         | 
| 35 |  | 
| 36 | 
             
                    for clause_data in clauses:
         | 
| 37 | 
             
                        if not isinstance(clause_data, dict):
         | 
| 38 | 
            -
                            print(f"Cảnh báo: Bỏ qua một mục trong 'clauses' vì không phải là dictionary: {clause_data}")
         | 
| 39 | 
             
                            continue
         | 
| 40 |  | 
| 41 | 
             
                        clause_metadata_base = article_metadata_base.copy()
         | 
| @@ -46,41 +39,81 @@ def process_law_data_to_chunks(structured_data_input): | |
| 46 |  | 
| 47 | 
             
                        points_in_clause = clause_data.get("points_in_clause", [])
         | 
| 48 | 
             
                        if not isinstance(points_in_clause, list):
         | 
| 49 | 
            -
                            print(f"Cảnh báo: 'points_in_clause' trong khoản {clause_metadata_base.get('clause_number')} của điều {article_metadata_base.get('article')} không phải là danh sách. Bỏ qua.")
         | 
| 50 | 
             
                            continue
         | 
| 51 | 
            -
             | 
| 52 | 
             
                        if points_in_clause:
         | 
| 53 | 
             
                            for point_data in points_in_clause:
         | 
| 54 | 
             
                                if not isinstance(point_data, dict):
         | 
| 55 | 
            -
                                    print(f"Cảnh báo: Bỏ qua một mục trong 'points_in_clause' vì không phải là dictionary: {point_data}")
         | 
| 56 | 
             
                                    continue
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 57 |  | 
| 58 | 
            -
                                 | 
| 59 | 
            -
                                if not chunk_text:
         | 
| 60 | 
             
                                    continue
         | 
| 61 |  | 
| 62 | 
             
                                current_point_metadata = clause_metadata_base.copy()
         | 
|  | |
|  | |
| 63 | 
             
                                point_specific_metadata = point_data.copy()
         | 
|  | |
|  | |
| 64 | 
             
                                if "point_text_original" in point_specific_metadata:
         | 
| 65 | 
             
                                    del point_specific_metadata["point_text_original"]
         | 
| 66 |  | 
| 67 | 
             
                                current_point_metadata.update(point_specific_metadata)
         | 
|  | |
| 68 | 
             
                                final_metadata_cleaned = {k: v for k, v in current_point_metadata.items() if v is not None}
         | 
| 69 |  | 
| 70 | 
            -
                                flat_list.append({ | 
|  | |
|  | |
|  | |
|  | |
| 71 | 
             
                        else:
         | 
|  | |
| 72 | 
             
                            chunk_text = clause_data.get("clause_text_original")
         | 
| 73 | 
             
                            if chunk_text:
         | 
| 74 | 
             
                                current_clause_metadata = clause_metadata_base.copy()
         | 
| 75 | 
            -
                                additional_clause_info = {}
         | 
| 76 | 
            -
                                 | 
| 77 | 
            -
                                    if key not in ["clause_text_original", "points_in_clause", "clause_number", "clause_metadata_summary"]:
         | 
| 78 | 
            -
                                        additional_clause_info[key] = value
         | 
| 79 | 
            -
                                
         | 
| 80 | 
            -
                                if additional_clause_info:
         | 
| 81 | 
            -
                                    current_clause_metadata.update(additional_clause_info)
         | 
| 82 | 
            -
                                
         | 
| 83 | 
             
                                final_metadata_cleaned = {k: v for k, v in current_clause_metadata.items() if v is not None}
         | 
| 84 | 
            -
                                 | 
|  | |
|  | |
|  | |
| 85 |  | 
|  | |
|  | |
|  | |
|  | |
| 86 | 
             
                return flat_list
         | 
|  | |
| 1 | 
             
            # file: data_processor.py
         | 
| 2 | 
             
            import json
         | 
| 3 | 
            +
            import re
         | 
| 4 |  | 
| 5 | 
             
            def process_law_data_to_chunks(structured_data_input):
         | 
|  | |
|  | |
|  | |
|  | |
| 6 | 
             
                flat_list = []
         | 
| 7 |  | 
|  | |
| 8 | 
             
                if isinstance(structured_data_input, dict) and "article" in structured_data_input:
         | 
| 9 | 
             
                    articles_list = [structured_data_input]
         | 
| 10 | 
             
                elif isinstance(structured_data_input, list):
         | 
| 11 | 
             
                    articles_list = structured_data_input
         | 
| 12 | 
             
                else:
         | 
| 13 | 
            +
                    print("Lỗi: Dữ liệu đầu vào không hợp lệ.")
         | 
| 14 | 
             
                    return flat_list
         | 
| 15 |  | 
| 16 | 
             
                for article_data in articles_list:
         | 
| 17 | 
             
                    if not isinstance(article_data, dict):
         | 
|  | |
| 18 | 
             
                        continue
         | 
| 19 |  | 
| 20 | 
             
                    article_metadata_base = {
         | 
|  | |
| 25 |  | 
| 26 | 
             
                    clauses = article_data.get("clauses", [])
         | 
| 27 | 
             
                    if not isinstance(clauses, list):
         | 
|  | |
| 28 | 
             
                        continue
         | 
| 29 |  | 
| 30 | 
             
                    for clause_data in clauses:
         | 
| 31 | 
             
                        if not isinstance(clause_data, dict):
         | 
|  | |
| 32 | 
             
                            continue
         | 
| 33 |  | 
| 34 | 
             
                        clause_metadata_base = article_metadata_base.copy()
         | 
|  | |
| 39 |  | 
| 40 | 
             
                        points_in_clause = clause_data.get("points_in_clause", [])
         | 
| 41 | 
             
                        if not isinstance(points_in_clause, list):
         | 
|  | |
| 42 | 
             
                            continue
         | 
| 43 | 
            +
                        
         | 
| 44 | 
             
                        if points_in_clause:
         | 
| 45 | 
             
                            for point_data in points_in_clause:
         | 
| 46 | 
             
                                if not isinstance(point_data, dict):
         | 
|  | |
| 47 | 
             
                                    continue
         | 
| 48 | 
            +
                                
         | 
| 49 | 
            +
                                # <<< THAY ĐỔI BẮT ĐẦU: LÀM GIÀU VĂN BẢN >>>
         | 
| 50 | 
            +
                                
         | 
| 51 | 
            +
                                # 1. Thu thập các thành phần văn bản để làm giàu
         | 
| 52 | 
            +
                                article_title = article_metadata_base.get('article_title', '')
         | 
| 53 | 
            +
                                point_text = point_data.get("point_text_original") or point_data.get("violation_description_summary")
         | 
| 54 | 
            +
                                
         | 
| 55 | 
            +
                                # Lấy thông tin tóm tắt của Khoản (thường là mức phạt chung)
         | 
| 56 | 
            +
                                clause_summary_dict = clause_data.get("clause_metadata_summary", {})
         | 
| 57 | 
            +
                                clause_summary_text = ""
         | 
| 58 | 
            +
                                if clause_summary_dict:
         | 
| 59 | 
            +
                                    # Lấy giá trị từ các key có thể có
         | 
| 60 | 
            +
                                    summary_keys = ["overall_fine_note_for_clause", "overall_points_deduction_note_for_clause"]
         | 
| 61 | 
            +
                                    for key in summary_keys:
         | 
| 62 | 
            +
                                        if key in clause_summary_dict:
         | 
| 63 | 
            +
                                            clause_summary_text = clause_summary_dict[key]
         | 
| 64 | 
            +
                                            break
         | 
| 65 | 
            +
                                
         | 
| 66 | 
            +
                                # Nếu không có tóm tắt ở Khoản, thử lấy trực tiếp từ text gốc của Khoản
         | 
| 67 | 
            +
                                if not clause_summary_text:
         | 
| 68 | 
            +
                                    clause_original_text = clause_data.get("clause_text_original", "")
         | 
| 69 | 
            +
                                    # Chỉ lấy dòng đầu tiên làm tóm tắt (thường là dòng mức phạt)
         | 
| 70 | 
            +
                                    clause_summary_text = clause_original_text.split('\n')[0]
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                                # 2. Tạo chuỗi văn bản giàu ngữ cảnh
         | 
| 73 | 
            +
                                text_parts = [
         | 
| 74 | 
            +
                                    part.strip() for part in [article_title, clause_summary_text, point_text] if part
         | 
| 75 | 
            +
                                ]
         | 
| 76 | 
            +
                                # Dùng ": " để nối các phần, giúp phân tách ngữ cảnh
         | 
| 77 | 
            +
                                enriched_text = ": ".join(text_parts)
         | 
| 78 | 
            +
                                
         | 
| 79 | 
            +
                                # <<< THAY ĐỔI KẾT THÚC >>>
         | 
| 80 |  | 
| 81 | 
            +
                                if not enriched_text:
         | 
|  | |
| 82 | 
             
                                    continue
         | 
| 83 |  | 
| 84 | 
             
                                current_point_metadata = clause_metadata_base.copy()
         | 
| 85 | 
            +
                                
         | 
| 86 | 
            +
                                # Giữ lại toàn bộ thông tin chi tiết trong metadata
         | 
| 87 | 
             
                                point_specific_metadata = point_data.copy()
         | 
| 88 | 
            +
                                
         | 
| 89 | 
            +
                                # Xóa trường text gốc khỏi metadata để tránh trùng lặp không cần thiết
         | 
| 90 | 
             
                                if "point_text_original" in point_specific_metadata:
         | 
| 91 | 
             
                                    del point_specific_metadata["point_text_original"]
         | 
| 92 |  | 
| 93 | 
             
                                current_point_metadata.update(point_specific_metadata)
         | 
| 94 | 
            +
                                
         | 
| 95 | 
             
                                final_metadata_cleaned = {k: v for k, v in current_point_metadata.items() if v is not None}
         | 
| 96 |  | 
| 97 | 
            +
                                flat_list.append({
         | 
| 98 | 
            +
                                    # Sử dụng văn bản đã được làm giàu
         | 
| 99 | 
            +
                                    "text": enriched_text,
         | 
| 100 | 
            +
                                    "metadata": final_metadata_cleaned
         | 
| 101 | 
            +
                                })
         | 
| 102 | 
             
                        else:
         | 
| 103 | 
            +
                            # Xử lý các Khoản không có Điểm
         | 
| 104 | 
             
                            chunk_text = clause_data.get("clause_text_original")
         | 
| 105 | 
             
                            if chunk_text:
         | 
| 106 | 
             
                                current_clause_metadata = clause_metadata_base.copy()
         | 
| 107 | 
            +
                                additional_clause_info = {k: v for k, v in clause_data.items() if k not in ["clause_text_original", "points_in_clause", "clause_number", "clause_metadata_summary"]}
         | 
| 108 | 
            +
                                current_clause_metadata.update(additional_clause_info)
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 109 | 
             
                                final_metadata_cleaned = {k: v for k, v in current_clause_metadata.items() if v is not None}
         | 
| 110 | 
            +
                                
         | 
| 111 | 
            +
                                # <<< THAY ĐỔI: Cũng làm giàu văn bản cho các Khoản đứng một mình >>>
         | 
| 112 | 
            +
                                article_title = article_metadata_base.get('article_title', '')
         | 
| 113 | 
            +
                                enriched_text = f"{article_title}: {chunk_text}" if article_title else chunk_text
         | 
| 114 |  | 
| 115 | 
            +
                                flat_list.append({
         | 
| 116 | 
            +
                                    "text": enriched_text,
         | 
| 117 | 
            +
                                    "metadata": final_metadata_cleaned
         | 
| 118 | 
            +
                                })
         | 
| 119 | 
             
                return flat_list
         | 
