Natural Language Processing

Related: 16. Recurrent Neural Networks | 15. Regression and Text Classification with FCNs | 2. Supervised Learning | Artificial Intelligence


NLP vs LLMs

  • NLP — the broader field of techniques for analysing human language (includes statistical models like N-gram, TF-IDF, BoW)
  • LLMs — a specific type of deep learning model within NLP, trained on massive datasets (e.g., GPT, BERT)

Levels of Linguistic Analysis

  • Syntax — what is grammatically correct
  • Semantics — what is the meaning

Ambiguity Types

TypeDescriptionExample
LexicalWords with multiple meanings”bank” (financial vs river)
SemanticSentences with multiple meanings”I saw him with a telescope”
AnaphoricPronouns referring to earlier mentions”He told his dog to sit, and it did”

NLP Pipeline

  1. Sentence segmentation — split text into sentences
  2. Tokenisation — split sentences into words/tokens
  3. Stemming — crude chopping of word endings (e.g., “eating” → “eat”)
  4. Lemmatisation — proper reduction using vocabulary (e.g., “better” → “good”, “drove” → “drive”)
  5. Stop word removal — remove common words (“the”, “a”, “is”)
  6. POS tagging — assign part of speech (noun, verb, adjective, etc.)
  7. Named Entity Recognition — identify persons, locations, times

Bag of Words (BoW)

Represents a document as a vector of word frequencies. Ignores word order entirely.

Process:

  1. Build a vocabulary of known words
  2. For each document, count occurrences of each vocabulary word
  3. Resulting vector represents the document

TF-IDF (Term Frequency — Inverse Document Frequency)

Measures importance of a word to a document within a corpus.

Words that appear frequently in one document but rarely across the corpus get high TF-IDF scores.

Cosine Similarity

Measures similarity between two document vectors:

N-gram Language Models

Uses the probabilities of word sequences to predict the next word.

Markov assumption: the probability of a word depends only on the previous words:

MLE for conditional probabilities:

Types:

  • Unigram:
  • Bigram:
  • Trigram:

Limitations: Computationally expensive for large ; gives zero probability to unseen sequences (sparsity). 16. Recurrent Neural Networks address these limitations by learning sequential patterns without fixed context windows.