DEV Community

Cover image for How AI is used for mental health therapy

Posted on

How AI is used for mental health therapy


As the current mental health screening system relies too heavily on manual assessments, how can sentiment analysis (SA) be used to streamline the new patient screening process and reduce the abysmal 10-week long waitlist to receive help from a therapist?


  • The issue

  • What’s all this jargon?

  • New patient debrief

  • CTRL+F for symptoms

  • Further research and considerations

  • Conclusion

The issue

For a generation of young people who have de-stigmatized mental illness, we are shocked to discover that finding a therapist is as appalling as our dating culture: an arduous ghosting game. Patients wait an average of 10 weeks to see a mental health professional, just to receive a diagnosis! So if you are absolutely thirsty for attention, then there are apps commonly sponsored by YouTubers that match you with a therapist, but results may vary.

Why is it so difficult to receive help? Since we rely solely on a mental health professional to review tens of pages of medical history and questionnaires for each new patient, and with the normalization of mental illness, the supply of qualified mental health professionals cannot keep up with the volume of patients seeking help. It’s alarming that while 20% of people are mentally ill at any given time, only 44% get help, and 100% of therapists are exhausted and can’t get up.

Image descriptionLife alert meme

Treatment plan

So, how can machine learning help in this situation?

In this blog post, we’ll be exploring how a method of sentiment analysis, named entity recognition (NER) can expedite the new patient screening process for psychologists by categorizing and summarizing large amounts of unwritten responses by patients. This is a powerful information extraction tool that can potentially organize and make sense of otherwise unstructured and qualitative data that we currently rely on extremely specialized, manual labor for.

Sentiment analysis in therapy is a very new area of study, and the goal is not to replace mental health professionals, but make their jobs easier using machine learning applications. I genuinely hope there is more research in medicine and mental health so that we can not only ease the burden of overworked psychologists dealing with everyone’s Zoom fatigue but also improve accessibility to mental healthcare for those in need.

Image descriptionSource

What’s all this jargon?

Before we discuss any AI, I’d like to spend some time addressing any confusion about the terms I just rattled off.

Sentiment analysis

Sentiment analysis (SA) is the way AI is able to extract human emotions and intensity through natural language processing and context.

Machine learning pipeline

The end goal of ML is to teach the model to do a task for you. Suppose you are a weeb and want to learn Japanese (did I just expose myself?)

Image descriptionDoki Doki Literature Club reference

I like to think of sentiment analysis (SA) in machine learning akin to learning Japanese from watching 1000 hours of anime (training stage), then going to an authentic Japanese restaurant to eavesdrop on some people speaking Japanese (testing stage).

Although you can’t understand entire statements, you used named entity recognition (NER) to pick up key phrases in the “PEOPLE” category/entity like mother-in-law, husband, and “unreasonable,” and that gives us a general vibe (or sentiment) that they’re likely talking 💩.

Named entity recognition

NER is actually an information extraction algorithm that trains itself on specialized jargon and then categorizes data into pre-labeled entities or concepts.

Now that we have a better idea of what SA and NER are, let’s see how named entity recognition is useful for psychoanalysis.

New patient debrief

Using NER, therapists can quickly familiarize themselves with new patients and which intense emotions they feel, behaviors they exhibit, which mental illness they suspect they have using the keywords patients used in their questionnaires.

Take this existing implementation of NER on identifying clinical terms in patient charts by John Snow Labs (screenshot of demo below) for example: the ML model is able to identify symptoms of illness and treatment methods after learning medical jargon from the PubMD data set.

Image descriptionNER_clinical demo

Thus, for therapists, instead of reading a patient’s entire life story, a sentence like this:

“When I’m feeling anxious, I overthink and lose my appetite.”

Would be reduced to:

anxious: B-ILLNESS
appetite: I-SYMPTOM

Since some ideas are multiple words long, we use the “B” tag in front of the entity to indicate the first word in the phrase, and those that follow after with the “I” tag.


This summary is significant in automating a time-consuming step by allowing therapists to quickly grasp what’s ailing the new patient. Particularly for patients that require care from both a psychiatrist and therapist, it would be revolutionary to have a tool that ensures communication between the medical professionals are concise, detailed, and missing no information.

A technical explanation of NER

If you are the godsend thinking of implementing a sentiment analysis using NER for therapists, thank you, and please refer to SnowLab’s implementation and Medium article as a guide.

Else, we can get a general idea of how this NER model by Spark NLP (an open source Natural Language Processing library) works through the same weeb analogy:

Since you cannot possibly learn Japanese from raw anime episodes, someone has to do the manual labor of adding English subtitles before you watch it. Therefore, we have to convert sentences into a format the NER model understands, CoNLL.

So my sentence “When I am feeling anxious, I overthink and lose my appetite,” has punctuation removed, and contractions like “I’m” expanded, parts of speech tags added, and is converted into this mess:

Image description

If you’re kind of confused yet curious, you can find the list for part of speech abbreviations here and try this example for yourself using my code snippet! Essentially, what’s happening in the code is that we need to separate sentences into individual words, mark their part of speech, and entity labels like in this excerpt:

Image descriptionusing “Paraiso” from

Fortunately, the Spark NLP library has a couple built-in functions that convert sentences into this format, so you don’t need to use the NLTK one I used in my example. The functions are this tokenizer for converting sentences into individual words, and a POS tagger for identifying part of speech.

But since they do not have entity labels for psychology terms like the “emotion” and “behavior” tags I used in my example, we’d need to find a library based on the The Diagnostic and Statistical Manual of Mental Disorders (DSM) that’ll be used to identify mental illnesses and their symptoms in a piece of text.

Finally, now that the training data is pre-processed, data scientists can now train the model, test it, and let therapists use it! Let me know when it happens! 😉

Image descriptiondraw an owl meme

CTRL+F for symptoms

Or CMD+F for you Mac users.

On the other hand, after identifying the symptoms, emotions, and behaviors of the patient from their questionnaire, we can do a reverse-search to look for more specific information. Let’s say the therapist noticed a patient used sleeping medication and wanted more context and why.

In SnowLab’s implementation, for example, they wrote a search function called get_clinical_entities that finds all mentions of medications for 100 patients, as well as specifications, if any, about the quantity and frequency the medication is consumed. The location of the sentence in the overall piece is also recorded, to locate the information easier.

Image descriptionfrom line 51 of NER_clinical


Once this information is identified, therapists can request data scientists to help them analyze and visualize trends in patient medical histories, and whether treatments are effective or not.

For example, a potential usage of analyzing a patient’s medication history for psychiatrists is monitoring whether the medicine they’re taking has improved their condition. It would be a lot to keep track of the condition of the numerous psychiatric patients, how dosages of several medications affect them, and whether there are unexpected side effects over time, but detecting anomalies and finding trends is what machine learning is great at.

Moreover, if certain medication is proven to be successful for a statistically significant number of patients with ADHD who experience the symptoms “x, y, and z,” it is crucial that this discovery is shared with a medical professional. While additional research is required, ML can expedite this cross-referencing process for psychiatrists. One possible application is an attempt at unbiased data reporting by generating a list of ADHD patients taking this medication, detailing whether they’ve recovered or not, the duration of consumption, and the side effects they experienced.

Further research and considerations

There are many more ways sentiment analysis would improve the quality of therapy and assist mental health professionals in treating more patients. In addition to the ways textual information extraction SAs determine emotion in unstructured, self-reported medical reports, other forms of sentiment analysis could improve other facets of mental healthcare, such as:

  • A classifier, based on various parameters describing patient condition, of whether a patient could be in a bad mental state and harm themselves. A support vector machine (SVM) is allegedly one of the best ML approaches that predict the polarity of a sentiment, according to the 3rd reference (“Emotion AI-Driven Sentiment Analysis”)

  • Somehow quantifying a severity score of a patient’s condition to indicate they’re in critical condition and get them help faster

  • Due to the personalized nature of therapy, it can also be difficult to get definitive feedback from patients. In this study, researchers have been trying to identify which words exchanged between therapist and patient were most effective at treating their illness using natural language processing (NLP). This is the umbrella sentiment analysis falls under

  • How to normalize qualitative data, like subjective description of emotions, so that a patient who describes their emotions more animatedly does not receive a biased score over a patient who keeps things to themselves


Whether you are a medical professional, data scientist, or someone with an interest, I thank you for taking the time to consider all of these ways sentiment analysis can benefit therapy. I believe your work and contribution to research in using machine learning to improve the quality of healthcare is noble and greatly appreciated in these times when we’re experiencing a mental health crisis and a shortage in healthcare workers.

Image descriptioncapoo

I’m grateful that the two most important people in my life have access to mental healthcare, and I hope that with further interdisciplinary research, this accessibility is extended to more people’s loved ones.

Discussion (2)

dianriek profile image
dianriek • Edited on

I don't know what causes my distrust of new therapies. I have heard about the virtual pediatrician, but I believe that artificial intelligence will never replace your child's real examination, where the pediatrician checks the child's breathing, heart rate, and more. Even when my child needs respiratory services, I take him to a private clinic because I believe you have to pay a lot of attention to your child's health. As for mental health, if necessary, I can hire a therapist who will do therapy via video link, but even so, it will be a live person. It's hard for people to move on to something new and trust their condition to a robot.

dangerous profile image
Tommy DANGerous

This is very much needed.