Lecture 03 · N-grams

المحاضرة 03 · نماذج N-grams

Why Predict the Next Word?

لماذا نتنبأ بالكلمة التالية؟

Slides 2–9الشرائح 2–9 P3 SKIM

What a language model actually computes, where it is used, and what the word "n-gram" means before any math shows up.

ما الذي يحسبه language model فعلياً، وأين يُستخدم، وما معنى كلمة n-gram قبل أن تظهر أي معادلات.

The whole idea in one sentence

الفكرة كلها في جملة واحدة

A language model assigns a probability to the next word, given the words before it.

يُعطي language model احتمالاً للكلمة التالية، بناءً على الكلمات التي سبقتها.

Slide 2 opens with the lecturer's example: "The water of Walden Pond is so beautifully ___". You immediately expect clear, blue, or green. You do not expect refrigerator. That intuition is a probability distribution. A language model is the formal version of it.

تبدأ الشريحة 2 بمثال المحاضر: "The water of Walden Pond is so beautifully ___". أنت تتوقع فوراً clear أو blue أو green، ولا تتوقع refrigerator. هذا الحدس هو في جوهره probability distribution، وlanguage model هو الصيغة الرياضية الرسمية له.

Two things to lock in from slide 2:

نقطتان يجب تثبيتهما من الشريحة 2:

Where it gets used

أين يُستخدم

Slide 3 lists four applications. Slides 4, 5, and 6 are screenshots of the same four. Learn the four, learn the disambiguation example attached to each.

تسرد الشريحة 3 أربعة تطبيقات، والشرائح 4 و5 و6 هي صور للأربعة نفسها. احفظ الأربعة، واحفظ مثال disambiguation المرتبط بكل واحد منها.

ApplicationالتطبيقWhat the LM decidesما الذي يقرره LMSlide exampleمثال الشريحة
Spelling / grammar correctionتصحيح الإملاء / القواعدWhich spelling is more probable in contextأي كتابة أكثر احتمالاً في السياقTheir are → There are
Speech recognitionالتعرف على الكلامWhich word sequence the identical acoustics should map toإلى أي تسلسل كلمات يجب أن تُربط الأصوات المتطابقةback soonish vs bassoon dishback soonish مقابل bassoon dish
Machine translationالترجمة الآليةWhich candidate translation is fluent in the target languageأي ترجمة مرشحة سلسة في اللغة الهدفprefer the fluent target sentenceتفضيل الجملة الهدف السلسة
AAC (assistive / augmentative communication)AAC (التواصل المساعد / المعزِّز)Which words to offer in a word-prediction menuأي كلمات تُعرض في قائمة التنبؤ بالكلماتranked prediction menu for users who type slowlyقائمة تنبؤات مرتبة للمستخدمين الذين يكتبون ببطء

يلخّص الجدول التطبيقات الأربعة من الشريحة 3: التصحيح الإملائي والنحوي، وspeech recognition، وmachine translation، وAAC. لاحظ أن الخيط المشترك بينها كلها واحد: في كل حالة هناك أكثر من احتمال ممكن، وlanguage model هو الذي يختار الأكثر ترجيحاً في السياق. العمود الأخير هو ما يُسأل عنه عادة، فاربط كل تطبيق بمثاله.

Memory trickحيلة للحفظ

S.S.M.A. Spelling, Speech, Machine translation, AAC. If the exam asks for applications and you can only remember two, the two with the memorable examples are speech recognition (bassoon dish) and spelling (Their are).

احفظها كـ S.S.M.A.: Spelling، Speech، Machine translation، AAC. وإذا سُئلت عن التطبيقات ولم تتذكر إلا اثنين، فاذكر الاثنين اللذين لهما أمثلة لا تُنسى: speech recognition (bassoon dish) والتصحيح الإملائي (Their are).

Exam trapفخ امتحاني

AAC stands for augmentative and alternative communication, not "automatic autocorrect" or anything with "AI" in it. On slide 3 it is glossed as "assistive communication: word prediction menus". Autocomplete on your phone is the everyday version of the same mechanism.

اختصار AAC يعني augmentative and alternative communication، أي التواصل المعزِّز والبديل، وليس "automatic autocorrect" ولا أي شيء فيه كلمة "AI". في الشريحة 3 شُرح بأنه "assistive communication: word prediction menus"، أي قوائم التنبؤ بالكلمات لمن يكتب ببطء. وخاصية autocomplete في جوالك هي النسخة اليومية من الآلية نفسها.

Two views of the same model

نظرتان إلى النموذج نفسه

Slide 7 is titled "What does an LM do?" and gives two bullets under a heading that literally says Two equivalent views. The exam loves this because students treat them as two different models. They are not.

عنوان الشريحة 7 هو "What does an LM do?"، وتحتها نقطتان تحت ترويسة تقول حرفياً Two equivalent views، أي منظورَان متكافئان. الامتحان يحب هذه النقطة لأن الطلاب يتعاملون معهما كأنهما نموذجان مختلفان، وهما ليسا كذلك: نموذج واحد وقراءتان.

View 1: predict the next word

النظرة 1: التنبؤ بالكلمة التالية

P(wt | w<t)

Given everything before position t, give me a distribution over what comes next. This is the view you use for autocomplete, for AAC menus, for generation.

بمعرفة كل ما جاء قبل الموضع t، أعطني distribution على ما يمكن أن يأتي بعده. هذا هو المنظور المستخدم في autocomplete، وفي قوائم AAC، وفي توليد النص.

View 2: score a whole sentence

النظرة 2: تقييم جملة كاملة

P(w1:T)

Given a complete word sequence, give me one number for how likely that sequence is. This is the view you use for reranking speech recognition or translation candidates.

بإعطاء تسلسل كلمات كامل، أعطني رقماً واحداً يقيس مدى ترجيح هذا التسلسل. هذا هو المنظور المستخدم في إعادة ترتيب (reranking) مخرجات speech recognition أو ترشيحات الترجمة.

Why are they the same thing? Because a joint probability over a sequence factors into a product of next-word probabilities, and a product of next-word probabilities multiplies back into a joint probability. That factorization is the chain rule, which is the next page. For now just hold the claim: one model, two ways of reading its output.

لماذا هما الشيء نفسه؟ لأن joint probability لتسلسل كامل تتفكك إلى حاصل ضرب احتمالات الكلمة التالية، وحاصل ضرب احتمالات الكلمة التالية يُعيد تركيب joint probability نفسها. هذا التفكيك هو chain rule، وهو موضوع الصفحة التالية. يكفيك الآن أن تثبّت الفكرة: نموذج واحد، وطريقتان لقراءة ناتجه.

VIEW 1 · distribution over the next word The water of Walden Pond is so beautifully ? history h clear 0.38 blue 0.22 green 0.17 transparent 0.08 all other words 0.15 refrigerator ~0.00 these bars sum to 1 over the whole vocabulary V VIEW 2 · one score for the whole sentence P(w 1:T ) = P(w 1 ) × P(w 2 | w 1 ) × ... × P(w T | w 1:T-1 ) Every factor on the right is one View-1 query. Multiply them and you have View 2. Same model. The chain rule is the bridge between the two views.
One model, two readings. The bar heights are illustrative shape only, not corpus-measured counts. The ranking is the point.نموذج واحد، وقراءتان. ارتفاعات الأعمدة توضيحية للشكل فقط، وليست عدّات مقيسة من corpus. المهم هو الترتيب.
Worked exampleمثال محلول

Slide 7 gives the sentence-scoring test case directly. The LM must satisfy:

تعطي الشريحة 7 حالة اختبار مباشرة لتقييم الجمل. يجب على language model أن يحقق الشرط التالي:

P("all of a sudden I notice three guys standing on the sidewalk")
      >
P("on guys all I of notice sidewalk three a sudden standing the")

Both strings contain exactly the same multiset of words. Twelve tokens each, identical bag. So any model that only counted individual words would score them identically. The only way to separate them is to let the probability of each word depend on which words came before it. That single observation is why unigram models are weak and why context length matters.

السلسلتان تحتويان على المجموعة نفسها من الكلمات تماماً: اثنا عشر token في كل منهما، وbag of words متطابق. لذلك أي نموذج يعدّ الكلمات المفردة فقط سيعطيهما الدرجة نفسها بالضبط. والطريق الوحيد للتفريق بينهما هو جعل احتمال كل كلمة يعتمد على الكلمات التي جاءت قبلها. هذه الملاحظة وحدها هي سبب ضعف نماذج unigram وسبب أهمية طول السياق.

Two families of language model

عائلتان من نماذج اللغة

Slide 8 is short: count-based (statistical) and neural. That is the whole slide. But know what distinguishes them, because it is a one-mark comparison question.

الشريحة 8 قصيرة: نماذج قائمة على العدّ (count-based أو statistical) ونماذج عصبية (neural). هذا كل ما فيها. لكن اعرف ما يميز كل عائلة عن الأخرى، لأنها سؤال مقارنة بدرجة واحدة.

Count-based (n-gram)

قائمة على العدّ (n-gram)

  • Probabilities come from counting word sequences in a corpus and dividing.

    الاحتمالات تأتي من عدّ تسلسلات الكلمات في corpus ثم القسمة.

  • Context is a fixed, short window: N - 1 words.

    السياق نافذة ثابتة وقصيرة: N - 1 كلمة فقط.

  • No learned representation. "dog" and "puppy" are unrelated symbols.

    لا يوجد تمثيل مُتعلَّم للكلمات، فكلمة "dog" وكلمة "puppy" رمزان لا علاقة بينهما إطلاقاً عند النموذج.

  • Unseen n-gram means probability zero unless you smooth.

    أي n-gram لم يظهر في بيانات التدريب يحصل على احتمال صفر، إلا إذا طبّقت smoothing.

  • Cheap, transparent, exactly reproducible by hand. This is why it is on the exam.

    رخيص وشفاف ويمكن إعادة حسابه يدوياً بدقة. ولهذا السبب بالتحديد هو مادة الامتحان.

Neural

عصبية (Neural)

  • Probabilities come from learned weights trained by gradient descent.

    الاحتمالات تأتي من أوزان مُتعلَّمة تُدرَّب بـ gradient descent.

  • Context can be long (transformers see thousands of tokens).

    السياق يمكن أن يكون طويلاً جداً، فنماذج transformers ترى آلاف الـ tokens.

  • Words become dense vectors, so similar words share statistical strength.

    الكلمات تتحول إلى متجهات كثيفة (dense vectors)، فتتشارك الكلمات المتشابهة قوتها الإحصائية بدل أن تبقى منفصلة.

  • Generalizes to unseen sequences without explicit smoothing.

    يُعمّم على تسلسلات لم يرها من قبل دون حاجة إلى smoothing صريح.

  • Expensive, opaque, and what every modern LLM is.

    مكلف وغامض الداخل، وهو ما تقوم عليه كل نماذج LLM الحديثة.

What is an n-gram?

ما هو الـ n-gram؟

Slide 9 definition, word for word: an n-gram is a contiguous sequence of n words or tokens.

تعريف الشريحة 9 حرفياً: الـ n-gram هو تسلسل متصل من n كلمة أو token.

Two things in that sentence do the work. Contiguous: the tokens must be adjacent, no skipping. n tokens: you count the tokens in the window, including the one at the end.

كلمتان في هذا التعريف هما اللتان تحملان كل المعنى. Contiguous أي متصل: يجب أن تكون الـ tokens متجاورة دون أي قفز بينها. وn tokens: أنت تعدّ الـ tokens داخل النافذة كلها، بما فيها الأخيرة.

The slide's own examples, on "the water of Walden":

أمثلة الشريحة نفسها، على العبارة "the water of Walden":

2-gram (bigram): the water2-gram (bigram): the water 2-gram: water of2-gram: water of 3-gram (trigram): the water of3-gram (trigram): the water of 3-gram: water of Walden3-gram: water of Walden
nNameالاسمExample from "The water of Walden Pond"مثال من "The water of Walden Pond"How many in an 8-token sentenceكم منها في جملة من 8 tokens
1unigramwater8
2bigramthe water7
3trigramthe water of6
44-gramthe water of Walden5
55-gramthe water of Walden Pond4

يربط الجدول بين قيمة n واسمها (unigram، bigram، trigram، ثم 4-gram و5-gram بلا أسماء خاصة) ومثال عليها. الشيء المهم فعلاً هو العمود الأخير: كلما كبرت n نقص عدد الـ n-grams بمقدار واحد في كل مرة، لأن العدد يساوي L - n + 1 في جملة طولها L = 8. لاحظ النمط 8, 7, 6, 5, 4.

Worked exampleمثال محلول

Where that last column comes from. Take the slide's sentence and tokenize it:

من أين جاء العمود الأخير. خذ جملة الشريحة وقسّمها إلى tokens:

The(1) water(2) of(3) Walden(4) Pond(5) is(6) so(7) beautifully(8)
L = 8 tokens

number of n-grams = L - n + 1

n = 1 :  8 - 1 + 1 = 8
n = 2 :  8 - 2 + 1 = 7
n = 3 :  8 - 3 + 1 = 6
n = 4 :  8 - 4 + 1 = 5
n = 5 :  8 - 5 + 1 = 4

Check n = 2 by hand: The water, water of, of Walden, Walden Pond, Pond is, is so, so beautifully. Seven. The formula is right. The sanity check is that an n-gram window needs n - 1 tokens of room to its right, so you lose n - 1 starting positions off the end.

تحقّق من n = 2 يدوياً: The water، water of، of Walden، Walden Pond، Pond is، is so، so beautifully. سبعة بالضبط، فالصيغة صحيحة. والمنطق خلفها أن نافذة n-gram تحتاج مساحة n - 1 من الـ tokens أمامها، فتفقد n - 1 من مواضع البداية عند نهاية الجملة.

Exam trapفخ امتحاني

Slide 9's last bullet is easy to skim past and it matters: "n-gram" names two different things.

النقطة الأخيرة في الشريحة 9 يسهل تجاوزها بسرعة، وهي مهمة جداً: كلمة "n-gram" تُطلق على شيئين مختلفين.

  1. The string: a contiguous run of n tokens, like water of Walden.

    الـ string: سلسلة متصلة من n من الـ tokens، مثل water of Walden.

  2. The model: the probabilistic language model built from counts of those strings.

    الـ model: النموذج اللغوي الاحتمالي المبني على عدّ تلك السلاسل.

So "a trigram" can mean three adjacent words or it can mean a model that conditions on two previous words. Read the question's wording. If it says "how many trigrams are in this sentence" it means sense 1. If it says "a trigram model" it means sense 2, and then the thing you must know is that the context is only 2 words, not 3. That off-by-one is the single most-tested confusion in this lecture and it gets its own treatment on the next page.

إذن كلمة "trigram" قد تعني ثلاث كلمات متجاورة، وقد تعني نموذجاً يشترط على كلمتين سابقتين. اقرأ صيغة السؤال بدقة: إذا قال "how many trigrams are in this sentence" فالمقصود المعنى الأول، وإذا قال "a trigram model" فالمقصود المعنى الثاني، وحينها الذي يجب أن تعرفه هو أن السياق كلمتان فقط وليس ثلاثاً. هذا الفرق بمقدار واحد (off-by-one) هو أكثر لبس يُختبر عليه في هذه المحاضرة، وله معالجة خاصة في الصفحة التالية.

Memory trickحيلة للحفظ

"The n counts the window, not the history." A 3-gram is 3 tokens wide. Two of them are history, one of them is the word you are predicting. Say it as "n tokens total, n - 1 of them behind me."

"الـ n تعدّ النافذة، لا التاريخ." الـ 3-gram عرضه ثلاثة tokens: اثنان منها هي التاريخ (history)، وواحد هو الكلمة التي تتنبأ بها. ردّدها هكذا: "n من الـ tokens إجمالاً، وn - 1 منها خلفي."

Check yourself

اختبر نفسك

Slide 7 says an LM has "two equivalent views". What are they?

تقول الشريحة 7 إن لـ language model منظورين متكافئين ("two equivalent views"). ما هما؟

C. Slide 7's heading is literally "Two equivalent views": predict the next word, or assign a probability to the full sequence. They are linked by the chain rule, so one model serves both. A is the content of slide 8 (types of LM), which is a different slide and a different distinction, which is why it is the tempting wrong answer.

الجواب C. ترويسة الشريحة 7 تقول حرفياً "Two equivalent views": إما التنبؤ بالكلمة التالية، أو إعطاء احتمال للتسلسل الكامل. وchain rule هو ما يربط بينهما، فنموذج واحد يخدم المنظورين. أما A فهو محتوى الشريحة 8 (أنواع الـ LM)، وهي شريحة أخرى وتمييز آخر تماماً، ولهذا هو الجواب الخاطئ المُغري.

How many trigrams does the token sequence The water of Walden Pond is so beautifully contain?

كم عدد الـ trigrams في تسلسل الـ tokens التالي: The water of Walden Pond is so beautifully؟

B. The sequence has L = 8 tokens, and the count of n-grams is L - n + 1 = 8 - 3 + 1 = 6. Answer C (5) is what you get if you wrongly subtract n instead of n - 1, which is the same off-by-one that makes people think a trigram has 3 words of context. Answer A is the unigram count.

الجواب B. التسلسل فيه L = 8 من الـ tokens، وعدد الـ n-grams يساوي L - n + 1 = 8 - 3 + 1 = 6. والجواب C (أي 5) هو ما تحصل عليه إذا طرحت n بدلاً من n - 1 بالخطأ، وهو الفرق بمقدار واحد نفسه الذي يجعل الناس يظنون أن trigram سياقه ثلاث كلمات. والجواب A هو عدد الـ unigrams.

Which pair from slide 3 is the speech-recognition disambiguation example?

أي زوج من الشريحة 3 هو مثال disambiguation الخاص بـ speech recognition؟

D. Those two strings have nearly identical acoustics, so the acoustic model cannot separate them and the language model has to. A is the spelling/grammar correction example from the same slide, which is why it is a strong distractor. B is the motivating next-word example from slide 2, not an application example.

الجواب D. هاتان السلسلتان لهما نفس الصوت تقريباً، فلا يستطيع acoustic model التفريق بينهما، ويصبح الحكم على language model. أما A فهو مثال التصحيح الإملائي والنحوي من الشريحة نفسها، ولهذا هو مُشتِّت قوي. وB هو مثال الكلمة التالية التحفيزي من الشريحة 2، وليس مثال تطبيق.

Why can a unigram model never distinguish "all of a sudden I notice three guys standing on the sidewalk" from a random permutation of those same words?

لماذا لا يستطيع نموذج unigram أبداً أن يفرّق بين "all of a sudden I notice three guys standing on the sidewalk" وبين ترتيب عشوائي للكلمات نفسها؟

A. Multiplication is commutative, and a unigram model's factors are just P(w) for each word with no conditioning. Identical bag of words means identical product means identical score. D is wrong on two counts: the permutation is not grammatical, and more importantly an LM assigns graded probabilities rather than a binary grammatical verdict.

الجواب A. عملية الضرب تبديلية (commutative)، وعوامل نموذج unigram ليست إلا P(w) لكل كلمة بدون أي شرط على ما قبلها. فإذا تطابق bag of words تطابق حاصل الضرب، وإذا تطابق حاصل الضرب تطابقت الدرجة. والجواب D خاطئ من وجهين: الترتيب العشوائي ليس صحيحاً نحوياً أصلاً، والأهم أن language model يعطي احتمالات متدرجة ولا يصدر حكماً ثنائياً بصحة الجملة نحوياً أو خطئها.

Which statement about count-based versus neural LMs is correct?

أي العبارات التالية صحيحة في المقارنة بين نماذج count-based ونماذج neural؟

C. Slide 8 splits LMs into count-based (statistical) and neural. The defining difference is where the probabilities come from: counting and dividing versus learned weights. A and B are the same facts with the two families swapped, which is the classic way this is written as a distractor. Reading the subject of the sentence carefully is the whole task.

الجواب C. تقسّم الشريحة 8 النماذج اللغوية إلى count-based (إحصائية) وneural (عصبية). والفرق الجوهري هو من أين تأتي الاحتمالات: من العدّ والقسمة، أم من أوزان مُتعلَّمة. والجوابان A وB يحملان الحقائق الصحيحة نفسها لكن مع تبديل العائلتين، وهذه هي الطريقة الكلاسيكية لصياغة المُشتِّتات. كل المهمة هنا أن تقرأ صاحب الجملة بانتباه قبل أن تقرأ الوصف.