python Spacy custom NER – how to prepare multi-words entities?

:) Please help :)

I`m preparing custom Name Entity Recognition using Spacy (blank) model. I use only one entity: Brand (we can name it 'ORG' as Organisation). I have short texts with ORGs & have prepared data like this (but I can change it):

train_data = [ 
    (‘First text in string with Name I want’, {'entities': [(START, END, ‘ORG')]}),
    (‘Second text with Name and Name2’, {'entities': [(START, END, ‘ORG'), (START2, END2, ‘ORG')]})
 ]

START, END – are the start and end indexes of the brand name in text , of course.

This is working well, but...

The problem I have is how to prepare entities for Brands that are made of 2 (or more) words. Lets say Brand Name is a full name of a company. How to prepare an entity?

Consider the tuple itself for a single text:

text = 'Third text with Brand Name'

company = 'Brand Name'

(‘Third text with Brand Name', {“entities”: [(16, 26, 'ORG')]})
(‘Third text with Brand Name', {“entities”: [(16, 21, 'ORG'), (22, 26, 'ORG')]})
(‘Third text with Brand Name', {“entities”: [(16, 21, 'B-ORG'), (22, 26, 'I-ORG')]})
(‘Third text with Brand Name', {"entities": ["O", "O", "O", "B-ORG", "I-ORG"]})

The question is on the format of the train_data for ‘Third text with Brand Name' - how to label the entity. If I have the format, I will handle the code. :)

The same question for 3 or more words entities. :)

Upvotes: 1

Views: 1676

Answers (1)

polm23
polm23

Reputation: 15623

You can just provide the start and end offsets for the whole entity. You describe this as "treating it as one word", but the character offsets don't have any direct relation to tokenization - they won't affect tokenizer output.

You will get an error if the start and end of your entity don't match token boundaries, but it doesn't matter if the entity is one token or many.

I recommend you take a look at the training data section in the spaCy docs. Your specific question isn't answered explicitly, but that's only because multi-token entries don't require special treatment. Examples include multi-token entities.

Regarding BIO tagging, for details on how to use it with spaCy you can see the docs for spacy convert.

Upvotes: 2

Related Questions