Python Natural Language Processing Cookbook

Book description

Get to grips with solving real-world NLP problems, such as dependency parsing, information extraction, topic modeling, and text data visualization

Key Features

  • Analyze varying complexities of text using popular Python packages such as NLTK, spaCy, sklearn, and gensim
  • Implement common and not-so-common linguistic processing tasks using Python libraries
  • Overcome the common challenges faced while implementing NLP pipelines

Book Description

Python is the most widely used language for natural language processing (NLP) thanks to its extensive tools and libraries for analyzing text and extracting computer-usable data. This book will take you through a range of techniques for text processing, from basics such as parsing the parts of speech to complex topics such as topic modeling, text classification, and visualization.

Starting with an overview of NLP, the book presents recipes for dividing text into sentences, stemming and lemmatization, removing stopwords, and parts of speech tagging to help you to prepare your data. You’ll then learn ways of extracting and representing grammatical information, such as dependency parsing and anaphora resolution, discover different ways of representing the semantics using bag-of-words, TF-IDF, word embeddings, and BERT, and develop skills for text classification using keywords, SVMs, LSTMs, and other techniques. As you advance, you’ll also see how to extract information from text, implement unsupervised and supervised techniques for topic modeling, and perform topic modeling of short texts, such as tweets. Additionally, the book shows you how to develop chatbots using NLTK and Rasa and visualize text data.

By the end of this NLP book, you’ll have developed the skills to use a powerful set of tools for text processing.

What you will learn

  • Become well-versed with basic and advanced NLP techniques in Python
  • Represent grammatical information in text using spaCy, and semantic information using bag-of-words, TF-IDF, and word embeddings
  • Perform text classification using different methods, including SVMs and LSTMs
  • Explore different techniques for topic modeling such as K-means, LDA, NMF, and BERT
  • Work with visualization techniques such as NER and word clouds for different NLP tools
  • Build a basic chatbot using NLTK and Rasa
  • Extract information from text using regular expression techniques and statistical and deep learning tools

Who this book is for

This book is for data scientists and professionals who want to learn how to work with text. Intermediate knowledge of Python will help you to make the most out of this book. If you are an NLP practitioner, this book will serve as a code reference when working on your projects.

Table of contents

  1. Python Natural Language Processing Cookbook
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
    4. Download the color images
    5. Conventions used
    6. Sections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    7. Get in touch
    8. Reviews
  6. Chapter 1: Learning NLP Basics
    1. Technical requirements
    2. Dividing text into sentences
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    3. Dividing sentences into words – tokenization
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    4. Parts of speech tagging
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Word stemming
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    6. Combining similar words – lemmatization
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    7. Removing stopwords
      1. Getting ready…
      2. How to do it…
      3. How it works…
      4. There's more…
  7. Chapter 2: Playing with Grammar
    1. Technical requirements
    2. Counting nouns – plural and singular nouns
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    3. Getting the dependency parse
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    4. Splitting sentences into clauses
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Extracting noun chunks
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    6. Extracting entities and relations
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    7. Extracting subjects and objects of the sentence
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    8. Finding references – anaphora resolution
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
  8. Chapter 3: Representing Text – Capturing Semantics
    1. Technical requirements
    2. Putting documents into a bag of words
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    3. Constructing the N-gram model
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. Representing texts with TF-IDF
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    5. Using word embeddings
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    6. Training your own embeddings model
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    7. Representing phrases – phrase2vec
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    8. Using BERT instead of word embeddings
      1. Getting ready
      2. How to do it…
      3. How it works…
    9. Getting started with semantic search
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
  9. Chapter 4: Classifying Texts
    1. Technical requirements
    2. Getting the dataset and evaluation baseline ready
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Performing rule-based text classification using keywords
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. Clustering sentences using K-means – unsupervised text classification
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Using SVMs for supervised text classification
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    6. Using LSTMs for supervised text classification
      1. Getting ready
      2. How to do it…
      3. How it works…
  10. Chapter 5: Getting Started with Information Extraction
    1. Technical requirements
    2. Using regular expressions
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    3. Finding similar strings: the Levenshtein distance
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    4. Performing named entity recognition using spaCy
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    5. Training your own NER model with spaCy
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    6. Discovering sentiment analysis
      1. Getting ready
      2. How to do it…
      3. How it works…
    7. Sentiment for short texts using LSTM: Twitter
      1. Getting ready
      2. How to do it…
      3. How it works…
    8. Using BERT for sentiment analysis
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
  11. Chapter 6: Topic Modeling
    1. Technical requirements
    2. LDA topic modeling with sklearn
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    3. LDA topic modeling with gensim
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. NMF topic modeling
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. K-means topic modeling with BERT
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. Topic modeling of short texts
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
  12. Chapter 7: Building Chatbots
    1. Technical requirements
    2. Building a basic chatbot with keyword matching
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    3. Building a basic Rasa chatbot
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    4. Creating question-answer pairs with Rasa
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Creating and visualizing conversation paths with Rasa
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. Creating actions for the Rasa chatbot
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
  13. Chapter 8: Visualizing Text Data
    1. Technical requirements
    2. Visualizing the dependency parse
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Visualizing parts of speech
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Visualizing NER
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Constructing word clouds
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    6. Visualizing topics
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    7. Why subscribe?
  14. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Leave a review - let other readers know what you think

Product information

  • Title: Python Natural Language Processing Cookbook
  • Author(s): Zhenya Antić
  • Release date: March 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781838987312