reframe / cbt_knowledge /reframing_tools.py
macayaven's picture
feat: initial implementation of the re-frame Cognitive Reframing Assistant, including core functionality, UI components, and localization support. Added .gitignore, configuration files, and initial tests.
27fd523
Raw
History Blame
7.74 kB
"""
Reframing Tools and Strategies
Functions for generating reframes, finding similar situations, and suggesting micro-actions.
"""
import random
def find_similar_situations(distortion_code: str, num_situations: int = 3) -> list[dict]:
"""
Find similar situations that involve the same cognitive distortion.
Args:
distortion_code: The cognitive distortion code
num_situations: Number of situations to return
Returns:
List of similar situation examples with reframes
"""
# Database of example situations for each distortion
situation_database = {
"AO": [
{
"situation": "Missing one deadline at work",
"distorted": "I'm a complete failure at my job",
"reframed": (
"I missed one deadline, but I've met many others. I can learn from this and "
"improve my time management."
),
},
{
"situation": "Friend cancels plans",
"distorted": "They hate me and never want to see me",
"reframed": (
"They cancelled once, which could be for many reasons. Our friendship has many "
"positive moments."
),
},
{
"situation": "Making a mistake in presentation",
"distorted": "The entire presentation was a disaster",
"reframed": (
"I made one error, but conveyed most information well. The audience was still "
"engaged."
),
},
],
"FT": [
{
"situation": "Job interview tomorrow",
"distorted": "I'll definitely mess up and embarrass myself",
"reframed": (
"Interviews can go many ways. I've prepared and have relevant experience to "
"share."
),
},
{
"situation": "Starting a new project",
"distorted": "This will never work out",
"reframed": (
"New projects have challenges and possibilities. I can adapt as I learn more."
),
},
{
"situation": "Asking someone out",
"distorted": "They'll definitely reject me",
"reframed": (
"They might say yes or no. Either way, I'm brave for trying and will "
"learn from it."
),
},
],
"CT": [
{
"situation": "Making a typo in an email",
"distorted": "This will ruin my professional reputation forever",
"reframed": (
"It's a small error that most people understand. My work quality speaks louder "
"than one typo."
),
},
{
"situation": "Feeling anxious at a party",
"distorted": "I'll have a panic attack and everyone will think I'm crazy",
"reframed": (
"I feel anxious, which is uncomfortable but manageable. I can take breaks if "
"needed."
),
},
{
"situation": "Child struggles in one subject",
"distorted": "They'll never succeed in life",
"reframed": (
"They're having difficulty in one area while doing well in others. We can get "
"help for this specific challenge."
),
},
],
"SH": [
{
"situation": "Taking a rest day",
"distorted": "I should always be productive",
"reframed": (
"I'd like to be productive, and rest is part of sustainable productivity."
),
},
{
"situation": "Asking for help",
"distorted": "I should handle everything myself",
"reframed": "I prefer independence, and asking for help when needed is a strength.",
},
{
"situation": "Making a parenting mistake",
"distorted": "I should be a perfect parent",
"reframed": (
"I aim to be a good parent, and making mistakes is part of learning and "
"growing."
),
},
],
"MW": [
{
"situation": "Boss seems quiet",
"distorted": "They think I'm doing a bad job",
"reframed": (
"They seem quiet, which could mean many things - "
"busy, tired, or thinking about something else."
),
},
{
"situation": "People laughing nearby",
"distorted": "They're laughing at me",
"reframed": (
"People are laughing, likely about their own conversation. I don't have "
"evidence it's about me."
),
},
{
"situation": "Partner is distant",
"distorted": "They're losing interest in me",
"reframed": (
"They seem distant today. They might be stressed about something. I could ask "
"how they're doing."
),
},
],
"LB": [
{
"situation": "Failed at a task",
"distorted": "I'm such a loser",
"reframed": (
"I struggled with this specific task. It doesn't define my worth as a person."
),
},
{
"situation": "Someone was rude",
"distorted": "They're a horrible person",
"reframed": (
"They acted rudely in this moment. People are complex and have good and bad "
"moments."
),
},
{
"situation": "Made a social mistake",
"distorted": "I'm so awkward and weird",
"reframed": (
"I had an awkward moment, which happens to everyone. It's one small part of "
"who I am."
),
},
],
}
# Get situations for this distortion, or return generic ones
situations = situation_database.get(distortion_code, [])
if not situations:
# Return generic situations if specific ones not found
situations = [
{
"situation": "Challenging work situation",
"distorted": "Everything is going wrong",
"reframed": "This situation has both challenges and manageable aspects.",
},
{
"situation": "Social interaction difficulty",
"distorted": "I always mess up socially",
"reframed": "I had one difficult interaction among many neutral or positive ones.",
},
{
"situation": "Personal setback",
"distorted": "I'll never recover from this",
"reframed": "This is a setback I can learn from and gradually move past.",
},
]
# Return requested number of situations (random selection if more available)
if len(situations) > num_situations:
return random.sample(situations, num_situations)
return situations