详解二元词组与三元词组
在 Token 上滑动窗口
详解二元词组与三元词组 是 CoddyKit 上的免费 NLP Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 NLP Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 NLP Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Naming the N-Grams
The n in n-gram is just a number. Pick n and you decide how many neighboring words each feature bundles together.
Meet the Bigram
A bigram is an n-gram of size two, every adjacent pair of words. It is the most common upgrade over plain single words. 👍
Bigrams in Action
From four words you get three overlapping pairs. Notice how each bigram shares one word with the next one.
words = "i love clean code".split()
bigrams = list(zip(words, words[1:]))
print(bigrams)Meet the Trigram
A trigram takes three words at a time. It captures even more context, like is not good as one unit.
words = "this is not good".split()
trigrams = list(zip(words, words[1:], words[2:]))
print(trigrams)How Many Do You Get?
For a sentence of N words, you get N minus n plus one n-grams. So 4 words give 3 bigrams and 2 trigrams.
The Window Slides
Each n-gram comes from a small window moving one word forward. Overlap is the point, since shared words stitch context together.
A Reusable Helper
You can build any n-gram with one tidy function using zip. Change the value of n and the same code handles bigrams or trigrams.
def ngrams(words, n):
return list(zip(*[words[i:] for i in range(n)]))Bigger N, More Context
Higher values of n hold longer phrases, so they capture context more precisely. That power comes with a cost you will meet soon.
Bigger N, Rarer Grams
The longer the run, the less often that exact sequence repeats. Large n-grams become rare, appearing in few documents.
Why Bigrams Are Popular
Bigrams hit a sweet spot: enough context to catch not bad, common enough to repeat across texts. That balance makes them a reliable default.
Mixing Sizes
You rarely use one size alone. Most pipelines combine unigrams and bigrams so the model sees both single words and their key pairs.
Quick Check
How many bigrams come from the sentence text is just data, which has four words?
Recap: Bigrams and Trigrams
A bigram joins two words, a trigram joins three. Bigger n holds more context but appears less often. Next you will let scikit-learn build them. ⚙️
常见问题解答
「详解二元词组与三元词组」课时是免费的吗?
是的 — 「详解二元词组与三元词组」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 NLP Academy 课程的其余内容,请升级到 CoddyKit PRO。 NLP Academy 课程共包含 4 节课。
「详解二元词组与三元词组」这节课中我会学到什么?
在 Token 上滑动窗口 你通过在浏览器中直接运行的动手代码来练习 NLP Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 NLP Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 NLP Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「详解二元词组与三元词组」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 NLP Academy 课中编写并运行代码吗?
能。每节 NLP Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。