Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Hands-On Natural Language Processing with Python
Hands-On Natural Language Processing with Python

Hands-On Natural Language Processing with Python: A practical guide to applying deep learning architectures to your NLP applications

Arrow left icon
Profile Icon Shanmugamani Profile Icon Muthuswamy Profile Icon Byiringiro Profile Icon Arumugam Profile Icon Joshi +1 more Show less
Arrow right icon
R$50 per month
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.8 (4 Ratings)
Paperback Jul 2018 312 pages 1st Edition
eBook
R$196.99
Paperback
R$245.99
Subscription
Free Trial
Renews at R$50p/m
Arrow left icon
Profile Icon Shanmugamani Profile Icon Muthuswamy Profile Icon Byiringiro Profile Icon Arumugam Profile Icon Joshi +1 more Show less
Arrow right icon
R$50 per month
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.8 (4 Ratings)
Paperback Jul 2018 312 pages 1st Edition
eBook
R$196.99
Paperback
R$245.99
Subscription
Free Trial
Renews at R$50p/m
eBook
R$196.99
Paperback
R$245.99
Subscription
Free Trial
Renews at R$50p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Hands-On Natural Language Processing with Python

Text Classification and POS Tagging Using NLTK

The Natural Language Toolkit (NLTK) is a Python library for handling natural language processing (NLP) tasks, ranging from segmenting words or sentences to performing advanced tasks, such as parsing grammar and classifying text. NLTK provides several modules and interfaces to work on natural language, useful for tasks such as document topic identification, parts of speech (POS) tagging, sentiment analysis, and so on. For experimentation with various NLP tasks, NLTK also includes modules for a wide range of text corpora, from basic text collections to tagged and structured texts, such as WordNet. While the NLTK library provides a vast set of APIs, we will only cover the most important aspects that are commonly used in practical NLP applications.

We will cover the following topics in this chapter:

  • Installing NLTK and its modules
  • Text...

Installing NLTK and its modules

Before getting started with the examples, we will set the system up with NLTK and other dependent Python libraries. The pip installer can be used to install NLTK, with an optional installation of numpy, as follows:

sudo pip install -U nltk
sudo pip install -U numpy

The NLTK corpora and various modules can be installed by using the common NLTK downloader in the Python interactive shell or a Jupyter Notebook, shown as follows:

import nltk
nltk.download()

The preceding command will open an NLTK Downloader, as follows. Select the packages or collections that are required:

As shown in the preceding screenshot, specific collections, text corpora, NLTK models, or packages, can be selected and installed. Navigate to stopwords and install it for future use. The following is a list of modules that are required for this chapter's examples:

No

Package...

Text preprocessing and exploratory analysis

First, we will provide a hands-on overview of NLTK by working on some basic NLP tasks, such as text preprocessing and exploratory analysis. The text preprocessing step involves tasks such as tokenization, stemming, and stop word removal. An exploratory analysis of prepared text data can be performed to understand its main characteristics, such as the main topic of the text and word frequency distributions.

Tokenization

Word tokens are the basic units of text involved in any NLP task. The first step, when processing text, is to split it into tokens. NLTK provides different types of tokenizers for doing this. We will look at how to tokenize Twitter comments from the Twitter samples...

Exploratory analysis of text

Once we have the tokenized data, one of the basic analyses that is commonly performed is counting words or tokens and their distributions in the document. This will enable us to know more about the main topics in the document. Let's start by analyzing the web text data that comes with NLTK:

>>> import nltk
>>> from nltk.corpus import webtext
>>> webtext_sentences = webtext.sents('firefox.txt')
>>> webtext_words = webtext.words('firefox.txt')
>>> len(webtext_sentences)
1142
>>> len(webtext_words)
102457

Note that we have only loaded the text related to the Firefox discussion forum (firefox.txt), though the web text data has other data, as well (like advertisements and movie script text). The preceding code output gives the number of sentences and words, respectively, in the entire...

POS tagging

We have analyzed some of the basic NLP preprocessing tasks, such as tokenization, stemming, and stop word removal. We have also explored how to determine and visualize word distribution in a text corpus. In this section, we will get deeper into NLTK by looking at POS tagging.

What is POS tagging?

POS refers to categorizing the words in a sentence into specific syntactic or grammatical functions. In English, the main parts of speech are nouns, pronouns, adjectives, verbs, adverbs, prepositions, determiners, and conjunctions. POS tagging is the task of attaching one of these categories to each of the words or tokens in a text. NLTK provides both a set of tagged text corpus and a set of POS trainers for creating custom...

Training a sentiment classifier for movie reviews

We will now look at classifying sentiments in the movie reviews corpus in NLTK. The complete Jupyter Notebook for this example is available at Chapter02/01_example.ipynb, in the book's code repository.

First, we will load the movie reviews based on the sentiment categories, which are either positive or negative, using the following code:

cats = movie_reviews.categories()
reviews = []
for cat in cats:
for fid in movie_reviews.fileids(cat):
review = (list(movie_reviews.words(fid)),cat)
reviews.append(review)
random.shuffle(reviews)

The categories() function returns either pos or neg, for positive and negative sentiments, respectively. There are 1,000 reviews in each of the positive and negative categories. We use the Python random.shuffle() function to convert the grouped positive and negative reviews into a random...

Training a bag-of-words classifier

In the previous section, we utilized simple binary features for the words in the reviews in order to learn positive and negative sentiments. A better approach would be to use latent features, such as the frequency of the words used in the text. Compared to a binary representation of the presence or absence of words, the count of the words may better capture the characteristics of the text or document. Bag-of-words is a vector representation of text. Each of the vector dimensions captures either the frequency, presence or absence, or weighted values of words in the text. A bag-of-words representation does not capture the order of the words.

The binary feature extraction that was discussed in the previous section is, therefore, a simple bag-of-words representation of text. We will now look at an example of classifying sentiments in tweets using...

Summary

In this chapter, we covered common NLP tasks, such as preprocessing and exploratory analysis of text using the NLTK library. The unstructured characteristics of real-world data need extensive preprocessing, such as tokenization, stemming, and stop word removal, to make it suitable for ML. As you saw in the examples, NLTK provides a very extensive API for carrying out these preprocessing steps. It provides built-in packages and modules, and supports flexibility to build custom modules, such as user-defined stemmers and tokenizers.

We also discussed using NLTK for POS tagging, which is another common NLP task, used for issues such as word sense disambiguation and answering questions. Applications such as sentiment classification are widely used for their research and business value. We covered some basic examples of text classification, in the context of sentiment analysis...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • • Weave neural networks into linguistic applications across various platforms
  • • Perform NLP tasks and train its models using NLTK and TensorFlow
  • • Boost your NLP models with strong deep learning architectures such as CNNs and RNNs

Description

Natural language processing (NLP) has found its application in various domains, such as web search, advertisements, and customer services, and with the help of deep learning, we can enhance its performances in these areas. Hands-On Natural Language Processing with Python teaches you how to leverage deep learning models for performing various NLP tasks, along with best practices in dealing with today’s NLP challenges. To begin with, you will understand the core concepts of NLP and deep learning, such as Convolutional Neural Networks (CNNs), recurrent neural networks (RNNs), semantic embedding, Word2vec, and more. You will learn how to perform each and every task of NLP using neural networks, in which you will train and deploy neural networks in your NLP applications. You will get accustomed to using RNNs and CNNs in various application areas, such as text classification and sequence labeling, which are essential in the application of sentiment analysis, customer service chatbots, and anomaly detection. You will be equipped with practical knowledge in order to implement deep learning in your linguistic applications using Python's popular deep learning library, TensorFlow. By the end of this book, you will be well versed in building deep learning-backed NLP applications, along with overcoming NLP challenges with best practices developed by domain experts.

Who is this book for?

Hands-on Natural Language Processing with Python is for you if you are a developer, machine learning or an NLP engineer who wants to build a deep learning application that leverages NLP techniques. This comprehensive guide is also useful for deep learning users who want to extend their deep learning skills in building NLP applications. All you need is the basics of machine learning and Python to enjoy the book.

What you will learn

  • •Implement semantic embedding of words to classify and find entities
  • •Convert words to vectors by training in order to perform arithmetic operations
  • •Train a deep learning model to detect classification of tweets and news
  • •Implement a question-answer model with search and RNN models
  • •Train models for various text classification datasets using CNN
  • •Implement WaveNet a deep generative model for producing a natural-sounding voice
  • •Convert voice-to-text and text-to-voice
  • •Train a model to convert speech-to-text using DeepSpeech

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 18, 2018
Length: 312 pages
Edition : 1st
Language : English
ISBN-13 : 9781789139495
Category :
Languages :
Concepts :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jul 18, 2018
Length: 312 pages
Edition : 1st
Language : English
ISBN-13 : 9781789139495
Category :
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
R$50 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
R$500 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just R$25 each
Feature tick icon Exclusive print discounts
R$800 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just R$25 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total R$ 737.97
Hands-On Natural Language Processing with Python
R$245.99
Natural Language Processing with TensorFlow
R$245.99
Natural Language Processing and Computational Linguistics
R$245.99
Total R$ 737.97 Stars icon

Table of Contents

14 Chapters
Getting Started Chevron down icon Chevron up icon
Text Classification and POS Tagging Using NLTK Chevron down icon Chevron up icon
Deep Learning and TensorFlow Chevron down icon Chevron up icon
Semantic Embedding Using Shallow Models Chevron down icon Chevron up icon
Text Classification Using LSTM Chevron down icon Chevron up icon
Searching and DeDuplicating Using CNNs Chevron down icon Chevron up icon
Named Entity Recognition Using Character LSTM Chevron down icon Chevron up icon
Text Generation and Summarization Using GRUs Chevron down icon Chevron up icon
Question-Answering and Chatbots Using Memory Networks Chevron down icon Chevron up icon
Machine Translation Using the Attention-Based Model Chevron down icon Chevron up icon
Speech Recognition Using DeepSpeech Chevron down icon Chevron up icon
Text-to-Speech Using Tacotron Chevron down icon Chevron up icon
Deploying Trained Models Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.8
(4 Ratings)
5 star 25%
4 star 25%
3 star 0%
2 star 0%
1 star 50%
Russell Jurney Jul 07, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book’s coverage of things you can do to text data using natural language processing is excellent! It is quite a menu to choose from. It does assume you know Python but it says so at the beginning so the negative reviews aren’t valid.
Amazon Verified review Amazon
Santhosh Jan 29, 2020
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Lacks depth in the discussions, though the book covers important topics in NLP.Not worth the price.I supplement the book with Internet resources.
Amazon Verified review Amazon
Gary Woodfine Feb 24, 2019
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I don't think I can quite put into words, just how disappointed I am with this book. I feel the authors did an extremely poor job of attempting to explain this really interesting subject. For the most part, one might get the impression that authors took snippets from other books on the subject and tried to weave it into their book.I have had to re-read several chapters of the book, several times in order to try understand what it is the authors are trying to explain. I am really interested in the subject, but in my opinion this book, has actually made it more difficult for me to understand the subject!It may of course, just be me, but I don't think this book offers a hands on approach at all
Amazon Verified review Amazon
Edward Nelson Apr 11, 2019
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I got to Chapter 4, where the authors begin to tackle neural network NLP models. The main code example in the book has errors and is incomplete. The authors use advanced functions such as a generator and name spaces, with no explanation (and a decent programmer would have trouble figuring out what is going, even after consulting information about the commands used on the Internet). So, I thought, they should have a working example in the code base for the chapter. What I found instead was a mess. I found no code for the only real example of code in the chapter. I found code that belonged under subject headings in other chapters. Of 10 files, I found only 3 with passing relevance to the chapter. These are incomplete (e.g. using data files the whereabouts of which I cannot determine) and are not code that is discussed in the chapter. I haven't taken a close look at subsequent chapters, but suspect, from a quick look through the code base and the fact that code from Ch 4 belonged in other sections of the book, that the situation is unlikely to be better there. I complained to the publisher and their solution (or more likely the authors') was to publish the code on GitHub. However, the GitHub code for chapter 4 is identical to the code with which I had problems (I did a diff across all the files).
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.