Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Mastering spaCy

You're reading from   Mastering spaCy Build structured NLP solutions with custom components and models powered by spacy-llm

Arrow left icon
Product type Paperback
Published in Feb 2025
Publisher Packt
ISBN-13 9781835880463
Length 238 pages
Edition 2nd Edition
Languages
Tools
Concepts
Arrow right icon
Authors (2):
Arrow left icon
Déborah Mesquita Déborah Mesquita
Author Profile Icon Déborah Mesquita
Déborah Mesquita
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 (17) Chapters Close

Preface 1. Part 1: Getting Started with spaCy FREE CHAPTER
2. Chapter 1: Getting Started with spaCy 3. Chapter 2: Core Operations with spaCy 4. Part 2: Advanced Linguistic and Semantic Analysis
5. Chapter 3: Extracting Linguistic Features 6. Chapter 4: Mastering Rule-Based Matching 7. Chapter 5: Extracting Semantic Representations with spaCy Pipelines 8. Chapter 6: Utilizing spaCy with Transformers 9. Part 3: Customizing and Integrating NLP Workflows
10. Chapter 7: Enhancing NLP Tasks Using LLMs with spacy-llm 11. Chapter 8: Training an NER Component with Your Own Data 12. Chapter 9: Creating End-to-End spaCy Workflows with Weasel 13. Chapter 10: Training an Entity Linker Model with spaCy 14. Chapter 11: Integrating spaCy with Third-Party Libraries 15. Index 16. Other Books You May Enjoy

Merging and splitting tokens

In some cases, we want to unite or split multiword named entities. For example, this is needed when the tokenizer does not perform so well on some unusual tokens, and you need 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.

doc.retokenize is used in a context manager and it’s the correct tool for merging and splitting the spans of doc objects. The retokenizer.merge() method should receive the spans to merge and the attributes to set on these merged tokens. Let’s see an example of retokenization by merging a multiword named entity, as follows:

  1. First, let’s create a doc from the sentence and print the entities:
    doc = nlp("She lived in New Hampshire.")
    print(doc.ents)
  2. Now let’s see how spaCy separated the tokens:
    print([(token.text, token.i) for token in doc])
    >>> [('She&apos...
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