-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemonstrate_stm_pipeline.py
More file actions
293 lines (263 loc) · 11 KB
/
demonstrate_stm_pipeline.py
File metadata and controls
293 lines (263 loc) · 11 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
#!/usr/bin/env python3
"""
SPDX-License-Identifier: MIT
Demonstrate the STM (Signal, Transform, Meaning) Pipeline
This script shows how raw signals (code) transform into semantic meaning (LJPW)
through the Python Code Harmonizer's STM pipeline.
"""
import sys
from pathlib import Path
# Add project root to path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
from harmonizer_integration import PythonCodeHarmonizer
def demonstrate_stm_pipeline():
"""Demonstrate the complete STM transformation pipeline."""
print("=" * 80)
print("STM FRAMEWORK DEMONSTRATION")
print("Signal → Transform → Meaning")
print("=" * 80)
print()
print("This demonstration shows how raw code signals are transformed")
print("into semantic meaning (LJPW coordinates) through multiple stages.")
print("=" * 80)
harmonizer = PythonCodeHarmonizer(quiet=True)
# =========================================================================
# STAGE 1: Single Signal → Meaning
# =========================================================================
print("\n" + "=" * 80)
print("STAGE 1: SINGLE SIGNAL TRANSFORMATION")
print("=" * 80)
print("\nDemonstrates: How a single code signal transforms into LJPW meaning")
print()
code_signal = """
def validate_user_input(data):
'''Validate user input with type checking'''
if not isinstance(data, dict):
raise TypeError("Invalid data type")
return data
"""
print("SIGNAL (Raw Code):")
print(code_signal)
print("\nTRANSFORM:")
print(" 1. Parse AST")
print(" 2. Extract intent from name: 'validate_user_input'")
print(" 3. Extract intent from docstring: 'Validate user input...'")
print(" 4. Extract execution from body: If statement, raise, return")
print(" 5. Map to semantic dimensions")
print(" 6. Calculate LJPW coordinates")
print()
result = harmonizer.analyze_file_content(code_signal)
if result and "validate_user_input" in result:
profile = result["validate_user_input"]["ice_result"]["ice_components"]["intent"].coordinates
print("MEANING (LJPW Coordinates):")
print(f" L={profile.love:.3f} (Love - connection, communication)")
print(f" J={profile.justice:.3f} (Justice - validation, correctness)")
print(f" P={profile.power:.3f} (Power - transformation, action)")
print(f" W={profile.wisdom:.3f} (Wisdom - understanding, knowledge)")
print()
print("INTERPRETATION:")
if profile.justice > 0.5:
print(" → High Justice: Strong validation/checking focus")
if profile.love > 0.3:
print(" → Moderate Love: User-oriented error handling")
if profile.wisdom > 0.3:
print(" → Moderate Wisdom: Understanding data types")
if profile.power > 0.3:
print(" → Moderate Power: Error raising capability")
# =========================================================================
# STAGE 2: Multi-Signal Aggregation
# =========================================================================
print("\n" + "=" * 80)
print("STAGE 2: MULTI-SIGNAL AGGREGATION")
print("=" * 80)
print("\nDemonstrates: How multiple signals aggregate into unified meaning")
print()
signals = {
"Signal 1": """
def add_simple(a, b):
'''Simple addition'''
return a + b
""",
"Signal 2": """
def validate_numeric(value):
'''Validate numeric input'''
if not isinstance(value, (int, float)):
raise TypeError("Must be numeric")
""",
"Signal 3": """
def log_operation(operation, a, b, result):
'''Log arithmetic operation'''
print(f"[LOG] {operation}({a}, {b}) = {result}")
""",
}
meanings = {}
print("SIGNALS (Three Components):")
for name, code in signals.items():
result = harmonizer.analyze_file_content(code)
if result:
func_name = list(result.keys())[0]
profile = result[func_name]["ice_result"]["ice_components"]["intent"].coordinates
meanings[name] = profile
print(f"\n{name}: {func_name}")
print(f" → L={profile.love:.3f}, J={profile.justice:.3f}, P={profile.power:.3f}, W={profile.wisdom:.3f}")
print("\n\nTRANSFORM (Aggregation):")
print(" 1. Analyze each signal individually")
print(" 2. Calculate centroid of LJPW coordinates")
print(" 3. Consider signal weights")
print()
if meanings:
# Calculate aggregate
avg_l = sum(p.love for p in meanings.values()) / len(meanings)
avg_j = sum(p.justice for p in meanings.values()) / len(meanings)
avg_p = sum(p.power for p in meanings.values()) / len(meanings)
avg_w = sum(p.wisdom for p in meanings.values()) / len(meanings)
print("MEANING (Aggregated LJPW):")
print(f" L={avg_l:.3f} (Average Love)")
print(f" J={avg_j:.3f} (Average Justice)")
print(f" P={avg_p:.3f} (Average Power)")
print(f" W={avg_w:.3f} (Average Wisdom)")
# =========================================================================
# STAGE 3: Intent-Modulated Transform
# =========================================================================
print("\n" + "=" * 80)
print("STAGE 3: INTENT-MODULATED TRANSFORMATION")
print("=" * 80)
print("\nDemonstrates: How intent signal modulates the final meaning")
print()
# Composition with intent
composed_signal = """
def secure_add(a, b):
'''
Securely add two numbers with validation and logging.
Ensures type safety and tracks all operations.
'''
if not isinstance(a, (int, float)):
raise TypeError("First argument must be numeric")
if not isinstance(b, (int, float)):
raise TypeError("Second argument must be numeric")
result = a + b
print(f"[LOG] secure_add({a}, {b}) = {result}")
return result
"""
print("SIGNAL (Composed Function):")
print(composed_signal)
print("\nTRANSFORM (Multi-Stage):")
print(" 1. Extract intent signal: 'secure_add'")
print(" → Keywords: 'secure', 'add'")
print(" → Intent LJPW: Justice (secure), Love (add)")
print()
print(" 2. Extract component signals:")
print(" → Validation logic (Justice)")
print(" → Arithmetic operation (Love)")
print(" → Logging behavior (Love)")
print()
print(" 3. Aggregate component meanings")
print(" 4. Blend with intent meaning (40% intent, 40% components)")
print(" 5. Add structural bonuses (20%)")
print()
result = harmonizer.analyze_file_content(composed_signal)
if result and "secure_add" in result:
profile = result["secure_add"]["ice_result"]["ice_components"]["intent"].coordinates
print("MEANING (Final LJPW):")
print(f" L={profile.love:.3f} (Love from addition + logging)")
print(f" J={profile.justice:.3f} (Justice from 'secure' + validation)")
print(f" P={profile.power:.3f} (Power from error raising)")
print(f" W={profile.wisdom:.3f} (Wisdom from 'secure' understanding)")
print()
print("EMERGENCE OBSERVED:")
print(" → Intent 'secure' contributes Justice and Wisdom")
print(" → Components contribute Love (add, log) and Justice (validate)")
print(" → Result: Balanced profile across multiple dimensions")
print(" → This demonstrates intent-modulated transformation!")
# =========================================================================
# STAGE 4: Signal Quality Analysis
# =========================================================================
print("\n" + "=" * 80)
print("STAGE 4: SIGNAL QUALITY COMPARISON")
print("=" * 80)
print("\nDemonstrates: How signal quality affects meaning clarity")
print()
quality_tests = [
("Low Quality", """
def do_stuff(x, y):
return x + y
"""),
("Medium Quality", """
def add_numbers(x, y):
'''Add two numbers'''
return x + y
"""),
("High Quality", """
def add_numbers_securely(x, y):
'''
Add two numbers with type validation and logging.
Ensures inputs are numeric and tracks the operation.
'''
if not isinstance(x, (int, float)):
raise TypeError("x must be numeric")
if not isinstance(y, (int, float)):
raise TypeError("y must be numeric")
result = x + y
print(f"[LOG] {x} + {y} = {result}")
return result
"""),
]
print("Signal Quality Impact on Meaning:\n")
for quality, code in quality_tests:
result = harmonizer.analyze_file_content(code)
if result:
func_name = list(result.keys())[0]
profile = result[func_name]["ice_result"]["ice_components"]["intent"].coordinates
# Calculate semantic richness
richness = sum([profile.love, profile.justice, profile.power, profile.wisdom])
dimensions_used = sum([1 for v in [profile.love, profile.justice, profile.power, profile.wisdom] if v > 0.1])
print(f"{quality:15s} Signal: {func_name}")
print(f"{'':15s} LJPW: L={profile.love:.2f}, J={profile.justice:.2f}, P={profile.power:.2f}, W={profile.wisdom:.2f}")
print(f"{'':15s} Richness: {richness:.2f} | Dimensions: {dimensions_used}/4")
print()
# =========================================================================
# SUMMARY
# =========================================================================
print("\n" + "=" * 80)
print("STM PIPELINE SUMMARY")
print("=" * 80)
print()
print("The STM Framework operates in stages:")
print()
print("1. SIGNAL CAPTURE")
print(" → Extract raw information from code")
print(" → Multiple channels: name, docstring, body, structure")
print()
print("2. SIGNAL PARSING")
print(" → Tokenize and analyze each channel")
print(" → Map to semantic dimensions (LJPW)")
print()
print("3. SIGNAL AGGREGATION")
print(" → Combine multiple signals")
print(" → Weight by importance (intent 40%, components 40%, structure 20%)")
print()
print("4. SIGNAL TRANSFORMATION")
print(" → Apply coupling (dimension amplification)")
print(" → Add structural bonuses")
print(" → Normalize to [0,1] range")
print()
print("5. MEANING GENERATION")
print(" → Output LJPW coordinates")
print(" → Calculate quality metrics")
print(" → Provide semantic interpretation")
print()
print("=" * 80)
print()
print("KEY INSIGHTS:")
print()
print("✓ Every code analysis is STM (Signal → Transform → Meaning)")
print("✓ Intent signals carry 40% of semantic weight")
print("✓ High-quality signals produce richer meanings")
print("✓ Multiple signals aggregate into unified meaning")
print("✓ Transforms can be calibrated and optimized")
print()
print("STM is the engine that powers LJPW semantic analysis! 🎯")
print("=" * 80)
if __name__ == "__main__":
demonstrate_stm_pipeline()