import spacy from spacy.pipeline.textcat import TextCategorizer from spacy.tokens import DocBin import random
nlp = spacy.blank("en")
categories = ["category1", "category2"]
textcat = nlp.add_pipe("textcat", last=True) for category in categories: textcat.add_label(category)
train_data = [ ("Text 1", {"cats": {"category1": 1, "category2": 0}}), ("Text 2", {"cats": {"category1": 0, "category2": 1}}), # Add more training data... ]
random.shuffle(train_data) # Shuffle the training data for text, annotations in train_data: doc = nlp.make_doc(text) example = (doc, annotations) textcat.update([example])
n_iter = 10 for i in range(n_iter): random.shuffle(train_data) for text, annotations in train_data: doc = nlp.make_doc(text) example = (doc, annotations) textcat.update([example])
output_dir = "/path/to/save/model" nlp.to_disk(output_dir)
loaded_model = spacy.load(output_dir)
test_text = "Test text" doc = loaded_model(test_text) print(doc.cats)