100% found this document useful (10 votes)
30 views

Immediate download (Ebook) Natural Language Processing Recipes: Unlocking Text Data with Machine Learning and Deep Learning Using Python by Akshay Kulkarni, Adarsha Shivananda ISBN 9781484273517, 9781484273500, 1484273508, 1484273516 ebooks 2024

The document provides information about various ebooks available for download, particularly focusing on titles related to Natural Language Processing and machine learning. It includes details such as authors, ISBN numbers, and links to access the ebooks. Additionally, it mentions the availability of different formats and instant downloads for readers.

Uploaded by

swamyjinnyqp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (10 votes)
30 views

Immediate download (Ebook) Natural Language Processing Recipes: Unlocking Text Data with Machine Learning and Deep Learning Using Python by Akshay Kulkarni, Adarsha Shivananda ISBN 9781484273517, 9781484273500, 1484273508, 1484273516 ebooks 2024

The document provides information about various ebooks available for download, particularly focusing on titles related to Natural Language Processing and machine learning. It includes details such as authors, ISBN numbers, and links to access the ebooks. Additionally, it mentions the availability of different formats and instant downloads for readers.

Uploaded by

swamyjinnyqp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

Download the Full Ebook and Access More Features - ebooknice.

com

(Ebook) Natural Language Processing Recipes:


Unlocking Text Data with Machine Learning and Deep
Learning Using Python by Akshay Kulkarni, Adarsha
Shivananda ISBN 9781484273517, 9781484273500,
1484273508, 1484273516
https://ebooknice.com/product/natural-language-processing-
recipes-unlocking-text-data-with-machine-learning-and-deep-
learning-using-python-34204734

OR CLICK HERE

DOWLOAD EBOOK

Download more ebook instantly today at https://ebooknice.com


Instant digital products (PDF, ePub, MOBI) ready for you
Download now and discover formats that fit your needs...

Start reading on any device today!

(Ebook) Natural Language Processing Recipes: Unlocking


Text Data with Machine Learning and Deep Learning Using
Python by Akshay Kulkarni, Adarsha Shivananda ISBN
9781484273500, 1484273508
https://ebooknice.com/product/natural-language-processing-recipes-
unlocking-text-data-with-machine-learning-and-deep-learning-using-
python-34204404
ebooknice.com

(Ebook) Time Series Algorithms Recipes: Implement Machine


Learning and Deep Learning Techniques with Python by
Akshay Kulkarni, Adarsha Shivananda, Anoosh Kulkarni, V
Adithya Krishnan ISBN 9781484289785, 9781484289778,
https://ebooknice.com/product/time-series-algorithms-recipes-
1484289781, 1484289773
implement-machine-learning-and-deep-learning-techniques-with-
python-47430754
ebooknice.com

(Ebook) Biota Grow 2C gather 2C cook by Loucas, Jason;


Viles, James ISBN 9781459699816, 9781743365571,
9781925268492, 1459699815, 1743365578, 1925268497
https://ebooknice.com/product/biota-grow-2c-gather-2c-cook-6661374

ebooknice.com

(Ebook) Natural Language Processing Projects: Build Next-


Generation NLP Applications Using AI Techniques by Akshay
Kulkarni, Adarsha Shivananda, Anoosh Kulkarni ISBN
9781484273852, 1484273850
https://ebooknice.com/product/natural-language-processing-projects-
build-next-generation-nlp-applications-using-ai-techniques-36506460

ebooknice.com
(Ebook) Applied Natural Language Processing with Python:
Implementing Machine Learning and Deep Learning Algorithms
for Natural Language Processing by Taweh Beysolow II ISBN
9781484237328, 1484237323
https://ebooknice.com/product/applied-natural-language-processing-
with-python-implementing-machine-learning-and-deep-learning-
algorithms-for-natural-language-processing-7185412
ebooknice.com

(Ebook) Python Natural Language Processing: Advanced


machine learning and deep learning techniques for natural
language processing by Jalaj Thanaki ISBN 9781787121423,
1787121429
https://ebooknice.com/product/python-natural-language-processing-
advanced-machine-learning-and-deep-learning-techniques-for-natural-
language-processing-7212480
ebooknice.com

(Ebook) Matematik 5000+ Kurs 2c Lärobok by Lena


Alfredsson, Hans Heikne, Sanna Bodemyr ISBN 9789127456600,
9127456609
https://ebooknice.com/product/matematik-5000-kurs-2c-larobok-23848312

ebooknice.com

(Ebook) SAT II Success MATH 1C and 2C 2002 (Peterson's SAT


II Success) by Peterson's ISBN 9780768906677, 0768906679

https://ebooknice.com/product/sat-ii-success-
math-1c-and-2c-2002-peterson-s-sat-ii-success-1722018

ebooknice.com

(Ebook) Deep Learning for Natural Language Processing:


Develop Deep Learning Models for Natural Language in
Python by Jason Brownlee
https://ebooknice.com/product/deep-learning-for-natural-language-
processing-develop-deep-learning-models-for-natural-language-in-
python-11575184
ebooknice.com
Natural Language
Processing
Recipes
Unlocking Text Data with Machine Learning
and Deep Learning Using Python

Second Edition

Akshay Kulkarni
Adarsha Shivananda
Natural Language
Processing Recipes
Unlocking Text Data with
Machine Learning and Deep Learning
Using Python
Second Edition

Akshay Kulkarni
Adarsha Shivananda
Natural Language Processing Recipes: Unlocking Text Data with Machine Learning
and Deep Learning Using Python
Akshay Kulkarni Adarsha Shivananda
Bangalore, Karnataka, India Bangalore, Karnataka, India

ISBN-13 (pbk): 978-1-4842-7350-0     ISBN-13 (electronic): 978-1-4842-7351-7


https://doi.org/10.1007/978-1-4842-7351-7

Copyright © 2021 by Akshay Kulkarni and Adarsha Shivananda


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Celestin Suresh John
Development Editor: Laura Berendson
Coordinating Editor: Shrikant Vishwakarma
Cover designed by eStudioCalamar
Cover image designed by Pexels
Distributed to the book trade worldwide by Springer Science+Business Media LLC, 1 New York Plaza, Suite
4600, New York, NY 10004. Phone 1-800-SPRINGER, fax (201) 348-4505, email orders-ny@springer-sbm.
com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner)
is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware
corporation.
For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback,
or audio rights, please e-mail bookpermissions@springernature.com, or visit http://www.apress.com/
rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at www.apress.com/978-­1-­4842-­7350-­0. For more
detailed information, please visit http://www.apress.com/source-­code.
Printed on acid-free paper
To our families
Table of Contents
About the Authors����������������������������������������������������������������������������������������������������xv

About the Technical Reviewer�������������������������������������������������������������������������������xvii


Acknowledgments��������������������������������������������������������������������������������������������������xix

Introduction������������������������������������������������������������������������������������������������������������xxi

Chapter 1: Extracting the Data��������������������������������������������������������������������������������� 1


Introduction����������������������������������������������������������������������������������������������������������������������������������� 1
Client Data������������������������������������������������������������������������������������������������������������������������������������ 1
Free Sources��������������������������������������������������������������������������������������������������������������������������������� 2
Web Scraping�������������������������������������������������������������������������������������������������������������������������������� 2
Recipe 1-1. Collecting Data���������������������������������������������������������������������������������������������������������� 2
Problem����������������������������������������������������������������������������������������������������������������������������������� 3
Solution����������������������������������������������������������������������������������������������������������������������������������� 3
How It Works��������������������������������������������������������������������������������������������������������������������������� 3
Recipe 1-2. Collecting Data from PDFs����������������������������������������������������������������������������������������� 4
Problem����������������������������������������������������������������������������������������������������������������������������������� 4
Solution����������������������������������������������������������������������������������������������������������������������������������� 5
How It Works��������������������������������������������������������������������������������������������������������������������������� 5
Recipe 1-3. Collecting Data from Word Files�������������������������������������������������������������������������������� 6
Problem����������������������������������������������������������������������������������������������������������������������������������� 6
Solution����������������������������������������������������������������������������������������������������������������������������������� 6
How It Works��������������������������������������������������������������������������������������������������������������������������� 6
Recipe 1-4. Collecting Data from JSON���������������������������������������������������������������������������������������� 7
Problem����������������������������������������������������������������������������������������������������������������������������������� 7
Solution����������������������������������������������������������������������������������������������������������������������������������� 7
How It Works��������������������������������������������������������������������������������������������������������������������������� 8

v
Table of Contents

Recipe 1-5. Collecting Data from HTML�������������������������������������������������������������������������������������� 10


Problem��������������������������������������������������������������������������������������������������������������������������������� 10
Solution��������������������������������������������������������������������������������������������������������������������������������� 10
How It Works������������������������������������������������������������������������������������������������������������������������� 10
Recipe 1-6. Parsing Text Using Regular Expressions������������������������������������������������������������������ 13
Problem��������������������������������������������������������������������������������������������������������������������������������� 13
Solution��������������������������������������������������������������������������������������������������������������������������������� 13
How It Works������������������������������������������������������������������������������������������������������������������������� 14
Recipe 1-7. Handling Strings������������������������������������������������������������������������������������������������������ 21
Problem��������������������������������������������������������������������������������������������������������������������������������� 21
Solution��������������������������������������������������������������������������������������������������������������������������������� 21
How It Works������������������������������������������������������������������������������������������������������������������������� 22
Recipe 1-8. Scraping Text from the Web������������������������������������������������������������������������������������� 23
Problem��������������������������������������������������������������������������������������������������������������������������������� 24
Solution��������������������������������������������������������������������������������������������������������������������������������� 24
How It Works������������������������������������������������������������������������������������������������������������������������� 24

Chapter 2: Exploring and Processing Text Data������������������������������������������������������ 31


Recipe 2-1. Converting Text Data to Lowercase������������������������������������������������������������������������� 32
Problem��������������������������������������������������������������������������������������������������������������������������������� 32
Solution��������������������������������������������������������������������������������������������������������������������������������� 32
How It Works������������������������������������������������������������������������������������������������������������������������� 32
Recipe 2-2. Removing Punctuation��������������������������������������������������������������������������������������������� 34
Problem��������������������������������������������������������������������������������������������������������������������������������� 34
Solution��������������������������������������������������������������������������������������������������������������������������������� 34
How It Works������������������������������������������������������������������������������������������������������������������������� 34
Recipe 2-3. Removing Stop Words���������������������������������������������������������������������������������������������� 36
Problem��������������������������������������������������������������������������������������������������������������������������������� 36
Solution��������������������������������������������������������������������������������������������������������������������������������� 36
How It Works������������������������������������������������������������������������������������������������������������������������� 37

vi
Table of Contents

Recipe 2-4. Standardizing Text��������������������������������������������������������������������������������������������������� 38


Problem��������������������������������������������������������������������������������������������������������������������������������� 38
Solution��������������������������������������������������������������������������������������������������������������������������������� 38
How It Works������������������������������������������������������������������������������������������������������������������������� 39
Recipe 2-5. Correcting Spelling�������������������������������������������������������������������������������������������������� 40
Problem��������������������������������������������������������������������������������������������������������������������������������� 40
Solution��������������������������������������������������������������������������������������������������������������������������������� 40
How It Works������������������������������������������������������������������������������������������������������������������������� 40
Recipe 2-6. Tokenizing Text�������������������������������������������������������������������������������������������������������� 42
Problem��������������������������������������������������������������������������������������������������������������������������������� 42
Solution��������������������������������������������������������������������������������������������������������������������������������� 42
How It Works������������������������������������������������������������������������������������������������������������������������� 42
Recipe 2-7. Stemming���������������������������������������������������������������������������������������������������������������� 44
Problem��������������������������������������������������������������������������������������������������������������������������������� 44
Solution��������������������������������������������������������������������������������������������������������������������������������� 44
How It Works������������������������������������������������������������������������������������������������������������������������� 44
Recipe 2-8. Lemmatizing������������������������������������������������������������������������������������������������������������ 45
Problem��������������������������������������������������������������������������������������������������������������������������������� 46
Solution��������������������������������������������������������������������������������������������������������������������������������� 46
How It Works������������������������������������������������������������������������������������������������������������������������� 46
Recipe 2-9. Exploring Text Data�������������������������������������������������������������������������������������������������� 47
Problem��������������������������������������������������������������������������������������������������������������������������������� 47
Solution��������������������������������������������������������������������������������������������������������������������������������� 47
How It Works������������������������������������������������������������������������������������������������������������������������� 48
Recipe 2-10. Dealing with Emojis and Emoticons���������������������������������������������������������������������� 52
Problem��������������������������������������������������������������������������������������������������������������������������������� 53
Solution��������������������������������������������������������������������������������������������������������������������������������� 53
How It Works������������������������������������������������������������������������������������������������������������������������� 53
Problem��������������������������������������������������������������������������������������������������������������������������������� 54
Solution��������������������������������������������������������������������������������������������������������������������������������� 54
How It Works������������������������������������������������������������������������������������������������������������������������� 54

vii
Table of Contents

Problem��������������������������������������������������������������������������������������������������������������������������������� 55
Solution��������������������������������������������������������������������������������������������������������������������������������� 55
How It Works������������������������������������������������������������������������������������������������������������������������� 55
Problem��������������������������������������������������������������������������������������������������������������������������������� 56
Solution��������������������������������������������������������������������������������������������������������������������������������� 56
How It Works������������������������������������������������������������������������������������������������������������������������� 57
Problem��������������������������������������������������������������������������������������������������������������������������������� 58
Solution��������������������������������������������������������������������������������������������������������������������������������� 58
How It Works������������������������������������������������������������������������������������������������������������������������� 58
Recipe 2-11. Building a Text Preprocessing Pipeline������������������������������������������������������������������ 59
Problem��������������������������������������������������������������������������������������������������������������������������������� 59
Solution��������������������������������������������������������������������������������������������������������������������������������� 59
How It Works������������������������������������������������������������������������������������������������������������������������� 60

Chapter 3: Converting Text to Features������������������������������������������������������������������ 63


Recipe 3-1. Converting Text to Features Using One-­Hot Encoding��������������������������������������������� 64
Problem��������������������������������������������������������������������������������������������������������������������������������� 64
Solution��������������������������������������������������������������������������������������������������������������������������������� 64
How It Works������������������������������������������������������������������������������������������������������������������������� 64
Recipe 3-2. Converting Text to Features Using a Count Vectorizer��������������������������������������������� 65
Problem��������������������������������������������������������������������������������������������������������������������������������� 65
Solution��������������������������������������������������������������������������������������������������������������������������������� 66
How It Works������������������������������������������������������������������������������������������������������������������������� 66
Recipe 3-3. Generating n-grams������������������������������������������������������������������������������������������������� 67
Problem��������������������������������������������������������������������������������������������������������������������������������� 67
Solution��������������������������������������������������������������������������������������������������������������������������������� 67
How It Works������������������������������������������������������������������������������������������������������������������������� 68
Recipe 3-4. Generating a Co-occurrence Matrix������������������������������������������������������������������������� 69
Problem��������������������������������������������������������������������������������������������������������������������������������� 69
Solution��������������������������������������������������������������������������������������������������������������������������������� 70
How It Works������������������������������������������������������������������������������������������������������������������������� 70

viii
Table of Contents

Recipe 3-5. Hash Vectorizing������������������������������������������������������������������������������������������������������ 72


Problem��������������������������������������������������������������������������������������������������������������������������������� 72
Solution��������������������������������������������������������������������������������������������������������������������������������� 72
How It Works������������������������������������������������������������������������������������������������������������������������� 72
Recipe 3-6. Converting Text to Features Using TF-­IDF���������������������������������������������������������������� 73
Problem��������������������������������������������������������������������������������������������������������������������������������� 73
Solution��������������������������������������������������������������������������������������������������������������������������������� 73
How It Works������������������������������������������������������������������������������������������������������������������������� 74
Recipe 3-7. Implementing Word Embeddings����������������������������������������������������������������������������� 75
Problem��������������������������������������������������������������������������������������������������������������������������������� 76
Solution��������������������������������������������������������������������������������������������������������������������������������� 77
How It Works������������������������������������������������������������������������������������������������������������������������� 77
Recipe 3-8. Implementing fastText��������������������������������������������������������������������������������������������� 84
Problem��������������������������������������������������������������������������������������������������������������������������������� 84
Solution��������������������������������������������������������������������������������������������������������������������������������� 84
How It Works������������������������������������������������������������������������������������������������������������������������� 84
Recipe 3-9. Converting Text to Features Using State-­of-­the-Art Embeddings���������������������������� 87
Problem��������������������������������������������������������������������������������������������������������������������������������� 87
Solution��������������������������������������������������������������������������������������������������������������������������������� 87
ELMo�������������������������������������������������������������������������������������������������������������������������������������� 88
Sentence Encoders���������������������������������������������������������������������������������������������������������������� 89
Open-AI GPT�������������������������������������������������������������������������������������������������������������������������� 91
How It Works������������������������������������������������������������������������������������������������������������������������� 91

Chapter 4: Advanced Natural Language Processing��������������������������������������������� 107


Recipe 4-1. Extracting Noun Phrases��������������������������������������������������������������������������������������� 109
Problem������������������������������������������������������������������������������������������������������������������������������� 109
Solution������������������������������������������������������������������������������������������������������������������������������� 109
How It Works����������������������������������������������������������������������������������������������������������������������� 109
Recipe 4-2. Finding Similarity Between Texts��������������������������������������������������������������������������� 110
Solution������������������������������������������������������������������������������������������������������������������������������� 110
How It Works����������������������������������������������������������������������������������������������������������������������� 110

ix
Table of Contents

Recipe 4-3. Tagging Part of Speech������������������������������������������������������������������������������������������ 113


Problem������������������������������������������������������������������������������������������������������������������������������� 113
Solution������������������������������������������������������������������������������������������������������������������������������� 113
How It Works����������������������������������������������������������������������������������������������������������������������� 113
Recipe 4-4. Extracting Entities from Text���������������������������������������������������������������������������������� 116
Problem������������������������������������������������������������������������������������������������������������������������������� 116
Solution������������������������������������������������������������������������������������������������������������������������������� 116
How It Works����������������������������������������������������������������������������������������������������������������������� 116
Recipe 4-5. Extracting Topics from Text������������������������������������������������������������������������������������ 118
Problem������������������������������������������������������������������������������������������������������������������������������� 118
Solution������������������������������������������������������������������������������������������������������������������������������� 118
How It Works����������������������������������������������������������������������������������������������������������������������� 118
Recipe 4-6. Classifying Text������������������������������������������������������������������������������������������������������ 121
Problem������������������������������������������������������������������������������������������������������������������������������� 121
Solution������������������������������������������������������������������������������������������������������������������������������� 121
How It Works����������������������������������������������������������������������������������������������������������������������� 122
Recipe 4-7. Carrying Out Sentiment Analysis��������������������������������������������������������������������������� 125
Problem������������������������������������������������������������������������������������������������������������������������������� 125
Solution������������������������������������������������������������������������������������������������������������������������������� 125
How It Works����������������������������������������������������������������������������������������������������������������������� 125
Recipe 4-8. Disambiguating Text���������������������������������������������������������������������������������������������� 127
Problem������������������������������������������������������������������������������������������������������������������������������� 127
Solution������������������������������������������������������������������������������������������������������������������������������� 127
How It Works����������������������������������������������������������������������������������������������������������������������� 127
Recipe 4-9. Converting Speech to Text������������������������������������������������������������������������������������� 128
Problem������������������������������������������������������������������������������������������������������������������������������� 129
Solution������������������������������������������������������������������������������������������������������������������������������� 129
How It Works����������������������������������������������������������������������������������������������������������������������� 129
Recipe 4-10. Converting Text to Speech����������������������������������������������������������������������������������� 131
Problem������������������������������������������������������������������������������������������������������������������������������� 131

x
Table of Contents

Solution������������������������������������������������������������������������������������������������������������������������������� 131
How It Works����������������������������������������������������������������������������������������������������������������������� 131
Recipe 4-11. Translating Speech���������������������������������������������������������������������������������������������� 132
Problem������������������������������������������������������������������������������������������������������������������������������� 132
Solution������������������������������������������������������������������������������������������������������������������������������� 132
How It Works����������������������������������������������������������������������������������������������������������������������� 132

Chapter 5: Implementing Industry Applications��������������������������������������������������� 135


Recipe 5-1. Implementing Multiclass Classification����������������������������������������������������������������� 135
Problem������������������������������������������������������������������������������������������������������������������������������� 136
Solution������������������������������������������������������������������������������������������������������������������������������� 136
How It Works����������������������������������������������������������������������������������������������������������������������� 136
Recipe 5-2. Implementing Sentiment Analysis������������������������������������������������������������������������� 143
Problem������������������������������������������������������������������������������������������������������������������������������� 143
Solution������������������������������������������������������������������������������������������������������������������������������� 143
How It Works����������������������������������������������������������������������������������������������������������������������� 143
Recipe 5-3. Applying Text Similarity Functions������������������������������������������������������������������������� 154
Problem������������������������������������������������������������������������������������������������������������������������������� 154
Solution������������������������������������������������������������������������������������������������������������������������������� 155
How It Works����������������������������������������������������������������������������������������������������������������������� 155
Recipe 5-4. Summarizing Text Data������������������������������������������������������������������������������������������ 165
Problem������������������������������������������������������������������������������������������������������������������������������� 166
Solution������������������������������������������������������������������������������������������������������������������������������� 166
How It Works����������������������������������������������������������������������������������������������������������������������� 166
Recipe 5-5. Clustering Documents������������������������������������������������������������������������������������������� 172
Problem������������������������������������������������������������������������������������������������������������������������������� 172
Solution������������������������������������������������������������������������������������������������������������������������������� 172
How It Works����������������������������������������������������������������������������������������������������������������������� 172
Recipe 5-6. NLP in a Search Engine����������������������������������������������������������������������������������������� 178
Problem������������������������������������������������������������������������������������������������������������������������������� 178
Solution������������������������������������������������������������������������������������������������������������������������������� 178
How It Works����������������������������������������������������������������������������������������������������������������������� 179

xi
Table of Contents

Recipe 5-7. Detecting Fake News��������������������������������������������������������������������������������������������� 181


Problem������������������������������������������������������������������������������������������������������������������������������� 181
Solution������������������������������������������������������������������������������������������������������������������������������� 182
How It Works����������������������������������������������������������������������������������������������������������������������� 182
Recipe 5-8. Movie Genre Tagging��������������������������������������������������������������������������������������������� 195
Problem������������������������������������������������������������������������������������������������������������������������������� 195
Solution������������������������������������������������������������������������������������������������������������������������������� 196
How It Works����������������������������������������������������������������������������������������������������������������������� 197

Chapter 6: Deep Learning for NLP������������������������������������������������������������������������ 213


Introduction to Deep Learning�������������������������������������������������������������������������������������������������� 213
Convolutional Neural Networks������������������������������������������������������������������������������������������������� 215
Data������������������������������������������������������������������������������������������������������������������������������������������� 215
Architecture������������������������������������������������������������������������������������������������������������������������������ 216
Convolution������������������������������������������������������������������������������������������������������������������������������� 216
Nonlinearity (ReLU)������������������������������������������������������������������������������������������������������������������� 216
Pooling�������������������������������������������������������������������������������������������������������������������������������������� 217
Flatten, Fully Connected, and Softmax Layers�������������������������������������������������������������������������� 217
Backpropagation: Training the Neural Network������������������������������������������������������������������������ 218
Recurrent Neural Networks������������������������������������������������������������������������������������������������������ 218
Training RNN: Backpropagation Through Time (BPTT)�������������������������������������������������������������� 219
Long Short-Term Memory (LSTM)��������������������������������������������������������������������������������������������� 219
Recipe 6-1. Retrieving Information������������������������������������������������������������������������������������������� 220
Problem������������������������������������������������������������������������������������������������������������������������������� 221
Solution������������������������������������������������������������������������������������������������������������������������������� 221
How It Works����������������������������������������������������������������������������������������������������������������������� 222
Recipe 6-2. Classifying Text with Deep Learning���������������������������������������������������������������������� 227
Problem������������������������������������������������������������������������������������������������������������������������������� 227
Solution������������������������������������������������������������������������������������������������������������������������������� 227
How It Works����������������������������������������������������������������������������������������������������������������������� 228

xii
Table of Contents

Recipe 6-3. Next Word Prediction��������������������������������������������������������������������������������������������� 240


Problem������������������������������������������������������������������������������������������������������������������������������� 241
Solution������������������������������������������������������������������������������������������������������������������������������� 241
How It Works����������������������������������������������������������������������������������������������������������������������� 241
Recipe 6-4. Stack Overflow question recommendation������������������������������������������������������������ 248
Problem������������������������������������������������������������������������������������������������������������������������������� 249
Solution������������������������������������������������������������������������������������������������������������������������������� 249
How It Works����������������������������������������������������������������������������������������������������������������������� 249

Chapter 7: Conclusion and Next-Gen NLP������������������������������������������������������������� 263


Recipe 7-1. Recent advancements in text to features or distributed representations������������� 265
Problem������������������������������������������������������������������������������������������������������������������������������� 265
Solution������������������������������������������������������������������������������������������������������������������������������� 265
Recipe 7-2. Advanced deep learning for NLP��������������������������������������������������������������������������� 265
Problem������������������������������������������������������������������������������������������������������������������������������� 265
Solution������������������������������������������������������������������������������������������������������������������������������� 265
Recipe 7-3. Reinforcement learning applications in NLP��������������������������������������������������������� 266
Problem������������������������������������������������������������������������������������������������������������������������������� 266
Solution������������������������������������������������������������������������������������������������������������������������������� 266
Recipe 7-4. Transfer learning and pre-trained models������������������������������������������������������������� 267
Problem������������������������������������������������������������������������������������������������������������������������������� 267
Solution������������������������������������������������������������������������������������������������������������������������������� 268
Recipe 7-5. Meta-learning in NLP��������������������������������������������������������������������������������������������� 273
Problem������������������������������������������������������������������������������������������������������������������������������� 273
Solution������������������������������������������������������������������������������������������������������������������������������� 273
Recipe 7-6. Capsule networks for NLP������������������������������������������������������������������������������������� 274
Problem������������������������������������������������������������������������������������������������������������������������������� 274
Solution������������������������������������������������������������������������������������������������������������������������������� 274

Index��������������������������������������������������������������������������������������������������������������������� 277

xiii
About the Authors
Akshay Kulkarni is a renowned AI and machine learning
evangelist and thought leader. He has consulted several
Fortune 500 and global enterprises on driving AI and
data science–led strategic transformation. Akshay has
rich experience in building and scaling AI and machine
learning businesses and creating significant impact. He
is currently a data science and AI manager at Publicis
Sapient, where he is part of strategy and transformation
interventions through AI. He manages high-priority
growth initiatives around data science and works on
various artificial intelligence engagements by applying
state-of-the-art techniques to this space.
Akshay is also a Google Developers Expert in machine learning, a published author
of books on NLP and deep learning, and a regular speaker at major AI and data science
conferences.
In 2019, Akshay was named one of the top “40 under 40 data scientists” in India.
In his spare time, he enjoys reading, writing, coding, and mentoring aspiring data
scientists. He lives in Bangalore, India, with his family.

Adarsha Shivananda is a lead data scientist at Indegene


Inc.’s product and technology team, where he leads a
group of analysts who enable predictive analytics and AI
features to healthcare software products. These are mainly
multichannel activities for pharma products and solving
the real-time problems encountered by pharma sales reps.
Adarsha aims to build a pool of exceptional data scientists
within the organization to solve greater health care problems
through brilliant training programs. He always wants to stay
ahead of the curve.

xv
About the Authors

His core expertise involves machine learning, deep learning, recommendation


systems, and statistics. Adarsha has worked on various data science projects across
multiple domains using different technologies and methodologies. Previously, he
worked for Tredence Analytics and IQVIA.
He lives in Bangalore, India, and loves to read, ride, and teach data science.

xvi
About the Technical Reviewer
Aakash Kag is a data scientist at AlixPartners and is a
co-founder of the Emeelan application. He has six years
of experience in big data analytics and has a postgraduate
degree in computer science with a specialization in big data
analytics. Aakash is passionate about developing social
platforms, machine learning, and meetups, where he often
talks.

xvii
Acknowledgments
We are grateful to our families for their motivation and constant support.
We want to express our gratitude to out mentors and friends for their input,
inspiration, and support. A special thanks to Anoosh R. Kulkarni, a data scientist at
Quantziq, for his support in writing this book and his technical input. A big thanks to the
Apress team for their constant support and help.
Finally, we would like to thank you, the reader, for showing an interest in this book
and making your natural language processing journey more exciting.
Note that the views and opinions expressed in this book are those of the authors.

xix
Introduction
According to industry estimates, more than 80% of the data being generated is in an
unstructured format in the form of text, images, audio, or video. Data is being generated
as we speak, write, tweet, use social media platforms, send messages on messaging
platforms, use ecommerce to shop, and do various other activities. The majority of this
data exists in textual form.

So, what is unstructured data? Unstructured data is information that doesn't reside
in a traditional relational database. Examples include documents, blogs, social media
feeds, pictures, and videos.
Most of the insights are locked within different types of unstructured data. Unlocking
unstructured data plays a vital role in every organization wanting to make improved and
better decisions. This book unlocks the potential of textual data.
Textual data is the most common and comprises more than 50% of unstructured
data. Examples include tweets/posts on social media, chat conversations, news, blogs,
articles, product or services reviews, and patient records in the healthcare sector. Recent
examples include voice-driven bots like Siri and Alexa.

xxi
Introduction

To retrieve significant and actionable insights from textual data and unlock its
potential, we use natural language processing coupled with machine learning and deep
learning.
But what is natural language processing? Machines and algorithms do not
understand text or characters, so it is very important to convert textual data into
a machine-understandable format (like numbers or binary) to analyze it. Natural
language processing (NLP) allows machines to understand and interpret the human
language.
If you want to use the power of unstructured text, this book is the right starting point.
This book unearths the concepts and implementation of natural language processing
and its applications in the real world. NLP offers unbounded opportunities for solving
interesting problems in artificial intelligence, making it the latest frontier for developing
intelligent, deep learning–based applications.

What Does This Book Cover?


Natural Language Processing Recipes is a handy problem/solution reference for learning
and implementing NLP solutions using Python. The book is packed with lots of code
and approaches that help you quickly learn and implement both basic and advanced
NLP techniques. You will learn how to efficiently use a wide range of NLP packages,
implement text classification, and identify parts of speech. You also learn about topic
modeling, text summarization, text generation, sentiment analysis, and many other NLP
applications.
This new edition of Natural Language Processing Recipes focuses on implementing
end-to-end projects using Python and leveraging cutting-edge algorithms and transfer
learning.
The book begins by discussing text data collections, web scraping, and different
types of data sources. You learn how to clean and preprocess text data and analyze
it using advanced algorithms. Throughout the book, you explore the semantic as
well as syntactic analysis of text. It covers complex NLP solutions that involve text
normalization, various advanced preprocessing methods, part-of-speech (POS)
tagging, parsing, text summarization, sentiment analysis, topic modeling, named-entity
recognition (NER), word2vec, seq2seq, and more.

xxii
Introduction

The book covers both fundamental and state-of-the-art techniques used in machine
learning applications and deep learning natural language processing. This edition
includes various advanced techniques to convert text to features, like GloVe, ELMo,
and BERT. It also explains how transformers work, using Sentence-BERT and GPT as
examples.
The book closes by discussing some of the advanced industrial applications of
NLP with a solution approach and implementation, also leveraging the power of deep
learning techniques for natural language processing and natural language generation
problems, employing advanced RNNs, like long short-term memory, to solve complex
text generation tasks. It also explores embeddings—high-quality representations of
words in a language.
In this second edition, few advanced state-of-art embeddings and industrial
applications are explained along with end-to-end implementation using deep learning.
Each chapter includes several code examples and illustrations.
By the end of the book, you will have a clear understanding of implementing natural
language processing. You will have worked on multiple examples that implement NLP
techniques in the real world. Readers will be comfortable with various NLP techniques
coupled with machine learning and deep learning and its industrial applications,
making the NLP journey much more interesting and improving your Python coding
skills.

Who This Book Is For


This book explains various concepts and implementations to get more clarity when
applying NLP algorithms to chosen data. You learn about all the ingredients you need
to become successful in the NLP space. Fundamental Python skills are assumed, as well
as some knowledge of machine learning and basic NLP. If you are an NLP or machine
learning enthusiast and an intermediate Python programmer who wants to quickly
master natural language processing, this learning path will do you a lot of good.
All you need to know are the basics of machine learning and Python to enjoy the book.

xxiii
Introduction

What You Will Learn


• The core concepts of implementing NLP, its various approaches, and
using Python libraries such as NLTK, TextBlob, spaCy, and Stanford
CoreNLP

• Text preprocessing and feature engineering in NLP along with


advanced methods of feature engineering

• Information retrieval, text summarization, sentiment analysis, text


classification, and other advanced NLP techniques solved leveraging
machine learning and deep learning

• The problems faced by industries and how to implement them using


NLP techniques

• Implementing an end-to-end pipeline of NLP life cycle projects,


which includes framing the problem, finding the data, collecting,
preprocessing the data, and solving it using cutting-edge techniques
and tools

What Do You Need for This Book?


To perform all the recipes in this book successfully, you need Python 3.x or higher
running on any Windows- or Unix-based operating system with a processor of 2.0 GHz
or higher and a minimum of 4 GB RAM. You can download Python from Anaconda and
leverage a Jupyter notebook for coding purposes. This book assumes you know Keras
basics and how to install the basic machine learning and deep learning libraries.
Please make sure you upgrade or install the latest version of all the libraries.
Python is the most popular and widely used tool for building NLP applications. It
has many sophisticated libraries to perform NLP tasks, from basic preprocessing to
advanced techniques.
To install any library in a Python Jupyter notebook, use ! before the pip install.
NLTK is a natural language toolkit and is commonly called “the mother of all NLP
libraries.” It is one of the primary resources when it comes to Python and NLP.

!pip install nltk


nltk.download()

xxiv
Introduction

spaCy is a trending library that comes with the added flavors of a deep learning
framework. Although spaCy doesn’t cover all NLP functionalities, it does many things well.

!pip install spacy


#if above doesn't work, try this in your terminal/ command prompt
conda install spacy
python -m spacy.en.download all
#then load model via
spacy.load('en')

TextBlob is one of data scientists’ favorite libraries when it comes to implementing


NLP tasks. It is based on both NLTK and Pattern. TextBlob isn’t the fastest or most
complete library, however.

!pip install textblob

CoreNLP is a Python wrapper for Stanford CoreNLP. The toolkit provides robust,
accurate, and optimized techniques for tagging, parsing, and analyzing text in various
languages.

!pip install CoreNLP

There are hundreds of other NLP libraries, but these are the widely used and
important ones.
There is an immense number of NLP industrial applications that are leveraged to
uncover insights. By the end of the book, you will have implemented many of these use
cases, from framing a business problem to building applications and drawing business
insights. The following are some examples.

• Sentiment analysis—a customer’s emotions toward products offered


by the business

• Topic modeling extracts the unique topics from the group of


documents.

• Complaint classifications/email classifications/ecommerce product


classification, and so on

• Document categorization/management using different clustering


techniques.

xxv
Introduction

• Résumé shortlisting and job description matching using similarity


methods

• Advanced feature engineering techniques (word2vec and fastText) to


capture context

• Information/document retrieval systems, for example, search


engines

• Chatbots, Q&A, and voice-to-text applications like Siri, Alexa


• Language detection and translation using neural networks

• Text summarization using graph methods and advanced techniques

• Text generation/predicting the next sequence of words using deep


learning algorithms

xxvi
CHAPTER 1

Extracting the Data


This chapter covers various sources of text data and the ways to extract it. Textual data
can act as information or insights for businesses. The following recipes are covered.

• Recipe 1. Text data collection using APIs

• Recipe 2. Reading a PDF file in Python

• Recipe 3. Reading a Word document

• Recipe 4. Reading a JSON object

• Recipe 5. Reading an HTML page and HTML parsing

• Recipe 6. Regular expressions

• Recipe 7. String handling

• Recipe 8. Web scraping

I ntroduction
Before getting into the details of the book, let’s look at generally available data sources.
We need to identify potential data sources that can help with solving data science use
cases.

C
 lient Data
For any problem statement, one of the sources is the data that is already present. The
business decides where it wants to store its data. Data storage depends on the type of
business, the amount of data, and the costs associated with the sources. The following
are some examples.

1
© Akshay Kulkarni and Adarsha Shivananda 2021
A. Kulkarni and A. Shivananda, Natural Language Processing Recipes,
https://doi.org/10.1007/978-1-4842-7351-7_1
Chapter 1 Extracting the Data

• SQL databases

• HDFS

• Cloud storage

• Flat files

F ree Sources
A large amount of data is freely available on the Internet. You just need to streamline the
problem and start exploring multiple free data sources.

• Free APIs like Twitter

• Wikipedia

• Government data (e.g., http://data.gov)

• Census data (e.g., www.census.gov/data.html)

• Health care claim data (e.g., www.healthdata.gov)

• Data science community websites (e.g., www.kaggle.com)

• Google dataset search (e.g., https://datasetsearch.research.


google.com)

W
 eb Scraping
Extracting the content/data from websites, blogs, forums, and retail websites for reviews
with permission from the respective sources using web scraping packages in Python.
There are a lot of other sources, such as news data and economic data, that can be
leveraged for analysis.

Recipe 1-1. Collecting Data


There are a lot of free APIs through which you can collect data and use it to solve
problems. Let’s discuss the Twitter API.

2
Chapter 1 Extracting the Data

P
 roblem
You want to collect text data using Twitter APIs.

S
 olution
Twitter has a gigantic amount of data with a lot of value in it. Social media marketers
make their living from it. There is an enormous number of tweets every day, and every
tweet has some story to tell. When all of this data is collected and analyzed, it gives a
business tremendous insights about their company, product, service, and so forth.
Let’s now look at how to pull data and then explore how to leverage it in the coming
chapters.

How It Works
Step 1-1. Log in to the Twitter developer portal
Log in to the Twitter developer portal at https://developer.twitter.com.
Create your own app in the Twitter developer portal, and get the following keys.
Once you have these credentials, you can start pulling data.

• consumer key: The key associated with the application (Twitter,


Facebook, etc.)

• consumer secret: The password used to authenticate with the


authentication server (Twitter, Facebook, etc.)
• access token: The key given to the client after successful
authentication of keys

• access token secret: The password for the access key

Step 1-2. Execute query in Python


Once all the credentials are in place, use the following code to fetch the data.

# Install tweepy
!pip install tweepy

# Import the libraries

3
Chapter 1 Extracting the Data

import numpy as np
import tweepy
import json
import pandas as pd
from tweepy import OAuthHandler

# credentials

consumer_key = "adjbiejfaaoeh"
consumer_secret = "had73haf78af"
access_token = "jnsfby5u4yuawhafjeh"
access_token_secret = "jhdfgay768476r"

# calling API

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)


auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

# Provide the query you want to pull the data. For example, pulling data
for the mobile phone ABC

query ="ABC"

# Fetching tweets

Tweets = api.search(query, count = 10,lang='en',exclude='retweets',


tweet_mode='extended')

This query pulls the top ten tweets when product ABC is searched. The API pulls
English tweets since the language given is 'en'. It excludes retweets.

Recipe 1-2. Collecting Data from PDFs


Most of your data is stored in PDF files. You need to extract text from these files and store
it for further analysis.

Problem
You want to read a PDF file.

4
Chapter 1 Extracting the Data

Solution
The simplest way to read a PDF file is by using the PyPDF2 library.

How It Works
Follow the steps in this section to extract data from PDF files.

Step 2-1. Install and import all the necessary libraries


Here are the first lines of code.

!pip install PyPDF2


import PyPDF2
from PyPDF2 import PdfFileReader

Note You can download any PDF file from the web and place it in the location
where you are running this Jupyter notebook or Python script.

Step 2-2. Extract text from a PDF file


Now let’s extract the text.

#Creating a pdf file object

pdf = open("file.pdf","rb")

#creating pdf reader object

pdf_reader = PyPDF2.PdfFileReader(pdf)

#checking number of pages in a pdf file

print(pdf_reader.numPages)

#creating a page object

page = pdf_reader.getPage(0)

#finally extracting text from the page

5
Chapter 1 Extracting the Data

print(page.extractText())

#closing the pdf file

pdf.close()

Please note that the function doesn’t work for scanned PDFs.

Recipe 1-3. Collecting Data from Word Files


Next, let’s look at another small recipe that reads Word files in Python.

Problem
You want to read Word files.

Solution
The simplest way is to use the docx library.

How It Works
Follow the steps in this section to extract data from a Word file.

Step 3-1. Install and import all the necessary libraries


The following is the code to install and import the docx library.

#Install docx
!pip install docx

#Import library
from docx import Document

Note You can download any Word file from the web and place it in the location
where you are running a Jupyter notebook or Python script.

6
Chapter 1 Extracting the Data

Step 3-2. Extract text from a Word file


Now let’s get the text.

#Creating a word file object

doc = open("file.docx","rb")

#creating word reader object

document = docx.Document(doc)

#create an empty string and call this document. #This document variable
stores each paragraph in the Word document.
#We then create a "for" loop that goes through each paragraph in the Word
document and appends the paragraph.

docu=""
for para in document.paragraphs.
       docu += para.text

#to see the output call docu


print(docu)

Recipe 1-4. Collecting Data from JSON


JSON is an open standard file format that stands for JavaScript Object Notation. It’s often
used when data is sent to a webpage from a server. This recipe explains how to read a
JSON file/object.

Problem
You want to read a JSON file/object.

Solution
The simplest way is to use requests and the JSON library.

7
Chapter 1 Extracting the Data

How It Works
Follow the steps in this section to extract data from JSON.

Step 4-1. Install and import all the necessary libraries


Here is the code for importing the libraries.

import requests
import json

Step 4-2. Extract text from a JSON file


Now let’s extract the text.

#extracting the text from "https://quotes.rest/qod.json"


r = requests.get("https://quotes.rest/qod.json")
res = r.json()
print(json.dumps(res, indent = 4))

#output
{
    "success": {
        "total": 1
    },
    "contents": {
        "quotes": [
            {
                "quote": "Where there is ruin, there is hope for a treasure.",
                "length": "50",
                "author": "Rumi",
                "tags": [
                    "failure",
                    "inspire",
                    "learning-from-failure"
                ],
                "category": "inspire",
                "date": "2018-09-29",

8
Chapter 1 Extracting the Data

                "permalink": "https://theysaidso.com/quote/
dPKsui4sQnQqgMnXHLKtfweF/
rumi-where-there-is-ruin-there-is-hope-for-a-treasure",
                "title": "Inspiring Quote of the day",
                "background": "https://theysaidso.com/img/bgs/
man_on_the_mountain.jpg",
                "id": "dPKsui4sQnQqgMnXHLKtfweF"
            }
        ],
        "copyright": "2017-19 theysaidso.com"
    }
}

#extract contents
q = res['contents']['quotes'][0]
q

#output

{'author': 'Rumi',
'background': 'https://theysaidso.com/img/bgs/man_on_the_mountain.jpg',
'category': 'inspire',
'date': '2018-09-29',
'id': 'dPKsui4sQnQqgMnXHLKtfweF',
'length': '50',
'permalink': 'https://theysaidso.com/quote/dPKsui4sQnQqgMnXHLKtfweF/
rumi-­where-­there-is-ruin-there-is-hope-for-a-treasure',
'quote': 'Where there is ruin, there is hope for a treasure.',
'tags': ['failure', 'inspire', 'learning-from-failure'],
'title': 'Inspiring Quote of the day'}

#extract only quote


print(q['quote'], '\n--', q['author'])

#output
It wasn't raining when Noah built the ark....
-- Howard Ruff

9
Chapter 1 Extracting the Data

Recipe 1-5. Collecting Data from HTML


HTML is short for HyperText Markup Language. It structures webpages and displays
them in a browser. There are various HTML tags that build the content. This recipe looks
at reading HTML pages.

Problem
You want to read parse/read HTML pages.

Solution
The simplest way is to use the bs4 library.

How It Works
Follow the steps in this section to extract data from the web.

Step 5-1. Install and import all the necessary libraries


First, import the libraries.

!pip install bs4


import urllib.request as urllib2
from bs4 import BeautifulSoup

Step 5-2. Fetch the HTML file


You can pick any website that you want to extract. Let’s use Wikipedia in this example.

response = urllib2.urlopen('https://en.wikipedia.org/wiki/
Natural_language_processing')
html_doc = response.read()

10
Chapter 1 Extracting the Data

Step 5-3. Parse the HTML file


Now let’s get the data.

#Parsing
soup = BeautifulSoup(html_doc, 'html.parser')
# Formating the parsed html file
strhtm = soup.prettify()

# Print few lines


print (strhtm[:1000])

#output

<!DOCTYPE html>
<html class="client-nojs" dir="ltr" lang="en">
<head>
  <meta charset="utf-8"/>
  <title>
   Natural language processing - Wikipedia
  </title>
  <script>
   document.documentElement.className = document.documentElement.className.
replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );
  </script>
  <script>
   (window.RLQ=window.RLQ||[]).push(function(){mw.config.set({"wgCanonical
Namespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":
0,"wgPageName":"Natural_language_processing","wgTitle":"Natural language
processing","wgCurRevisionId":860741853,"wgRevisionId":860741853,
"wgArticleId":21652,"wgIsArticle":true,"wgIsRedirect":false,"wgAction":
"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Web
archive template wayback links","All accuracy disputes","Articles
with disputed statements from June 2018","Wikipedia articles with
NDL identifiers","Natural language processing","Computational
linguistics","Speech recognition","Computational fields of stud

11
Chapter 1 Extracting the Data

Step 5-4. Extract a tag value


You can extract a tag’s value from the first instance of the tag using the following code.

print(soup.title)
print(soup.title.string)
print(soup.a.string)
print(soup.b.string)

#output
<title>Natural language processing - Wikipedia</title>
Natural language processing - Wikipedia
None
Natural language processing

Step 5-5. Extract all instances of a particular tag


Here we get all the instances of the tag that we are interested in.

for x in soup.find_all('a'): print(x.string)

#sample output
None
Jump to navigation
Jump to search
Language processing in the brain
None
None
automated online assistant
customer service
[1]
computer science
artificial intelligence
natural language
speech recognition
natural language understanding
natural language generation

12
Chapter 1 Extracting the Data

Step 5-6. Extract all text from a particular tag


Finally, we get the text.

for x in soup.find_all('p'): print(x.text)

#sample output
Natural language processing (NLP) is an area of computer science and
artificial intelligence concerned with the interactions between computers
and human (natural) languages, in particular how to program computers to
process and analyze large amounts of natural language data.

Challenges in natural language processing frequently involve speech


recognition, natural language understanding, and natural language
generation.

The history of natural language processing generally started in the 1950s,


although work can be found from earlier periods.
In 1950, Alan Turing published an article titled "Intelligence" which
proposed what is now called the Turing test as a criterion of intelligence.

Note that the p tag extracted most of the text on the page.

Recipe 1-6. Parsing Text Using Regular Expressions


This recipe discusses how regular expressions are helpful when dealing with text
data. Regular expressions are required when dealing with raw data from the web that
contains HTML tags, long text, and repeated text. During the process of developing your
application, as well as in output, you don’t need such data.
You can do allsorts of basic and advanced data cleaning using regular expressions.

Problem
You want to parse text data using regular expressions.

Solution
The best way is to use the re library in Python.

13
Chapter 1 Extracting the Data

How It Works
Let’s look at some of the ways we can use regular expressions for our tasks.
The basic flags are I, L, M, S, U, X.
• re.I ignores casing.
• re.L finds a local dependent.
• re.M finds patterns throughout multiple lines.
• re.S finds dot matches.
• re.U works for Unicode data.
• re.X writes regex in a more readable format.

The following describes regular expressions’ functionalities.


• Find a single occurrence of characters a and b: [ab]
• Find characters except for a and b: [^ab]
• Find the character range of a to z: [a-z]
• Find a character range except a to z: [^a-z]
• Find all the characters from both a to z and A to Z: [a-zA-Z]
• Find any single character: []
• Find any whitespace character: \s
• Find any non-whitespace character: \S
• Find any digit: \d
• Find any non-digit: \D
• Find any non-words: \W
• Find any words: \w
• Find either a or b: (a|b)
• The occurrence of a is either zero or one
• Matches zero or not more than one occurrence: a? ; ?
• The occurrence of a is zero or more times: a* ; * matches
zero or more than that
14
Chapter 1 Extracting the Data

• The occurrence of a is one or more times: a+ ; + matches


occurrences one or more than one time

• Match three simultaneous occurrences of a: a{3}

• Match three or more simultaneous occurrences of a: a{3,}

• Match three to six simultaneous occurrences of a: a{3,6}

• Start of a string: ^

• End of a string: $

• Match word boundary: \b

• Non-word boundary: \B

The re.match() and re.search() functions find patterns, which are then processed
according to the requirements of the application.
Let’s look at the differences between re.match() and re.search().

• re.match() checks for a match only at the beginning of the string. So,
if it finds a pattern at the beginning of the input string, it returns the
matched pattern; otherwise, it returns a noun.

• re.search() checks for a match anywhere in the string. It finds all


the occurrences of the pattern in the given input string or data.

Now let’s look at a few examples using these regular expressions.

Tokenizing
Tokenizing means splitting a sentence into words. One way to do this is to use re.split.

# Import library

import re

#run the split query

re.split('\s+','I like this book.')

['I', 'like', 'this', 'book.']

For an explanation of regex, please refer to the main recipe.

15
Chapter 1 Extracting the Data

Extracting Email IDs


The simplest way to extract email IDs is to use re.findall.

1. Read/create the document or sentences.

doc = "For more details please mail us at: xyz@abc.com, pqr@mno.com"

2. Execute the re.findall function.

addresses = re.findall(r'[\w\.-]+@[\w\.-]+', doc)


for address in addresses.
    print(address)

#Output
xyz@abc.com
pqr@mno.com

Replacing Email IDs


Let’s replace email IDs in sentences or documents with other email IDs. The simplest
way to do this is by using re.sub.

1. Read/create the document or sentences.

doc = "For more details please mail us at xyz@abc.com"

2. Execute the re.sub function.

new_email_address = re.sub(r'([\w\.-]+)@([\w\.-]+)',
r'pqr@mno.com', doc)
print(new_email_address)

#Output
For more details please mail us at pqr@mno.com

For an explanation of regex, please refer to Recipe 1-6.


If you observe in both instances when dealing with email using regex, we have
implemented a very basic one. We state that words separated by @ help capture email
IDs. However, there could be many edge cases; for example, the dot (.) incorporates
domain names and handles numbers, the + (plus sign), and so on, because they can be
part of an email ID.

16
Chapter 1 Extracting the Data

The following is an advanced regex to extract/find/replace email IDs.

([a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)

There are even more complex ones to handle all the edge cases (e.g., “.co.in” email
IDs). Please give it a try.

Extracting Data from an eBook and Performing regex


Let’s solve a case study that extracts data from an ebook by using the techniques you
have learned so far.

1. Extract the content from the book.

# Import library

import re
import requests

#url you want to extract


url = 'https://www.gutenberg.org/files/2638/2638-0.txt'

#function to extract
def get_book(url).
# Sends a http request to get the text from project Gutenberg
raw = requests.get(url).text
# Discards the metadata from the beginning of the book
start = re.search(r"\*\*\* START OF THIS PROJECT GUTENBERG EBOOK
.* \*\*\*",raw ).end()
# Discards the metadata from the end of the book
stop = re.search(r"II", raw).start()
# Keeps the relevant text
text = raw[start:stop]
return text

# processing
def preprocess(sentence).
return re.sub('[^A-Za-z0-9.]+' , ' ', sentence).lower()

#calling the above function

17
Chapter 1 Extracting the Data

book = get_book(url)
processed_book = preprocess(book)
print(processed_book)

# Output
produced by martin adamson david widger with corrections by andrew
sly the idiot by fyodor dostoyevsky translated by eva martin
part i i. towards the end of november during a thaw at nine o
clock one morning a train on the warsaw and petersburg railway
was approaching the latter city at full speed. the morning was so
damp and misty that it was only with great difficulty that the day
succeeded in breaking and it was impossible to distinguish anything
more than a few yards away from the carriage windows. some of the
passengers by this particular train were returning from abroad
but the third class carriages were the best filled chiefly with
insignificant persons of various occupations and degrees picked up
at the different stations nearer town. all of them seemed weary and
most of them had sleepy eyes and a shivering expression while their
complexions generally appeared to have taken on the colour of the
fog outside. when da

2. Perform an exploratory data analysis on this data using regex.

# Count number of times "the" is appeared in the book


len(re.findall(r'the', processed_book))

#Output
302

#Replace "i" with "I"


processed_book = re.sub(r'\si\s', " I ", processed_book)
print(processed_book)

#output
produced by martin adamson david widger with corrections by
andrew sly the idiot by fyodor dostoyevsky translated by eva
martin part I i. towards the end of november during a thaw at
nine o clock one morning a train on the warsaw and petersburg

18
Chapter 1 Extracting the Data

railway was approaching the latter city at full speed. the


morning was so damp and misty that it was only with great
difficulty that the day succeeded in breaking and it was
impossible to distinguish anything more than a few yards away
from the carriage windows. some of the passengers by this
particular train were returning from abroad but the third class
carriages were the best filled chiefly with insignificant persons
of various occupations and degrees picked up at the different
stations nearer town. all of them seemed weary and most of
them had sleepy eyes and a shivering expression while their
complexions generally appeared to have taken on the colour of the
fog outside. when da

#find all occurance of text in the format "abc--xyz"


re.findall(r'[a-zA-Z0-9]*--[a-zA-Z0-9]*', book)

#output
['ironical--it',
'malicious--smile',
'fur--or',
'astrachan--overcoat',
'it--the',
'Italy--was',
'malady--a',
'money--and',
'little--to',
'No--Mr',
'is--where',
'I--I',
'I--',
'--though',
'crime--we',
'or--judge',
'gaiters--still',
'--if',
'through--well',
'say--through',
19
Chapter 1 Extracting the Data

'however--and',
'Epanchin--oh',
'too--at',
'was--and',
'Andreevitch--that',
'everyone--that',
'reduce--or',
'raise--to',
'listen--and',
'history--but',
'individual--one',
'yes--I',
'but--',
't--not',
'me--then',
'perhaps--',
'Yes--those',
'me--is',
'servility--if',
'Rogojin--hereditary',
'citizen--who',
'least--goodness',
'memory--but',
'latter--since',
'Rogojin--hung',
'him--I',
'anything--she',
'old--and',
'you--scarecrow',
'certainly--certainly',
'father--I',
'Barashkoff--I',
'see--and',
'everything--Lebedeff',
'about--he',

20
Chapter 1 Extracting the Data

'now--I',
'Lihachof--',
'Zaleshoff--looking',
'old--fifty',
'so--and',
'this--do',
'day--not',
'that--',
'do--by',
'know--my',
'illness--I',
'well--here',
'fellow--you']

Recipe 1-7. Handling Strings


This recipe discusses how to handle strings and deal with textual data. You can do all
sorts of basic text explorations using string operations.

Problem
You want to explore handling strings.

Solution
The simplest way is to use the following string functionality.

• s.find(t) is an index of the first instance of string t inside s (–1 if not


found)

• s.rfind(t) is an index of the last instance of string t inside s (–1 if


not found)

• s.index(t) is like s.find(t) except it raises ValueError if not found


• s.rindex(t) is like s.rfind(t) except it raises ValueError if not found

• s.join(text) combines the words of the text into a string using s as


the glue
21
Chapter 1 Extracting the Data

• s.split(t) splits s into a list wherever a t is found (whitespace by


default)

• s.splitlines() splits s into a list of strings, one per line

• s.lower() is a lowercase version of the string s

• s.upper() is an uppercase version of the string s

• s.title() is a titlecased version of the string s

• s.strip() is a copy of s without leading or trailing whitespace

• s.replace(t, u) replaces instances of t with u inside s

How It Works
Now let’s look at a few of the examples.

Replacing Content
Create a string and replace the content. Creating strings is easy. It is done by enclosing
the characters in single or double quotes. And to replace, you can use the replace
function.

1. Create a string.

String_v1 = "I am exploring NLP"

#To extract particular character or range of characters from


string

print(String_v1[0])

#output
"I"

#To extract the word “exploring”

print(String_v1[5:14])

#output
exploring

22
Discovering Diverse Content Through
Random Scribd Documents
public of certain classics in the nude Roman poetry, like the Arab,
and of the same date."
Certainly Burton leaves out nothing of the nakedness that startles
one in the verse of Catullus: a nakedness that is as honest as
daylight and as shameless as night. When the text is obscene his
translation retains its obscenity; which, on the whole, is rare: for the
genius of Catullus is elemental, primitive, nervous, passionate,
decadent in the modern sense and in the modern sense perverse. In
his rhymed version of the Attis Burton has made a prodigious
attempt to achieve the impossible. Not being a poet, he was
naturally unable to follow the rhythm—the Galliambic metre, in
which Catullus obtains variety of rhythm; for, as Robinson Ellis says:

It remains unique as a wonderful expression of abnormal feeling


in a quasi-abnormal meter. Quasi-abnormal, however, only: for
no poem of Catullus follows stricter laws, or succeeds in
conveying the idea of a wild freedom under a more carefully
masked regularity.

As one must inevitably compare two translations of the same


original, I have to point out that Burton's rendering is, both
metrically and technically, inaccurate; whereas, in another rendering,
the translator has at least preserved the exact metre, the exact
scansion, and the double endings at the end of every line; not, of
course, in this case, employing the double rhymes Swinburne used
in his translation from Aristophanes. These are Burton's first lines:—
O'er high deep seas in speedy ship his voyage Atys sped
Until he trod the Phrygian grove with hurried, eager
tread,
And as the gloomy tree-shorn stead, the she-God's
home he sought,
There sorely stung with fiery ire and madman's raging
thought,
Share he with sharpened flint the freight wherewith
his frame was fraught.
These are the first lines of the other version:—
Over ocean Attis sailing in a swift ship charioted
When he reached the Phrygian forests, and with rash
foot violently
Trod the dark and shadowy regions of the goddess,
wood-garlanded,
And with ravening madness ravished, and his reason
abandoning him,
Seized a pointed flint and sundered from his flesh his
virility.

II

Burton himself admitted that he was a devil; for, said he: "the Devil
entered into me at Oxford." Evidently, also, besides his mixture of
races, he was a mixture of the normal and the abnormal; he was
perverse and passionate; he was imaginative and cruel; he was
easily stirred to rage. Nearly six feet in height, he had, together with
his broad shoulders, the small hands and feet of the Orientals; he
was Arab in his prominent cheek-bones; he was gypsy in his terrible,
magnetic eyes—the sullen eyes of a stinging serpent. He had a
deeply bronzed complexion, a determined mouth, half-hidden by a
black mustache, which hung down in a peculiar fashion on both
sides of his chin. This peculiarity I have often seen in men of the
wandering tribe in Spain and in Hungary. Wherever he went he was
welcomed by the gypsies; he shared with them their horror of a
corpse, of death-scene, and of graveyards. "He had the same
restlessness," wrote his wife, "which could stay nowhere long nor
own any spot on earth. Hagar Burton, a Gypsy woman, cast my
horoscope, in which she said: 'You will bear the name of our Tribe,
and be right proud of it. You will be as we are, but far greater than
we.' I met Richard two months later, in 1856, and was engaged to
him." It is a curious fact that John Varley, who cast Blake's
horoscope in 1820, also cast Burton's; who, as he says, had finished
his Zodiacal Physiognomy so as to prove that every man resembled
after a fashion the sign under which he was born. His figures are
either human or bestial; some remind me of those where men are
represented in the form of animals in Giovanni della Porta's
Fisonomia dell' Huomo (Venice, 1668), which is before me as I write;
Swinburne himself once showed to me his copy of the same book.
Nor have I ever forgotten his saying to me—in regard to Burton's
nervous fears: "The look of unspeakable horror in those eyes of his
gave him, at times, an almost unearthly appearance." He added:
"This reminds me of what Kiomi says in Meredith's novel: 'I'll dance
if you talk of dead people,' and so begins to dance and to whoop at
the pitch of her voice. I suppose both had the same reason for this
force of fear: to make the dead people hear." Then he flashed at me
this unforgettable phrase: "Burton had the jaw of a Devil and the
brow of a God."
In one of his letters he says, I suppose by way of persiflage in
regard to himself and Burton: "En moi vous voyez Les Malheurs de la
Virtu, en lui Les Prospérités du Vice." In any case, it is to entertain
Burton when he writes: "I have in hand a scheme of mixed verse
and prose—a sort of étude à la Balzac plus the poetry—which I
flatter myself will be more offensive and objectionable to Britannia
than anything I have done: Lesbia Brandon. You see I have now a
character to keep up, and by the grace of Cotytto I will."
Swinburne began Lesbia Brandon in 1859; he never finished it; what
remains of it consists of seventy-three galleys, numbered 25 to 97,
besides four unprinted chapters. The first, "A Character," was written
in 1864; "An Episode" in 1866; "Turris Eburnea" in 1886; "La
Bohême Dédorée" must have been written a year or two later. Mr.
Gosse gives a vivid description of Swinburne, who was living in 13,
Great James Street, and who was never weary of his unfinished
novel, reading to him parts of two chapters in June, 1877. "He read
two long passages, the one a ride over a moorland by night, the
other the death of his heroine, Lesbia Brandon. After reading aloud
all these things with amazing violence, he seemed quite exhausted."
It is possible to decipher a few sentences from two pages of his
manuscript; first in "Turris Eburnea. 'Above the sheet, below the
boudoir,' said the sage. Her ideal was marriage, to which she clung,
which revealed to astonished and admiring friends the vitality of a
dubious intellect within her. She had not even the harlot's talent of
discernment." This is Leonora Harley. In La Bohême Dédorée we
read:

Two nights later Herbert received a note from Mr. Linley inviting
him to a private supper. Feverish from the contact of Mariani
and hungry for a chance of service, he felt not unwilling to win
a little respite from the vexation of patience. The sage had
never found him more amenable to the counsel he called
reason. Miss Brandon had not lately crossed his ways. Over their
evening Leonora Harley guided with the due graces of her
professional art. It was not her fault if she could not help asking
her younger friend when he had last met a darker beauty: she
had seen him once with Lesbia.

III

In 1848 Burton determined to pass in India for an Oriental; the


disguise he assumed was that of a half-Arab, half-Iranian, thousands
of whom can be met along the northern shore of the Persian Gulf.
He set out on his first pilgrimage as Mirza Abdulla the Bushiri, as a
buzzaz, vendor of fine linen, muslins and bijouterie; he was admitted
to the harems, he collected the information he required from the
villagers; he won many women's hearts, he spent his evenings in the
mosques; and, after innumerable adventures, he wended his way to
Mecca. His account of this adventure is thrilling. The first cry was:
"Open the way for the Haji who would enter the House!" Then:

Two stout Meccans, who stood below the door, raised me in


their arms, whilst a third drew me from above into the building.
At the entrance I was accosted by a youth of the Benu Shazban
family, the true blood of the El Hejaz. He held in his hand the
huge silver-gilt padlock of the Ka'abeh, and presently, taking his
seat upon a kind of wooden press in the left corner of the hall,
he officially inquired my mother-nation and other particulars.
The replies were satisfactory, and the boy Mohammed was
authoritatively ordered to conduct me round the building and to
recite the prayers. I will not deny that, looking at the
windowless walls, the officials at the door, and a crowd of
excited fanatics below—
"And the place death, considering who I was,"
my feelings were those of the trapped-rat description,
acknowledged by the immortal nephew of his uncle Perez. A
blunder, a hasty action, a misjudged word, a prayer or bow, not
strictly the right shibboleth, and my bones would have whitened
the desert sand. This did not, however, prevent my carefully
observing the scene during our long prayer, and making a rough
plan with a pencil upon my white ihram.

After having seen the howling Dervishes in Scutari in Asia, I can


imagine Burton's excitement when in Cairo he suddenly left his stolid
English friends, joined in the shouting, gesticulating circle, and
behaved as if to the manner born: he held his diploma as a master
Dervish. In Scutari I felt the contagion of these dancers, where the
brain reels, and the body is almost swept into the orgy. I had all the
difficulty in the world from keeping back the woman who sat beside
me from leaping over the barrier and joining the Dervishes. In these
I felt the ultimate, because the most animal, the most irrational, the
most insane, form of Eastern ecstasy. It gave me an impression of
witchcraft; one might have been in Central Africa, or in some
Saturnalia of barbarians.
There can be no doubt that Burton always gives a vivid and virile
impression of his adventures; yet, as I have said before, something
is lacking in his prose; not the vital heat, but the vision of what is
equivalent to vital heat. I have before me a letter sent from
Hyderabad by Sarojini Naidu, who says: "All is hot and fierce and
passionate, ardent and unashamed in its exulting and importunate
desire for life and love. And, do you know, the scarlet lilies are
woven petal by petal from my heart's blood, those quivering little
birds are my soul made incarnate music, these heavy perfumes are
my emotions dissolved into aerial essence, this flaming blue and gold
sky is the 'Very You' that part of me that incessantly and insolently,
yes, and a little deliberately, triumphs over that other part—a thing
of nerves and tissues that suffers and cries out, and that must die
tomorrow perhaps, or twenty years hence." In these sentences the
whole passionate, exotic and perfumed East flashes before me—a
vision of delight and of distresses—and, as it were, all that slumbers
in their fiery blood.
"Not the fruit of experience," wrote Walter Pater, "but experience
itself, is the end. A counted number of pulses only is given us of a
variegated dramatic life. To burn always with this hard, gemlike
flame, to maintain this ecstasy, is success in life." Alas, how few lives
out of the cloud-covered multitude of existences have burned always
with this flame! I have said somewhere that we can always, in this
world, get what we want if we will it intensely enough. So few
people succeed greatly because so few people can conceive a great
end, and work toward that without tiring and without deviating. The
adventurer of whom I am writing failed, over and over again, in spite
of the fact that he conceived and could have executed great ends:
never by his own fault, always by the fault of others.

IV

Richard Burton dedicated his literal version of the epic of Camões


"To the Prince of the Lyric Poets of his Day, Algernon Charles
Swinburne." He begins:

My dear Swinburne, accept the unequal exchange—my brass for


your gold. Your Poems and Ballads began to teach the Philistine
what might there is in the music of language, and what marvel
of lyric inspiration, far subtler and more ethereal than poetry,
means to the mind of man.
In return for this Swinburne dedicated to him Poems and Ballads,
Second Series.

Inscribed to Richard F. Burton in redemption of an old pledge


and in recognition of a friendship which I must always count
among the highest honors of my life.

It was nine years before then, when they were together in the south
of France, that Swinburne was seized by a severe illness; and, as he
assured me, it was Burton who, with more than a woman's care and
devotion, restored him to health. The pledge—it was not the
covenant sealed between the two greatest, the two most passionate,
lovers in the world, Iseult and Tristan, on the deck of that ship which
was the ship of Life, the ship of Death, in the mere drinking of wine
out of a flagon, which, being of the nature of a most sweet poison,
consumed their limbs and gave intoxication to their souls and to
their bodies—but a pledge in the wine Swinburne and Burton drank
in the hot sunshine:—
For life's helm rocks to windward and lee,
And time is as wind, and waves are we,
And song is as foam that the sea-waves fret,
Though the thought at its heart should be deep as the
sea
It was in July, 1869, that Swinburne joined the Burtons and Mrs.
Sartoris at Vichy. As I have never forgotten Swinburne's wonderful
stories about Burton—besides those on Rossetti and Mazzini—I find
in a letter of his to his mother words he might really have altered.

If you had seen him, when the heat and the climb and the
bothers of travelling were too much for me—in the very hot
weather—helping, waiting on me—going out to get me books to
read in bed—and always kind, thoughtful, ready, and so bright
and fresh that nothing but a lizard (I suppose that is the most
insensible thing going) could have resisted his influence—I feel
sure you would like him (you remember you said you didn't) and
then—love him, as I do. I never expect to see his like again—
but him I do hope to see again, and when the time comes to
see him at Damascus as H.B.M. Consul.

They traveled in carriages, went to Clermont-Ferrand, where Pascal


was born; then to Le Puy-en-Velay. In 1898 I stayed with the
Countess De la Tour in the Château de Chaméane, Puy de Dôme,
and after leaving her I went to Puy-en-Velay. I hated it, the Burtons
did not. Stuck like a limpet on a rock, the main part of the town
seems to be clinging to the side of the hill on which the monstrous
statue desecrates the sky. At night I saw its gilt crown merge into a
star, but by day it is intolerably conspicuous, and at last comes to
have an irrational fascination, leading one to the very corners where
it can be seen best. And always, do what you will, you can not get
away from this statue. It spoils the sky. The little cloister, with its
ninth-century columns, is the most delightful spot in Le Puy; only the
intolerable statue from which one can not escape showed me nature
and humanity playing pranks together, at their old game of
parodying the ideal. This is Swinburne's comment:—
Set far between the ridged and foamless waves
Of earth more fierce and fluctuant than the sea,
The fearless town of towers that hails and braves,
The heights that gild, the sun that brands Le Puy.
This year there has been a great Pardon at Le Puy. I have seen
several pilgrimages, in Moscow, for instance, at Serjevo, which is an
annual pilgrimage to the Troitsa Monastery, and in these people
there was no fervor, no excitement, but a dogged desire of doing
something which they had set out to do. They were mostly women,
and they flung themselves down on the ground; they lay there with
their hands on their bundles, themselves like big bundles of rags.
How different a crowd from this must have assembled at Le Puy;
made so famous so many centuries ago by the visitations of
Charlemagne and Saint Louis, who left, in 1254, in the Cathedral a
little image of Horns and Isis. Then there was Jeanne d'Arc, who in
1429 sent her mother there instead of herself, being much too busy:
she was on the way to Orléans.
As it is, Our Lady gets all the honors; only, there is a much older
Chapel of Saint Michael, which is perched on the sheer edge of a
rock; it is perhaps more original than any in France, with the
exception of the Chapel of Saint Bonizel in Avignon. When I stood
there and looked down from that great height I remembered—but
with what a difference!—Montserrat in Spain, where the monastery
seemed a part of the mountain; and from this narrow ledge between
earth and heaven, a mere foothold on a great rock, I looked up only
at sheer peaks, and down only into veiled chasms, or over
mountainous walls to a great plain, ridged as if the naked ribs of the
earth were laid bare.

I have been assured, by many who knew him, that Richard Burton
had a vocabulary which was one of his inventions; a shameless one
—as shameless as the vocabularies invented by Paul Verlaine and by
Henri de Toulouse-Lautrec, which are as vivid to me as when I heard
their utterance. These shared with Villiers de Isle-Adam that sardonic
humor which is not so much satire as the revenge of beauty on
ugliness, the persecution of the ugly: the only laughter of our
generation which is as fundamental as that of Rabelais and of Swift.
Burton, who had much the same contempt for women that
Baudelaire imagined he had, only with that fixed stare of his that
disconcerted them, did all that with deliberate malice. There was
almost nothing in this world that he had not done, exulted in, gloried
in. Like Villiers, he could not pardon stupidity; to both it was
incomprehensible; both saw that stupidity is more criminal than even
vice, if only because stupidity is incurable, if only because vice is
curable. Burton, who found the Arabs, in their delicate depravity,
ironical—irony being their breath of life—might have said with
Villiers: "L'Esprit du Siècle, ne l'oublions pas, est aux machines."
Every individual face has as many different expressions as the soul
behind it has moods; therefore, the artist's business is to create on
paper, or on his canvas, the image which was none of these, but
which those helped to make in his own soul. I see, as it were, surge
before me an image of Swinburne in his youth, when, with his
passionate and pale face, with its masses of fiery hair, he has almost
the aspect of Ucello's Galeazzo Malatesta. Burton's face has no
actual beauty in it; it reveals a tremendous animalism, an air of
repressed ferocity, a devilish fascination. There is almost a tortured
magnificence in this huge head, tragic and painful, with its mouth
that aches with desire, with those dilated nostrils that drink in I
know not what strange perfumes.

EDGAR SALTUS

Edgar Saltus owes much of his bizarre talent to his mixed origin, for
he is of Dutch and American extraction; indeed, for much of what I
might call his rather unholy genius. His pages exhale a kind of exotic
and often abnormal perfume of colors, color of sensations, of heats,
of crowded atmospheres. He gives his women baneful and baleful
names, such as Stella Sixmouth, Shorn Wyvell; these vampires and
wicked creatures who ruin men's lives as cruelly as they ruin their
own. His men have prodigious nerves, even more than his women;
they commit all sorts of crimes, assassinations, poisonings, out of
sheer malice and out of overexcited imaginations.
Of that most terrible of tragedies, the tragedy of a soul, he is for the
most part utterly unconscious; and the very abracadabra of his art is
in a sense—a curious enough and ultramodern sense—lifted from
the Elizabethan dramatists. In them—as in many of his pages—a fine
situation must have a murder in it, and some odious character
removed by another more stealthy kind of obliteration. But, when he
gives one a passing shudder, he leaves nothing behind it; yet in his
perverted characters there can be found sensitiveness,
hallucinations, obsessions; and some have that lassitude which is
more than mere contempt. Some go solemnly on the path of blood,
with no returning by a way so thronged with worse than memories.
"No need for more crime," such men have cried, and for such
reasons reaped the bitter harvest of tormenting dreams. Some have
imagination that stands in the place of virtue; some, as in the case
of Lady Macbeth, still keep the sensation of blood on their guilty
hands.
Mary of Magdala (1891) is a vain attempt to do what Flaubert had
done before Saltus in his Hérodias, and what Wilde has done after
him in Salome, a drama that has a strange not easily defined
fascination, which I can not dissociate from Beardsley's illustrations,
in which what is icily perverse in the dialogue (it can not be
designated drama) becomes in the ironical designs pictorial, a series
of poses. To Wilde passion was a thing to talk about with elaborate
and colored words. Salome is a doll, as many have imagined her,
soulless, set in motion by some pitiless destiny, personified
momentarily by her mother; Herod is a nodding mandarin in a
Chinese grotesque.
In one page of Saltus's Oscar Wilde: An Idler's Impressions (1917)
he evokes, with his cynical sense of the immense disproportion of
things in this world and the next, the very innermost secret of Wilde.
They dine in a restaurant in London and Wilde reads his MS.
"Suddenly his eyes lifted, his mouth contracted, a spasm of pain—or
was it dread?—had gripped him, a moment only. I had looked away.
I looked again. Before me was a fat pauper, florid and over-dressed,
who in the voice of an immortal, was reading the fantasies of the
damned. In his hand was a manuscript, and we were supping on
Salome."
Mr. Incoul's Misfortune seems to have its origin in some strange
story of Poe's; for it gives one the sense of a monster, diabolical,
inhuman, malevolent and merciless, who, after a mock marriage,
abnormally sets himself to the devil's business of ruining his wife's
lover's life, and of giving his wife a sudden death in three hideous
forms: a drug to make her sleep, the gas turned on; and the door
locked with "a nameless instrument."
The Truth about Tristan Varick (1888) is based on social problems of
the most unaccountable kind. It has something strangely convincing
in both conception and execution; it has suspense, ugly enough and
uglier crises; and that the unlucky Varick is supposed to be partially
insane is part of the finely woven plot, which is concerned with
strange and perilous incidents and accidents; and which is based on
his passionate pursuit of the ravishing Viola Raritan; the pursuit,
really, of the chimera of his imagination.
And among the hazards comes one, of an evil kind—such as I have
often experienced in foreign cities—that, in turning down one street
instead of the next, a man's existence, and not his only, may be
thereby changed. To have stopped one's rival's lying mouth and his
lying life at the same instant is to have done something original—it is
done by a poisoned pin's point. Then, this Orestes having found no
Electra to return his love, but finding her vile, he lets himself
disappear out of life in an almost incredible fashion, leaving the
woman who never loved him to say, "I will come to see him
sentenced:" a sentence which writes her down a modern
Clytemnestra.
What Saltus says of Gonfallon can almost be said of Saltus: "With a
set of people that fancied themselves in possession of advanced
views and were still in the Middle Ages, he achieved the impossible:
he not only consoled, he flattered, he persuaded and fascinated as
well." Saltus can not console, he can sometimes persuade; but he
can flatter and fascinate his public, as with
A breeze of fame made manifest.
The novelist is the comedian of the pen: it is his duty to amuse, to
entertain—or else to hold his peace: to one in his trade nothing
imaginable comes amiss. It is not sin that appeals him, but the
consequences of sin; such as the fact that few sinners have ever
turned into saints. In a word, he writes with his nerves.
Take, for instance, A Transaction of Hearts (1887), one of the
queerest novels ever written and written with a kind of deliberate
malice. Gonfallon, who becomes a bishop, falls passionately in love
with an ardent and insolent girl who is his wife's sister; and before
her beauty everything vanishes: virtue, genius, everything. "For a
second that was an eternity he was conscious of her emollient
mouth on his, her fingers intertwined with his own. For that second
he really lived—perhaps he really lived." One wonders why Saltus
uses so many ugly phrases—a kind of decadent French fashion of
transposing words; such as the one I have quoted, together with
"Ruedelapaixia" (meant to describe a dress), "Rafflesia, Mashed
grasshoppers baked in saffron;" phrases chosen at random which
are too frequently scattered in much too obvious a profusion over
much too luxurious pages. I read somewhere that Oscar Wilde said
to Amélie Rives: "In Edgar Saltus's work passion struggles with
grammar on every page," which is certainly one of Wilde's finest
paradoxes. I "cap this"—as Dowson often said to me in jest—with
Léon Bloy's admirable phrase on Huysmans: "That he drags his
images by the heels or the hair upside down the worm-eaten
staircase of terrified syntax."
Imperial Purple (1906) shows the zenith of Saltus's talent, not in
conceiving imaginary beings, but in giving modern conceptions of
the most amazing creatures in the Roman Decadence, and in lyrical
prose, which ought to have had for motto Victoria's stanza:—
Je suis l'Empire à la fin de la décadence,
Qui regarde passer les grands Barbares blancs,
En composant des acrostiches indolents,
D'un style d'or où la langueur du soleil danse.
Only Saltus is not Tacitus, in spite of having delved into his pages.

RECOLLECTIONS OF RÉJANE
NOTES ON THE ART OF THE GREAT FRENCH
ACTRESS

Meilhac's play, Ma Cousine, which owed most of its success, when it


was produced at the Variétés, October 27th, 1890, to the acting of
Réjane, is one of those essentially French plays which no ingenuity
can ever accommodate to an English soil. It is the finer spirit of
farce, it is meant to be taken as a kind of intellectual exercise; it is
human geometry for the masses. There are moments when the
people of the play are on the point of existing for themselves, and
have to be brought back, put severely in their places, made to fit
their squares of the pattern. The thing as a whole has no more
resemblance to real life than Latin verses have to a school-boy's
conversation. Reality, that, after all, probably holds us in it, comes
into it accidentally, in the form of detail, in little touches of character,
little outbursts of temperament. The rest is done after a plan, it is an
entanglement by rule; it exists because people have agreed to think
that they like suspense; the tantalization of curiosity on the stage.
We see the knot tied by the conjurer; we want to know what he will
do with it. In France, and in such a piece as Ma Cousine, the
conjurer is master of his trade; he gives us our illusions and our
enlightenment in exactly the right doses.
And Réjane in this wittily artificial play suits herself perfectly to her
subject, becomes everything there is in the character of Riquette; an
actress who plays a comedy in real life, quite in the spirit of the
stage. She has to save the situation from being taken too seriously,
from becoming tragic: she has to take the audience into her
confidence, to assure them that it is all a joke. And so we see her
constantly overdoing her part, fooling openly. She does two things at
once: the artificial comedy, which is uppermost in the play, and the
character part which is implicit in it. And she is perfect in both.
The famous Chahut, which went electrically through Paris, when it
was first given, in all its audacity, shows us one side of her art. The
delicate by-plays with eyes and voice, or rather the voice and the
overhanging eyelid of the right eye, shows us another. She is always
the cleverest person on the stage. Her face in repose seems waiting
for every expression to quicken its own form of life. When the face is
in movement, one looks chiefly at the mouth, the thick, heavily
painted lips, which twist upward, and wrinkle into all kinds of earthly
subtleties. Her face is full of an experienced, sullen, chuckling
gaminerie, which seems, after all, to be holding back something: it
has a curious, vulgar undertone, a succulent and grossly joyous
gurgle.

RÉJANE IN "MA COUSINE"

Here, in Ma Cousine, she abandons herself to all the frank and shady
humors of the thing with the absolute abandonment of the artist. It
is like a picture by Forain, made of the same material with the same
cynicism and with the same mastery of line.
Ma Cousine, on seeing it a second time, is frankly and not too
obviously amusing, a piece in which everybody plays at something,
in which Réjane plays at being an actress who has a part to act in
real life. "Elle est impayable, cette Riquette!" And it is with an
intensely conscious abandonment of herself that she renders this
good-hearted Cabotine, so worldly wise, so full of all the physical
virtues, turned Bohemian. She has, in this part, certain guttural and
nasal laughs, certain queer cries and shouts, which are after all a
part of her métier; she runs through her whole gamut of shrugs and
winks and nods. There is, of course, over again, the famous Chahut,
in which she summarizes the whole art of the Moulin-Rouge; there is
her long scene of pantomime, in which every gesture is at once
vulgar and distinguished, vulgarly rendered with distinction. There
are other audacities, all done with equal discretion.
I am not sure that Réjane is not at her best in this play: she has
certainly never been more herself in what one fancies to be herself.
There is all her ravishing gaminerie, her witty intelligence, her dash,
her piquancy, her impudence, her mastery. I find that her high
spirits, in this play, affect me like pathos: they run to a kind of
emotion. I compared her art with the art of Forain; I said that here
was a picture, made out of the same material, with the same
cynicism, the same mastery of line. She suggested, in her costume
of the Second Act, a Beardsley picture; there was the same kind of
tragic grotesque, in which a kind of ugliness became a kind of
beauty. The whole performance was of the best Parisian kind, with
genius in one, admirably disciplined talent in all.

MELODRAMA WITH AN IDEA

Paul Hervieu's La Course du Flambeau, which was given by Réjane


at the Vaudeville, April 17th, 1901, is first of all a sentimental thesis.
It begins with an argument as to the duty of mother to child and of
child to mother. A character who apparently represents the author's
views declares life is a sort of Lampadophoria, or La Course du
Flambeau, in which it is the chief concern of each generation to
hand on the torch of life to the next generation. Sabine protests that
the duty is equal, and offers herself as an example. "I," she says,
"stand between mother and daughter; I love them myself; I could
sacrifice myself equally for either." Maravan replies: "You do not
know yourself. You do not know how good a mother you are, and I
hope you will never know how bad a daughter." The rest of the play
is ingeniously constructed to show, point by point, gradation by
gradation, the devotion of Sabine to her daughter and the readiness
with which she will sacrifice, not only herself, but her mother.
The only answer to the author's solution is to reinstate the problem
in terms of precisely contrary facts; we have another solution, which
may be made in terms no less inevitable. The play itself proves
nothing, and it seems to me that the writer's persistence in arguing
the point in action has given a somewhat needless and unnatural air
of melodrama to his piece. It is a melodrama with an idea, a clue,
but it is none the less a melodrama, because the idea and the clue
are alike so arbitrary. One is never left quietly alone with nature; the
showman's hand is always visible, around the corner of the curtain,
pulling the strings. Whenever one sees a human argument
struggling to find its way through the formal rhetoric of the speaker,
it is the French equivalent of sentiment.
The piece is really the comedy of a broken heart, and what Réjane
has to do is to represent all the stages of the slow process of
heartbreak. She does it as only a great artist could do; but she
allows us to see that she is acting. She does it consciously,
deliberately, with method.
She has forced herself to become bourgeois; she takes upon herself
the bourgeois face and appearance, and also the bourgeois soul.
The wit and bewildering vulgarity have gone out of her, and a
middle-class dignity has taken their place. She shows us the stage
picture of a mother marvelously: that is to say, she interprets the
play according to the author's intentions; when she is most effective
as an actress she is not content with the simplicity of nature, as in
the tirade in the third act. She brings out the melodramatic points
with the finest skill; but the melodrama itself is a wilful divergency
from nature; and she has few chances to be her finest self. She
proves the soundness of her art as an actress by the ability to play
such a part finely, seriously, effectively. Her own temperament
counts for nothing; it is not even a hindrance: it is all the skill of a
métier, the mastery of her art.

"MADAME SANS-GÊNE"

In 1893 Réjane created, at the Vaudeville, the woman whose part


she had to act, in Madame Sans-Gêne. For some reason unknown to
me, Réjane is best known in England by her performances in this
thoroughly poor play, which shows us Sardou working mechanically,
and for character effects of a superficial kind. There are none of the
ideas, none of the touches of nature of La Parisienne; none of the
comic vitality of Ma Cousine; none of the emotional quality of Sapho.
It is full of piquancies for acting, and Réjane makes the most of
them. Her acting is admirable, from beginning to end; it has her
distinguished vulgarity; her gross charm; she is everything that
Sardou meant, and something more.
But all that Sardou meant was not a very interesting thing, and
Réjane can not make it what it is not. She brightens her part, she
does not make a different thing of it. There were moments when it
seemed to me as if she played it with a certain fatigue. The thing is
so artificial in itself, and yet pretends to be nature; it is so palpably
ingenious, so frank an appeal to the stage! It has about it an absurd
air of honest simplicity, a pretense of being bourgeois in some
worthy sense.
Réjane plays her game with the thing, shows her impeccable
cleverness, makes point after point, carries the audience with her.
But I find nowhere in it what seems to me her finest qualities, at
most no more than a suggestion of them. It is a picture painted so
sweepingly that every subtlety would be out of place in it. She plays
it sweepingly, with heavy contrasts, an undisguised exaggeration;
one eye is always on the audience. That is, no doubt, the way the
piece should be played; but I must complain of Sardou while I justify
Réjane.

THE IRONIC COMEDY OF BECQUE

La Parisienne of Henri Becque, like most of his plays, has never lost
its interest, like the topical plays of that period. It is a hard, ironical
piece of realism, founded on a keen observation of life and on
certain definite ideas. It is called a comedy, but there is no
straightforward fun in it, as in Ma Cousine, for instance; it has all
that transposed sadness which we call irony. It shows us rather a
mean gray world, rather contemptuously; and it leaves us with a
bitter taste in the mouth. That is, if one takes it seriously. Part of the
actor's art in such a piece is to prevent one from taking it too
seriously.
Throughout Réjane is the faultless artist, and her acting is so much
of a piece that it is difficult to praise it in detail. A real woman lives
before one, seems to be overseen on the stage at certain moments
of her daily existence. We see her life going on, not, as with Duse, a
profound inner life, but the life of the character, a vivid, worldly life,
hard, selfish, calculating, deceiving naturally, naturally wary, the
woman of the world, the Parisian. Compare Clotilde with Sapho and
you will see two opposite types rendered with an equal skill; the
woman in love, to whom nothing else matters, and the woman with
lovers, the (what shall I say?) business woman of the emotions.
There is a moment near the beginning where Lafont asks Clotilde if
she has been to see her milliner or her dressmaker, and she answers
sarcastically: "Both!" Her face, as she submits to the question, has
an absurd stare, a stare of profound dissimulation, with something
of a cat who waits. Her whole character, her whole plan of campaign
are in that moment; they but show themselves more pointedly, later
on, when her nerves get the better of her through all the
manifestations of her impatience, up to the return into herself at the
end of the second act, when she stands motionless and speechless,
while her lover entreats her, upbraids her, finally insults her. Her
face, her whole body, endures, wearied into a desperate languor,
seething with suppressed rage and exasperation; at last, her whole
body droops on itself, as if it Can no longer stand upright.
Throughout she speaks with that somewhat discontented grumbling
tone which she can make so expressive; she empties her speech
with little side shrugs of one shoulder, her sinister right eye speaks a
whole subtle language of its own. The only moments throughout the
play when I found anything to criticize are the few moments of
pathos, when she becomes Sarah at second hand.
After La Parisienne came Lolotte, a one-act play of Meilhac and
Halévy. It is amusing, and it gives Réjane the opportunity of showing
us little samples of nearly all her talents. She is both canaille and
bonne fille; above all she is triumphantly, defiantly clever. Again I
was reminded of a Forain drawing: for here is an art which does
everything that it is possible to do with a given material, and what
more can one demand of an artist?
"LA ROBE ROUGE"

A greater contrast could hardly be imagined than that between these


two plays and Brieux's sombre argument in the drama La Robe
Rouge. Unlike Les Avariés, where the argument swamps the drama,
La Robe Rouge is at once a good argument and a good play. There
are perhaps too many points at issue, and the story is perhaps too
much broken into section, but the whole thing takes hold of one,
and, acted as it is acted by Réjane, and her company, it seems to lift
one out of the theater into some actual place where people are
talking and doing good or evil and suffering and coming into conflict
with great impersonal forces; where, in fact, they are living. Without
ever becoming literature, it comes, at times, almost nearer to every-
day reality than literature can permit itself to come. There is not a
good sentence in the play, or a sentence that does not tell. It is the
subject and the hard, unilluminated handling of the subject that
makes the play, and it is a model of that form of drama which deals
sternly with actual things. It gives a great actress, who is concerned
mainly with being true to nature, an incomparable opportunity, and
it gives opportunities to every member of a good company. The
second act tortures one precisely as such a scene in court would
torture one. Its art is the distressingly, overwhelmingly real.
La Robe Rouge is a play so full of solid and serious qualities that it is
not a little difficult not to exaggerate its merits or to praise it for
merits it does not possess. The play deals with vital questions, and it
does not deal with them, as Brieux is apt to do, in a merely
argumentative way. It is not only that abstract question: What is
justice? May the law not be capable of injustice? but the question of
conscience in the lawyer, the judge, the administration of which goes
by the name of justice. It is tragedy within tragedy. How extremely
admirably the whole thing acts, and how admirably it was acted!
After seeing this play, I realize what I have often wondered, that
Réjane is a great tragic actress, and that she can be tragic without
being grotesque. She never had a part in which she was so simple
and so great. When I read the play I found many passages of mere
rhetoric in the part of Zanetta; by her way of saying them Réjane
turned them into simple natural feeling. I can imagine Sarah saying
some of these passages, and making them marvelously effective.
When Réjane says them they go through you like a knife. After
seeing La Robe Rouge, I am not sure that of three great living
actresses, Duse, Sarah, and Réjane, Réjane is not, as a sheer
actress, the greatest of the three.
Réjane has all the instincts, as I have said, of the human animal, of
the animal woman, whom man will never quite civilize. Réjane, in
Sapho or in Zaza for instance, is woman naked and shameless,
loving and suffering with all her nerves and muscles, a gross,
pitiable, horribly human thing, whose direct appeal seizes you by the
throat. In Sapho or Zaza she speaks the language of the senses, no
more; and her acting reminds you of all that you may possibly have
forgotten of how the senses speak when they speak through an
ignorant woman in love. It is like an accusing confirmation of some
of one's guesses at truth, before the realities of the flesh and of the
affections of the flesh. Skepticism is no longer possible: the thing is
before you, abominably real, a disquieting and irrefutable thing,
which speaks with its own voice, as it has never spoken on the stage
through any other actress.
In Zaza, a play made for Réjane by two playwrights who had set
themselves humbly to a task, the task of fitting her with a part, she
is seen doing Sapho over again, with a difference. Zaza is a vulgar
woman, a woman without instruction or experience; she has not
known poets and been the model of a great sculptor; she comes
straight from the boards of a café-concert to the kept woman's
house in the country. She has caught her lover vulgarly, to win a bet;
and so, to the end, you realize that she is, well, a woman who would
do that. She has no depth of passion, none of Sapho's roots in the
earth; she has a "beguin" for Dufresne, she will drop everything else
for it, such as it is, and she is capable of good hearty suffering.
Réjane gives her to us as she is, in all her commonness. The picture
is full of humor; it is, as I so often feel with Réjane, a Forain. Like
Forain, she uses her material without ever being absorbed by it,
without relaxing her impersonally artistic energy. In being Zaza, she
is so far from being herself (what is the self of a great actress?) that
she has invented a new way of walking, as well as new tones and
grimaces. There is not an effect in the play which she has not
calculated; only, she has calculated every effect so exactly that the
calculation is not seen. When you watch Jane Hading, you see her
effects coming several seconds before they are there; when they
come, they come neatly, but with no surprise in them, and therefore
with no conviction. There lies all the difference between the actress
who is an actress equally by her temperament and by her brain and
the actress who has only the brain (and, with Jane Hading, beauty)
to rely on. Everything that Réjane can think of she can do; thought
translates itself instantly into feeling, and the embodied impulse is
before you.
When Réjane is Zaza, she acts and is the woman she acts; and you
have to think, before you remember how elaborate a science goes to
the making of that thrill which you are almost cruelly enjoying.

THE RUSSIAN BALLETS

The dance is life, animal life, having its own way passionately. Part
of that natural madness which men were once wise enough to
include in religion, it began with the worship of the disturbing
deities, the gods of ecstasy, for whom wantonness and wine, and all
things in which energy passes into evident excess, were sacred.
From the first it has mimed the instincts; but we lose ourselves in
the boundless bewilderments of its contradictions.
As the dancers dance, under the changing lights, so human, so
remote, so desirable, so evasive, coming and going to the sound of a
thin heady music which marks the rhythm of their movements like a
kind of clinging drapery, they seem to sum up in themselves the
appeal of everything in the world that is passing and colored and to
be enjoyed. Realizing all humanity to be but a mask of shadows, and
this solid world an impromptu stage as temporary as they, it is with
a pathetic desire of some last illusion, which shall deceive even
ourselves, that we are consumed with this hunger to create, to make
something for ourselves, if at least the same shadowy reality as that
about us. The art of the ballet awaits us, with its shadowy and real
life, its power of letting humanity drift into a rhythm so much of its
own, and with ornament so much more generous than its wont.
And, as all this is symbolical, a series of living symbols, it can but
reach the brain through the eyes, in the visual and imaginative way,
so that the ballet concentrates in itself a great deal of the modern
ideal in matters of artistic impression.
I am avid of impressions and sensations; and in the Russian Ballet at
the Coliseum, certainly, there is a new impression of something not
easily to be seen elsewhere. I need not repeat that, in art, rhythm
means everything. And there can be a kind of rhythm even in
scenery, such as one sees on the stage. Convention, even here, as in
all plastic art, is founded on natural truth very closely studied. The
rose is first learned, in every wrinkle of its petals, petal by petal,
before that reality is elaborately departed from, in order that a new,
abstract beauty may be formed out of these outlines, all but those
outlines being left out.
So, in these Russian Ballets, so many of which are founded on
ancient legends, those who dance and mime and gesticulate have at
once all that is humanity and more than is in humanity. And their
place there permits them, without disturbing our critical sense of the
probability of things, to seem to assume a superhuman passion; for,
in the Art of the Ballet, reality must fade into illusion, and then
illusion must return into a kind of unreal reality.
The primitive and myth-making imagination of the Russians shows a
tendency to regard metaphors as real and to share these tendencies
with the savage, that is to say with the savagery that is in them,
dependent as they are on rudimentary emotions. Other races, too
long civilized, have accustomed themselves to the soul, to mystery.
Russia, with centuries of savagery behind it, still feels the earth
about its roots, and the thirst in it of the primitive animal. It has lost
none of its instincts, and it has just discovered the soul. So, in these
enigmatical dancers, the men and the women, who emerge before
us, across the flaming gulf of the footlights, who emerge as they
never did in any ballet created by Wagner, one finds the
irresponsibility, the gaiety, the sombreness, of creatures who exist on
the stage for their own pleasure and for the pleasure of pleasing us,
and in them something large and lyrical, as if the obscure forces of
the earth half-awakened had begun to speak. And these live,
perhaps, an exasperated life—the life of the spirit and of the senses
—as no others do; a life to most people inconceivable; to me, who
have traveled in Russia, conceivable.
In what is abstract in Russian music there is human blood. It does
not plead and implore like Wagner's. It is more somber, less carnal,
more feverish, more unsatisfied in the desire of the flesh, more
inhuman, than the ballet music in Parsifal. Even in that music,
though shafts of light sometimes pierce the soul like a sword, there
is none of the peace of Bach; it has the unsatisfied desire of a kind
of flesh of the spirit. But in Tchaikovsky's music the violins run up
and down the scales like acrobats; and he can deform the rhythms
of nature with the caprices of half-civilized impulses. In your delight
in finding any one so alive, you are inclined to welcome him without
reserve, and to forget that a man of genius is not necessarily a great
artist, and that, if he is not a great artist, he is not a quite
satisfactory man of genius.
When I heard his music in The Enchanted Princess I was struck by
the contrast of this ballet music with the overture to Francesca da
Rimini I had heard years before. The red wind of hell, in which the
lovers are afloat, blows and subsides. There is a taste of sulphur in
the mouth as it ends, after the screams and spasms. Scrawls of hell-
fire rush across the violins into a sharpened agony; above all, not
Dante's; always hell-fire, not the souls of unhappy lovers who have
loved too well.
Lydia Lopokova is certainly a perfect artist, whose dancing is a
delight to the eyes, as her miming appeals to the senses. She has
passion, and of an excitable kind; in a word, Russian passion. She
can be delicious, malicious, abrupt in certain movements when she
walks; she has daintiness and gaiety; her poses and poises are
exquisite; there is an amazing certainty in everything she does. A
creature of sensitive nerves, in whom the desire of perfection is the
same as her desire for fame, she is on the stage and off the stage
essentially the same; and in her conversations with me I find
imagination, an unerring instinct, an intense thirst for life and for her
own art; she has la joie de vivre.
Her technique, of course, is perfect; and, as in the case of every
artist, it is the result of tireless patience. Technique and the artist:
that is a question of interest to the student of every art. Without
technique, perfect of its kind, no one is worth consideration in any
art. The rope-dancer or the acrobat must be perfect in technique
before he appears on the stage at all; in his case, a lapse from
perfection brings its own penalty, death perhaps; his art begins
when his technique is already perfect. Artists who deal in materials
less fragile than human life should have no less undeviating a sense
of responsibility to themselves and to art. So Ysaye seems to me the
type of the artist, not because he is faultless in technique, but
because he begins to create his art at the point where faultless
technique leaves off.
Lubov Tchernicheva is a snake-like creature, beautiful and hieratic,
solemn; and in her aspect, as in her gestures, a kind of Russian
Cleopatra. Swinburne might have sung of her as he sang of the
queen who ruled the world and Antony:—
Her mouth is fragrant as a vine,
A vine with birds in all its boughs;
Serpent and scarab for a sign
Between the beauty of her brows
And the amorous deep lids divine.
And it is a revelation to our jaded imaginations of much less jaded
imaginations. These may be supposed to be characters in
themselves of little interest to the world in general; to have come by
strange accident from the ends of the world. Yet these are thrown
into chosen situations, apprehended in some delicate pauses of life;
they have their moments of passion thrown into relief in an exquisite
way. To discriminate them we need a cobweb of illusions, double
and treble reflections of the mind upon itself, with the artificial light
of the stage cast over them and, as it were, constructed and broken
over this or that chosen situation—on how fine a needle's point that
little world of passion is balanced!

II

Apart from the loveliness of Manfred—the almost aching loveliness


of Astarte—and the whole of the Carnival, Kreisleriana, and several
other pieces, I have never been able to admire Schumann's music.
When I wrote on Strauss I said that he has many moments in which
he tries to bring humor into music. Turn from the "Annie" motive in
Enoch Arden to the "Eusebius" of the Carnival, and you will readily
see all the difference between two passages which it is quite
possible to compare with one another. The "Annie" motive is as
pretty as can be; it is adequate enough as a suggestion of the
somewhat colorless heroine of Tennyson's poem; but how lacking in
distinction it is, if you but set it beside the "Eusebius," in which
music requires nothing but music to be its own interpreter. But it is
in his attempts at the grotesque that Schumann seems at times
actually to lead the way to Strauss. It is from Schumann that Strauss
has learned some of those hobbling rhythms, those abrupt starts, as
of a terrified peasant, by which he has sometimes suggested his
particular kind of humor in music.
Schumann, like Strauss, reminds me at one time of De Quincey or
Sydney Dobell, at another of Gustave Moreau or of Arnold Bocklin,
and I know that all these names have had their hour of worship. All
have some of the qualities which go to the making of great art; all,
in different ways, fail through lack of the vital quality of sincerity, the
hard and wiry line of rectitude and certainty. All are rhetorical, all
produce their effect by an effort external to the thing itself which
they are saying or singing or painting.
On seeing the Carnival for the second time I am more than ever
struck by the fact that the ballet is a miracle of moving motion. In
the dance of Columbine and Harlequin—they danced and mimed like
living marionettes—I recalled vividly my impression on seeing a
ballet, a farce and the fragments of an opera performed by the
marionettes at the Costanzi Theatre in Rome. I was inclined to ask
myself why we require the intervention of any less perfect medium
between the meaning of a piece, as the audience conceived it, and
that other meaning which it derives from our reception of it. In those
inspired pieces of living painted wood I saw the illusion that I always
desire to find, either in the wings of the theater or from a stall. In
our marionettes, then, we get personified gesture, endless gesture,
like all other forms of emotion, generalized. The appeal in what
seems to you those childish maneuvers is to a finer, because to a
more innately poetic sense of things than the rationalistic appeal of
very modern plays. If at times we laugh—as one must in this ballet—
it is with wonder at seeing humanity so gay, heroic and untiring.
There is the romantic suggestion of magic in this beauty. So, in
Harlequin, I find the personification of grace, of souplesse, in his
miming and dancing; and when he is grotesque, I find a singular
kind of beauty. A sinister gaiety pervades the ballet; a malevolent
undercurrent of subtle meanings gives one the sense of an intricate
intrigue; and I almost forgive the fact that the music is German!
I am, on the whole, disappointed with the Cleopatra ballet; for the
scenery certainly does not suggest Egypt; but, to my mind, suggests
rather the scenery used in Paris when I saw Alfred Jarry's Ubu Roi, a
symbolist farce, given under strange conditions. The action took
place in the land of nowhere; and the scenery was painted to
represent by adroit conventions temperate and torrid zones at once.
Then there were closed windows and a fireplace, containing an
alchemist's crucible. These were crudely symbolical, but those in the
Coliseum were not. In our search for sensation we have exhausted
sensation; and, in that theater, before a people who have perfected
the fine shades to their vanishing point, who have subtilized delicacy
of perception into the annihilation of the very senses through which
we take in ecstasy, I heard a literary Sans-culotte shriek for hours
that unspeakable word of the gutter which was the refrain of this
comedy of masks. Just as the seeker after pleasure whom pleasure
has exhausted, so the seeker after the material illusions of a literary
artifice turns finally to that first, subjugated, never quite
exterminated element of cruelty which is one of the links which bind
us to the earth.
The Russians have cruelty enough, but not this kind of cruelty; they
are more complex than cruel, and why credit them with any real
sense of morality? They are gifted with a kind of sick curiosity which
makes them infinitely interesting to themselves. And—to concern
myself again with these Russian dancers—they live in a prodigious
illusion; their life in them is so tremendous that they are capable of
imagining anything. And, in the words of Gorki, "in every being who
lives there is hidden a vagabond more or less conscious of himself;"
but—for all those who revolt—he has one phrase: l'Épouvante du
mal de vivre.
Now, Lubov Tchernicheva, who looked Cleopatra and was dressed
after Cleopatra's fashion, had nothing whatever to do, except to be
repellent and attractive. She was given no chance to show that the
queen she represented was one of those diabolical creatures whose
coquetry is all the more dangerous because it is susceptible of
passion; one in whom passion was at times like a will-o'-the-wisp
that is suddenly extinguished after having given light to a
conflagration.
Scheherazade is barbaric and gorgeous in décor, and in costume
exotic and tragic and Oriental as the Russian music is; only, to me,
the music is not quite satisfying; it has rather an irritating effect on
the nerves. The dances are bewildering, intricate and elaborate, and
intensely alive with animal desire. It is really a riot in color, amid an
ever-moving crowd of revellers; in which Massine shows himself as
the personification of lust, as he makes—with furious and too
convulsive leaps in the air and with too obvious gestures and
grimaces—frantic love to Zobeide, mimed by Tchernicheva, who has
the stateliness of a princess, who glides mysteriously and is
wonderful in the plastic quality of her movements, which I can only
image as that of a tiger-cat.
Carlo Goldoni has been compared as a great comic dramatist with
Pietro Longhi, who, in his amazingly amusing pictures, reflects also
the follies and revels and miseries of the period. Longhi used to tell
Goldoni that they—the painter and the playwright—were brethren in
art; and one of Goldoni's sonnets records this saying:—
Longhi, tu che la mia musa sorella
Chiami del tuo pennel che cerca il vero.
It seems that their contemporaries were alive to the similar qualities
and the common aims of the two men; for Gasparo Gozzi drew a
parallel between them in a number of his Venetian Gazzetta.
It struck me, as I saw the Goldoni ballet and heard the music of
Domenico Scarlatti, that all of the costumes and much of the effect
of the miming—which were the most delicious and capricious that I
have ever seen—had been designed after Longhi's paintings and
drawings; for in many of these he gives a wonderful sense of living
motion; but certainly nothing of what is abominably alive in the
great and grim and sardonic genius of Hogarth.
In Venice I have often spent delightful hours before, for instance,
such innumerable drawings of his as: painters at the easel, ballet
girls with castanets, maid-servants holding trays, music and dancing
masters (indeed, is not Enrico Cecchetti in the ballet a most
admirable and most Italian dancing master?), tavern-keepers, street
musicians, beggars, waiters; the old patrician lolling in his easy-chair
and toying with a fan; the cavaliere in their fantastic dresses; the
women with their towering head-dresses. The whole sense of Venice
returned to me as I saw Lydia Lopokova—always so bird-like, so like
a butterfly with painted wings, so witty in gestures, so absolutely an
artist in every dance she dances, in every mime she mimics, in her
wild abandonment to the excitement of these shifting scenes, where
all these masked and unmasked living puppets have fine nerves and
delicate passions—putting powder on the face of the Marquise
Silvestra and mocking her behind her back. I saw then Casanova's
favorite haunts: the ridotti, the gambling-houses, the cafés in San
Marco's, the carnivals, the masked balls, the intrigues; the traghetti
where I seemed to see mysterious figures flitting to and fro in wide
miraculous route beneath the light of flickering flambeaux.
I see before me, as I write, the night when I went from the
Giddecca to the Teatro Rossini, where a company of excellent Italian
comedians gave one of Goldoni's comedies, and, as when the
chatter in the gallery ends, the chatter begins on the stage, I found
for once the perfect illusion; there is no difference between the one
and the other. Voluble, living Venice, with its unchanging attitude
toward things, the prompt gaiety and warmth of its temperament,
finds equal expression in the gallery, and in the interpretation of
Goldoni, on that stage. Going to the theater in Venice is like a
fantastic overture to the play, and sets one's mood properly in tune.
You step into the gondola, which darts at once across a space of
half-lighted water, and turns down a narrow canal between walls
which seem to reach more than half-way to the stars. Here and
there a lamp shines from a bridge or at the water-gate of a house,
but with no more than enough light to make the darkness seen. You
see in flashes: an alley with people moving against the light, the
shape of a door or balcony, seen dimly and in a wholly new aspect, a
dark church-front, a bridge overhead, the water lapping against the
green stone of a wall which your elbow all but touches, a head
thrust from a window, the gondola that passes you, sliding gently
and suddenly alongside, and disappearing into an unseen quiet.
Sadko is simply a magical and magnificent pantomime, and Rimski-
Korsakov's ballet music gives me the sense of the swirl and
confusion, of the bewilderments and infinite changes in the realm of
the sea-king. And, apart from the riotous Russian dancing, most of
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebooknice.com

You might also like