Spaces:
Sleeping
Sleeping
Update create_granular_chunks.py
Browse files- create_granular_chunks.py +218 -208
create_granular_chunks.py
CHANGED
|
@@ -1,231 +1,241 @@
|
|
| 1 |
-
# create_granular_chunks.py
|
| 2 |
-
import os
|
| 3 |
import json
|
| 4 |
import re
|
| 5 |
-
|
| 6 |
-
import
|
|
|
|
| 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 |
-
def
|
| 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 |
-
if
|
| 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 |
-
text_parts.append(f", the composition is: {'; '.join(composition_parts)}.")
|
| 113 |
-
if "remarks" in context and context["remarks"]:
|
| 114 |
-
remarks_text = format_remarks(context["remarks"])
|
| 115 |
-
text_parts.append(f" Important remarks include: {remarks_text}")
|
| 116 |
-
return " ".join(text_parts).strip()
|
| 117 |
-
|
| 118 |
-
def count_tokens(text: str) -> int:
|
| 119 |
-
encoding = get_encoding()
|
| 120 |
-
return len(encoding.encode(text))
|
| 121 |
-
|
| 122 |
-
def get_token_overlap(text: str, overlap_tokens: int) -> str:
|
| 123 |
-
"""Return the last `overlap_tokens` worth of text from the input string."""
|
| 124 |
-
encoding = get_encoding()
|
| 125 |
-
tokens = encoding.encode(text)
|
| 126 |
-
if len(tokens) <= overlap_tokens:
|
| 127 |
-
return text
|
| 128 |
-
# Decode only the last overlap_tokens tokens
|
| 129 |
-
overlapped = encoding.decode(tokens[-overlap_tokens:])
|
| 130 |
-
# Remove possible split word inconsistencies by finding last complete sentence
|
| 131 |
-
# This is optional: can simply return overlapped
|
| 132 |
-
last_period = overlapped.rfind('.')
|
| 133 |
-
if last_period != -1 and last_period < len(overlapped) - 2:
|
| 134 |
-
return overlapped[last_period+1:].strip()
|
| 135 |
-
return overlapped.strip()
|
| 136 |
-
|
| 137 |
-
def split_text_by_tokens(text: str, max_tokens: int = MAX_TOKENS, overlap_tokens: int = OVERLAP_TOKENS) -> List[str]:
|
| 138 |
-
"""Split text into chunks based on token count, with specified overlap."""
|
| 139 |
-
encoding = get_encoding()
|
| 140 |
-
sents = nltk.tokenize.sent_tokenize(text, language='english')
|
| 141 |
chunks = []
|
| 142 |
current_chunk = ""
|
| 143 |
current_tokens = 0
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
chunks.append(current_chunk.strip())
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
| 154 |
current_chunk = overlap_text + " " + sentence
|
| 155 |
-
current_tokens = len(encoding.encode(current_chunk))
|
| 156 |
else:
|
| 157 |
current_chunk = sentence
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
chunks.append(current_chunk.strip())
|
|
|
|
| 161 |
return chunks
|
| 162 |
|
| 163 |
-
def
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
# Handler 1: Simple Item Lists
|
| 168 |
-
list_key = next((key for key in ["items", "exclusions"] if key in data and isinstance(data.get(key), list)), None)
|
| 169 |
-
if list_key:
|
| 170 |
-
base_title = context.get('title', 'a policy')
|
| 171 |
-
for item in data[list_key]:
|
| 172 |
-
if isinstance(item, str):
|
| 173 |
-
text = f"A rule regarding '{base_title}' is: {item}."
|
| 174 |
-
for sub_chunk in split_text_by_tokens(text):
|
| 175 |
-
chunks.append(create_chunk(context, sub_chunk))
|
| 176 |
-
return chunks
|
| 177 |
-
|
| 178 |
-
# Handler 2: Recursive traversal for nested dicts/lists
|
| 179 |
-
has_recursed = False
|
| 180 |
-
for key, value in data.items():
|
| 181 |
-
if isinstance(value, list) and value and all(isinstance(item, dict) for item in value):
|
| 182 |
-
for item in value:
|
| 183 |
-
chunks.extend(process_entry(item, context))
|
| 184 |
-
has_recursed = True
|
| 185 |
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
chunks.append(create_chunk(context, chunk_text))
|
| 191 |
-
|
| 192 |
-
return chunks
|
| 193 |
-
|
| 194 |
-
def main():
|
| 195 |
-
print(f"Starting to process '{INPUT_FILE}' with token-based chunking and keyword enhancement...")
|
| 196 |
all_chunks = []
|
| 197 |
-
|
|
|
|
|
|
|
| 198 |
try:
|
| 199 |
-
with open(
|
| 200 |
-
for
|
| 201 |
try:
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
continue
|
|
|
|
| 209 |
except FileNotFoundError:
|
| 210 |
-
print(f"Error:
|
| 211 |
return
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
| 213 |
print(f"Generated {len(all_chunks)} chunks before deduplication.")
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
if __name__ == "__main__":
|
| 231 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
# create_granular_chunks.py (place this in root directory)
|
|
|
|
| 2 |
import json
|
| 3 |
import re
|
| 4 |
+
import hashlib
|
| 5 |
+
from typing import List, Dict, Any, Set
|
| 6 |
+
import tiktoken
|
| 7 |
|
| 8 |
+
def count_tokens(text: str, model: str = "gpt-3.5-turbo") -> int:
|
| 9 |
+
"""Count tokens using tiktoken."""
|
| 10 |
+
try:
|
| 11 |
+
encoding = tiktoken.encoding_for_model(model)
|
| 12 |
+
return len(encoding.encode(text))
|
| 13 |
+
except Exception:
|
| 14 |
+
# Fallback to simple word-based estimation
|
| 15 |
+
return len(text.split()) * 1.3
|
| 16 |
+
|
| 17 |
+
def extract_financial_keywords(text: str) -> List[str]:
|
| 18 |
+
"""Extract financial keywords from text."""
|
| 19 |
+
financial_patterns = [
|
| 20 |
+
r'₹[\d,]+(?:\.\d{1,2})?(?:\s*(?:crore|lakh|thousand))?',
|
| 21 |
+
r'\b(?:budget|cost|expenditure|estimate|payment|procurement)\b',
|
| 22 |
+
r'\b(?:tender|contract|purchase|award)\b',
|
| 23 |
+
r'\b(?:crore|lakh|thousand)\b'
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
keywords = set()
|
| 27 |
+
for pattern in financial_patterns:
|
| 28 |
+
matches = re.findall(pattern, text, re.IGNORECASE)
|
| 29 |
+
keywords.update(matches)
|
| 30 |
+
|
| 31 |
+
return list(keywords)[:10] # Limit to 10 keywords
|
| 32 |
+
|
| 33 |
+
def extract_authority_keywords(text: str) -> List[str]:
|
| 34 |
+
"""Extract authority/designation keywords from text."""
|
| 35 |
+
authority_patterns = [
|
| 36 |
+
r'\b(?:D\([TPF]\)|ED|CGM|GM|DGM|Sr\.?\s*M(?:anager)?)\b',
|
| 37 |
+
r'\b(?:Director|Manager|Chief|Head)\b',
|
| 38 |
+
r'\b(?:CMD|BOD|HOP|HOD|HOF)\b',
|
| 39 |
+
r'\b(?:approval|sanction|delegation|authority|power)\b'
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
keywords = set()
|
| 43 |
+
for pattern in authority_patterns:
|
| 44 |
+
matches = re.findall(pattern, text, re.IGNORECASE)
|
| 45 |
+
keywords.update(matches)
|
| 46 |
+
|
| 47 |
+
return list(keywords)[:10] # Limit to 10 keywords
|
| 48 |
+
|
| 49 |
+
def create_chunk_text_from_item(item: Dict) -> str:
|
| 50 |
+
"""Create comprehensive chunk text from a single item."""
|
| 51 |
+
parts = []
|
| 52 |
+
|
| 53 |
+
# Add section and title context
|
| 54 |
+
if item.get('section'):
|
| 55 |
+
parts.append(f"Regarding the policy '{item.get('title', 'Unknown')}' under section '{item['section']}':")
|
| 56 |
+
|
| 57 |
+
# Add main description
|
| 58 |
+
if item.get('description'):
|
| 59 |
+
parts.append(item['description'])
|
| 60 |
+
|
| 61 |
+
# Add items if present
|
| 62 |
+
if item.get('items'):
|
| 63 |
+
if len(item['items']) == 1:
|
| 64 |
+
parts.append(f"This covers: {item['items'][0]}")
|
| 65 |
+
else:
|
| 66 |
+
parts.append("This covers the following:")
|
| 67 |
+
for i, sub_item in enumerate(item['items'], 1):
|
| 68 |
+
parts.append(f"{i}. {sub_item}")
|
| 69 |
+
|
| 70 |
+
# Add delegation information
|
| 71 |
+
if item.get('delegation'):
|
| 72 |
+
parts.append("Authority delegation:")
|
| 73 |
+
for role, limit in item['delegation'].items():
|
| 74 |
+
if limit and limit != "NIL":
|
| 75 |
+
parts.append(f"- {role}: {limit}")
|
| 76 |
+
|
| 77 |
+
# Add subclauses
|
| 78 |
+
if item.get('subclauses'):
|
| 79 |
+
parts.append("This includes:")
|
| 80 |
+
for subclause in item['subclauses']:
|
| 81 |
+
if subclause.get('description'):
|
| 82 |
+
parts.append(f"• {subclause['description']}")
|
| 83 |
+
if subclause.get('delegation'):
|
| 84 |
+
for role, limit in subclause['delegation'].items():
|
| 85 |
+
if limit and limit != "NIL":
|
| 86 |
+
parts.append(f" - {role}: {limit}")
|
| 87 |
+
|
| 88 |
+
# Add methods (for complex delegation structures)
|
| 89 |
+
if item.get('methods'):
|
| 90 |
+
for method in item['methods']:
|
| 91 |
+
if method.get('delegation'):
|
| 92 |
+
parts.append(f"For {method.get('method', 'this method')}:")
|
| 93 |
+
for role, limit in method['delegation'].items():
|
| 94 |
+
if limit and limit != "NIL":
|
| 95 |
+
parts.append(f"- {role}: {limit}")
|
| 96 |
+
|
| 97 |
+
# Add remarks
|
| 98 |
+
if item.get('remarks'):
|
| 99 |
+
parts.append("Important notes:")
|
| 100 |
+
if isinstance(item['remarks'], list):
|
| 101 |
+
for remark in item['remarks']:
|
| 102 |
+
if isinstance(remark, str):
|
| 103 |
+
parts.append(f"• {remark}")
|
| 104 |
+
elif isinstance(item['remarks'], str):
|
| 105 |
+
parts.append(f"• {item['remarks']}")
|
| 106 |
+
|
| 107 |
+
return " ".join(parts)
|
| 108 |
+
|
| 109 |
+
def split_into_token_chunks(text: str, max_tokens: int = 400, overlap_tokens: int = 50) -> List[str]:
|
| 110 |
+
"""Split text into chunks based on token count."""
|
| 111 |
+
sentences = re.split(r'[.!?]\s+', text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
chunks = []
|
| 113 |
current_chunk = ""
|
| 114 |
current_tokens = 0
|
| 115 |
+
|
| 116 |
+
for sentence in sentences:
|
| 117 |
+
sentence = sentence.strip()
|
| 118 |
+
if not sentence:
|
| 119 |
+
continue
|
| 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 |
+
# Start new chunk with overlap
|
| 128 |
+
if overlap_tokens > 0 and chunks:
|
| 129 |
+
overlap_text = current_chunk[-overlap_tokens*5:] # Rough overlap estimation
|
| 130 |
current_chunk = overlap_text + " " + sentence
|
|
|
|
| 131 |
else:
|
| 132 |
current_chunk = sentence
|
| 133 |
+
current_tokens = count_tokens(current_chunk)
|
| 134 |
+
else:
|
| 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 process_jsonl_file(file_path: str, output_path: str):
|
| 149 |
+
"""Process the JSONL file and create granular chunks."""
|
| 150 |
+
print(f"Starting to process '{file_path}' with token-based chunking and keyword enhancement...")
|
| 151 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
all_chunks = []
|
| 153 |
+
chunk_hashes = set() # For deduplication
|
| 154 |
+
chunk_id_counter = 1
|
| 155 |
+
|
| 156 |
try:
|
| 157 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 158 |
+
for line_num, line in enumerate(file, 1):
|
| 159 |
try:
|
| 160 |
+
item = json.loads(line.strip())
|
| 161 |
+
|
| 162 |
+
# Create comprehensive text from the item
|
| 163 |
+
chunk_text = create_chunk_text_from_item(item)
|
| 164 |
+
|
| 165 |
+
if not chunk_text.strip():
|
| 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: File '{file_path}' not found.")
|
| 210 |
return
|
| 211 |
+
except Exception as e:
|
| 212 |
+
print(f"Error reading file: {e}")
|
| 213 |
+
return
|
| 214 |
+
|
| 215 |
print(f"Generated {len(all_chunks)} chunks before deduplication.")
|
| 216 |
+
print(f"{len(chunk_hashes)} unique chunks after deduplication.")
|
| 217 |
+
|
| 218 |
+
# Write chunks to output file
|
| 219 |
+
try:
|
| 220 |
+
with open(output_path, 'w', encoding='utf-8') as output_file:
|
| 221 |
+
for chunk in all_chunks:
|
| 222 |
+
json.dump(chunk, output_file, ensure_ascii=False)
|
| 223 |
+
output_file.write('\n')
|
| 224 |
+
|
| 225 |
+
print(f"Successfully wrote improved granular chunks to '{output_path}'.")
|
| 226 |
+
print(f"Sample chunk structure:")
|
| 227 |
+
if all_chunks:
|
| 228 |
+
sample = all_chunks[0]
|
| 229 |
+
print(f" ID: {sample['id']}")
|
| 230 |
+
print(f" Text length: {len(sample['text'])} chars")
|
| 231 |
+
print(f" Section: {sample['metadata']['section']}")
|
| 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 |
+
input_file = "combined_context.jsonl"
|
| 240 |
+
output_file = "granular_chunks_final.jsonl"
|
| 241 |
+
process_jsonl_file(input_file, output_file)
|