Merging and Splitting Tokens
Explore how to merge multiword named entities into single tokens and split tokens to correct typos or customize tokenization in spaCy. Understand using doc.retokenize to adjust token spans while maintaining linguistic attributes and updating dependency trees to enhance NLP accuracy and flexibility.
We'll cover the following...
Overview
We extracted the name entities in the previous section, but what 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 we want to split them by hand? In this lesson, 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:
This is what we did in the preceding code:
Line 3: We created a
docobject from the sample sentence.Line 4: We printed its entities with
doc.ents, and the result wasNew Hampshire, as expected.Line ...