开始使用免费开始使用

使用 spaCy 进行 NER

命名实体识别(NER)可以帮助您快速识别文档中的关键信息,例如人物和地点名称。它有助于整理非结构化数据并发现重要信息,这在处理大型数据集时尤为关键。本练习将带您实践命名实体识别。

en_core_web_sm 已作为 nlp 为您加载。来自 Airline Travel Information System(ATIS)数据集的 3 条评论已放在名为 texts 的列表中供您使用。

本练习是课程的一部分

使用 spaCy 的自然语言处理

查看课程

练习说明

  • 使用列表推导式,将 texts 中每个文本分别转换为 Doc 容器,组成列表 documents
  • 对于每个 doc 容器,通过遍历 doc.ents,打印每个实体的文本及其对应的标签。
  • 打印第 6 个标记的文本,以及第二个 Doc 容器中的实体类型。

交互式实操练习

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

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

# Print the entity text and label for the entities in each document
for doc in documents:
    print([(____, ____) for ent in ____])
    
# Print the 6th token's text and entity type of the second document
print("\nText:", documents[1][5].____, "| Entity type: ", documents[1][5].____)
编辑并运行代码