Book Image

Hands-On Python Natural Language Processing

By : Aman Kedia, Mayank Rasu
4 (1)
Book Image

Hands-On Python Natural Language Processing

4 (1)
By: Aman Kedia, Mayank Rasu

Overview of this book

Natural Language Processing (NLP) is the subfield in computational linguistics that enables computers to understand, process, and analyze text. This book caters to the unmet demand for hands-on training of NLP concepts and provides exposure to real-world applications along with a solid theoretical grounding. This book starts by introducing you to the field of NLP and its applications, along with the modern Python libraries that you'll use to build your NLP-powered apps. With the help of practical examples, you’ll learn how to build reasonably sophisticated NLP applications, and cover various methodologies and challenges in deploying NLP applications in the real world. You'll cover key NLP tasks such as text classification, semantic embedding, sentiment analysis, machine translation, and developing a chatbot using machine learning and deep learning techniques. The book will also help you discover how machine learning techniques play a vital role in making your linguistic apps smart. Every chapter is accompanied by examples of real-world applications to help you build impressive NLP applications of your own. By the end of this NLP book, you’ll be able to work with language data, use machine learning to identify patterns in text, and get acquainted with the advancements in NLP.
Table of Contents (16 chapters)
1
Section 1: Introduction
4
Section 2: Natural Language Representation and Mathematics
9
Section 3: NLP and Learning

To get the most out of this book

You will need Python 3 installed on your system. You can use any IDE to practice the code samples provided in the book, but since the code samples are provided as Jupyter notebooks, we recommend installing the Jupyter IDE. All code examples have been tested on the Windows OS. However, the programs are platform agnostic and should work with other 32/64-bit OSes as well. Other system requirements include RAM of 4 GB or higher, and at least 6 GB of free disk space.

We recommend installing the Python libraries discussed in this book using pip or conda. The code snippets in the book mention the relevant command to install a given library on the Windows OS. Please refer to the source page of the library for installation instructions for other OSes.

Software/hardware covered in the book

OS requirements

pandas

Windows 7 or later, macOS, Linux

NumPy

Windows 7 or later, macOS, Linux

Jupyter

Windows 7 or later, macOS, Linux

beautifulsoup4

Windows 7 or later, macOS, Linux

scikit-learn

Windows 7 or later, macOS, Linux

Keras

Windows 7 or later, macOS, Linux

NLTK

Windows 7 or later, macOS, Linux

The last project covered in this book requires a higher-spec machine. However, you can run the program on the Google Colab GPU machine if needs be.

If you are using the digital version of this book, we advise you to type the code yourself or access the code via the GitHub repository (link available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packt.com.
  2. Select the Support tab.
  3. Click on Code Downloads.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Hands-On-Python-Natural-Language-Processing. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://static.packt-cdn.com/downloads/9781838989590_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "We will be performing preprocessing on the Tips dataset, which comes with the seaborn Python package."

A block of code is set as follows:

import pandas as pd
data = pd.read_csv("amazon_cells_labelled.txt", sep='\t', header=None)

X = data.iloc[:,0] # extract column with review
y = data.iloc[:,-1] # extract column with sentiment

# tokenize the news text and convert data in matrix format
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer(stop_words='english')
X_vec = vectorizer.fit_transform(X)
X_vec = X_vec.todense() # convert sparse matrix into dense matrix

# Transform data by applying term frequency inverse document frequency (TFIDF)
from sklearn.feature_extraction.text import TfidfTransformer
tfidf = TfidfTransformer()
X_tfidf = tfidf.fit_transform(X_vec)
X_tfidf = X_tfidf.todense()

Any command-line input or output is written as follows:

pip install requests
pip install beautifulsoup4

Bold: Indicates a new term, an important word, or words that you see on screen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "This is called cross-validation and is an important part of ML model training. "

Warnings or important notes appear like this.
Tips and tricks appear like this.