Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering spaCy

You're reading from   Mastering spaCy An end-to-end practical guide to implementing NLP applications using the Python ecosystem

Arrow left icon
Product type Paperback
Published in Jul 2021
Publisher Packt
ISBN-13 9781800563353
Length 356 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Duygu Altınok Duygu Altınok
Author Profile Icon Duygu Altınok
Duygu Altınok
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Section 1: Getting Started with spaCy
2. Chapter 1: Getting Started with spaCy FREE CHAPTER 3. Chapter 2: Core Operations with spaCy 4. Section 2: spaCy Features
5. Chapter 3: Linguistic Features 6. Chapter 4: Rule-Based Matching 7. Chapter 5: Working with Word Vectors and Semantic Similarity 8. Chapter 6: Putting Everything Together: Semantic Parsing with spaCy 9. Section 3: Machine Learning with spaCy
10. Chapter 7: Customizing spaCy Models 11. Chapter 8: Text Classification with spaCy 12. Chapter 9: spaCy and Transformers 13. Chapter 10: Putting Everything Together: Designing Your Chatbot with spaCy 14. Other Books You May Enjoy

Merging and splitting tokens

We extracted the name entities in the previous section, but how about if we want to unite or split multiword named entities? And what if the tokenizer performed this not so well on some exotic tokens and you want to split them by hand? In this subsection, we'll cover a very practical remedy for our multiword expressions, multiword named entities, and typos.

doc.retokenize is the correct tool for merging and splitting the spans. Let's see an example of retokenization by merging a multiword named entity, as follows:

 doc = nlp("She lived in New Hampshire.")
 doc.ents
(New Hampshire,)
 [(token.text, token.i) for token in doc]
[('She', 0), ('lived', 1), ('in', 2), ('New', 3), ('Hampshire', 4), ('.', 5)]
 len(doc)
6
 with doc.retokenize() as retokenizer:
     retokenizer.merge(doc[3:5], \
     attrs={"LEMMA": "new hampshire...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image