[‘Natural‘, ‘language‘, ‘processing‘, ‘is‘, ‘a‘, ‘subfield‘, ‘of‘, ‘linguistics‘, ‘,‘, ‘computer‘, ‘science‘, ‘,‘, ‘and‘, ‘artificial‘, ‘intelligence‘, ‘.‘, ‘It‘, ‘deals‘, ‘with‘, ‘the‘, ‘interactions‘, ‘between‘, ‘computers‘, ‘and‘, ‘human‘, ‘language‘, ‘.‘]
Natural Language Processing, or NLP for short, is a branch of artificial intelligence focused on enabling computers to understand, interpret, and generate human language. NLP combines techniques from computer science, linguistics, and machine learning to bridge the gap between human communication and computer understanding.
Some common applications of NLP include:
- Sentiment analysis to determine the emotion or opinion behind a piece of text
- Chatbots and virtual assistants that can engage in human-like conversation
- Machine translation between different languages
- Information extraction to pull out key details from long documents
- Text summarization to generate synopses of articles
- Spam email detection based on the content of messages
As the amount of unstructured text data in the world continues to grow exponentially, NLP is becoming an increasingly important tool to help make sense of it all. By 2025, it‘s estimated that the global NLP market will be worth over $35 billion as more industries find applications for the technology.
While NLP is a complex field spanning multiple disciplines, getting started with the basics is easier than you might think thanks to open source libraries like the Natural Language Toolkit, or NLTK for short. NLTK is a powerful Python library that provides a wide range of tools for working with human language data.
In this guide, we‘ll walk through the fundamentals of NLP and show you how to use NLTK to perform a variety of common text processing tasks. Whether you‘re an aspiring data scientist, software engineer, or just have an interest in AI, this guide will give you a practical introduction to the field of natural language processing. Let‘s dive in!
Installing NLTK
Before we can start using the NLTK library, we first need to install it. The easiest way to install NLTK is using pip, Python‘s standard package manager. Simply open a terminal or command prompt and run:
pip install nltk
If you‘re using Anaconda, you can alternatively install it from the conda-forge channel with:
conda install -c conda-forge nltk
Once the installation finishes, you can verify that NLTK was installed successfully by importing it in the Python REPL:
import nltk
If no error messages appear, you‘re good to go! With the installation out of the way, we can start exploring the core functionality NLTK provides.
Tokenization
The first step in many NLP tasks is to break raw text down into smaller pieces called tokens. Tokenization allows us to work with text at a more granular level, like individual words or sentences.
NLTK provides a few different tokenizers in its tokenize module. To use them, we first need to import the module:
from nltk.tokenize import word_tokenize, sent_tokenize
The word_tokenize function splits a string into a list of words, while sent_tokenize splits text into a list of sentences. Here‘s an example of using both:
text = "Natural language processing is a subfield of linguistics, computer science, and artificial intelligence. It deals with the interactions between computers and human language."print(word_tokenize(text))
print(sent_tokenize(text))
Tokenization is an essential preprocessing step that allows us to work with smaller, more manageable pieces of text. In the next section, we‘ll look at ways to normalize and clean up these tokens.
Text Normalization
Text data in the real world is messy. It often includes a lot of noise in the form of punctuation, special characters, or differences in capitalization and spelling that aren‘t relevant to the meaning of the text itself. Text normalization is the process of cleaning up the text to make it more consistent and easier to work with.
A common normalization step is to remove non-alphabet characters from the text using a simple regular expression:
import retext = "This &is a! sample 2sentence." clean_text = re.sub(r‘[^a-zA-Z]‘, ‘ ‘, text) print(clean_text)
We can also convert the text to lowercase to avoid treating the same word differently just because of capitalization differences:
text = "The quick Brown Fox." print(text.lower()) # the quick brown fox.
Another type of noise in text are high-frequency words that don‘t contribute a lot of meaning, like "a", "an", "the", "in", "on", etc. These are known as stop words and are usually filtered out before further processing. NLTK provides a built-in list of English stop words we can use:
from nltk.corpus import stopwordsstop_words = set(stopwords.words(‘english‘))
tokens = [‘i‘, ‘am‘, ‘going‘, ‘to‘, ‘go‘, ‘to‘, ‘the‘, ‘store‘, ‘and‘, ‘park‘] filtered_tokens = [w for w in tokens if not w in stop_words]
print(filtered_tokens)
By removing stop words, we‘re left with only the more meaningful words in the text. This can help improve the results of downstream tasks like text classification.
Stemming and Lemmatization
Stemming and lemmatization are two text normalization techniques used to reduce words to their base or root form. The goal is to group together different inflected forms of the same word to improve text consistency.
Stemming is a cruder heuristic approach that simply chops off the ends of words using a set of rules. NLTK implements several popular stemming algorithms, like the Porter and Lancaster stemmers:
from nltk.stem import PorterStemmer, LancasterStemmerporter = PorterStemmer() lancaster = LancasterStemmer()
words = ["connect", "connected", "connecting", "connection", "connections"]
print([porter.stem(w) for w in words])
print([lancaster.stem(w) for w in words])
Stemming is fast, but it often produces incomplete or incorrect roots since it doesn‘t take the context of the word into account.
Lemmatization is a more sophisticated approach that uses vocabulary and morphological analysis to reduce words to their base dictionary form, known as the lemma. For example:
from nltk.stem import WordNetLemmatizerlemmatizer = WordNetLemmatizer()
print(lemmatizer.lemmatize("better"))
print(lemmatizer.lemmatize("best", pos="a"))
Lemmatization takes into account the part of speech of the word, allowing it to distinguish between different base forms depending on context. The downside is it‘s slower than stemming and requires a large dictionary of root forms.
In practice, stemming is used more often for simpler text processing, while lemmatization is preferred when the meaning of the words is important, like in text mining and information retrieval applications.
Part-of-speech Tagging
Part-of-speech (POS) tagging is the process of labeling each word in a text with its corresponding part of speech, like noun, verb, adjective, etc. POS tagging can help disambiguate the meaning of words and enables higher-level text analysis.
NLTK provides a pre-trained POS tagger that can label words with their part-of-speech tag from the Penn Treebank tagset:
from nltk import pos_tagtext = "The quick brown fox jumped over the lazy dog." tokens = word_tokenize(text)
print(pos_tag(tokens))
The tagger uses a statistical model trained on a labeled dataset to predict the most likely tag for each word based on the surrounding context. POS tagging enables finding patterns in text, like extracting all the nouns or verbs, that can be useful for applications like named entity recognition and document classification.
Named Entity Recognition
Named Entity Recognition (NER) is the task of automatically identifying and categorizing named entities in text into predefined categories like person names, organizations, locations, etc. NER is an important component of information extraction systems and is used in applications like content classification and question answering.
NLTK provides a built-in NER model that can recognize named entities in text:
from nltk import word_tokenize, pos_tag, ne_chunktext = "Apple is looking to buy U.K. startup for $1 billion." tokens = word_tokenize(text) tags = pos_tag(tokens) tree = ne_chunk(tags)
print(tree)
The model tags "Apple" as an organization, "U.K." as a geopolitical entity, and "$1 billion" as money. NLTK uses a classifier trained on a labeled dataset to predict the named entity tags. The classifier looks at features like the POS tag, word shape (e.g. titlecase, digits), and surrounding context to make its predictions.
Text Classification
Text classification is the task of automatically assigning predefined categories to documents or pieces of text. Some common applications include sentiment analysis, topic classification, and spam detection.
NLTK provides a few different classifiers we can use for text classification, like the Naive Bayes, Decision Tree, and Maximum Entropy classifiers. Here‘s an example of training a Naive Bayes classifier on a toy dataset to predict positive/negative sentiment:
from nltk.classify import NaiveBayesClassifierdef extract_features(text): return dict([(word, True) for word in text])
positive_reviews = [(‘I love this movie‘, ‘pos‘), (‘great acting‘, ‘pos‘), (‘awesome cinematography‘, ‘pos‘)]
negative_reviews = [(‘terrible film‘, ‘neg‘),
(‘disappointing‘, ‘neg‘), (‘boring‘, ‘neg‘)]datasets = negative_reviews + positive_reviews
model = NaiveBayesClassifier.train([(extract_features(d), c) for (d,c) in datasets])
text = ‘awesome effects but boring plot‘ print(model.classify(extract_features(text.split())))
The key steps are:
- Prepare labeled training data of documents and their categories
- Define a feature extraction function to convert the documents into feature dictionaries
- Train the classifier on the feature dictionaries
- Use the trained model to predict categories for new text
This is obviously a very simplified example, but a similar process can be used to build more sophisticated text classification models on larger datasets. The choice of classifier depends on the size and nature of the data. Naive Bayes is a good baseline to start with, while more advanced methods like SVMs and neural networks can yield better performance on complex tasks.
Other NLP Libraries
While NLTK is a great general-purpose NLP library, there are a number of other popular Python libraries for different NLP tasks and use cases:
-
spaCy – Focused on production usage, spaCy features fast tokenization, POS tagging, dependency parsing, and named entity recognition. It has a concise and expressive API.
-
Gensim – Specialized library for unsupervised topic modeling and document similarity retrieval. Includes implementations of word2vec, doc2vec, and latent semantic analysis.
-
Stanford CoreNLP – Java library that provides a Python wrapper for tasks like POS tagging, NER, coreference resolution, and sentiment analysis. Useful for applications that require running a Java NLP pipeline.
-
Hugging Face Transformers – State-of-the-art Python library for building and training transformer-based language models like BERT, GPT, and XLNet. Quickly gaining adoption for transfer learning in NLP.
Each library has its own strengths and use cases, so the choice ultimately depends on the specific requirements of your NLP project. It‘s common to combine multiple libraries and use each for what it does best.
Where to Go from Here
This guide provided a whirlwind tour of basic NLP concepts and how to implement them using the NLTK library in Python. We covered fundamentals like tokenization, text normalization, part-of-speech tagging, named entity recognition, and text classification to give you a solid foundation to start building NLP applications.
To dive deeper into natural language processing, here are some recommended resources:
-
Natural Language Processing with Python (NLTK Book) – The official NLTK book that provides a comprehensive introduction to NLP concepts and NLTK, with extensive code samples.
-
Speech and Language Processing – The seminal textbook on statistical NLP methods by Stanford professors Dan Jurafsky and James H. Martin. Includes chapters on advanced topics like machine translation, dialog systems, and question answering.
-
Natural Language Processing Specialization (Coursera) – A series of 4 courses that cover a wide range of NLP techniques, including probabilistic models, sequence models, vector space models, and neural network models. Taught by leading experts from the National Research University Higher School of Economics.
-
Awesome NLP – A curated list of resources dedicated to Natural Language Processing, including libraries, datasets, tutorials, and research papers.
By exploring these resources and experimenting with the NLTK library and other NLP tools, you‘ll be well on your way to becoming an NLP practitioner. Natural language processing is a vast and rapidly evolving field with numerous exciting challenges and opportunities. I hope this guide inspired you to embark on your own NLP journey!