Sentiment analysis is a natural language processing technique that deals with the classification of opinions or emotions in digital text. Sentiment analysis is commonly used in analyzing customer feedback, monitoring social media, and performing market research to automate the analysis of large bodies of text.
Sentiment analysis involves the application of advanced computational methods to identify and extract subjective information from text data. Sentiment analysis leverages machine learning algorithms, ruled-based approaches, or a hybrid of both to classify text into sentiment categories such as positive, negative, or neutral.
By analyzing linguistic features, contextual semantics, and syntactic structures, sentiment analysis provides a granular understanding of opinion in vast data sets. As a result, sentiment analysis is very effective in understanding semantic nuances and automating the interpretation of human emotion in data-rich environments.
Sentiment analysis starts with the collection of text inputs from data sources, such as social media, reviews, or surveys. The two key steps for sentiment analysis are preparing the data to ensure it is suitable for analysis and classifying the emotion in the data. Selecting the right classifier depends on the data and the required flexibility.

Key steps in sentiment analysis.
Challenges of Sentiment Analysis
Several challenges complicate the accurate interpretation and classification of sentiment in text data. One significant challenge is the inherent ambiguity and variability of natural language. Text data can contain sarcasm, irony, or idiomatic expressions, which are difficult for algorithms to interpret correctly. For example, the phrase “This is a wonderful investment” has a very different interpretation if it contains sarcasm.
Another challenge in sentiment analysis is classifying domain-specific text data. Sentiment analysis classifiers trained on general data sets might not perform well in specialized domains, such as engineering or science. For example, consider the very specialized terminology used by a mechanical engineer or a neurologist. Typically, pretrained sentiment analysis classifiers are not likely trained on this terminology.
Overcoming these challenges requires using advanced AI models for sentiment analysis, which are capable of contextual understanding. And even these models might require fine-tuning to effectively process domain-specific data.

Examples of text data that present challenges in sentiment analysis.
Text data for sentiment analysis can come from social media, online reviews, and direct customer feedback. Analyzing this data manually would be very time-consuming and inefficient, not to mention error prone. Sentiment analysis adds automation by using natural language processing (NLP) and artificial intelligence (AI) techniques to automatically interpret and classify the sentiment expressed in large volumes of text data.
This automated process not only speeds up data analysis but also empowers businesses to make better and early decisions. For example, they can track user sentiment from the moment a product or feature is launched to quickly and efficiently decide on the future direction of the product.

Text data for sentiment analysis can come from social media, online reviews, and direct customer feedback.
Examples of Sentiment Analysis
Organizations in a broad range of industries use sentiment analysis in many applications, including:
- Trading strategy: Gauging market sentiment from financial news articles and social media to inform investment decisions. By understanding market emotions, traders can anticipate market movements and adjust their strategies accordingly.
- Market research: Analyzing customer reviews and social media posts to understand public perception of a brand or product. This enables companies to tailor their marketing strategies and improve their offerings based on consumer sentiment.
- Health research: Assessing the emotional tone and opinions expressed in health-related texts, such as patient reviews, social media posts, or medical forums. This analysis helps healthcare organizations understand public perception of healthcare services, treatments, and health policies, enabling them to identify trends, improve patient care, and tailor public health strategies based on real-world feedback.
- User insights: Identifying customer satisfaction levels by analyzing feedback from surveys and support tickets. This helps businesses to address specific customer concerns and improve overall service quality.
- Social media monitoring: Tracking brand mentions and understanding the overall sentiment toward the company and its products on social media platforms. This enables companies to engage with their audience effectively and manage their online perception proactively.

Examples of use cases for sentiment analysis.
Benefits of Sentiment Analysis for Companies
Businesses are quickly adopting sentiment analysis because it enables them to:
- Enhance product and service development: Sentiment analysis systems enable companies to refine their offerings based on authentic and specific customer feedback. AI technologies can identify real-world entities linked to negative sentiments.
- Scale analysis efforts: Businesses continuously extract insights from vast amounts of unstructured data, such as emails, chatbot interactions, surveys, CRM records, and product reviews. Cloud-based sentiment analysis tools empower companies to efficiently scale the extraction of customer emotions from textual data in a cost-effective manner.
- Obtain real-time insights: In today’s rapidly evolving market environment, businesses must swiftly respond to potential crises or emerging trends. Marketers depend on sentiment analysis software to gauge customer sentiment regarding their brand, products, and services in real time, enabling immediate action based on these insights.
Benefits of Sentiment Analysis for Engineers
By leveraging sentiment analysis, engineers can enhance product quality, improve user satisfaction, and contribute to the overall success of their projects. Benefits include:
- Enhanced user feedback analysis: Sentiment analysis enables engineers to automatically process and interpret large volumes of user feedback, identifying prevailing sentiments about products or features. This helps prioritize development efforts by focusing on areas that require immediate attention, such as bug fixes or feature enhancements.
- Improved product development: By understanding customer sentiment, engineers can make informed decisions about product design and feature prioritization. Insights gained from sentiment analysis can guide the development process, ensuring that new features align with user expectations and market demands.
- Data-driven decision-making: Sentiment analysis provides engineers with actionable insights derived from textual data, supporting data-driven decision-making processes. By understanding sentiment trends, engineers can better strategize product road maps and development cycles.
- Competitive advantage: By analyzing sentiment data, engineers can identify trends and patterns that offer a competitive edge. Understanding how users perceive competing products can inform strategic improvements and innovations, positioning a product more favorably in the market.
- Resource optimization: Automated sentiment analysis helps engineers allocate resources more efficiently by highlighting which aspects of a product or service are most critical to users. This ensures that time and effort are spent on areas that will have the most significant impact on user satisfaction.
Keep Exploring This Topic
The key steps for sentiment analysis are data preparation and classifying the emotion in the data. The sentiment analysis classifier can be a rule-based system or a machine learning model.
Data Preparation for Sentiment Analysis
Techniques used to convert text from an unstructured format to a structured format, which the classifier can process, include:
- Tokenization: Typically, this is the first step in text processing for natural language processing. It refers to splitting up the text into sentences or words. Tokenization is a common data preparation step for sentiment analysis. Depending on the type and predictive power of the sentiment analysis classifier, additional data preprocessing might be required.
- Stop word removal: Words like “the,” “and,” and “to,” known as stop words, can add noise to text data. By removing stop words, the sentiment analysis classifier can focus on more meaningful and informative words.
- Lemmatization: This text normalization technique uses vocabulary and morphological analysis to remove affixes of words. For example, “building has floors” reduces to “build have floor.”

Data preparation steps for sentiment analysis.
When your data has been converted to the right structured format, you must select a classifier that can accurately predict sentiment.
Rule-Based Sentiment Analysis
In rule-based sentiment analysis, the algorithm automatically labels the input data based on a set of predefined rules and lexicons. This approach relies on evaluating the presence of certain words or phrases, often categorized in sentiment lexicons, which are lists of words annotated with their associated sentiment polarity (positive, negative, or neutral). The analysis involves parsing the text to identify these words and applying syntactic rules to account for negations, intensifiers, and other modifiers that can alter sentiment.
While rule-based systems are simple to understand and implement, they often lack the flexibility and nuance to handle complex language constructs, such as sarcasm or context-dependent expressions, which can limit their accuracy compared with AI-based approaches. Despite these limitations, rule-based sentiment analysis can be effective in specific tasks where the language is relatively predictable and well defined.

Example of rule-based sentiment analysis, classifying the sentiment in sentences based on a set of predefined rules.
Sentiment Analysis with AI
AI models are typically more accurate than rule-based systems for sentiment analysis. They can process large text data sets and learn different explicit and implicit patterns in the data. A sentiment analysis model is built by using a supervised learning algorithm.
Similar to other classifiers, such as those for image data, the sentiment analysis model is trained with a large-enough data set and corresponding labels. After the model is trained, it can be used for classification. During the inference phase, the model predicts the right label for its input.

Process comparison for the training and inference phases of sentiment analysis and image classification using AI.
Types of AI Models for Sentiment Analysis
Sentiment analysis leverages various AI models, which can be broadly categorized into traditional machine learning models and deep learning models.
Traditional machine learning models, such as decision trees and support vector machines (SVMs), rely on feature extraction techniques like bag-of-words to convert text into numerical representations. These models are trained on labeled data sets to learn patterns associated with different sentiment classes. Although they are computationally efficient and interpretable, their performance depends on the quality of feature engineering and they might struggle with capturing complex linguistic nuances and context.
Deep learning models offer a more sophisticated approach to understanding language. Recurrent neural networks (RNNs) and their variants, such as long short-term memory (LSTM) networks, can handle sequential data, making them suitable for learning long-term dependencies in text data. Convolutional neural networks (CNNs) can also be used for sentiment analysis in combination with word embedding like word2vec.
A special type of deep learning model is transformer models. The introduction of transformer models, such as BERT (Bidirectional Encoder Representations from Transformers), to NLP has significantly advanced sentiment analysis capabilities.

Comparison of AI models for sentiment analysis in terms of predictive power, complexity, and training data size.
Sentiment Analysis with LLMs
Large language models (LLMs), such as GPT models, have an underlying transformer architecture. Transformer models can capture complex relationships between words and nuances in human language. These models are computationally demanding and require large training data sets but offer the most accuracy and flexibility for sentiment analysis.
LLMs can be fine-tuned for sentiment analysis tasks. Fine-tuning involves adjusting a pretrained model for a specific task. This approach enables the use of a smaller data set to retrain a large model while still achieving high accuracy. For instance, you can add additional layers to a BERT model and retrain using a smaller data set to perform sentiment analysis.

Fine-tuning a BERT model for sentiment analysis.
With MATLAB®, Text Analytics Toolbox™, and Deep Learning Toolbox™ you can perform sentiment analysis for a variety of applications by using built-in tools and leveraging LLMs.
Data Preparation
You can transform unstructured language data to a structured format interactively and clean your data with the Preprocess Text Data Live Editor task. Alternatively, you can prepare your NLP data programmatically with built-in functions.
Using word clouds and scatter plots, you can visualize text data, models, and sentiment analysis outcomes.

Visualizing positive and negative words using word clouds that illustrate word frequency. (See MATLAB code.)
Rule-Based Sentiment Analysis
You can analyze text by using the built-in functions vaderSentimentScores and ratioSentimentScores for sentiment analysis. These functions compute the sentiment score of a sentence based on the sentiment scores of the composite words, which are given by the VADER sentiment lexicon. You can also generate domain-specific lexicons (by using word embeddings to capture semantic details of words) and custom sentiment analysis models (for example, for sentiment analysis in languages other than English).
Sentiment Analysis with AI
You can train many types of machine learning models for sentiment analysis. For example, you can create and train LSTMs with a few lines of MATLAB code. You can also use a pretrained BERT model, which is a transformer neural network that can be fine-tuned for many NLP tasks, including sentiment analysis.
Sentiment Analysis with LLMs
The Large Language Models (LLMs) with MATLAB repository provides code you can use to access and interact with LLMs using MATLAB. You can connect MATLAB to the OpenAI® Chat Completions API (which powers ChatGPT™), Ollama™ (for local LLMs), and Azure® OpenAI services.
Example: Performing Sentiment Analysis Using a GPT Model
Specify the system prompt. The system prompt tells the assistant how to behave, in this case, as a sentiment analyzer. It also provides the system with simple examples on how to perform sentiment analysis.
systemPrompt = "You are a sentiment analyser. You will look at a sentence and output"+...
" a single word that classifies that sentence as either 'positive' or 'negative'. "+...
"Examples: \n"+...
"The project was a complete failure. \n"+...
"negative \n\n"+...
"The team successfully completed the project ahead of schedule. "+...
"positive \n\n"+...
"His attitude was terribly discouraging to the team. \n"+...
"negative \n\n";
Initialize the OpenAI Chat object by passing a system prompt.
chat = openAIChat(systemPrompt,ApiKey=my_key);
Generate a response by passing a new sentence for classification.
text = generate(chat,"The team is feeling very motivated.")
text = "positive"
30-Day Free Trial
Get startedSelect a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: United States.
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)