DSLO's picture
Add contextless meaning engine implementation v0.1
b70e5b5 verified
Raw
History Blame Contribute Delete
2.5 kB
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]