I Built a CBT Thought Journal in 50 Lines of Python ๐ง
Last week I caught myself thinking: "I always mess everything up." My therapist would recognize this as overgeneralization โ a cognitive distortion where one mistake becomes "everything." But I didn't want to wait for my next session to check. So I built a journal that flags distortions automatically, using a free API I deployed myself. Here's the whole thing in 50 lines. The API: CBT Thoughtโฆ
Last week, the author realized they were prone to thinking: "I always mess everything up." This thought is an example of overgeneralization, a cognitive distortion where a single mistake is perceived as a complete failure. The author wanted to check this immediately, so they created a journal that automatically flags cognitive distortions using a CBT Thought Analyzer API they deployed themselves.
The API can identify 11 cognitive distortions, 8 procrastination patterns, and 4 attachment styles. The analysis is based on evidence-based keyword pattern matching and does not require an AI model.
The journal entry is implemented in 50 lines of Python code. It includes functions to analyze thoughts for cognitive distortions and procrastination patterns, as well as to write a journal entry with automatic CBT analysis. The code uses the requests library to send thoughts to the CBT analyzer API and the datetime library to add a timestamp to each journal entry. The author tested the journal with four example thoughts and received automatic analysis results for each one.
The output shows the timestamp, the thought, and any detected distortions or procrastination patterns. For each thought, the analyst provides a confidence percentage for each detected distortion or pattern, as well as suggested reframes or interventions. If no distortions or procrastination patterns are detected, the analyst notes that the thought appears balanced. The journal provides immediate feedback on thought patterns, helping users identify and challenge cognitive distortions and procrastination tendencies.
Written by urgent.news from Dev.to's reporting โ not their text. Machine-written โ may contain errors; check the original before relying on it.