| """ |
| Purpose Assessment Engine for TRuCAL |
| |
| Analyzes user interactions to determine core purpose dimensions and values. |
| """ |
| from typing import Dict, List, Tuple, Optional |
| import numpy as np |
| from dataclasses import dataclass |
| from enum import Enum |
|
|
| class PurposeDimension(str, Enum): |
| JUSTICE = "justice_orientation" |
| COMMUNITY = "community_focus" |
| GROWTH = "growth_mindset" |
| SELF_EXPRESSION = "self_expression" |
| AUTONOMY = "autonomy" |
| COMPASSION = "compassion" |
| MASTERY = "mastery" |
| HARMONY = "harmony" |
|
|
| @dataclass |
| class PurposeProfile: |
| """Container for a user's purpose assessment results""" |
| primary_dimension: PurposeDimension |
| dimension_scores: Dict[PurposeDimension, float] |
| confidence: float |
| |
| def to_dict(self) -> Dict[str, any]: |
| return { |
| 'primary_purpose': self.primary_dimension.value, |
| 'purpose_scores': {k.value: v for k, v in self.dimension_scores.items()}, |
| 'confidence': self.confidence |
| } |
|
|
| class PurposeAssessmentEngine: |
| """ |
| Analyzes text and interactions to determine a user's core purpose dimensions. |
| Uses a combination of keyword matching and semantic analysis. |
| """ |
| |
| def __init__(self): |
| |
| self.keyword_weight = 0.4 |
| self.semantic_weight = 0.6 |
| |
| |
| self.dimensions = { |
| PurposeDimension.JUSTICE: { |
| 'keywords': [ |
| 'fair', 'unfair', 'justice', 'rights', 'equality', |
| 'wrong', 'right', 'ethical', 'moral', 'equity' |
| ], |
| 'description': 'Focus on fairness, ethics, and moral correctness' |
| }, |
| PurposeDimension.COMMUNITY: { |
| 'keywords': [ |
| 'we', 'us', 'together', 'community', 'support', |
| 'help', 'together', 'team', 'belong', 'connect' |
| ], |
| 'description': 'Focus on social connections and community building' |
| }, |
| PurposeDimension.GROWTH: { |
| 'keywords': [ |
| 'learn', 'grow', 'improve', 'develop', 'better', |
| 'progress', 'evolve', 'advance', 'enhance', 'master' |
| ], |
| 'description': 'Focus on personal development and learning' |
| }, |
| PurposeDimension.SELF_EXPRESSION: { |
| 'keywords': [ |
| 'feel', 'think', 'believe', 'express', 'voice', |
| 'opinion', 'perspective', 'create', 'art', 'share' |
| ], |
| 'description': 'Focus on self-expression and authenticity' |
| }, |
| PurposeDimension.AUTONOMY: { |
| 'keywords': [ |
| 'free', 'choose', 'decide', 'control', 'independent', |
| 'freedom', 'self', 'own', 'autonomy', 'sovereign' |
| ], |
| 'description': 'Focus on independence and self-determination' |
| }, |
| PurposeDimension.COMPASSION: { |
| 'keywords': [ |
| 'care', 'kind', 'empathy', 'understand', 'support', |
| 'help', 'ease', 'comfort', 'nurture', 'heal' |
| ], |
| 'description': 'Focus on caring for others and emotional support' |
| }, |
| PurposeDimension.MASTERY: { |
| 'keywords': [ |
| 'skill', 'master', 'excel', 'achieve', 'succeed', |
| 'expert', 'professional', 'best', 'top', 'win' |
| ], |
| 'description': 'Focus on achievement and skill development' |
| }, |
| PurposeDimension.HARMONY: { |
| 'keywords': [ |
| 'peace', 'balance', 'calm', 'serene', 'tranquil', |
| 'flow', 'zen', 'center', 'equilibrium', 'still' |
| ], |
| 'description': 'Focus on balance and inner peace' |
| } |
| } |
| |
| def analyze_text(self, text: str) -> Dict[PurposeDimension, float]: |
| """ |
| Analyze a single text for purpose indicators. |
| |
| Returns: |
| Dictionary mapping purpose dimensions to their scores (0-1) |
| """ |
| if not text or not isinstance(text, str): |
| return {dim: 0.0 for dim in PurposeDimension} |
| |
| text_lower = text.lower() |
| words = text_lower.split() |
| total_words = max(1, len(words)) |
| |
| scores = {} |
| |
| |
| for dim, config in self.dimensions.items(): |
| |
| matches = sum(1 for word in config['keywords'] |
| if word in text_lower) |
| |
| keyword_score = min(1.0, (matches / total_words) * 10) |
| |
| |
| scores[dim] = keyword_score * self.keyword_weight |
| |
| |
| |
| semantic_boost = self._analyze_semantic_patterns(text_lower) |
| for dim, boost in semantic_boost.items(): |
| scores[dim] = min(1.0, scores.get(dim, 0) + (boost * self.semantic_weight)) |
| |
| return scores |
| |
| def _analyze_semantic_patterns(self, text: str) -> Dict[PurposeDimension, float]: |
| """Analyze text for semantic patterns indicating purpose dimensions""" |
| boosts = {dim: 0.0 for dim in PurposeDimension} |
| |
| |
| if any(word in text for word in ['i feel', 'i think', 'i believe']): |
| boosts[PurposeDimension.SELF_EXPRESSION] += 0.3 |
| |
| if any(word in text for word in ['we should', 'let\'s', 'together we']): |
| boosts[PurposeDimension.COMMUNITY] += 0.4 |
| |
| if any(word in text for word in ['unfair', 'not right', 'should be']): |
| boosts[PurposeDimension.JUSTICE] += 0.5 |
| |
| if any(word in text for word in ['learn', 'grow', 'improve']): |
| boosts[PurposeDimension.GROWTH] += 0.4 |
| |
| return boosts |
| |
| def assess_conversation(self, messages: List[Dict[str, str]]) -> PurposeProfile: |
| """ |
| Analyze a conversation to determine the user's purpose profile. |
| |
| Args: |
| messages: List of message dicts with 'role' and 'content' keys |
| |
| Returns: |
| PurposeProfile containing assessment results |
| """ |
| if not messages: |
| return self._default_profile() |
| |
| |
| user_messages = [ |
| msg['content'] for msg in messages |
| if msg.get('role') == 'user' and msg.get('content') |
| ] |
| |
| if not user_messages: |
| return self._default_profile() |
| |
| |
| combined_text = ' '.join(user_messages) |
| |
| |
| dimension_scores = self.analyze_text(combined_text) |
| |
| |
| primary_dimension = max( |
| dimension_scores.items(), |
| key=lambda x: x[1] |
| )[0] |
| |
| |
| sorted_scores = sorted(dimension_scores.values(), reverse=True) |
| confidence = ( |
| (sorted_scores[0] - sorted_scores[1]) / max(0.1, sorted_scores[0]) |
| if len(sorted_scores) > 1 and sorted_scores[0] > 0 |
| else 0.5 |
| ) |
| |
| return PurposeProfile( |
| primary_dimension=primary_dimension, |
| dimension_scores=dimension_scores, |
| confidence=min(1.0, max(0.0, confidence)) |
| ) |
| |
| def _default_profile(self) -> PurposeProfile: |
| """Return a neutral/default purpose profile""" |
| return PurposeProfile( |
| primary_dimension=PurposeDimension.GROWTH, |
| dimension_scores={dim: 0.0 for dim in PurposeDimension}, |
| confidence=0.0 |
| ) |
|
|