使用spaCy构建文本分类器可以通过以下步骤来完成:
import spacy
from spacy.lang.en import English
from spacy.pipeline.textcat import TextCategorizer
nlp = spacy.load('en_core_web_sm')
train_data = [
("This is a positive review", {"cats": {"positive": 1, "negative": 0}}),
("This is a negative review", {"cats": {"positive": 0, "negative": 1}})
]
textcat = nlp.create_pipe("textcat")
nlp.add_pipe(textcat, last=True)
textcat.add_label("positive")
textcat.add_label("negative")
for text, annotations in train_data:
nlp.update([text], [annotations])
doc = nlp("This is a positive sentence")
print("Categories:", doc.cats)
通过以上步骤,你可以使用spaCy构建一个简单的文本分类器,用于对文本进行情感分类或其他类型的分类任务。你还可以根据实际需求对模型进行优化和调整来提高分类的准确性和性能。