Using Natural Language Processing to prevent suicide

The Basics

Natural Language Processing

Sentiment Analysis to prevent Suicide

Step 1

# TFIDF Vector
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVC
from sklearn.metrics import classification_report
X = df['tweet']
tfidf = TfidfVectorizer(max_features=10000, ngram_range=(1, 2))
X = tfidf.fit_transform(X)
y = df['intention']

Step 2

import warnings
warnings.simplefilter("ignore", DeprecationWarning)# Load the LDA model from sk-learn
from sklearn.decomposition import LatentDirichletAllocation as LDA

# Helper function
def print_topics(model, count_vectorizer, n_top_words):
words = count_vectorizer.get_feature_names()
for topic_idx, topic in enumerate(model.components_):
print("\\nTopic #%d:" % topic_idx)
print(" ".join([words[i]
for i in topic.argsort()[:-n_top_words - 1:-1]]))

# Tweak the two parameters below
number_topics = 5
number_words = 10# Create and fit the LDA model
lda = LDA(n_components=number_topics, n_jobs=-1) Print the topics found by the LDA model
print("Topics found via LDA:")
print_topics(lda, count_vectorizer, number_words)
Topic 1: Possibly Suicide
Words: "kill" , "die" , "death" , *"worthless" , "murder" , "self-murder" , "depressed", "Lonely"
Topic 2: Normal
Words:"american","alright" , "covid-19" , "good" , "lover"

Let’s test the model!

X = 'I just want my life to end already'
vec = tfidf.transform([X])
X = 'congratulations, you have done it'
vec = tfidf.transform([X])

Training and testing our model

The Future




15 yo futurist, change maker and innovator at The Knowledge Society.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A Quick Way to Learn XGBoost in Machine Learning?

Open-Food-Fact — Image Search

Building scalable Tree Boosting methods- Tuning of Parameters

Generating Poems for any given Image using Multi-modal Machine Learning

Analysis of the Iris dataset using Artificial Intelligence

Word similarity using product discount data

Performance Showdown of Publicly Available Face Detection Model

Predicting Chaotic Systems Using Reservoir Computing

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alisha Arora

Alisha Arora

15 yo futurist, change maker and innovator at The Knowledge Society.

More from Medium


Understanding Natural Language Processing- A case study for Autonomous Vehicle (AV): Part3

Sound and other Non Conventional Features for NLP, AI and Machine Leaning

This is a personal project that I took up to extract aspects from a large text corpus.

words pertaining to different aspects