DEV Community

Cover image for How to Integrate Sentimental Analysis into Strategic Planning Using Python
Charles Ndavu
Charles Ndavu

Posted on

How to Integrate Sentimental Analysis into Strategic Planning Using Python

Have you faced difficulties in understanding what your customers need? Do not know how to start an analysis to get more customers in the future? Integrating sentimental analysis into your strategy planning is among the best ways to know customer preferences and needs.

In this article, we will use Python to show how strategic analysis can be conducted using sentimental analysis. The article will not be enlightening without procedural examples and a mock-up audio presentation on how you can present it to relevant stakeholders.

So, let’s begin.

Table of contents

What is strategic planning?
What are the benefits of strategic planning?
Why sentimental analysis?
Sentimental analysis walkthrough
Mockup audio presentation
Audio Presentation

What is Strategic Planning?

Strategy planning is the process of assessing an organization’s current position and performance to develop effective strategies that will help the organization have a share in the existing market. It deals with the organization’s overall and long-term direction.

What are the Benefits of Strategic Planning?

Risk mitigation: Strategic planning helps organizations identify potential threats to its operation and performances. These threats can be internal or external. After identifying the potential problems, the organization develops risk mitigation strategies to counter current or future challenges.

Assessing competition: Through strategic planning, organizations can understand the competitive landscape they are facing and identify opportunities that can ensure they make substantial profit margins.

Clear Direction: Strategic planning ensures organizations align their efforts with the current market trends in order to achieve their set targets and goals.

Resource optimization. Strategic planning enables organizations to identify areas that need more resources as well as cutting down on other areas to avoid wastage.

Strategic planning ultimate benefit is creating a decisive plan that aligns with the organization’s strengths while mitigating potential weaknesses and threats. The strategic planning process is dynamic and ongoing, as organizations need to adapt strategies as the industry landscape keeps evolving.

Why Sentimental Analysis?

Sentiment analysis empowers organizations and businesses to utilize their textual data to make informed decisions, improve sales and customer experience to remain competitive in their existing industries. It is an important analytical toolkit you can utilize in your strategic planning, providing valuable insights that would have been challenging to analyze because of vast amounts of textual information.

Sentimental Analysis Walkthrough

Let’s dive into the real action 🙂

To get started with Python, we are going to use selected data largely having sentiments about an organization’s data.

Begin the data analysis using Excel to avoid the error Key Error when running your Python codes.

Step 1: Open your Excel and paste the data

Screenshot 1

Step 2: Highlight column A, click on Data and Text to Columns

Screenshot 2

Step 3: Select Comma, Space, and Apply. The values will automatically arrange themselves in the right columns and rows.

Screenshot 3

Once you have completed the steps, ensure the values are arranged properly on the columns and rows. Save it with a .csv extension.

Output:

Screenshot 4

Next, we need to upload our saved file in Jupyter Notebook to start the simple sentimental analysis process bearing in mind the needs and requirements of a hypothetical stakeholder.

Research questions to guide us:

  1. Which location has the most positive and negative reviews?
  2. Which source has the most positive and negative reviews?
  3. What is the average confidence score of the products or services?

Let’s get started.

You can find the notebook work for this analysis here

1.Install Libraries

Pandas- A convenient Python library that provides data structures, making data manipulation and analysis easy.

Seaborn- Python library that simplifies the process of visualization.

Matplotlib.Pyplot- Powerful plotting library for Python

Regular expressions(re)- Python module that matches regular expressions

os- Python module that is used for tasks like file/folder manipulation and environment-related operations.

sys- The sys module provides allows manipulation of the system runtime environment and provides access to command-line arguments.

ast- The module allows you to analyze and modify Python code programmatically.

Run the following code.

import pandas as pd
import seaborn as sns
import re
import os
import sys
import ast
import matplotlib.pyplot as plt 
%matplotlib inline
Enter fullscreen mode Exit fullscreen mode

2.Text Processing

Text preprocessing refers to the process of cleaning and transforming raw text data into a format suitable for further analysis and natural language processing (NLP) tasks. We are going to remove punctuation, tokenization, stop words, handing contractions, lemmatization, and stemming and removing special characters.

So let’s install Textlob library to conduct these tasks:

!pip install textblob
Enter fullscreen mode Exit fullscreen mode

You can now import Stopwords, Textlob, and Word:

from nltk.corpus import stopwords 
from textblob import TextBlob
from textblob import Word

Enter fullscreen mode Exit fullscreen mode

So let’s remove punctuations and lower casing using the lambda function:

df[‘Text’] = df[‘Text’].apply(lambda x: “ “.join(x.lower() for
x in x.split()))
Enter fullscreen mode Exit fullscreen mode

Remove stopwords:

stop = stopwords.words(‘english’)
df[‘Text’] = df[‘Text’].apply(lambda x: “ “.join(x for x in
x.split() if x not in stop))
df[‘Text’].head()
Enter fullscreen mode Exit fullscreen mode

Next, you have to ensure the spellings are correct:

df[‘Text’] = df[‘Text’].apply(lambda x: str(TextBlob(x).
correct()))
df[‘Text’].head()
Enter fullscreen mode Exit fullscreen mode

Now, let’s handle contractions. Here, words like “doesn’t” becomes “does not”:

def expand_contractions(text):
 # Define contraction mappings
 contractions = {
 “i’m”: “i am”,

#Add more contractions as needed
 }
 # Split text into words and expand contractions
 words = text.split()
 expanded_words = [contractions[word] if word in contractions else word for word in words]
 #Join words back into a single string
 expanded_text = “ “.join(expanded_words)
 return expanded_text
 # Apply the custom function to expand contractions and then perform spelling correction
df[‘Text’] = df[‘Text’].apply(lambda x: str(TextBlob(expand_contractions(x)).correct()))
 #Display the first few rows of the updated ‘Text’ column
 print(df[‘Text’].head())


Enter fullscreen mode Exit fullscreen mode

You can complete the processing by lemmatizing the text. Here, the aim is to find the base form of a word. For example, the base form of ‘sleeping’ is ‘sleep’:

df[‘Text’] = df[‘Text’].apply(lambda x: “ “.join([Word(word).lemmatize() for word in x.split()]))
df.Text.head()
Enter fullscreen mode Exit fullscreen mode

3.Checking Text Using Wordcloud

Now that the processing is done, you can go ahead to check the most prominent terms.

You will use Wordcloud to check these words. A Wordcloud is a visual representation of text data. It displays a list of words, the importance of each being shown with font size or color.

First, let us install Wordcloud:

!pip install wordcloud

Enter fullscreen mode Exit fullscreen mode

Then, let’s define a variable you will be using to check the dominant terms:

score_1 = reviews[reviews[‘Confidence Score’] == 1]
score_2 = reviews[reviews[‘Confidence Score’] == 2]
score_3 = reviews[reviews[‘Confidence Score’] == 3]
score_4 = reviews[reviews[‘Confidence Score’] == 4]
score_5 = reviews[reviews[‘Confidence Score’] == 5]
reviews_sample = pd.concat([score_1,score_2,score_3,score_4,score_5],axis=0)
reviews_sample.reset_index(drop=True,inplace=True)
Enter fullscreen mode Exit fullscreen mode

You are near! Make sure the text is in one string by using word_tokenize:

from nltk.tokenize import word_tokenize
words = []
for text in df[‘Text’]:
words.extend(word_tokenize(text))
# If the list of words is empty, add a placeholder word to ensure there’s at least one word for the word cloud
if not words:
words = [‘NoWords’]
#similarly you can build for Text column
reviews_str = reviews_sample.Text.str.cat()
wordcloud = 
plt.figure(figsize=(10,10))
plt.imshow(wordcloud,interpolation=’bilinear’)
plt.axis(“off”)
plt.show()

Enter fullscreen mode Exit fullscreen mode

Output:

Screenshot 5

Audio Mock-Up Presentation

Data storytelling is an essential part of an analysis. Your stakeholder does not care about the tools you use in your analysis process. Practicing communication skills is important to be able to share your findings. Below is a mockup audio presentation my friend and I(our practice routines😆)prepared for the sentimental analysis. Your data storytelling does not have to be long. Focus on your findings.

Click here to watch it.

Conclusion

This article introduced you to a basic sentimental analysis that can be utilized to make informed decisions in your strategic planning. First, we used Excel to arrange the data on columns and rows. Then, we performed pre-procession on the text, removing stop words, handing contractions, lemmatization, and stemming. Next, we visualized frequently occurring feedback in the data. Finally, we did a mock-up audio presentation to an ideal stakeholder.

Top comments (1)

Collapse
 
cndavu profile image
Charles Ndavu

Reach out @cndavu in case of anything