Spacy使用手册

本文介绍了如何安装和使用Spacy进行自然语言处理,包括选择效率和精度的模型,如en_core_web_sm和en_core_web_trf。通过Spacy,可以进行词性标注和依赖关系分析,并展示了如何打印依赖树结构。此外,还提供了批量处理文本的方法。
摘要由CSDN通过智能技术生成

1. 安装Spacy如下:https://spacy.io/usage

我的选项如下,其中Select pipeline for efficiency对应en_core_web_sm,Select pipeline for accuracy 对应en_core_web_trf;

2. 手动下载Spacy模型,调用如下命令安装:

pip install en_core_web_sm-3.0.0.tar.gz
pip install en_core_web_trf-3.0.0.tar.gz

en_core_web_sm文件:https://github.com/explosion/spacy-models/releases//tag/en_core_web_sm-3.0.0

en_core_web_trf文件:https://github.com/explosion/spacy-models/releases//tag/en_core_web_trf-3.0.0

3. 常见的NLP处理流程:

知乎介绍:https://zhuanlan.zhihu.com/p/63110761

官网介绍:https://spacy.io/usage/linguistic-features

4. Spacy Labels介绍:

  POS Tagging Labels:https://melaniewalsh.github.io/Intro-Cultural-Analytics/features/Text-Analysis/POS-Keywords.html

  Dependency Labels:https://github.com/clir/clearnlp-guidelines/blob/master/md/specifications/dependency_labels.md

5. Spacy中token属性:https://www.jianshu.com/p/488e29470755

 

6. Spacy依赖树结构介绍和打印:

import spacy
from nltk import Tree

nlp = spacy.load("en_core_web_sm")
doc = nlp(u"This is some sentence that spacy will not appreciate .")

# 打印当前单词,父单词,所有子单词,左子单词(单词索引在其之前),右子单词(单词索引在其之后)
# 注:子单词是直接相连,间接相连为子孙单词
for token in doc:
    print(token.text, token.head.text, [child for child in token.children],
          [left_child for left_child in token.lefts], [right_child for right_child in token.rights], )

def to_nltk_tree(node):
    if node.n_lefts + node.n_rights > 0:
        return Tree(node.orth_, [to_nltk_tree(child) for child in node.children])
    else:
        return node.orth_

print([to_nltk_tree(sent.root).pretty_print() for sent in doc.sents])

结果:

6. 批量处理文本:https://spacy.io/usage/processing-pipelines

texts = ["This is a text", "These are lots of texts", "..."]
docs = list(nlp.pipe(texts))

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spacy是一个流行的自然语言处理库,它允许用户训练自己的定制化模型,特别是通过它的管道(Pipeline)系统和`TrfTransformer`模块。以下是使用Spacy训练模型的基本步骤: 1. **安装和加载预训练模型**:首先,确保已经安装了Spacy和相关的语言模型。例如,如果你想要处理英文文本,可以下载`en_core_web_sm`。 ```bash pip install spacy python -m spacy download en_core_web_sm ``` 2. **数据准备**:你需要标记好语料库,通常包括句子级别的标注,如命名实体识别(NER)、依存句法分析等。你可以使用Spacy的数据集,或者自定义CSV、JSON文件。 3. **创建训练数据**:将数据转换成Spacy可以理解的格式,通常是`Doc`对象列表,其中包含标签信息。 4. **定义模型配置**:创建`TrfTransformer`的配置,可以选择模型架构(比如BERT、RoBERTa等),以及超参数,如学习率、批次大小等。 ```python from spacy.pipeline import create_pipe from spacy.training.example import Example # 创建空的训练器和模型 nlp = spacy.blank("en") ner = create_pipe("ner") nlp.add_pipe(ner) ``` 5. **添加训练器到模型**:将训练器添加到模型上,并设置训练目标(如NER)。 6. **训练模型**: ```python optimizer = nlp.begin_training() for i in range(n_iter): losses = {} batches = get_train_batches() for batch in batches: texts, annotations = zip(*batch) examples = [Example.from_dict(nlp.make_doc(text), annotations) for text in texts] nlp.update(examples, sgd=optimizer, drop=0.5, losses=losses) ``` 7. **评估和保存模型**:训练完成后,你可以用新的数据评估模型性能,然后保存以便后续使用。 ```python eval_data = [...] # 新的数据集 evaluator = spacy eval ... # 创建评估器 scores = evaluator.score(...) nlp.to_disk(output_dir) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值