Spaces:
Running
Running
Update create_granular_chunks.py
Browse files- create_granular_chunks.py +195 -219
create_granular_chunks.py
CHANGED
|
@@ -1,241 +1,217 @@
|
|
| 1 |
-
# create_granular_chunks.py
|
|
|
|
| 2 |
import json
|
| 3 |
import re
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
import tiktoken
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
chunks = []
|
| 113 |
current_chunk = ""
|
| 114 |
-
|
| 115 |
-
|
| 116 |
for sentence in sentences:
|
| 117 |
-
|
| 118 |
-
if
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
sentence_tokens = count_tokens(sentence)
|
| 122 |
-
|
| 123 |
-
# If adding this sentence would exceed max_tokens, finalize current chunk
|
| 124 |
-
if current_tokens + sentence_tokens > max_tokens and current_chunk:
|
| 125 |
chunks.append(current_chunk.strip())
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
overlap_text = current_chunk[-overlap_tokens*5:] # Rough overlap estimation
|
| 130 |
-
current_chunk = overlap_text + " " + sentence
|
| 131 |
else:
|
| 132 |
current_chunk = sentence
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
current_chunk += (" " if current_chunk else "") + sentence
|
| 136 |
-
current_tokens += sentence_tokens
|
| 137 |
-
|
| 138 |
-
# Add the last chunk if it has content
|
| 139 |
-
if current_chunk.strip():
|
| 140 |
chunks.append(current_chunk.strip())
|
| 141 |
-
|
| 142 |
return chunks
|
| 143 |
|
| 144 |
-
def create_chunk_hash(text: str) -> str:
|
| 145 |
-
"""Create a hash of the chunk text for deduplication."""
|
| 146 |
-
return hashlib.md5(text.encode('utf-8')).hexdigest()[:12]
|
| 147 |
|
| 148 |
-
def
|
| 149 |
-
"""
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
all_chunks = []
|
| 153 |
-
|
| 154 |
-
chunk_id_counter = 1
|
| 155 |
-
|
| 156 |
try:
|
| 157 |
-
with open(
|
| 158 |
-
for
|
| 159 |
try:
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
continue
|
| 167 |
-
|
| 168 |
-
# Split into token-based chunks
|
| 169 |
-
text_chunks = split_into_token_chunks(chunk_text)
|
| 170 |
-
|
| 171 |
-
for i, chunk in enumerate(text_chunks):
|
| 172 |
-
if not chunk.strip():
|
| 173 |
-
continue
|
| 174 |
-
|
| 175 |
-
# Check for duplicates
|
| 176 |
-
chunk_hash = create_chunk_hash(chunk)
|
| 177 |
-
if chunk_hash in chunk_hashes:
|
| 178 |
-
continue
|
| 179 |
-
chunk_hashes.add(chunk_hash)
|
| 180 |
-
|
| 181 |
-
# Extract keywords
|
| 182 |
-
financial_keywords = extract_financial_keywords(chunk)
|
| 183 |
-
authority_keywords = extract_authority_keywords(chunk)
|
| 184 |
-
|
| 185 |
-
# Create chunk object
|
| 186 |
-
chunk_obj = {
|
| 187 |
-
'id': f'chunk-{chunk_id_counter}',
|
| 188 |
-
'text': chunk,
|
| 189 |
-
'metadata': {
|
| 190 |
-
'section': item.get('section', ''),
|
| 191 |
-
'clause': item.get('clause', ''),
|
| 192 |
-
'title': item.get('title', ''),
|
| 193 |
-
'chunk_index': i,
|
| 194 |
-
'source_line': line_num,
|
| 195 |
-
'financial_keywords': financial_keywords,
|
| 196 |
-
'authority_keywords': authority_keywords,
|
| 197 |
-
'token_count': count_tokens(chunk)
|
| 198 |
-
}
|
| 199 |
-
}
|
| 200 |
-
|
| 201 |
-
all_chunks.append(chunk_obj)
|
| 202 |
-
chunk_id_counter += 1
|
| 203 |
-
|
| 204 |
-
except json.JSONDecodeError as e:
|
| 205 |
-
print(f"Warning: Invalid JSON on line {line_num}: {e}")
|
| 206 |
continue
|
| 207 |
-
|
| 208 |
except FileNotFoundError:
|
| 209 |
-
print(f"Error:
|
| 210 |
return
|
| 211 |
-
|
| 212 |
-
print(f"Error reading file: {e}")
|
| 213 |
-
return
|
| 214 |
-
|
| 215 |
print(f"Generated {len(all_chunks)} chunks before deduplication.")
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
print(f" Financial keywords: {sample['metadata']['financial_keywords'][:3]}...")
|
| 233 |
-
print(f" Token count: {sample['metadata']['token_count']}")
|
| 234 |
-
|
| 235 |
-
except Exception as e:
|
| 236 |
-
print(f"Error writing output file: {e}")
|
| 237 |
|
| 238 |
if __name__ == "__main__":
|
| 239 |
-
|
| 240 |
-
output_file = "granular_chunks_final.jsonl"
|
| 241 |
-
process_jsonl_file(input_file, output_file)
|
|
|
|
| 1 |
+
# create_granular_chunks.py
|
| 2 |
+
import os
|
| 3 |
import json
|
| 4 |
import re
|
| 5 |
+
from typing import List, Dict, Any
|
| 6 |
+
import nltk
|
|
|
|
| 7 |
|
| 8 |
+
# Download punkt tokenizer if not already done (Ensure this runs once in your environment setup)
|
| 9 |
+
nltk.download('punkt')
|
| 10 |
+
nltk.download('punkt_tab') # Also download punkt_tab to avoid LookupError
|
| 11 |
+
|
| 12 |
+
# --- Configuration ---
|
| 13 |
+
INPUT_FILE = "combined_context.jsonl"
|
| 14 |
+
OUTPUT_FILE = "granular_chunks_final.jsonl" # Keep filename consistent
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# --- Global State ---
|
| 18 |
+
chunk_counter = 0
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def get_unique_id() -> str:
|
| 22 |
+
"""Returns a unique, incrementing ID for each chunk."""
|
| 23 |
+
global chunk_counter
|
| 24 |
+
chunk_counter += 1
|
| 25 |
+
return f"chunk-{chunk_counter}"
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def create_chunk(context: Dict, text: str) -> Dict:
|
| 29 |
+
"""Creates a standardized chunk dictionary with rich metadata."""
|
| 30 |
+
metadata = {
|
| 31 |
+
"section": context.get("section"),
|
| 32 |
+
"clause": context.get("clause") or context.get("Clause"),
|
| 33 |
+
"title": context.get("title"),
|
| 34 |
+
"source_description": context.get("description"),
|
| 35 |
+
}
|
| 36 |
+
# Add other primitive metadata keys
|
| 37 |
+
for key, value in context.items():
|
| 38 |
+
if key not in metadata and isinstance(value, (str, int, float, bool)):
|
| 39 |
+
metadata[key] = value
|
| 40 |
+
|
| 41 |
+
return {
|
| 42 |
+
"id": get_unique_id(),
|
| 43 |
+
"text": text.strip(),
|
| 44 |
+
"metadata": {k: v for k, v in metadata.items() if v is not None}
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def format_delegation_text(delegation: Any) -> str:
|
| 49 |
+
"""
|
| 50 |
+
Formats a delegation dictionary or string into a readable string.
|
| 51 |
+
Explicitly includes "NIL" or "---" to capture no power cases.
|
| 52 |
+
"""
|
| 53 |
+
if not isinstance(delegation, dict):
|
| 54 |
+
return str(delegation)
|
| 55 |
+
parts = [f"the limit for {auth} is {limit if limit and str(limit) != '---' else 'NIL'}" for auth, limit in delegation.items()]
|
| 56 |
+
return ", ".join(parts) if parts else "No specific delegation provided."
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def format_remarks(remarks: Any) -> str:
|
| 60 |
+
"""Safely formats the 'remarks' field, handling various data types."""
|
| 61 |
+
if isinstance(remarks, list):
|
| 62 |
+
remark_parts = []
|
| 63 |
+
for item in remarks:
|
| 64 |
+
if isinstance(item, dict):
|
| 65 |
+
for key, value in item.items():
|
| 66 |
+
remark_parts.append(f"{key}: {value}")
|
| 67 |
+
else:
|
| 68 |
+
remark_parts.append(str(item))
|
| 69 |
+
return " ".join(remark_parts)
|
| 70 |
+
return str(remarks)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def build_descriptive_text(context: Dict) -> str:
|
| 74 |
+
"""
|
| 75 |
+
Builds a clear, descriptive, natural language text by combining fields.
|
| 76 |
+
Focused for best relevance and contextual richness.
|
| 77 |
+
"""
|
| 78 |
+
text_parts = []
|
| 79 |
+
|
| 80 |
+
if context.get("title"):
|
| 81 |
+
text_parts.append(f"Regarding the policy '{context['title']}'")
|
| 82 |
+
|
| 83 |
+
specific_desc = context.get('description') or context.get('method')
|
| 84 |
+
if specific_desc and specific_desc != context.get('title'):
|
| 85 |
+
text_parts.append(f"specifically for '{specific_desc}'")
|
| 86 |
+
|
| 87 |
+
if "delegation" in context:
|
| 88 |
+
delegation_text = format_delegation_text(context["delegation"])
|
| 89 |
+
text_parts.append(f", financial delegations are: {delegation_text}.")
|
| 90 |
+
elif "composition" in context:
|
| 91 |
+
composition_parts = []
|
| 92 |
+
for item in context["composition"]:
|
| 93 |
+
if isinstance(item, dict):
|
| 94 |
+
for role, members in item.items():
|
| 95 |
+
member_text = (f"the {role} is {members}" if isinstance(members, str)
|
| 96 |
+
else f"the {role} are: {', '.join(members)}")
|
| 97 |
+
composition_parts.append(member_text)
|
| 98 |
+
text_parts.append(f", the composition is: {'; '.join(composition_parts)}.")
|
| 99 |
+
|
| 100 |
+
if "remarks" in context and context["remarks"]:
|
| 101 |
+
remarks_text = format_remarks(context["remarks"])
|
| 102 |
+
text_parts.append(f" Important remarks include: {remarks_text}")
|
| 103 |
+
|
| 104 |
+
# Join all parts into a flowing sentence
|
| 105 |
+
return " ".join(text_parts).strip()
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def split_text_into_chunks(text: str, max_char_length: int = 1500, overlap: int = 200) -> List[str]:
|
| 109 |
+
"""
|
| 110 |
+
Splits a long text into smaller chunks with controlled overlap.
|
| 111 |
+
Uses sentence tokenization for natural splits.
|
| 112 |
+
"""
|
| 113 |
+
text = text.strip()
|
| 114 |
+
if len(text) <= max_char_length:
|
| 115 |
+
return [text]
|
| 116 |
+
|
| 117 |
+
# Explicitly specify language to avoid punkt_tab error
|
| 118 |
+
sentences = nltk.tokenize.sent_tokenize(text, language='english')
|
| 119 |
chunks = []
|
| 120 |
current_chunk = ""
|
| 121 |
+
|
|
|
|
| 122 |
for sentence in sentences:
|
| 123 |
+
# +1 for space/newline likely added between sentences
|
| 124 |
+
if len(current_chunk) + len(sentence) + 1 <= max_char_length:
|
| 125 |
+
current_chunk += (" " + sentence) if current_chunk else sentence
|
| 126 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
chunks.append(current_chunk.strip())
|
| 128 |
+
# Start next chunk with overlap from end of previous chunk (by characters)
|
| 129 |
+
if overlap < len(current_chunk):
|
| 130 |
+
current_chunk = current_chunk[-overlap:] + " " + sentence
|
|
|
|
|
|
|
| 131 |
else:
|
| 132 |
current_chunk = sentence
|
| 133 |
+
|
| 134 |
+
if current_chunk:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
chunks.append(current_chunk.strip())
|
|
|
|
| 136 |
return chunks
|
| 137 |
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
+
def process_entry(data: Dict, parent_context: Dict = None) -> List[Dict]:
|
| 140 |
+
"""
|
| 141 |
+
Processes a JSON policy entry and returns granular, context-rich chunks.
|
| 142 |
+
Applies recursive traversal and implements chunk size limiting.
|
| 143 |
+
"""
|
| 144 |
+
context = {**(parent_context or {}), **data}
|
| 145 |
+
chunks = []
|
| 146 |
+
|
| 147 |
+
# Handler 1: Simple Item Lists (ex: rules, exclusions)
|
| 148 |
+
list_key = next((key for key in ["items", "exclusions"] if key in data and isinstance(data.get(key), list)), None)
|
| 149 |
+
if list_key:
|
| 150 |
+
base_title = context.get('title', 'a policy')
|
| 151 |
+
for item in data[list_key]:
|
| 152 |
+
if isinstance(item, str):
|
| 153 |
+
# Build chunk text with clear descriptive prefix for relevance
|
| 154 |
+
text = f"A rule regarding '{base_title}' is: {item}."
|
| 155 |
+
# Split if too long
|
| 156 |
+
for sub_chunk in split_text_into_chunks(text):
|
| 157 |
+
chunks.append(create_chunk(context, sub_chunk))
|
| 158 |
+
return chunks
|
| 159 |
+
|
| 160 |
+
# Handler 2: Recursive traversal for nested dictionaries/lists
|
| 161 |
+
has_recursed = False
|
| 162 |
+
for key, value in data.items():
|
| 163 |
+
if isinstance(value, list) and value and all(isinstance(item, dict) for item in value):
|
| 164 |
+
for item in value:
|
| 165 |
+
chunks.extend(process_entry(item, context))
|
| 166 |
+
has_recursed = True
|
| 167 |
+
|
| 168 |
+
# Handler 3: Leaf nodes with delegation, composition or description
|
| 169 |
+
if not has_recursed and ("delegation" in data or "composition" in data or "description" in data):
|
| 170 |
+
text = build_descriptive_text(context)
|
| 171 |
+
# Split long descriptive text intelligently
|
| 172 |
+
for chunk_text in split_text_into_chunks(text):
|
| 173 |
+
chunks.append(create_chunk(context, chunk_text))
|
| 174 |
+
|
| 175 |
+
return chunks
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
def main():
|
| 179 |
+
"""Main orchestration to read input, process, and write chunks."""
|
| 180 |
+
print(f"Starting to process '{INPUT_FILE}' for improved granular chunking...")
|
| 181 |
all_chunks = []
|
| 182 |
+
|
|
|
|
|
|
|
| 183 |
try:
|
| 184 |
+
with open(INPUT_FILE, 'r', encoding='utf-8') as f:
|
| 185 |
+
for i, line in enumerate(f):
|
| 186 |
try:
|
| 187 |
+
data = json.loads(line)
|
| 188 |
+
processed = process_entry(data)
|
| 189 |
+
if processed:
|
| 190 |
+
all_chunks.extend(processed)
|
| 191 |
+
except json.JSONDecodeError:
|
| 192 |
+
print(f"Warning: Skipping malformed JSON on line {i+1}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
continue
|
|
|
|
| 194 |
except FileNotFoundError:
|
| 195 |
+
print(f"Error: Input file '{INPUT_FILE}' not found.")
|
| 196 |
return
|
| 197 |
+
|
|
|
|
|
|
|
|
|
|
| 198 |
print(f"Generated {len(all_chunks)} chunks before deduplication.")
|
| 199 |
+
|
| 200 |
+
# Deduplicate by text content (retaining last occurrences)
|
| 201 |
+
unique_chunks_map = {}
|
| 202 |
+
for chunk in all_chunks:
|
| 203 |
+
unique_chunks_map[chunk['text']] = chunk
|
| 204 |
+
|
| 205 |
+
unique_chunks = list(unique_chunks_map.values())
|
| 206 |
+
print(f"{len(unique_chunks)} unique chunks after deduplication.")
|
| 207 |
+
|
| 208 |
+
# Write output in JSONL format for later vector DB ingestion
|
| 209 |
+
with open(OUTPUT_FILE, 'w', encoding='utf-8') as outf:
|
| 210 |
+
for chunk in unique_chunks:
|
| 211 |
+
outf.write(json.dumps(chunk, ensure_ascii=False) + "\n")
|
| 212 |
+
|
| 213 |
+
print(f"Successfully wrote improved granular chunks to '{OUTPUT_FILE}'.")
|
| 214 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
if __name__ == "__main__":
|
| 217 |
+
main()
|
|
|
|
|
|