Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Natural language processing and its challenges

Overview of why natural language processing (NLP) is useful for engineers and the main practical and theoretical challenges (ambiguity, sparsity, domain mismatch, multilinguality, evaluation and ethics), with precise definitions, key equations (Zipf, OOV, type-token ratio), worked exam-style example

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two sentences: Natural language processing (NLP) enables computers to read, understand and generate human language so engineers can build search, chatbots, translation, sentiment analysis and many other systems. Knowing the main challenges helps you choose the right algorithms (for example, tokenisation, POS tagging, parsing, embeddings) and to design datasets, evaluation and failure modes in real projects.

Key ideas

All new terms are defined before use.

  • Natural language processing (NLP): the field that studies computational methods to process and analyse human (natural) language. Assumption: input is text or speech; later stages (speech, embeddings, models) are separate topics.

  • Corpus: a collection of text documents used for training or testing. A corpus contains tokens and types (see below).

  • Token: a single occurrence of a word-like unit in text. Example: in "the cat sat", there are three tokens. Tokens are counted by position.

  • Type: a distinct word form (unique token string) in a corpus. Example: "cat" occurs several times but is one type.

  • Vocabulary: the set of types in a corpus or model.

  • Out-of-vocabulary (OOV): a token in test data whose type is not present in training vocabulary.

  • Ambiguity: when language has more than one interpretation. Main kinds:

    • Lexical ambiguity (word-sense): a word has multiple meanings (e.g., "bank" = financial institution or river side).
    • Syntactic ambiguity (structural): sentence can be parsed in two ways (e.g., "I saw the man with a telescope").
    • Semantic/pragmatic ambiguity: meaning depends on context beyond sentence level.
  • Sparsity: many possible word forms exist but most appear rarely in a finite corpus. Sparsity causes data-driven models to see few examples per form.

  • Morphological richness: languages with many inflected forms per lemma (root) produce larger vocabularies and more sparsity (Hindi, Tamil, Telugu vs. English). This affects tokenisation, stemming and lemmatisation approaches.

  • Domain mismatch / domain adaptation: models trained on one type of text (news) often perform worse on another (social media) because word distributions, style and vocabulary differ.

  • Code-switching / code-mixing: mixing of multiple languages in the same utterance. This creates challenges for tokenisation, language identification and downstream models.

  • Evaluation mismatch: standard automatic metrics (like accuracy, BLEU) may not reflect user satisfaction or task utility.

  • Data bias and ethical issues: training data can contain social biases; models can amplify harmful stereotypes. Engineers must test for and mitigate bias.

Exact statements and simple derivations often asked in exams:

  1. Zipf's law (empirical law about word frequency): if types are rank-ordered by decreasing frequency, then frequency f(r) is approximately proportional to 1/r^s, commonly s ≈ 1. More precisely:

    • Zipf's law (definition): f(r) = C / r^s, where r is rank, C is a constant and s is the exponent (often ≈1). Assumption: large corpus of natural language text.
    • Derivation step usually shown in exams: take logarithms to transform to a linear relation for plotting:
      • log f(r) = log C − s · log r.
      • So on a log-log plot of frequency vs rank, you should draw a straight line with slope −s and intercept log C. Diagram to draw: x-axis = log(rank), y-axis = log(frequency); plot empirical points and draw the fitted straight line; label slope −s and intercept log C.
  2. Consequence for sparsity: because f(r) decays slowly, many types have very low frequency (hapax legomena—types that occur once). This explains high OOV rates when moving to new domains.

  3. Formal definition used in many papers: Type-Token Ratio (TTR) = |V| / N where |V| is vocabulary size (number of types) and N is number of tokens. Higher TTR indicates greater lexical variety (often higher in morphologically rich languages). See Formulas for exact expression.

    Also commonly asked: Heaps' law (empirical) for vocabulary growth: |V(N)| ≈ K · N^b with b typically in the range 0.4–0.6 for many corpora; log |V| = log K + b · log N. Implication: vocabulary grows sublinearly with corpus size, so OOV persists without subword methods.

  4. Relationship between training vocabulary and test OOV: be explicit about whether you measure by tokens or types.

    • Token-based OOV rate: if the test contains T_test tokens and T_oov of them are not in the training vocabulary, OOV_rate_tokens = T_oov / T_test.
    • Type-based OOV rate: if the test contains |V_test| distinct types and U of those types are unseen in training, OOV_rate_types = U / |V_test|. Clarify which definition your exam question asks for.
  5. Practical remedies for these challenges (brief pointers, each is a topic later in the course):

    • Smoothing and language modelling (see N-gram language models and smoothing).
    • Subword methods (byte-pair encoding, character-level modelling) to reduce OOV, especially for morphologically rich languages.
    • Transfer learning and domain adaptation (pretrained embeddings and fine-tuning: see Contextual embeddings and BERT).
    • Lexical resources (WordNet, IndoWordNet) and rule-based modules for low-resource languages.
    • Evaluation design: human evaluation, task-based metrics, error analysis.

State your assumptions in exam answers: size of corpus, whether you consider types or token counts, and whether code-switched data is treated single-language or multi-language.

Formulas

f(r) = C / r^s — Zipf's law: f(r) is frequency of the word at rank r; C is normalising constant (unit: tokens); s is exponent (dimensionless); applies empirically to large corpora.

log f(r) = log C - s · log r — log-log linear form; same symbols as above; use this to justify a straight line on a log-log plot.

TTR = |V| / N — Type-Token Ratio; |V| is number of types (vocabulary size, unit: count), N is number of tokens (unit: count); applies to a corpus or text fragment.

OOV_rate = (# OOV tokens in test) / (total # tokens in test) — Out-of-vocabulary rate; unitless fraction; use counts of tokens.

Hapax_proportion = H / |V| — fraction of types which are hapax legomena (occur once); H is number of types with frequency 1.

|V(N)| ≈ K · N^b — Heaps' law (empirical): vocabulary size as a function of tokens N; K is a corpus- and tokenisation-dependent constant; b is dimensionless and typically 0.4–0.6.

log |V| = log K + b · log N — log–log linear form of Heaps' law; use to justify a straight line on a log–log plot with slope b and intercept log K.

(If your exam asks, state whether OOV is measured by types or tokens and be consistent.)

Worked examples

  1. Long-answer model question: "Explain main challenges in building an English–Hindi chatbot and how you would address them." Model answer (exam style):

    • Start by defining code-switching: mixing English and Hindi in same utterance. Give an example: "Are you coming kal?" where "kal" is Hindi.
    • Challenges: tokenisation (Latin vs Devanagari scripts), language identification at token level, transliteration (Hindi written with Latin letters), vocabulary sparsity (many inflected forms), data scarcity for parallel corpora, morphological richness of Hindi, domain mismatch for casual chat, ambiguous intent due to short utterances.
    • Remedies: use token-level language ID (train classifier with features: script, character n-grams), apply subword tokenisation (BPE) to handle transliteration and morphs, use transfer learning from large multilingual pretrained models (see Contextual embeddings and BERT), employ transliteration modules and lexicons (IndoWordNet), augment data with synthetic code-mixed examples, evaluate with human annotators for pragmatic correctness.
    • Conclude with experimental plan: collect annotated code-switched corpus, baseline with bilingual BERT model, measure intent accuracy and OOV rate, iterate.
  2. Numerical exam-style example: Given corpus A has N = 10,000 tokens and vocabulary |V| = 3,000 types. There are H = 600 hapax legomena (types with frequency 1). A test set has 1,200 tokens, of which 100 tokens are of types unseen in training. Compute TTR, Hapax proportion and OOV rate (token-based). Steps:

    • Formula: TTR = |V| / N.
    • Compute: TTR = 3000 / 10000 = 0.3 (unitless). Write as 0.300.
    • Formula: Hapax_proportion = H / |V|.
    • Compute: Hapax_proportion = 600 / 3000 = 0.2 (20%).
    • Formula: OOV_rate = (# OOV tokens in test) / (total # tokens in test).
    • Compute: OOV_rate = 100 / 1200 ≈ 0.08333 → 0.0833 (8.33%).
    • State interpretation: moderate lexical variety (TTR 0.3), many rare words (20% hapax) and test OOV 8.33% which will hurt exact-match systems.
  3. Short model answer (theory): "Define Zipf's law and explain its implication for smoothing in language models."

    • Define Zipf: f(r) = C / r^s with s ≈ 1.
    • Implication: many low-frequency events -> naive maximum-likelihood estimates will assign zero probability to unseen events. Smoothing (add-one, Good-Turing, backoff) redistributes probability mass to handle unseen events. Point to N-gram smoothing topic for formulas.

Common mistakes

  • Saying Zipf's law is a mathematical theorem; it is an empirical law — say "empirical" and give the typical exponent value as approximate.
  • Ignoring script and orthography in multilingual settings (treating transliterated words as same as native-script forms without normalisation).
  • Treating evaluation metrics (accuracy, BLEU) as complete measures of quality without mentioning human or task-based evaluation.
  • Forgetting to state assumptions (corpus size, domain, tokenisation) before deriving consequences.

For your exam

Kinds of questions:

  • Definitions and short notes: define ambiguity types, corpus, OOV, sparsity, morphological richness.
  • Diagrams: draw log-log plot for Zipf's law and label slope −s and intercept log C.
  • Derivations: show log transform of Zipf and explain implications for frequency distribution.
  • Numericals: compute TTR, OOV rate, hapax proportion from given counts (show formula, steps, final value).
  • Long-answer: propose system design for multilingual or code-switched data, list challenges and remedies clearly.

What full-marks answers include:

  • Clear definitions (bold the term if asked), explicit assumptions, step-by-step calculations with formulas, labelled diagrams described exactly, and short justification for mitigation choices referencing related topics (tokenisation, stemming/lemmatisation, POS tagging, embeddings, domain adaptation).

Quick check

  1. What is the difference between a token and a type?
  2. Write the log-log form of Zipf's law.
  3. If training has 5,000 tokens and 1,500 types, what is the TTR?
    Answers: 1) A token is an occurrence; a type is a distinct word form. 2) log f(r) = log C − s · log r. 3) TTR = 1500 / 5000 = 0.3.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?