使用 spaCy 提取实体
列出实体及其标签
使用 spaCy 提取实体 是 CoddyKit 上的免费 NLP Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 NLP Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 NLP Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
spaCy Does the Work
spaCy ships a trained NER component that finds entities for you. You feed it text and read back labeled spans. 🐍
Load a Model
First load a pipeline that includes NER. The small English model en_core_web_sm is a quick, friendly starting point.
import spacy
nlp = spacy.load("en_core_web_sm")Process Your Text
Pass a string into the pipeline to get a Doc object. The Doc holds tokens, sentences, and the entities spaCy found.
doc = nlp("Sundar Pichai leads Google in California.")The ents Property
Every Doc exposes doc.ents, a tuple of detected entities. Loop over it to inspect each one individually.
for ent in doc.ents:
print(ent)Read the Text
Each entity has a .text attribute holding the exact words matched. This is the raw span lifted from your sentence.
print(ent.text) # e.g. Sundar PichaiRead the Label
Use .label_ (with the trailing underscore) to get a readable label string like PERSON, ORG, or GPE.
print(ent.label_) # e.g. PERSONPutting It Together
One small loop prints both the text and label of every entity, turning a sentence into clean structured data.
for ent in doc.ents:
print(ent.text, ent.label_)Character Positions
Entities also expose start_char and end_char, the offsets in the original string. Handy for highlighting matches later.
print(ent.start_char, ent.end_char)Explain a Label
Forgot what GPE means? Call spacy.explain for a plain-English description of any entity label.
spacy.explain("GPE") # 'Countries, cities, states'Entities Are Spans
Each entity is a Span, a slice of the Doc. So it behaves like a mini-document you can further inspect token by token.
Collect Into a List
A quick comprehension gathers every entity into a list of tuples, ready to save, count, or feed into a report.
results = [(e.text, e.label_) for e in doc.ents]Quick Check
Which attribute gives the readable entity label?
Recap
Load a model, run nlp() on text, then loop doc.ents reading .text and .label_. That is core NER in spaCy. ✅
常见问题解答
「使用 spaCy 提取实体」课时是免费的吗?
是的 — 「使用 spaCy 提取实体」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 NLP Academy 课程的其余内容,请升级到 CoddyKit PRO。 NLP Academy 课程共包含 4 节课。
「使用 spaCy 提取实体」这节课中我会学到什么?
列出实体及其标签 你通过在浏览器中直接运行的动手代码来练习 NLP Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 NLP Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 NLP Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「使用 spaCy 提取实体」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 NLP Academy 课中编写并运行代码吗?
能。每节 NLP Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 什么算作实体
- 使用 spaCy 提取实体
- 使用 displaCy 可视化实体
- 添加自定义实体规则