开始使用免费开始使用

使用 spaCy 进行句子分割

在本练习中,您将练习句子分割。对于 NLP,将文档划分为句子是一个有用的基础操作。它是许多更复杂 NLP 任务(例如命名实体识别)的前置步骤之一。此外,统计句子数量也能帮助您判断文本包含的信息量。

在名为 texts 的列表中,您可以访问 10 条美食点评。

en_core_web_sm 模型已作为 nlp 为您加载好。

本练习是课程的一部分

使用 spaCy 的自然语言处理

查看课程

练习说明

  • texts 列表中的每个元素运行 spaCy 模型,生成 documents,即所有 Doc 容器组成的列表。
  • 通过遍历 documents 列表,从每个 doc 容器中提取句子,并将其追加到名为 sentences 的列表中。
  • 使用 sentences 列表统计每个 doc 容器中的句子数量。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Generating a documents list of all Doc containers
documents = [____(text) for text in texts]

# Iterate through documents and append sentences in each doc to the sentences list
sentences = []
for doc in documents:
  sentences.append([s for s in ____.____])
  
# Find number of sentences per each doc container
print([len(____) for s in sentences])
编辑并运行代码