I have an NER training data using Spacy in the following format.
[('Christmas Perot 2021 TSO\nSkip to Main Content HOME CONCERTS EVENTS ABOUT STAFF EDUCATION SUPPORT US More Use tab to navigate through the menu items. BUY TICKETS SUNDAY, DECEMBER 12, 2021 I PEROT THEATRE I 4:00 PM\nPOPS I Christmas at The Perot\nCLICK HERE to purchase tickets, or contact the Texarkana Symphony Orchestra at 870.773.3401\nA Texarkana Tradition Join the TSO, the Texarkana Jazz Orchestra, and the TSO Chamber Singers, for this holiday concert for the whole family.\nDon’t miss seeing the winner of TSO’s 11th Annual Celebrity Conductor Competition\nBack to Events 2019 Texarkana Symphony Orchestra',
{'entities': [(375, 399, 'organization'),
(290, 318, 'organization'),
(220, 242, 'production_name'),
(169, 186, 'performance_date'),
(189, 202, 'auditorium'),
(205, 212, 'performance_starttime'),
(409, 428, 'organization')]})]
Data is the first element in the tuple. Within entities, the numbers represent the character position (start and end) of entities in data. Some lines do not have any entities. For example first line Christmas Perot 2021 TSO do not have any entities. I need to remove sentences which do not have any entities. Removing of sentences can be done based in . and \n characters. I got the entity data based on the character number but i didnt manage to get the removal of sentences which are not tagged
Code
from tqdm import tqdm
import spacy
nlp = spacy.blank("en") # load a new spacy model
db = DocBin() # create a DocBin object
for text, annot in tqdm(train_data): # data in previous format
doc = nlp.make_doc(text) # create doc object from text
ents = []
for start, end, label in annot["entities"]: # add character indexes
span = doc.char_span(start, end, label=label, alignment_mode="contract")
print(start,end,span,label)
if span is None:
print("Skipping entity")
else:
ents.append(span)
doc.ents = ents # label the text with the ents