Sentiment Analysis Tutorial

July 20, 2021 — by Kilian Thiel & Lada Rudnitckaia

A tutorial on how to approach sentiment classification with supervised machine learning algorithms

Sentiment analysis of free-text documents is a common task in the field of text mining. In sentiment analysis predefined sentiment labels, such as "positive" or "negative" are assigned to texts. Texts (here called documents) can be reviews about products or movies, articles, tweets, etc.

In this article, we show you how to assign predefined sentiment labels to documents, using the KNIME Text Processing extension in combination with traditional KNIME learner and predictor nodes.

A set of 2000 documents has been sampled from the training set of the Large Movie Review Dataset v1.0. The Large Movie Review Dataset v1.0 contains 50000 English movie reviews along with their associated sentiment labels "positive" and "negative". We sampled 1000 documents of the positive group and 1000 documents of the negative group. The goal here is to assign the correct sentiment label to each document.

Try out the workflow associated with this post (Fig. 1) yourself. It is available to download from the KNIME Hub.

Fig. 1. This workflow pre-processes the text documents, creates document vectors, and trains two machine learning models to assign sentiment labels to the documents.

Read the Review Texts: Text Data

The workflow starts with a CSV Reader node, reading a CSV file, that contains the review texts, its associated sentiment label, the IMDb URL of the corresponding movie, and its index in the Large Movie Review Dataset v1.0. Important are the text and the sentiment columns. In the first metanode "Document Creation", Document cells are created from the string cells, using the Strings to Document node; the sentiment labels are stored in the category field of each document, to remain available for later tasks; and all columns, except the Document column, are filtered out.

The output of the first metanode "Document Creation" is a data table with only one column containing the Document cells.

Pre-process the Text

The textual data is preprocessed by various nodes provided by the KNIME Text Processing extension. All preprocessing steps are applied in the second metanode "Preprocessing", as shown in fig. 2.

Fig. 2. The “Preprocessing” metanode performs text specific data preprocessing

First, punctuation marks are removed by the Punctuation Erasure node, then numbers and stop words are filtered, and all terms are converted to lowercase. After that, the stem is extracted from each word using the Snowball Stemmer node. Indeed, the words “selection”, “selecting” and “to select” refer to the same lexical concept and carry the same information in a document classification or topic detection context. Besides English texts, the Snowball Stemmer node can be applied on texts of various languages, e.g. German, French, Italian, Spanish, etc. The node is using the Snowball stemming library.

Extract Features and Create Document Vectors

After all this pre-processing, we reach the central point of the analysis which is to extract the terms to use as components of the document vectors and as input features to the classification model.

To create the document vectors for the texts, first we create their bag of words using the Bag Of Words Creator node; then we feed the data tables containing the bag of words into the Document Vector node. The Document Vector node will take into account all terms contained in the bag of words to create the corresponding document vector.

Notice that a text is made of words and a document - which is a text including some additional information such as category or authors - contains terms i.e., words including some additional information such as grammar, gender, or stem.

Tip: How to Avoid Performance Issues when Texts are Long?

Since texts are quite long, the corresponding bags of words can contain many words, the corresponding document vector can have a very high dimensionality (too many components), and the classification algorithm can suffer in terms of speed performance. However, not all words in a text document are equally important.

A common practice is to filter out all least informative words and keep only the most significant ones. A good measure of word importance can be indicated by the number of occurrences of words in each single document as well as in the whole dataset.

Based on this consideration, after the bag of words has been created, we filter out all terms that occur in less than 20 documents inside the dataset. Within a GroupBy node, we group by terms and count all unique documents containing a term at least once. The output is a list of terms with the number of documents in which they occur.

We filter this list of terms to keep only those terms with a number of documents greater than 20, and then we filter the terms in each bag of words accordingly, with the Reference Row Filter node. In this way, we reduce the feature space from 22379 distinct words to 1499. This feature extraction process is part of the "Preprocessing" metanode and can be seen in fig. 3.

Fig. 3. Feature extraction - creating and filtering the bags of words.

We set the minimum number of documents to 20 since we assume that a term has to occur in at least 1% of all documents (20 of 2000) in order to represent a useful feature for classification. This is a rule of thumb and of course can be optimized.

Document vectors are now created, based on these extracted words (features). Document vectors are numerical representations of documents. Here each word in the dictionary becomes a component of the vector and for each document assumes a numerical value, that can be 0/1 (0 absence, 1 presence of word in document) or a measure of the word importance within the document (e.g. word scores or frequencies). The Document Vector node allows for the creation of bit vectors (0/1) or numerical vectors. As numerical values, previously calculated word scores or frequencies can be used, e.g. by the TF or IDF nodes. In our case bit vectors are used.

Use Supervised Mining Algorithm for Classification

For classification we can use any of the traditional supervised mining algorithms available in KNIME Analytics Platform (e.g. decision trees, random forest, support vector machines, neural networks, and much more).

As in all supervised mining algorithms we need a target variable. In our example, the target is the sentiment label, stored in the document category. Therefore, the target or class column is extracted from the documents and appended as a string column, using the Category To Class node. Based on the category, a color is assigned to each document by the Color Manager node. Documents with label "positive" are colored green, documents with label "negative" are colored red.

As classification algorithms we used a Decision Tree and a XGBoost Tree Ensemble applied on a training (70%) and test set (30%), randomly extracted from the original dataset. The accuracy of the Decision Tree is 91.3%, the accuracy of the XGBoost Tree Ensemble 92.0%. The corresponding ROC curves are presented in fig. 4.

Fig. 4. ROC Curve for the Decision Tree and the XGBoost models.

While performing slightly better, the XGBoost Tree Ensemble doesn’t have the interpretability that the Decision Tree can provide. The next figure (Fig. 5) shows a view of the first two levels of the Decision Tree. The most discriminative terms w.r.t. separation of the two classes are "bad", "wast", and "film". If the term "bad" occurs in a document it is likely to have a negative sentiment. If "bad" does not occur but "wast" (stem of waste), it is again likely to be a negative document, and so on.

Fig. 5. Decision Tree view allows us to investigate which features, in our case words or their stems, contribute the most to separating documents in the two sentiment classes.

Find more information in free ebook: From Words to Wisdom

How to Improve Classification

What we’ve shown in this blog post is just a quick tutorial on how to approach sentiment classification with supervised machine learning algorithms. Of course, much more than that can be done on much larger datasets and on much more complex sentences. There are three broader ways we can improve this classification:

  • Use a larger dataset
  • Improve data preparation
  • Improve the models, either by optimizing the trained ones or by using different architectures

The preprocessing chain could be optimized for better cleaning and transformations especially in terms of the adopted classification algorithm. For example, instead of bit vectors, numerical vectors could also be created by the Document Vector node. Furthermore n-gram features could be used in addition to single words to take into account negations, such as "not good" or "not bad".

Fine tuning the hyper-parameters can also improve the performance of the trained models. 

Other classification learners, such as the Tree Ensemble Learner, Naive Bayes Learner, or SVM Learner can be applied as well. 

Deep learning methods, based on neural networks, especially Recurrent Neural Networks (RNN) and Long Short-Term Memory (LSTM) networks, have recently been successfully applied to sentiment analysis. Some neural layers can extract the text features by projecting them on a lower dimensionality space; some neural architectures, such as LSTM based networks, can take word order and context into account. This can help detect the sentiment of more complex sentences based on context. You can build neural networks in the KNIME Analytics Platform with the KNIME Deep Learning Extension – Keras Integration.

More information can be found in the ebook “From Words to Wisdom” available on KNIME Press.


- KNIME Text Processing extension


You may also like

“Once Upon A Time … “ by LSTM Network

Recurrent Neural Networks (RNN) are the state of the art for sequence analysis 5 6. With the release of KNIME Analytics Platform 3.6, KNIME extended its set of ...

November 26, 2018 – by Kathrin Melcher