SpaCy 是一个开源的 Python 库,专为高级自然语言处理任务而设计,例如文本分析、实体识别、依存分析等。
SpaCy 注重性能和易用性,为各种 NLP 任务提供统一的 API,使其成为文本处理领域的初学者和专家的理想工具。
SpaCy 的一些主要功能包括。
- 高性能:SpaCy 专为速度和效率而设计,使你能够快速轻松地处理和分析大量文本。
- 易于使用:通过直观的 API 和全面的文档,SpaCy 简化了复杂的 NLP 任务,并为所有技能水平的用户提供了平滑的学习曲线。
- 可定制性:SpaCy 允许创建自定义管道和扩展,使你能够根据你的特定需求和要求定制库。
- 与其他库集成:SpaCy 可以轻松与其他流行的 Python 库(例如 TensorFlow、PyTorch 和 scikit-learn)集成,进一步扩展其功能和应用。
特点
它具有如下特点。
- 支持 70 多种语言
- 针对不同语言和任务的训练管道
- 使用 BERT 等预训练 Transformer 进行多任务学习
- 支持预训练的词向量和嵌入
- 最先进的速度
- 用于命名实体识别、词性标记、依存分析、句子分割、文本分类、词形还原、形态分析、实体链接等的组件
- 可通过自定义组件和属性轻松扩展
- 支持 PyTorch、TensorFlow 和其他框架中的自定义模型
- 内置语法和 NER 可视化工具
- 轻松的模型打包、部署和工作流程管理
- 稳健且经过严格评估的准确性
老规矩:如果觉得文章不错!欢迎大家点赞、转发安排起来。
初体验
库的安装
要开始使用 SpaCy,你需要安装该库及其依赖项。你可以使用 pip 执行此操作。
pip install spacy
安装完成后,你还可以下载所需语言的预训练模型。例如,要下载英文模型,请运行。
python -m spacy download en_core_web_sm
基本文本处理技术
在本节中,我们将探讨一些基本的文本处理技术,这些技术构成了任何 NLP 项目的基础。
标记化
标记化是将文本分解为单个标记(例如单词、句子或短语)的过程。使用 SpaCy,只需几行代码即可实现标记化。
import spacy
nlp = spacy.load("en_core_web_sm")
text = "This is a sample sentence."
doc = nlp(text)
for token in doc:
print(token.text)
词形还原
词形还原是将单词还原为其基本形式的过程。
这有助于规范文本并合并相似的单词以进行进一步分析。
for token in doc:
print(token.text, token.lemma_)
词性标注
词性 (POS) 标记涉及为文本中的每个标记分配语法类别,例如名词、动词或形容词。
for token in doc:
print(token.text, token.pos_)
命名实体识别
命名实体识别 (NER) 是对文本中的命名实体(例如人员、组织或位置)进行识别和分类的过程。
for ent in doc.ents:
print(ent.text, ent.label_)
文本处理技术进阶
在本节中,我们将深入研究更先进的文本处理技术,以便更深入地分析和理解文本数据。
文本分类
文本分类是根据文本内容将文本分类为预定义类别的任务。
使用 SpaCy,你可以训练自定义文本分类器来执行情感分析、主题分类或垃圾邮件检测等任务。
import spacy
import random
from spacy.training.example import Example
# Load the pre-trained model
nlp = spacy.load("en_core_web_sm")
# Create a blank TextCategorizer with the "textcat" name
if "textcat" not in nlp.pipe_names:
textcat = nlp.create_pipe("textcat", config={"exclusive_classes": True, "architecture": "simple_cnn"})
nlp.add_pipe(textcat, last=True)
else:
textcat = nlp.get_pipe("textcat")
# Add labels (categories) to the text classifier
textcat.add_label("LABEL_1")
textcat.add_label("LABEL_2")
# Add more labels as needed
# Prepare the training data
train_data = [("Text example 1", {"cats": {"LABEL_1": 1, "LABEL_2": 0}}),
("Text example 2", {"cats": {"LABEL_1": 0, "LABEL_2": 1}}),
# Add more training examples with their corresponding labels
]
# Training loop
random.seed(1)
spacy.util.fix_random_seed(1)
optimizer = nlp.begin_training()
for epoch in range(10): # You can adjust the number of epochs
random.shuffle(train_data)
losses = {}
# Batch the training data
for batch in spacy.util.minibatch(train_data, size=2):
texts, annotations = zip(*batch)
example = []
# Update the model with iterating each text
for i in range(len(texts)):
doc = nlp.make_doc(texts[i])
example.append(Example.from_dict(doc, annotations[i]))
nlp.update(example, drop=0.5, losses=losses)
print(losses)
# Save the trained model to a file
nlp.to_disk("custom_model")
# Test the trained model
test_text = "This is a test text."
doc = nlp(test_text)
print("Predicted categories:", doc.cats)
文本提取
文本提取涉及从非结构化文本中提取特定信息或模式。
SpaCy 强大的 NER 功能可以扩展以提取自定义实体和模式。
import spacy
from spacy.matcher import Matcher
from spacy.tokens import Doc, Span
# Load a SpaCy model (you can use a pre-trained model or a blank one)
nlp = spacy.load("en_core_web_sm")
# Define a custom entity type
CustomEntity = nlp.vocab.strings.add("CUSTOM_ENTITY")
# Create a custom component to add the entity to the Doc
def add_custom_entity_to_doc(doc, start, end, label):
entity = Span(doc, start, end, label=label)
doc.ents += (entity,)
return doc
# Example: Let's say you want to extract "OpenAI" as a custom entity
matcher = Matcher(nlp.vocab)
matcher.add("CustomEntityPattern", None, [{"LOWER": "openai"}])
# Custom function to handle matched patterns and add custom entities
def custom_entity_matcher(doc, matcher, custom_entity_type):
matches = matcher(doc)
spans = [doc[start:end] for match_id, start, end in matches]
for span in spans:
doc = add_custom_entity_to_doc(doc, span.start, span.end, custom_entity_type)
return doc
# Add the custom entity matcher to the pipeline
nlp.add_pipe(custom_entity_matcher, last=True, config={"custom_entity_type": CustomEntity})
# Process a text document
text = "OpenAI is an AI research lab. openai develops advanced AI models."
doc = nlp(text)
# Iterate through entities and print them
for ent in doc.ents:
print(f"Entity: {ent.text}, Label: {ent.label_}")
# Output will show the custom entity "OpenAI" as extracted
SpaCy 与其他库集成
SpaCy 可以轻松与其他流行的 Python 库(例如 TensorFlow、PyTorch 和 scikit-learn)集成,以扩展其功能和应用程序。
import spacy
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
# Load SpaCy model and NLTK sentiment analyzer
nlp = spacy.load("en_core_web_sm")
nltk.download("vader_lexicon")
sid = SentimentIntensityAnalyzer()
# Text to analyze
text = "SpaCy and NLTK integration is great! I love working with both."
# SpaCy for tokenization and part-of-speech tagging
doc = nlp(text)
# Tokenize and perform part-of-speech tagging with SpaCy
tokens = [token.text for token in doc]
pos_tags = [token.pos_ for token in doc]
print("Tokens:", tokens)
print("Part-of-Speech Tags:", pos_tags)
# NLTK for sentiment analysis
# Note: NLTK's sentiment analysis is not built-in, so we use the VADER sentiment analyzer
sentiment = sid.polarity_scores(text)
print("Sentiment Analysis Results:")
for key, value in sentiment.items():
print(f"{key}: {value}")
# You can integrate and use other libraries, such as scikit-learn for machine learning or other NLP libraries, as needed for your project.
通过 SpaCy 掌握 NLP 的强大功能,你可以释放文本分析和语言处理的潜力,提供有价值的见解和自动化功能。
来源—–小寒