Add contextless meaning engine implementation v0.1
Browse files
model.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from typing import Dict, Any
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class ContextlessMeaningEngine:
|
| 6 |
+
"""
|
| 7 |
+
Contextless Meaning Engine v0.1
|
| 8 |
+
- Takes a single input string.
|
| 9 |
+
- Produces a deterministic meaning-state.
|
| 10 |
+
- Ignores any history or conversation context.
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
def __init__(self, config: Dict[str, Any] = None):
|
| 14 |
+
self.config = config or {}
|
| 15 |
+
|
| 16 |
+
@classmethod
|
| 17 |
+
def from_pretrained(cls, repo_path: str = ".", **kwargs):
|
| 18 |
+
"""Simple loader that reads config.json from the repo directory."""
|
| 19 |
+
try:
|
| 20 |
+
with open(f"{repo_path}/config.json", "r", encoding="utf-8") as f:
|
| 21 |
+
config = json.load(f)
|
| 22 |
+
except FileNotFoundError:
|
| 23 |
+
config = {}
|
| 24 |
+
return cls(config=config)
|
| 25 |
+
|
| 26 |
+
def __call__(self, text: str) -> Dict[str, Any]:
|
| 27 |
+
"""Core engine: takes ONLY the current input text."""
|
| 28 |
+
cleaned = text.strip()
|
| 29 |
+
tone = self._infer_tone(cleaned)
|
| 30 |
+
intent = self._infer_intent(cleaned)
|
| 31 |
+
complexity = self._infer_complexity(cleaned)
|
| 32 |
+
keywords = self._extract_keywords(cleaned)
|
| 33 |
+
|
| 34 |
+
return {
|
| 35 |
+
"input": cleaned,
|
| 36 |
+
"meaning_state": {
|
| 37 |
+
"tone": tone,
|
| 38 |
+
"intent": intent,
|
| 39 |
+
"complexity": complexity,
|
| 40 |
+
"keywords": keywords,
|
| 41 |
+
},
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
def _infer_tone(self, text: str) -> str:
|
| 45 |
+
text_lower = text.lower()
|
| 46 |
+
if any(w in text_lower for w in ["thank", "appreciate", "grateful"]):
|
| 47 |
+
return "appreciative"
|
| 48 |
+
if any(w in text_lower for w in ["angry", "upset", "frustrated"]):
|
| 49 |
+
return "frustrated"
|
| 50 |
+
if "?" in text:
|
| 51 |
+
return "inquisitive"
|
| 52 |
+
return "neutral"
|
| 53 |
+
|
| 54 |
+
def _infer_intent(self, text: str) -> str:
|
| 55 |
+
text_lower = text.lower()
|
| 56 |
+
if any(w in text_lower for w in ["how", "what", "why", "where"]):
|
| 57 |
+
return "seeking_information"
|
| 58 |
+
if any(w in text_lower for w in ["please", "could you", "can you"]):
|
| 59 |
+
return "request"
|
| 60 |
+
return "statement"
|
| 61 |
+
|
| 62 |
+
def _infer_complexity(self, text: str) -> str:
|
| 63 |
+
length = len(text.split())
|
| 64 |
+
if length <= 5:
|
| 65 |
+
return "simple"
|
| 66 |
+
if length <= 20:
|
| 67 |
+
return "moderate"
|
| 68 |
+
return "complex"
|
| 69 |
+
|
| 70 |
+
def _extract_keywords(self, text: str):
|
| 71 |
+
words = [w.strip(".,!?").lower() for w in text.split()]
|
| 72 |
+
stopwords = {"the", "and", "or", "a", "an", "to", "of", "in", "on", "for"}
|
| 73 |
+
return [w for w in words if w and w not in stopwords]
|