使用 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])