MLG 019 Natural Language Processing 2
Classical natural language processing (NLP) runs on n-gram language models, naive Bayes, MaxEnt, hidden Markov models and LDA, still fast baselines beside transformers. Also covers generative vs discriminative models and search with TF-IDF and cosine similarity. Links Notes and resources at ocdevel.com/mlg/19 Join the free MLG group: listen to my other AI shows, ask me questions, and talk with other listeners Classical NLP vs transformers Classification and sentiment now run on fine-tuned transformers or LLMs (MLG 033); naive Bayes and TF-IDF remain fast baselines. Levenshtein distance and the Porter stemmer Levenshtein distance counts the single-letter insertions, deletions and substitutions between two words. The Porter stemmer chops suffixes by rule, overstemming organizer and organization to "organ"; their lemma is "organize". N-grams and n-gram language models An n-gram is a run of n tokens: "hello how are you doing" has five unigrams and four overlapping bigrams (hello how, how are, …). A language model scores how probable a sentence is: "I took my dog to the park" beats "I took my dog to the egg". The chain rule conditions each word on those before it; an n-gram model keeps only the previous n−1. A plain product of probabilities holds only for independent events (MLG 013). bigram: P(w1 … wn) ≈ P(w1) × P(w2 | w1) × … × P(wn | wn−1) P(how | hello) = count(hello how) / count(hello) Conditioning on one previous word is a bigram model, on two a trigram model. Larger n is more accurate but costs storage and data. Uses: predictive text and spelling correction. Naive Bayes for text classification Naive Bayes classifies documents. Bayes' rule turns P(words | spam), easy to count, into P(spam | words). As a bag-of-words model it ignores order, missing "not pleasant", and its independence assumption counts "Hong Kong" twice. Maximum entropy (MaxEnt) models MaxEnt is multinomial (softmax) logistic regression. For part-of-speech (POS) tagging and named entity recognition (NER) it classifies words from hand-built features: suffix (-ly suggests an adverb), neighbors, and a capital mid-sentence, which suggests a proper noun. Hidden Markov models (HMM) for POS tagging A hidden Markov model is a Markov chain (MLG 014) whose states are hidden, each emitting what you observe, as weather shows in what people wear. For tagging, the hidden states are POS tags and the observations are words. Generative vs discriminative models Discriminative models learn P(y | x), a decision boundary: logistic regression, MaxEnt, SVMs, neural networks. Generative models learn the joint P(x, y), usually as P(x | y)·P(y), so they can generate data: naive Bayes, HMMs, LDA. They need less training data. Topic modeling with LDA Latent Dirichlet allocation (LDA), unsupervised, models each document as a mixture of topics and each topic as a distribution over words: a document can be 30% pets and 20% baseball, where k-means gives one cluster. LSA/LSI instead factor the term-document matrix with SVD. Bag of words and TF-IDF A bag-of-words vector has one column per vocabulary word, holding 0/1 or a count, mostly zeros: sparse. Raw counts reward keyword stuffing and common words. TF-IDF weights a term by its rarity across documents, so stop words sink without being removed. tf-idf(t, d) = tf(t, d) × log(N / df(t)) N = documents, df(t) = documents containing t Dense vectors are learned embeddings: word2vec (MLG 022), then transformer embeddings. Cosine similarity vs Euclidean distance vs Jaccard Jaccard: overlap over union. Euclidean: straight-line distance, so a long document on the topic lands far from a short one. Cosine: the angle between vectors, independent of length. Search ranks the documents containing the query terms by cosine similarity of TF-IDF vectors. Elasticsearch has scored with BM25, a refinement of TF-IDF, by default since 2016, and also runs dense-vector search. Where to learn classical NLP Speech and Language Processing by Jurafsky and Martin. Stanford CS224N. The NLTK book, Natural Language Processing with Python. Next: MLG 020, on syntax parsing and the larger NLP tasks.
Embed this episode
No transcript yet.