NLP Tutorials Part I: From Basics to Advance
Welcome to this comprehensive tutorial series on Natural Language Processing (NLP)! Over the course of these articles, we‘ll dive deep into the fundamentals of NLP and gradually progress to advanced techniques. By the end, you‘ll have a solid grasp of core NLP concepts and be able to implement them in Python.
Our target audience is anyone who is new to NLP or wants to systematically learn the topic from the ground up. The only prerequisite is a basic understanding of Python programming. We‘ll be utilizing popular Python libraries for NLP such as NLTK, SpaCy, Gensim, and Keras.
Here‘s an overview of the key topics we‘ll cover in this series:
- Text preprocessing and cleaning
- Tokenization and stop word removal
- Stemming and lemmatization
- Vectorization techniques (Bag-of-Words, TF-IDF, word embeddings)
- Topic modeling with Latent Dirichlet Allocation (LDA)
- Word embedding approaches (word2vec, GloVe, FastText)
- Advanced techniques – text generation and transfer learning
But first, let‘s address the fundamental question – what exactly is NLP?
What is Natural Language Processing (NLP)?
NLP is a branch of artificial intelligence that enables computers to understand, interpret, and manipulate human language. The goal is to build systems that can process and analyze large amounts of natural language data, and perform language-related tasks similar to how humans do.
Some common real-world applications of NLP include:
- Sentiment analysis (determining the emotion/opinion behind text)
- Text summarization (generating concise summaries of long articles)
- Machine translation (translating between languages)
- Chatbots and virtual assistants
- Information extraction (extracting structured data from unstructured text)
- Text classification (categorizing text into predefined categories)
At a high level, NLP systems take raw text data as input, apply a series of preprocessing and feature engineering steps, and produce structured, machine-readable representations of the text that can be used for downstream tasks. Let‘s walk through these steps one-by-one.
Step 1: Text Preprocessing and Cleaning
The first step in any NLP pipeline is to clean and preprocess the raw text data. Real-world text data is often messy and unstructured, containing noise such as HTML tags, special characters, misspellings, slang, etc. The goal of text preprocessing is to clean up the data and standardize it into a consistent format.
Some common text preprocessing steps include:
-
Converting to lowercase: This standardizes the case so that "Hello" and "hello" are treated the same.
-
Removing HTML tags and special characters: HTML tags like <p>, <br>, etc. are removed, along with special characters like @, #, $, %, etc.
-
Expanding contractions: Contractions like "don‘t", "I‘ll", "he‘s" are expanded to their full forms "do not", "I will", "he is".
-
Correcting misspellings: Misspelled words are corrected using spell-checking libraries.
Here‘s an example of performing these preprocessing steps in Python using the re (regular expressions) and TextBlob libraries:
import re
from textblob import TextBlob
def preprocess(text):
# Convert to lowercase
text = text.lower()
# Remove HTML tags
text = re.sub(r‘<.*?>‘, ‘‘, text)
# Remove special characters
text = re.sub(r‘[^a-zA-Z0-9\s]‘, ‘‘, text)
# Expand contractions
text = re.sub(r"n\‘t", " not", text)
text = re.sub(r"\‘re", " are", text)
text = re.sub(r"\‘s", " is", text)
text = re.sub(r"\‘d", " would", text)
text = re.sub(r"\‘ll", " will", text)
# Correct misspellings
text = str(TextBlob(text).correct())
return text
After preprocessing, our text is much cleaner and standardized:

Step 2: Tokenization
The next step is to break down the preprocessed text into smaller units called tokens. Tokenization refers to splitting text into individual words, phrases, symbols, or other meaningful elements. The resulting tokens are the building blocks for further analysis.
The most common type of tokenization is word tokenization, which splits the text into individual words. This can be done using the split() method in Python, or more sophisticated tokenizers from NLP libraries like NLTK or SpaCy.
Here‘s an example of performing word tokenization using NLTK:
from nltk.tokenize import word_tokenize
text = "Hello world! This is a sample sentence."
tokens = word_tokenize(text)
print(tokens)
# Output: [‘Hello‘, ‘world‘, ‘!‘, ‘This‘, ‘is‘, ‘a‘, ‘sample‘, ‘sentence‘, ‘.‘]
Tokenization is an essential step that allows further analysis and processing to be performed on individual words or tokens.
Step 3: Stop Word Removal
Stop words are commonly occurring words in a language that typically do not contribute to the meaning of a sentence, such as "the", "a", "an", "in", etc. Removing stop words helps reduce the dimensionality of the text data and allows downstream algorithms to focus on the most informative words.
Stop word removal is usually done by comparing each token to a pre-defined list of stop words and filtering out the matches. Popular NLP libraries like NLTK and SpaCy provide built-in lists of stop words.
Here‘s an example of performing stop word removal using NLTK:
from nltk.corpus import stopwords
stop_words = set(stopwords.words(‘english‘))
tokens = [‘i‘, ‘am‘, ‘learning‘, ‘nlp‘, ‘in‘, ‘python‘]
filtered_tokens = [word for word in tokens if word.lower() not in stop_words]
print(filtered_tokens)
# Output: [‘learning‘, ‘nlp‘, ‘python‘]
After removing stop words, we‘re left with the most meaningful and informative tokens in the text.
Step 4: Stemming and Lemmatization
Stemming and lemmatization are text normalization techniques used to reduce inflectional and derivational forms of words to a common base or dictionary form.
Stemming reduces words to their word stem or root form by removing suffixes. For example, "running", "runs", and "ran" would all be reduced to the stem "run". Stemming is a crude heuristic that chops off word endings based on a set of rules, without considering the context or part of speech. This often leads to stems that are not valid words.
Lemmatization, on the other hand, reduces words to their base or dictionary form (lemma) based on a vocabulary and morphological analysis. For example, "running" and "ran" would be lemmatized to "run", while "better" and "good" would both be lemmatized to "good". Lemmatization considers the context and part of speech to produce valid lemmas.
In practice, lemmatization produces more meaningful results than stemming, but is also more computationally expensive. The choice between stemming and lemmatization depends on the specific application and trade-off between speed and accuracy.
Here‘s an example of performing stemming and lemmatization using NLTK:
from nltk.stem import PorterStemmer, WordNetLemmatizer
stemmer = PorterStemmer()
lemmatizer = WordNetLemmatizer()
words = [‘running‘, ‘runs‘, ‘ran‘, ‘better‘, ‘good‘]
stemmed = [stemmer.stem(word) for word in words]
print("Stemmed:", stemmed)
# Output: [‘run‘, ‘run‘, ‘ran‘, ‘better‘, ‘good‘]
lemmatized = [lemmatizer.lemmatize(word) for word in words]
print("Lemmatized:", lemmatized)
# Output: [‘running‘, ‘run‘, ‘run‘, ‘good‘, ‘good‘]
As we can see, stemming reduces all forms of "run" to "run", but doesn‘t handle irregular forms like "better" and "good". Lemmatization correctly handles these and produces valid lemmas.
What‘s Next?
In this first part of the NLP tutorial series, we covered the basic steps of text preprocessing, including cleaning, tokenization, stop word removal, stemming, and lemmatization. These techniques help standardize and normalize the text data into a structured format that can be further processed and analyzed.
In the next part of this series, we‘ll dive into feature extraction and vectorization techniques that convert text into numerical representations suitable for machine learning models. We‘ll cover approaches like Bag-of-Words, TF-IDF, and word embeddings. We‘ll also explore topic modeling using Latent Dirichlet Allocation (LDA) to uncover hidden topics in text data.
Further ahead, we‘ll delve into advanced NLP techniques like text generation using deep learning, and transfer learning with pre-trained language models like BERT and GPT.
Stay tuned for more informative articles in this NLP tutorial series! In the meantime, check out these resources to learn more:
- Speech and Language Processing by Dan Jurafsky and James H. Martin
- Natural Language Processing with Python by Steven Bird, Ewan Klein, and Edward Loper
- NLP‘s ImageNet moment has arrived by Sebastian Ruder
Also feel free to connect with me on LinkedIn or follow me on Twitter for more NLP content!