File size: 2,497 Bytes
b70e5b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from typing import Dict, Any


class ContextlessMeaningEngine:
    """
    Contextless Meaning Engine v0.1
    - Takes a single input string.
    - Produces a deterministic meaning-state.
    - Ignores any history or conversation context.
    """

    def __init__(self, config: Dict[str, Any] = None):
        self.config = config or {}

    @classmethod
    def from_pretrained(cls, repo_path: str = ".", **kwargs):
        """Simple loader that reads config.json from the repo directory."""
        try:
            with open(f"{repo_path}/config.json", "r", encoding="utf-8") as f:
                config = json.load(f)
        except FileNotFoundError:
            config = {}
        return cls(config=config)

    def __call__(self, text: str) -> Dict[str, Any]:
        """Core engine: takes ONLY the current input text."""
        cleaned = text.strip()
        tone = self._infer_tone(cleaned)
        intent = self._infer_intent(cleaned)
        complexity = self._infer_complexity(cleaned)
        keywords = self._extract_keywords(cleaned)

        return {
            "input": cleaned,
            "meaning_state": {
                "tone": tone,
                "intent": intent,
                "complexity": complexity,
                "keywords": keywords,
            },
        }

    def _infer_tone(self, text: str) -> str:
        text_lower = text.lower()
        if any(w in text_lower for w in ["thank", "appreciate", "grateful"]):
            return "appreciative"
        if any(w in text_lower for w in ["angry", "upset", "frustrated"]):
            return "frustrated"
        if "?" in text:
            return "inquisitive"
        return "neutral"

    def _infer_intent(self, text: str) -> str:
        text_lower = text.lower()
        if any(w in text_lower for w in ["how", "what", "why", "where"]):
            return "seeking_information"
        if any(w in text_lower for w in ["please", "could you", "can you"]):
            return "request"
        return "statement"

    def _infer_complexity(self, text: str) -> str:
        length = len(text.split())
        if length <= 5:
            return "simple"
        if length <= 20:
            return "moderate"
        return "complex"

    def _extract_keywords(self, text: str):
        words = [w.strip(".,!?").lower() for w in text.split()]
        stopwords = {"the", "and", "or", "a", "an", "to", "of", "in", "on", "for"}
        return [w for w in words if w and w not in stopwords]