-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathcaveman_compress.py
More file actions
executable file
·353 lines (280 loc) · 11.4 KB
/
caveman_compress.py
File metadata and controls
executable file
·353 lines (280 loc) · 11.4 KB
1
2
3
4
5
6
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env python3
"""
Caveman Compression Tool
Converts normal English to caveman compression and vice versa using OpenAI API
"""
import os
import sys
import argparse
from pathlib import Path
from openai import OpenAI
import spacy
import numpy as np
from .utils import load_api_key
# Load prompts from files
PROMPTS_DIR = Path(__file__).parent / 'prompts'
def load_prompt(filename):
"""Load prompt from prompts directory"""
prompt_path = PROMPTS_DIR / filename
if not prompt_path.exists():
print(f"Error: Prompt file not found: {prompt_path}", file=sys.stderr)
sys.exit(1)
return prompt_path.read_text()
COMPRESSION_PROMPT = load_prompt('compression.txt')
DECOMPRESSION_PROMPT = load_prompt('decompression.txt')
# Language model cache
_nlp_models = {}
def get_nlp_model(lang='en'):
"""Load or retrieve cached spaCy model"""
if lang in _nlp_models:
return _nlp_models[lang]
model_name = 'en_core_web_sm' # Hardcoding for this script
try:
nlp = spacy.load(model_name)
except OSError:
print(f"Error: spaCy model '{model_name}' not found.", file=sys.stderr)
print(f"Please install it by running:", file=sys.stderr)
print(f" python -m spacy download {model_name}", file=sys.stderr)
sys.exit(1)
_nlp_models[lang] = nlp
return nlp
def count_tokens(text):
"""Estimate tokens using character count / 4"""
return len(text.strip()) // 4
def is_text_content(text):
"""Detect if content is natural language text vs code/structured data"""
# Check for common code indicators
code_indicators = [
'def ', 'class ', 'function ', 'import ', 'const ', 'let ', 'var ',
'public ', 'private ', 'protected ', '#include', 'package ',
'=>', '->', '::', '!=', '==', '<=', '>=', '&&', '||',
]
# Count code-like patterns
code_score = sum(1 for indicator in code_indicators if indicator in text)
# Check for balanced braces/brackets (common in code)
brace_count = text.count('{') + text.count('}') + text.count('[') + text.count(']')
# Check for natural language indicators
words = text.split()
if len(words) < 5:
return True # Short text, treat as natural language
# If high code indicators or many braces, treat as code
if code_score >= 2 or brace_count > len(words) * 0.2:
return False
return True
def split_sentences(text):
"""Split text into sentences using spaCy"""
nlp = get_nlp_model()
doc = nlp(text)
return [sent.text for sent in doc.sents]
def get_embedding(client, text, model="text-embedding-3-large"):
"""Get embedding vector for text"""
try:
response = client.embeddings.create(
input=text,
model=model
)
return response.data[0].embedding
except Exception as e:
print(f"Warning: Failed to get embedding: {e}", file=sys.stderr)
return None
def cosine_similarity(vec1, vec2):
"""Calculate cosine similarity between two vectors"""
vec1 = np.array(vec1)
vec2 = np.array(vec2)
dot_product = np.dot(vec1, vec2)
norm1 = np.linalg.norm(vec1)
norm2 = np.linalg.norm(vec2)
return dot_product / (norm1 * norm2)
def calculate_embedding_loss(client, original_text, compressed_text, model="text-embedding-3-large"):
"""Calculate embedding similarity loss between original and compressed text"""
original_emb = get_embedding(client, original_text, model)
compressed_emb = get_embedding(client, compressed_text, model)
if original_emb is None or compressed_emb is None:
return None
similarity = cosine_similarity(original_emb, compressed_emb)
# Loss is 1 - similarity (0 = perfect preservation, 1 = total loss)
loss = 1 - similarity
return loss, similarity
def compress_text(text, model="gpt-4o", calculate_embeddings=True):
"""Compress normal English to caveman compression"""
client = OpenAI(api_key=load_api_key())
# Detect if this is natural language text
is_text = is_text_content(text)
# If it's text, use sentence-by-sentence compression with gpt-4o-mini
if is_text:
sentences = split_sentences(text)
# If only one sentence or very short, compress as whole
if len(sentences) <= 1:
model = "gpt-4o-mini"
prompt = COMPRESSION_PROMPT.format(text=text)
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are an expert at caveman compression. Always compress the provided text, never ask for clarification."},
{"role": "user", "content": prompt}
],
temperature=0.3,
)
compressed = response.choices[0].message.content.strip()
else:
# Compress sentence by sentence with gpt-4o-mini
compressed_sentences = []
for sentence in sentences:
if not sentence.strip():
continue
prompt = COMPRESSION_PROMPT.format(text=sentence)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are an expert at caveman compression. Always compress the provided text, never ask for clarification."},
{"role": "user", "content": prompt}
],
temperature=0.3,
)
compressed_sent = response.choices[0].message.content.strip()
compressed_sentences.append(compressed_sent)
compressed = ' '.join(compressed_sentences)
else:
# For code/structured data, use original model and compress as whole
prompt = COMPRESSION_PROMPT.format(text=text)
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are an expert at caveman compression. Always compress the provided text, never ask for clarification."},
{"role": "user", "content": prompt}
],
temperature=0.3,
)
compressed = response.choices[0].message.content.strip()
# Calculate statistics
original_tokens = count_tokens(text)
compressed_tokens = count_tokens(compressed)
reduction = ((original_tokens - compressed_tokens) / original_tokens * 100) if original_tokens > 0 else 0
# Calculate embedding loss if requested
embedding_loss = None
embedding_similarity = None
if calculate_embeddings:
result = calculate_embedding_loss(client, text, compressed)
if result is not None:
embedding_loss, embedding_similarity = result
return compressed, original_tokens, compressed_tokens, reduction, embedding_loss, embedding_similarity
def decompress_text(text, model="gpt-4o"):
"""Decompress caveman compression to normal English"""
client = OpenAI(api_key=load_api_key())
prompt = DECOMPRESSION_PROMPT.format(text=text)
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are an expert at expanding compressed text."},
{"role": "user", "content": prompt}
],
temperature=0.3,
)
decompressed = response.choices[0].message.content.strip()
# Calculate statistics
caveman_tokens = count_tokens(text)
normal_tokens = count_tokens(decompressed)
expansion = ((normal_tokens - caveman_tokens) / caveman_tokens * 100) if caveman_tokens > 0 else 0
return decompressed, caveman_tokens, normal_tokens, expansion
def main():
parser = argparse.ArgumentParser(
description='Caveman Compression Tool - Compress or decompress text',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Compress normal English to caveman
python caveman_compress.py compress "In order to optimize the database..."
# Decompress caveman to normal English
python caveman_compress.py decompress "Need fast queries. Add index..."
# Read from file
python caveman_compress.py compress -f input.txt
# Save output to file
python caveman_compress.py compress -f input.txt -o output.txt
"""
)
parser.add_argument(
'mode',
choices=['compress', 'decompress', 'c', 'd'],
help='Mode: compress (c) or decompress (d)'
)
parser.add_argument(
'text',
nargs='?',
help='Text to process (omit if using -f)'
)
parser.add_argument(
'-f', '--file',
help='Read input from file'
)
parser.add_argument(
'-o', '--output',
help='Write output to file'
)
parser.add_argument(
'-m', '--model',
default='gpt-4o',
help='OpenAI model to use (default: gpt-4o)'
)
args = parser.parse_args()
# Normalize mode
mode = 'compress' if args.mode in ['compress', 'c'] else 'decompress'
# Get input text
if args.file:
with open(args.file, 'r') as f:
input_text = f.read().strip()
elif args.text:
input_text = args.text
else:
parser.error("Must provide either text argument or -f/--file option")
if not input_text:
print("Error: Input text is empty", file=sys.stderr)
sys.exit(1)
# Process text
print(f"\n{'='*60}")
print(f"MODE: {mode.upper()}")
print(f"MODEL: {args.model}")
print(f"{'='*60}\n")
if mode == 'compress':
print("ORIGINAL TEXT:")
print(f"{input_text}\n")
print("Compressing...\n")
result, orig_tokens, comp_tokens, reduction, emb_loss, emb_sim = compress_text(input_text, args.model)
print("CAVEMAN COMPRESSED:")
print(f"{result}\n")
print(f"{'='*60}")
print("STATISTICS:")
print(f" Original: {len(input_text):4d} chars ≈ {orig_tokens:3d} tokens")
print(f" Compressed: {len(result):4d} chars ≈ {comp_tokens:3d} tokens")
print(f" Reduction: {reduction:.1f}%")
if emb_loss is not None and emb_sim is not None:
print(f"\n Embedding Similarity: {emb_sim:.4f}")
print(f" Embedding Loss: {emb_loss:.4f}")
if emb_sim >= 0.95:
print(f" Quality: Excellent - virtually identical semantic meaning")
elif emb_sim >= 0.90:
print(f" Quality: Good - minor semantic drift")
elif emb_sim >= 0.85:
print(f" Quality: Moderate - noticeable drift")
else:
print(f" Quality: Poor - significant semantic drift")
print(f"{'='*60}\n")
else: # decompress
print("CAVEMAN TEXT:")
print(f"{input_text}\n")
print("Decompressing...\n")
result, cave_tokens, norm_tokens, expansion = decompress_text(input_text, args.model)
print("NORMAL ENGLISH:")
print(f"{result}\n")
print(f"{'='*60}")
print("STATISTICS:")
print(f" Caveman: {len(input_text):4d} chars ≈ {cave_tokens:3d} tokens")
print(f" Normal: {len(result):4d} chars ≈ {norm_tokens:3d} tokens")
print(f" Expansion: {expansion:.1f}%")
print(f"{'='*60}\n")
# Save to file if requested
if args.output:
with open(args.output, 'w') as f:
f.write(result)
print(f"Output saved to: {args.output}\n")
if __name__ == "__main__":
main()