@plac.annotations(<EOL>model=("<STR_LIT>", "<STR_LIT>", "<STR_LIT:m>", str),<EOL>output_dir=("<STR_LIT>", "<STR_LIT>", "<STR_LIT:o>", Path),<EOL>n_iter=("<STR_LIT>", "<STR_LIT>", "<STR_LIT:n>", int),<EOL>)<EOL>def main(model=None, output_dir=None, n_iter=<NUM_LIT:100>):
if model is not None:<EOL><INDENT>nlp = spacy.load(model)  <EOL>print("<STR_LIT>" % model)<EOL><DEDENT>else:<EOL><INDENT>nlp = spacy.blank("<STR_LIT>")  <EOL>print("<STR_LIT>")<EOL><DEDENT>if "<STR_LIT>" not in nlp.pipe_names:<EOL><INDENT>ner = nlp.create_pipe("<STR_LIT>")<EOL>nlp.add_pipe(ner, last=True)<EOL><DEDENT>else:<EOL><INDENT>ner = nlp.get_pipe("<STR_LIT>")<EOL><DEDENT>for _, annotations in TRAIN_DATA:<EOL><INDENT>for ent in annotations.get("<STR_LIT>"):<EOL><INDENT>ner.add_label(ent[<NUM_LIT:2>])<EOL><DEDENT><DEDENT>other_pipes = [pipe for pipe in nlp.pipe_names if pipe != "<STR_LIT>"]<EOL>with nlp.disable_pipes(*other_pipes):  <EOL><INDENT>if model is None:<EOL><INDENT>nlp.begin_training()<EOL><DEDENT>for itn in range(n_iter):<EOL><INDENT>random.shuffle(TRAIN_DATA)<EOL>losses = {}<EOL>batches = minibatch(TRAIN_DATA, size=compounding(<NUM_LIT>, <NUM_LIT>, <NUM_LIT>))<EOL>for batch in batches:<EOL><INDENT>texts, annotations = zip(*batch)<EOL>nlp.update(<EOL>texts,  <EOL>annotations,  <EOL>drop=<NUM_LIT:0.5>,  <EOL>losses=losses,<EOL>)<EOL><DEDENT>print("<STR_LIT>", losses)<EOL><DEDENT><DEDENT>for text, _ in TRAIN_DATA:<EOL><INDENT>doc = nlp(text)<EOL>print("<STR_LIT>", [(ent.text, ent.label_) for ent in doc.ents])<EOL>print("<STR_LIT>", [(t.text, t.ent_type_, t.ent_iob) for t in doc])<EOL><DEDENT>if output_dir is not None:<EOL><INDENT>output_dir = Path(output_dir)<EOL>if not output_dir.exists():<EOL><INDENT>output_dir.mkdir()<EOL><DEDENT>nlp.to_disk(output_dir)<EOL>print("<STR_LIT>", output_dir)<EOL>print("<STR_LIT>", output_dir)<EOL>nlp2 = spacy.load(output_dir)<EOL>for text, _ in TRAIN_DATA:<EOL><INDENT>doc = nlp2(text)<EOL>print("<STR_LIT>", [(ent.text, ent.label_) for ent in doc.ents])<EOL>print("<STR_LIT>", [(t.text, t.ent_type_, t.ent_iob) for t in doc])<EOL><DEDENT><DEDENT>
Load the model, set up the pipeline and train the entity recognizer.