Skip to main content

Natural Language Processing Basics

NLP turns text into numbers so models can learn from it.

Pipeline​

raw text -> clean -> tokenise -> vectorise -> model -> prediction

Preprocessing​

import re

def clean(text: str) -> str:
text = text.lower()
text = re.sub(r"http\S+", " ", text) # remove URLs
text = re.sub(r"[^a-z0-9\s]", " ", text) # keep letters and digits
return re.sub(r"\s+", " ", text).strip()

print(clean("Check https://x.io: PrepVerse is GREAT!!!"))
# check prepverse is great

Typical steps: lowercasing, tokenisation, stop-word removal, stemming or lemmatisation. Modern transformer models usually need little preprocessing.

Bag of words and TF-IDF​

RepresentationIdeaLimitation
Bag of wordsCount of each wordIgnores order and meaning
TF-IDFDown-weights words common to all documentsStill sparse, no semantics
EmbeddingsDense vectors where similar words are closeNeeds training data
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline

texts = ["great course, learned a lot", "terrible, waste of time",
"excellent explanations", "boring and confusing"]
labels = [1, 0, 1, 0]

model = make_pipeline(TfidfVectorizer(ngram_range=(1, 2)), LogisticRegression())
model.fit(texts, labels)
print(model.predict(["very clear and helpful"])) # [1]

Embeddings and transformers​

  • Word embeddings (word2vec, GloVe): one vector per word.
  • Contextual embeddings (BERT and relatives): the vector depends on the sentence.
  • Transformers use self-attention to relate every token to every other token; large language models are transformers trained on huge text corpora.
from transformers import pipeline

clf = pipeline("sentiment-analysis")
print(clf("The lessons are clear and well structured."))

Common tasks​

TaskExample
ClassificationSpam detection, sentiment
Named entity recognitionFind people, places, dates
SummarisationShort abstract of an article
Question answeringAnswer from a passage
Machine translationEnglish to Hindi
CAUTION

Evaluate on realistic data, check for bias, and never assume a model's fluent output is correct.