@plac.annotations(<EOL>model=("<STR_LIT>", "<STR_LIT>", "<STR_LIT:m>", str),<EOL>new_model_name=("<STR_LIT>", "<STR_LIT>", "<STR_LIT>", 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, new_model_name="<STR_LIT>", output_dir=None, n_iter=<NUM_LIT:30>):
random.seed(<NUM_LIT:0>)<EOL>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)<EOL><DEDENT>else:<EOL><INDENT>ner = nlp.get_pipe("<STR_LIT>")<EOL><DEDENT>ner.add_label(LABEL)  <EOL>ner.add_label("<STR_LIT>")<EOL>if model is None:<EOL><INDENT>optimizer = nlp.begin_training()<EOL><DEDENT>else:<EOL><INDENT>optimizer = nlp.resume_training()<EOL><DEDENT>move_names = list(ner.move_names)<EOL>other_pipes = [pipe for pipe in nlp.pipe_names if pipe != "<STR_LIT>"]<EOL>with nlp.disable_pipes(*other_pipes):  <EOL><INDENT>sizes = compounding(<NUM_LIT:1.0>, <NUM_LIT>, <NUM_LIT>)<EOL>for itn in range(n_iter):<EOL><INDENT>random.shuffle(TRAIN_DATA)<EOL>batches = minibatch(TRAIN_DATA, size=sizes)<EOL>losses = {}<EOL>for batch in batches:<EOL><INDENT>texts, annotations = zip(*batch)<EOL>nlp.update(texts, annotations, sgd=optimizer, drop=<NUM_LIT>, losses=losses)<EOL><DEDENT>print("<STR_LIT>", losses)<EOL><DEDENT><DEDENT>test_text = "<STR_LIT>"<EOL>doc = nlp(test_text)<EOL>print("<STR_LIT>" % test_text)<EOL>for ent in doc.ents:<EOL><INDENT>print(ent.label_, ent.text)<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.meta["<STR_LIT:name>"] = new_model_name  <EOL>nlp.to_disk(output_dir)<EOL>print("<STR_LIT>", output_dir)<EOL>print("<STR_LIT>", output_dir)<EOL>nlp2 = spacy.load(output_dir)<EOL>assert nlp2.get_pipe("<STR_LIT>").move_names == move_names<EOL>doc2 = nlp2(test_text)<EOL>for ent in doc2.ents:<EOL><INDENT>print(ent.label_, ent.text)<EOL><DEDENT><DEDENT>
Set up the pipeline and entity recognizer, and train the new entity.