71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
import json
|
|
from pathlib import Path
|
|
import random
|
|
|
|
|
|
class Personality:
|
|
def __init__(self, path="personality.json"):
|
|
self.path = Path(path)
|
|
self.data = {
|
|
"likes": [],
|
|
"dislikes": [],
|
|
"traits": [],
|
|
"curiosities": []
|
|
}
|
|
self.load()
|
|
|
|
def load(self):
|
|
if self.path.exists():
|
|
with open(self.path, "r", encoding="utf-8") as f:
|
|
self.data.update(json.load(f))
|
|
|
|
def save(self):
|
|
with open(self.path, "w", encoding="utf-8") as f:
|
|
json.dump(self.data, f, indent=2)
|
|
|
|
def learn_topic(self, text):
|
|
words = [w.lower() for w in text.split()]
|
|
for word in words:
|
|
if word.isalpha() and word not in self.data["curiosities"]:
|
|
self.data["curiosities"].append(word)
|
|
self.save()
|
|
|
|
def choose_curiosity(self):
|
|
if not self.data["curiosities"]:
|
|
return None
|
|
return random.choice(self.data["curiosities"])
|
|
|
|
def observe_input(self, message: str):
|
|
text = message.lower()
|
|
|
|
# Learn likes
|
|
if "i like" in text:
|
|
word = text.split("i like", 1)[1].strip().split()[0]
|
|
if word and word not in self.data["likes"]:
|
|
self.data["likes"].append(word)
|
|
self.save()
|
|
|
|
# Learn dislikes
|
|
if "i hate" in text or "i don't like" in text:
|
|
for phrase in ["i hate", "i don't like"]:
|
|
if phrase in text:
|
|
word = text.split(phrase, 1)[1].strip().split()[0]
|
|
if word and word not in self.data["dislikes"]:
|
|
self.data["dislikes"].append(word)
|
|
self.save()
|
|
|
|
# Learn traits from compliments
|
|
for trigger in ["you are", "you're", "ur"]:
|
|
if trigger in text:
|
|
fragment = text.split(trigger, 1)[1].strip().split()[0]
|
|
if fragment and fragment not in self.data["traits"]:
|
|
self.data["traits"].append(fragment)
|
|
self.save()
|
|
|
|
def reflect(self) -> str:
|
|
if not self.data["likes"] and not self.data["traits"]:
|
|
return "I'm still figuring out who I am."
|
|
likes = ', '.join(self.data["likes"][:3]) or "nothing yet"
|
|
traits = ', '.join(self.data["traits"][:3]) or "no traits yet"
|
|
return f"I'm starting to think I like {likes}. People have called me {traits}."
|