spaCy icon indicating copy to clipboard operation
spaCy copied to clipboard

BILOU scheme for annotating entities

Open jacob7182 opened this issue 8 years ago • 6 comments

Hi, what could be the BILOU scheme for annotating following sentences?

  1. 309 Cedar Street, #2323
  2. self-attested in the U.S.
  3. Dr. med. XYZ DEF (Principal Investigator), XYZ ABC (Co-investigator)

the confusion is when special characters are joined to the normal words like self-attested, (Co-investigator), Dr. med.

Should we treat special character as separate annotation or part of the word?

  • Operating System: Windows OS 64 bit
  • Python Version Used: 3.6
  • spaCy Version Used: 2.0
  • Environment Information: Anaconda 3

jacob7182 avatar Jun 12 '18 08:06 jacob7182

How you annotate your data and train your model ultimately depends on what you want it to produce at runtime, what your data looks like etc.

Since the BILUO tags are per-token tags, you definitely want to look at spaCy's tokenization, though and create your entity tags accordingly (or adjust the tokenization if it doesn't work for your use case). While you can provide gold-standard tokenization during training, you usually do want the number of tokens and number of BILUO tags to match.

nlp = spacy.load('en_core_web_sm')
doc = nlp(u"self-attested in the U.S.")
print([token.text for token in doc])
# ['self', '-', 'attested', 'in', 'the', 'U.S.']

Your tags could then look like this:

['self', '-', 'attested', 'in', 'the', 'U.S.']
['O', 'O', 'O', 'O', 'O', 'U-GPE']

Or, assuming you want the model to predict "Dr. med. XYZ" as a PERSON entity (looking at this example, we might want to add a tokenizer exception for "med."):

['Dr.', 'med', '.', 'XYZ', 'DEF', '(', 'Principal', 'Investigator', ')']
['B-PERSON', 'I-PERSON', 'I-PERSON', 'I-PERSON', 'L-PERSON', 'O', 'O', 'O', 'O']

Alternatively, you can also provide the character offsets instead of the BILUO tags, and spaCy will figure out the mapping of the tags to the tokens. However, if the offsets don't match the tokenization, spaCy will regard the labels for those words as missing. So if possible, it's always better to provide annotations in line with the tokenization, to make sure your model is actually learning from all available examples.

ines avatar Jun 12 '18 09:06 ines

Thanks for the info. In my program, I am just putting the input sentence and the BILOU tags for training. I have not specified tokens anywhere. Is that done by spacy automatically by looking at the number of annotations provided? how does spacy know it has to treat #2323 as single string or as separate strings # and 2323. even if I tag it as 'O', 'U-CARDINAL', how will it know where is the boundary in #2323 . could it take #23 and 23 as the tokens?

my program below

train_data = ('309 Cedar Street, #2323',['U-CARDINAL','B-GPE','L-GPE','O','O']) for raw_text, entity_offsets in train_data: doc = nlp.make_doc(raw_text) #position of doc object assignment is nlp.tagger(doc) gold = GoldParse(doc, entities=entity_offsets)
nlp.entity.update([doc], [gold], drop=0.5, sgd=optimizer)

jacob7182 avatar Jun 12 '18 10:06 jacob7182

Yes, when you use spaCy and call nlp on a string of text, the first step is always to tokenize the text and create a Doc object. The Doc object will then hold all information about the text. For example, the individual words, their lexical attributes and, if available, their word vectors, part-of-speech tags and dependencies, and entity boundaries.

Tokenization takes both language-independent as well as language-specific rules and exceptions into account. Those are provided in the language data in spacy/lang.

First, the raw text is split on whitespace characters, similar to text.split(' '). Then, the tokenizer processes the text from left to right. On each substring, it performs two checks:

  • Does the substring match a tokenizer exception rule? For example, "don't" does not contain whitespace, but should be split into two tokens, "do" and "n't", while "U.K." should always remain one token.
  • Can a prefix, suffix or infix be split off? For example punctuation like commas, periods, hyphens or quotes.

You can find more details on the tokenization algorithm here: https://spacy.io/usage/spacy-101#annotations-token

train_data = ('309 Cedar Street, #2323',['U-CARDINAL','B-GPE','L-GPE','O','O'])

One quick comment: I'm not sure how your full training script and input data looks, but if you want your application to be able to label those entities in context, make sure to also provide examples that show them in context – not just extracted phrases. To get the best results, your training examples should be as similar as possible to the texts you need to process at runtime.

ines avatar Jun 12 '18 11:06 ines

thanks, Yes, I am providing the training inputs by picking sentences from the real data to be processed. I understand that to train, we need to provide sentences and annotate words in them whether using BILOU scheme or character offsets.

Will it work if we only provide the entity words as training input rather than sentences ? (I feel it may not, since it is neural network training and the model learns from patterns within sentences).
The reason I am asking this is because based on the results from NLP, we are thinking of using incorrect entity results in (GPE/ORG/Person etc) to send back as training inputs to the model and further improve results. Otherwise the training activity will be an offline process with annotating the sentences and feeding to training scripts.

jacob7182 avatar Jun 12 '18 11:06 jacob7182

Will it work if we only provide the entity words as training input rather than sentences ? (I feel it may not, since it is neural network training and the model learns from patterns within sentences).

Yes, to achieve the best results, you should always try to keep it as close to the real sentences as possible. So, full sentence or paragraph with the entity / entities labeled. Otherwise, your model might accidentally learn a general policy like "If the text is very short and there's no context around it, the whole thing is an entity. Otherwise, entities don't exist."

The reason I am asking this is because based on the results from NLP, we are thinking of using incorrect entity results in (GPE/ORG/Person etc) to send back as training inputs to the model and further improve results.

Yes, that sounds like a good strategy. Also make sure to always include examples that the model previously got right. For example, if you're correcting wrong GPE entities, you could mix in a selection of correct examples as well. This prevents the model from learning a policy like "Nothing is a GPE anymore!".

ines avatar Jun 12 '18 19:06 ines

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jul 12 '18 20:07 lock[bot]