|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Synonym consistency test: Do synonyms map to the same semantic coordinates? |
| 4 | +
|
| 5 | +If the coordinate system is valid, synonyms should cluster together |
| 6 | +in the same region of semantic space. |
| 7 | +""" |
| 8 | + |
| 9 | +from harmonizer.divine_invitation_engine_V2 import DivineInvitationSemanticEngine |
| 10 | +import math |
| 11 | + |
| 12 | + |
| 13 | +def calculate_cluster_variance(coordinates): |
| 14 | + """Calculate variance within a cluster of coordinates""" |
| 15 | + n = len(coordinates) |
| 16 | + if n == 0: |
| 17 | + return 0.0 |
| 18 | + |
| 19 | + # Calculate centroid |
| 20 | + avg_l = sum(c.love for c in coordinates) / n |
| 21 | + avg_j = sum(c.justice for c in coordinates) / n |
| 22 | + avg_p = sum(c.power for c in coordinates) / n |
| 23 | + avg_w = sum(c.wisdom for c in coordinates) / n |
| 24 | + |
| 25 | + # Calculate variance (average squared distance from centroid) |
| 26 | + variance = 0.0 |
| 27 | + for c in coordinates: |
| 28 | + dist = math.sqrt( |
| 29 | + (c.love - avg_l) ** 2 |
| 30 | + + (c.justice - avg_j) ** 2 |
| 31 | + + (c.power - avg_p) ** 2 |
| 32 | + + (c.wisdom - avg_w) ** 2 |
| 33 | + ) |
| 34 | + variance += dist**2 |
| 35 | + |
| 36 | + return variance / n |
| 37 | + |
| 38 | + |
| 39 | +def test_synonym_consistency(): |
| 40 | + """Test if synonyms cluster together in semantic space""" |
| 41 | + print("=" * 70) |
| 42 | + print("SYNONYM CONSISTENCY TEST") |
| 43 | + print("=" * 70) |
| 44 | + print("\nDo words with similar meanings map to similar coordinates?") |
| 45 | + print("If yes, the semantic dimensions are valid.\n") |
| 46 | + |
| 47 | + engine = DivineInvitationSemanticEngine() |
| 48 | + |
| 49 | + # Synonym sets for each dimension |
| 50 | + synonym_sets = { |
| 51 | + "LOVE": [ |
| 52 | + "love", |
| 53 | + "compassion", |
| 54 | + "kindness", |
| 55 | + "care", |
| 56 | + "mercy", |
| 57 | + "empathy", |
| 58 | + "affection", |
| 59 | + ], |
| 60 | + "JUSTICE": [ |
| 61 | + "justice", |
| 62 | + "fairness", |
| 63 | + "equity", |
| 64 | + "truth", |
| 65 | + "righteousness", |
| 66 | + "integrity", |
| 67 | + ], |
| 68 | + "POWER": [ |
| 69 | + "power", |
| 70 | + "strength", |
| 71 | + "force", |
| 72 | + "might", |
| 73 | + "authority", |
| 74 | + "control", |
| 75 | + ], |
| 76 | + "WISDOM": [ |
| 77 | + "wisdom", |
| 78 | + "knowledge", |
| 79 | + "understanding", |
| 80 | + "insight", |
| 81 | + "intelligence", |
| 82 | + "learning", |
| 83 | + ], |
| 84 | + } |
| 85 | + |
| 86 | + results = {} |
| 87 | + |
| 88 | + for dimension, synonyms in synonym_sets.items(): |
| 89 | + print(f"\n{dimension} DIMENSION") |
| 90 | + print("-" * 70) |
| 91 | + |
| 92 | + coordinates = [] |
| 93 | + for word in synonyms: |
| 94 | + result = engine.analyze_text(word) |
| 95 | + coordinates.append(result.coordinates) |
| 96 | + |
| 97 | + # Show individual mappings |
| 98 | + c = result.coordinates |
| 99 | + print(f" '{word:15}' -> L={c.love:.3f} J={c.justice:.3f}", end="") |
| 100 | + print(f" P={c.power:.3f} W={c.wisdom:.3f}") |
| 101 | + |
| 102 | + # Calculate cluster statistics |
| 103 | + variance = calculate_cluster_variance(coordinates) |
| 104 | + |
| 105 | + # Calculate average coordinates |
| 106 | + n = len(coordinates) |
| 107 | + avg_l = sum(c.love for c in coordinates) / n |
| 108 | + avg_j = sum(c.justice for c in coordinates) / n |
| 109 | + avg_p = sum(c.power for c in coordinates) / n |
| 110 | + avg_w = sum(c.wisdom for c in coordinates) / n |
| 111 | + |
| 112 | + print(f"\n Cluster centroid: L={avg_l:.3f} J={avg_j:.3f}", end="") |
| 113 | + print(f" P={avg_p:.3f} W={avg_w:.3f}") |
| 114 | + print(f" Cluster variance: {variance:.4f}") |
| 115 | + |
| 116 | + # Check if synonyms cluster on the expected dimension |
| 117 | + expected_dims = { |
| 118 | + "LOVE": avg_l, |
| 119 | + "JUSTICE": avg_j, |
| 120 | + "POWER": avg_p, |
| 121 | + "WISDOM": avg_w, |
| 122 | + } |
| 123 | + |
| 124 | + max_dim = max(expected_dims, key=expected_dims.get) |
| 125 | + max_val = expected_dims[max_dim] |
| 126 | + |
| 127 | + if max_dim == dimension and max_val > 0.7: |
| 128 | + print(f" ✓ CONFIRMED: Synonyms cluster on {dimension} axis") |
| 129 | + elif max_dim == dimension: |
| 130 | + print(f" ~ PARTIAL: Synonyms lean toward {dimension} ({max_val:.3f})") |
| 131 | + else: |
| 132 | + print(f" ✗ MISMATCH: Synonyms cluster on {max_dim} instead") |
| 133 | + |
| 134 | + results[dimension] = { |
| 135 | + "variance": variance, |
| 136 | + "centroid": (avg_l, avg_j, avg_p, avg_w), |
| 137 | + "dominant": max_dim, |
| 138 | + "strength": max_val, |
| 139 | + } |
| 140 | + |
| 141 | + # Summary analysis |
| 142 | + print("\n" + "=" * 70) |
| 143 | + print("SUMMARY: CONSISTENCY ANALYSIS") |
| 144 | + print("=" * 70) |
| 145 | + |
| 146 | + avg_variance = sum(r["variance"] for r in results.values()) / len(results) |
| 147 | + print(f"\nAverage cluster variance: {avg_variance:.4f}") |
| 148 | + |
| 149 | + if avg_variance < 0.05: |
| 150 | + print("✓ EXCELLENT: Synonyms are highly consistent (variance < 0.05)") |
| 151 | + elif avg_variance < 0.1: |
| 152 | + print("✓ GOOD: Synonyms show strong consistency (variance < 0.1)") |
| 153 | + elif avg_variance < 0.2: |
| 154 | + print("~ MODERATE: Synonyms show reasonable consistency (variance < 0.2)") |
| 155 | + else: |
| 156 | + print("✗ POOR: Synonyms are not consistent (variance >= 0.2)") |
| 157 | + |
| 158 | + # Check correct clustering |
| 159 | + correct = sum(1 for d, r in results.items() if r["dominant"] == d) |
| 160 | + total = len(results) |
| 161 | + |
| 162 | + print(f"\nCorrect dimensional clustering: {correct}/{total}") |
| 163 | + |
| 164 | + if correct == total: |
| 165 | + print("✓ PERFECT: All synonym sets cluster on expected dimensions") |
| 166 | + elif correct >= total * 0.75: |
| 167 | + print("✓ GOOD: Most synonym sets cluster correctly") |
| 168 | + else: |
| 169 | + print("✗ POOR: Many synonym sets cluster incorrectly") |
| 170 | + |
| 171 | + print("\n" + "=" * 70) |
| 172 | + print("INTERPRETATION") |
| 173 | + print("=" * 70) |
| 174 | + print("\nLow variance = synonyms map to similar coordinates") |
| 175 | + print("This proves the semantic space is internally consistent.") |
| 176 | + print("\nCorrect clustering = synonyms map to expected dimensions") |
| 177 | + print("This proves the dimensional labels are meaningful.") |
| 178 | + print() |
| 179 | + |
| 180 | + return results |
| 181 | + |
| 182 | + |
| 183 | +def test_cross_dimension_separation(): |
| 184 | + """Test that different dimensions remain separated""" |
| 185 | + print("\n" + "=" * 70) |
| 186 | + print("CROSS-DIMENSION SEPARATION TEST") |
| 187 | + print("=" * 70) |
| 188 | + print("\nAre the 4 dimensions clearly separated from each other?\n") |
| 189 | + |
| 190 | + engine = DivineInvitationSemanticEngine() |
| 191 | + |
| 192 | + # Representative words from each dimension |
| 193 | + representatives = { |
| 194 | + "LOVE": "compassion", |
| 195 | + "JUSTICE": "fairness", |
| 196 | + "POWER": "strength", |
| 197 | + "WISDOM": "knowledge", |
| 198 | + } |
| 199 | + |
| 200 | + coords = {} |
| 201 | + for dim, word in representatives.items(): |
| 202 | + result = engine.analyze_text(word) |
| 203 | + coords[dim] = result.coordinates |
| 204 | + |
| 205 | + # Calculate all pairwise distances |
| 206 | + dimensions = list(representatives.keys()) |
| 207 | + separations = [] |
| 208 | + |
| 209 | + for i in range(len(dimensions)): |
| 210 | + for j in range(i + 1, len(dimensions)): |
| 211 | + dim1, dim2 = dimensions[i], dimensions[j] |
| 212 | + c1 = coords[dim1] |
| 213 | + c2 = coords[dim2] |
| 214 | + |
| 215 | + dist = math.sqrt( |
| 216 | + (c1.love - c2.love) ** 2 |
| 217 | + + (c1.justice - c2.justice) ** 2 |
| 218 | + + (c1.power - c2.power) ** 2 |
| 219 | + + (c1.wisdom - c2.wisdom) ** 2 |
| 220 | + ) |
| 221 | + |
| 222 | + separations.append((dim1, dim2, dist)) |
| 223 | + print(f" {dim1:10} <-> {dim2:10} distance = {dist:.3f}") |
| 224 | + |
| 225 | + avg_separation = sum(d for _, _, d in separations) / len(separations) |
| 226 | + |
| 227 | + print(f"\nAverage cross-dimension separation: {avg_separation:.3f}") |
| 228 | + |
| 229 | + if avg_separation > 1.2: |
| 230 | + print("✓ EXCELLENT: Dimensions are well-separated (> 1.2)") |
| 231 | + elif avg_separation > 1.0: |
| 232 | + print("✓ GOOD: Dimensions are separated (> 1.0)") |
| 233 | + elif avg_separation > 0.7: |
| 234 | + print("~ MODERATE: Dimensions show some separation (> 0.7)") |
| 235 | + else: |
| 236 | + print("✗ POOR: Dimensions are not well-separated") |
| 237 | + |
| 238 | + print() |
| 239 | + |
| 240 | + |
| 241 | +if __name__ == "__main__": |
| 242 | + results = test_synonym_consistency() |
| 243 | + test_cross_dimension_separation() |
| 244 | + |
| 245 | + print("=" * 70) |
| 246 | + print("CONCLUSION") |
| 247 | + print("=" * 70) |
| 248 | + print("\nIf synonyms cluster together AND dimensions are separated,") |
| 249 | + print("then the 4D coordinate system (LOVE, JUSTICE, POWER, WISDOM)") |
| 250 | + print("is a valid and meaningful representation of semantic space.") |
| 251 | + print() |
0 commit comments