加载模型并处理文档
一次调用即可通过 nlp() 处理文本
加载模型并处理文档 是 CoddyKit 上的免费 NLP Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 NLP Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 NLP Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Install First
Before you load anything, you download a model once. The en_core_web_sm model is small, fast, and perfect for getting started.
python -m spacy download en_core_web_smLoad the Model
You bring spaCy to life with spacy.load. It reads the model from disk and hands you an nlp object ready to process text.
import spacy
nlp = spacy.load("en_core_web_sm")What nlp Is
That nlp object is your whole pipeline wrapped up. Calling it on text runs every stage: tokenizer, tagger, parser, and entity recognizer.
Process Some Text
To analyze a sentence, just call nlp() on a string. The result is a Doc, a rich container holding all the analysis.
doc = nlp("Apple is hiring in Berlin.")One Call Does It All
That single call already ran tokenizing, tagging, and parsing. The Doc now carries every result, so you never repeat the work.
Loop Over Tokens
A Doc behaves like a sequence, so you can loop through it. Each item you get back is a Token with its own attributes.
for token in doc:
print(token.text)Read Token Text
The raw word lives in token.text. It is exactly the surface string spaCy found while splitting your sentence.
Load Once, Reuse
Loading a model is slow, so do it once at startup. Then reuse the same nlp object for every document you process.
Process Many Docs
For lots of texts, use nlp.pipe. It batches them efficiently and is far faster than calling nlp() in a plain loop.
for doc in nlp.pipe(texts):
print(len(doc))Disable for Speed
Need only tokens? You can disable unused components when loading to skip work and run even faster.
nlp = spacy.load("en_core_web_sm", disable=["parser"])Blank Pipelines
You can also start from spacy.blank for a tokenizer-only pipeline. It is handy when you want to build everything yourself.
nlp = spacy.blank("en")Quick Check
What do you get back from calling nlp() on a string?
Recap
You install a model, load it once with spacy.load, then call nlp() to get a Doc. One call runs the whole pipeline and you reuse it everywhere. 🎯
常见问题解答
「加载模型并处理文档」课时是免费的吗?
是的 — 「加载模型并处理文档」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 NLP Academy 课程的其余内容,请升级到 CoddyKit PRO。 NLP Academy 课程共包含 4 节课。
「加载模型并处理文档」这节课中我会学到什么?
一次调用即可通过 nlp() 处理文本 你通过在浏览器中直接运行的动手代码来练习 NLP Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 NLP Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 NLP Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「加载模型并处理文档」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 NLP Academy 课中编写并运行代码吗?
能。每节 NLP Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。