← SnapRecaps

Natural Language Processing (NLP) Tutorial with Python & NLTK

► 405,771 views ⏲ 38:10 Watch on YouTube ↗

Summary

The video explains NLP's role in processing unstructured data, notes NLU's difficulty due to ambiguities, and demonstrates tokenization and word frequency analysis using NLTK.

Executive Summary

The video explains that natural language processing (NLP) is essential because the vast majority of data is unstructured, and it enables machines to communicate with humans using natural language. It highlights that while natural language generation (NLG) is relatively straightforward, natural language understanding (NLU) is far more difficult, primarily due to three types of ambiguity: lexical, syntactical, and referential. To address these challenges, the video introduces NLTK, a leading Python toolkit for processing human language data, and demonstrates core NLP concepts. The main hands-on focus is tokenization, the foundational step of breaking text into individual word tokens, illustrated using built-in corpora like Brown and Gutenberg. Finally, the video shows how to use NLTK's frequency distribution tools to analyze token counts and identify the most common words in a given text.

Key Points

  • ▶ 0:50 Human success is driven by communication; language evolved from informal oral sharing to standardized drawings and then to structured languages governed by grammar rules.
  • ▶ 2:01 Only 21% of available data is structured; the vast majority of text data from tweets, chats, and messages is unstructured, creating the need for NLP to extract actionable insights.
  • ▶ 2:34 NLP is an AI method for communicating with intelligent systems using natural language, used for tasks such as sentiment analysis, machine translation, chatbots, and voice assistants.
  • ▶ 5:50 NLG involves mapping a sentence plan into the actual sentence structure.
  • ▶ 5:59 NLU is much harder than NLG, despite even small children understanding language easily.
  • ▶ 6:17 NLU's core challenges are three types of ambiguity: lexical, syntactical, and referential.
  • ▶ 6:34 Lexical ambiguity is introduced as the first level of ambiguity, defined as a single word having two or more possible meanings, also called semantic ambiguity.
  • ▶ 6:45 Example "She is looking for a match" shows how one word ("match") can mean a contest/game or a romantic partner, illustrating context-dependent interpretation.
  • ▶ 7:01 Example "The fisherman went to a bank" reinforces that lexical ambiguity occurs when a word like "bank" refers to either a financial institution or a riverbank.
  • ▶ 7:13 Syntactical ambiguity is when a single sentence has two or more possible meanings; also called structure or grammatical ambiguity.
  • ▶ 7:24 “The chicken is ready to eat” demonstrates ambiguity: the chicken may be ready to eat something, or ready for us to eat—very hard for a computer to resolve.
  • ▶ 7:49 “I saw the man with the binoculars” is ambiguous because it's unclear who had the binoculars, showing that grammatical structure alone doesn't determine meaning.
  • ▶ 8:15 Referential ambiguity occurs when a pronoun's referent is unclear.
  • ▶ 8:21 Example: "The boy told his father the theft. He was very upset." – "he" is ambiguous.
  • ▶ 8:32 "He" could mean the boy, the thief, or the father, so nobody knows who is meant.
  • ▶ 8:42 NLTK (Natural Language Toolkit) is a leading Python platform for working with human language data, offering easy access to 50 corpora, lexical resources like WordNet, and text-processing libraries for classification, tokenization, stemming, and tagging.

  • ▶ 9:12 To install NLTK, run nltk.download() in the Python shell; this opens the NLTK Downloader window where you should select "all" and click Download to fetch all corpora and text packages into a single location.

  • ▶ 9:46 The speaker recommends installing NLTK inside the Python directory itself, making it easier to access all downloaded files and resources.

  • ▶ 9:50 The section introduces core NLP terminology, beginning with tokenization as the foundational concept for processing text.
  • ▶ 9:57 Tokenization is defined as breaking strings into tokens, where tokens are small units used for further processing.
  • ▶ 10:10 Tokenization consists of three steps: splitting sentences into words, understanding each word's importance, and assessing each word's context within the sentence.
  • ▶ 10:16 Tokenization is defined as producing a structural description of an input sentence by breaking it down into fundamental components.
  • ▶ 10:19 The example sentence "Today we will understand tokenization" contains five tokens: Today, we, will, understand, tokenization.
  • ▶ 10:32 In computer terms, each word in a sentence is known as a token, making tokenization the foundational step for processing raw text.
  • ▶ 10:45 The presenter uses Jupyter Notebook for the demo, but notes any IDE works; first steps are importing os, nltk, and nltk.corpus.
  • ▶ 11:02 The demo explores NLTK's built-in corpora, showing many data files (e.g., stopwords, state union names, Twitter sample) for different NLP tasks.
  • ▶ 11:23 NLTK provides a wide variety of dataset types, giving a practical overview of available resources before starting tokenization.
  • ▶ 11:26 NLTK provides the Brown Corpus as a downloadable resource, and it can be loaded by importing it directly with from nltk.corpus import brown.
  • ▶ 11:37 Using brown.words() returns the corpus as a list of tokenized word strings, e.g., beginning with "Fulton County Grand Jury said ...".
  • ▶ 11:41 The corpus is already pre-tokenized, so each word is a separate Python string ready for further NLP processing.
  • ▶ 11:43 The NLTK Gutenberg corpus contains many classic texts, including works by Austen, Shakespeare, Blake, Carroll, and Whitman.
  • ▶ 12:14 The instructor uses Shakespeare's Hamlet as the example, showing how to inspect its raw text beginning with "The Tragedy of Hamlet by..."
  • ▶ 12:28 You can slice and view the first 500 words of a text (e.g., hamlet[:500]), demonstrating that any NLTK corpus text can be used for NLP practice.
  • ▶ 13:02 A custom paragraph about artificial intelligence is introduced to demonstrate tokenization on a text string.
  • ▶ 13:24 The word_tokenize function is imported from nltk.tokenize to split the paragraph into tokens.
  • ▶ 13:47 Running tokenization shows that NLTK treats punctuation marks like commas and hyphens as separate tokens alongside words.
  • ▶ 13:57 After tokenization, the total number of tokens is counted using Python's len(), revealing the text contains 273 tokens.
  • ▶ 14:09 NLTK's FreqDist builds a word count distribution, with each token converted to lowercase via .lower() so uppercase and lowercase versions are not counted separately.
  • ▶ 14:39 The printed frequency distribution includes punctuation counts (comma 30, full stop 9, question mark 1) and word frequencies such as intelligence (6) and intelligent (6).
  • ▶ 14:55 Use NLTK's FreqDist to get word frequencies (e.g., "artificial" appears 3 times), count distinct tokens with len() (121 distinct vs 273 total), and view top recurring tokens via most_common(10) — with comma (,) as the most frequent.
  • ▶ 16:16 The blank line tokenizer (blankline_tokenize) splits a document into separate paragraphs based on newlines; for the example paragraph it returns 9 distinct paragraphs, which can be indexed individually.
  • ▶ 17:07 Generate n-grams using NLTK: bigrams and trigrams create consecutive two- and three-word sequences, while ngrams(tokens, n) allows custom lengths (e.g., n=5) for any number of consecutive words.
  • ▶ 18:37 The instructor identifies the “number” parameter as the control for N-gram length.
  • ▶ 18:39 Setting this parameter to five specifies an N-gram of length five.
  • ▶ 18:41 The output confirms sequences of five consecutive words/tokens are generated.
  • ▶ 18:52 Stemming is introduced as the technique that normalizes words into their base root form, e.g., affectation, effects, affections, affected, affection, and affecting.
  • ▶ 19:10 The stemming algorithm works by cutting off the beginning or end of a word based on a list of common prefixes and suffixes, making it a rule-based, mechanical process.
  • ▶ 19:19 This "indiscriminate cutting" can work sometimes but not always, and the instructor notes that this approach "presents some limitations."
  • ▶ 19:36 Introduces the Porter Stemmer as an algorithm for reducing words to root forms, implemented in NLTK via PorterStemmer and the .stem() method.
  • ▶ 19:50 Demonstrates stemming: "having" becomes "have", and a word list (give, giving, given, gave) is stemmed with mixed results.
  • ▶ 20:18 Highlights a key limitation: the Porter Stemmer only removes suffixes like "ing" (giving → give), but does not fully normalize all grammatical variants (e.g., "given" and "gave" remain unchanged).
  • ▶ 20:23 The tutorial introduces the Lancaster Stemmer, another commonly used NLTK stemming algorithm, and applies it to the same set of words as the Porter Stemmer to compare outputs.
  • ▶ 20:36 To implement it, the presenter imports LancasterStemmer from nltk.stem and runs it in the same manner as the earlier Porter demonstration.
  • ▶ 20:49 The key takeaway is that the Lancaster Stemmer is more aggressive than the Porter Stemmer, reducing words more drastically—which risks over-stemming where unrelated words collapse to the same root.
  • ▶ 20:58 Choosing a stemmer depends on the specific NLP task, not a one-size-fits-all solution.
  • ▶ 21:07 The Snowball Stemmer requires you to provide the language being used, distinguishing it from other stemmers.
  • ▶ 21:29 Stemmer selection is task-driven—e.g., use Lancaster for counting a word like "give," while Snowball/Porter suit other purposes.
  • ▶ 21:44 Stemming does not always work properly and does not always produce the true root word.
  • ▶ 21:53 Example: "fish," "fishes," and "fishing" all stem to "fish," illustrating the limitation.
  • ▶ 22:02 Unlike stemming, lemmatization uses morphological analysis and a detailed dictionary to link a word form back to its canonical lemma.
  • ▶ 22:23 Lemmatization groups different inflected forms of a word into a single base form called a lemma, similar to stemming.
  • ▶ 22:34 Unlike stemming, lemmatization always outputs a proper, linguistically valid word (e.g., not a non-word like "give").
  • ▶ 22:50 Example: "gone" and "going" both lemmatize to the base word "go".
  • ▶ 22:53 Lemmatization reduces different inflected forms like "gone," "going," and "goes" to a single base word "go."
  • [22:56–23:01] The instructor shifts to a hands-on demonstration, testing the same example words with lemmatization directly in NLTK.
  • ▶ 23:04 To use WordNet lemmatization in NLTK, you must import both the wordnet dictionary and the WordNetLemmatizer class.
  • ▶ 23:28 The lemmatizer correctly maps "corpora" to "corpus", showing it can reduce plural forms to their base lemma.
  • ▶ 23:38 Without part-of-speech (POS) tags, the lemmatizer leaves words unchanged because it defaults to a single tag (likely noun), making explicit POS tags necessary for accurate results.
  • ▶ 24:06 Stop words like "I," "at," "for," and "various" are essential for grammatical English, but they do not help in NLP tasks.
  • ▶ 24:27 These words are called stop words; although useful in language, they carry little semantic meaning and add noise to text analysis.
  • ▶ 24:42 NLTK provides a built-in stop word list that can be imported from nltk.corpus and filtered by language (e.g., English).
  • ▶ 25:15 Inspecting fdist.top(10) shows the most frequent tokens are mostly stop words, digits, or special characters—not meaningful content words.
  • ▶ 25:39 Use re.compile to create a pattern that matches digits and special characters, then append only clean, punctuation-free words to a new list.
  • ▶ 25:53 The cleaned list, named post_punctuation, successfully removes numbers, commas, and other special characters, leaving more meaningful tokens.
  • ▶ 26:05 Parts of speech (POS) define the grammatical type and function of a word, such as verb, noun, adjective, adverb, or article.
  • ▶ 26:26 A word can have multiple POS roles depending on context, making natural language understanding harder than generation; e.g., “Google” used as a verb.
  • ▶ 27:19 POS tagging is demonstrated in practice by assigning roles to each word, such as determiner, noun, and verb, in a sample sentence.
  • ▶ 27:49 NLTK's pos_tag automatically assigns grammatical roles to all tokens in a tokenized sentence, e.g. "Timothy" as noun, "is" as verb, "a" as determiner, and "natural" as adjective.
  • ▶ 28:34 On the example "John is eating a delicious cake", NLTK tags both "is" and "eating" as verbs, treating the continuous verb phrase as a single verb unit.
  • ▶ 28:47 A key limitation: POS taggers can struggle with verb phrase grouping, failing to distinguish auxiliary vs. main verb roles more precisely.
  • ▶ 28:54 NER detects named entities in text, including persons, organizations, locations, movies, monetary values, and quantities.
  • ▶ 29:17 NER has three phases: noun phrase identification, phrase classification, and entity disambiguation.
  • ▶ 29:52 Entity disambiguation adds a validation layer, optionally using knowledge graphs like Google Knowledge Graph, IBM Watson, or Wikipedia to fix misclassifications.
  • ▶ 30:09 NER example labels “Google” as Organization, “Sundar Pichai” as Person, “Minnesota” as Location, and “Roy Center event” as Organization.
  • ▶ 30:43 NLTK implementation steps: import ne_chunk, tokenize the sentence, add POS tags, then pass the tagged tokens into ne_chunk.
  • ▶ 31:20 Output shows “US” recognized as Organization and “White House” grouped as a single Facility entity, demonstrating NER as an additional layer on POS tagging for deeper understanding.
  • ▶ 31:40 NER entity categories simplify language processing by enabling the system to identify and classify key types of information.
  • ▶ 31:44 The NER entity list includes geo-political entities, facilities, locations, organizations, and persons.
  • ▶ 31:56 The previous example is referenced to show how the entity list maps to real outputs, correctly identifying an organization and a facility.
  • ▶ 32:03 Syntax is defined as the set of rules, principles, and processes that govern sentence structure in a language.
  • ▶ 32:22 Syntax dictates which parts of a sentence appear in which position, establishing word order and structural arrangement.
  • ▶ 32:29 When a sentence is provided as input, a syntax tree is generated—a tree structure representing the hierarchical arrangement of words and phrases.
  • ▶ 32:37 Syntax trees (parse trees) represent the syntactic structure of sentences or strings as a hierarchical, tree-like structure.
  • ▶ 32:44 In programming languages, syntax trees are used for generating symbol tables for compilers and later for code generation.
  • ▶ 33:00 An example with "the cat sat on a mat" shows how the tree breaks down from sentence (S) into noun phrase, verb, and prepositional phrase, down to individual parts of speech.
  • ▶ 33:20 To visualize syntax trees directly in a Jupyter notebook, an additional external tool must be installed.
  • ▶ 33:20 The required tool is Ghostscript, which acts as a rendering engine for the tree diagrams, and can be downloaded from its official downloads page.
  • ▶ 33:34 The instructor explicitly skips an in-depth installation walkthrough, moving on without further setup details.
  • ▶ 33:36 Chunking is the process of grouping individual pieces of information into bigger pieces called chunks.
  • ▶ 33:53 Chunking is described as the opposite of tokenization, grouping individual words or tokens into larger meaningful chunks.
  • ▶ 34:40 Chunking helps language understanding by grouping words into phrases (e.g., "the pink panther" as a noun phrase), making language easier to process.
  • ▶ 35:22 Tokenization and POS tagging can be done in one compact NLTK command, pairing each word with its tag for chunking.
  • ▶ 35:39 A noun-phrase grammar is defined and used with a regular-expression parser to generate chunk output from the tagged sentence.
  • ▶ 36:20 Even without a visual tree, the text representation shows clear NP chunks like “the big cat,” “the little mouse,” and “the fresh cheese.”
  • ▶ 36:50 The tutorial wraps up, recapping that viewers should now understand what NLP is and how it is used.
  • ▶ 36:58 Core NLP text-processing steps are summarized: tokenization, stop words, stemming, and lemmatization.
  • ▶ 37:14 The recap includes syntax, noting that viewers created a syntax tree to understand sentence arrangement and structural relationships.
  • ▶ 37:19 The exact arrangement of words in a sentence is important because it directly affects how meaning is understood.
  • ▶ 37:22 The pipeline combines the dictionary, POS tags, and tokenization to construct a parse tree that forms the sentence's meaning.
  • ▶ 37:31 Chunking is the final step, grouping small tokens into larger, meaningful units.
  • ▶ 37:36 The session wraps up, encouraging viewers to start implementing NLP procedures using NLTK.
  • ▶ 37:45 NLTK is highlighted as containing extensive text data and built-in examples, with the session covering only the beginning.
  • ▶ 37:59 Viewers are encouraged to explore deeper NLP topics, such as context-free grammar, already available in NLTK.

Video Sections

  • ▶ 0:00 Introduction and NLP Foundations (0:00 - 5:56) - - Opens the session, explains human language and the need for NLP, defines NLP, and covers applications and NLU/NLG components.
  • ▶ 5:54 NLP Challenges, Pipeline, and NLTK Demos (5:54 - 14:55) - - Covers NLP difficulties, introduces NLTK, and demonstrates tokenization, stop word removal, stemming, lemmatization, and POS tagging.
  • ▶ 14:55 Token Statistics and N-grams (14:55 - 18:41) - - Covers token counts, frequency distributions, top tokens, blank-line tokenization, and bigram/trigram/n-gram generation.
  • ▶ 18:41 Stemming Techniques (18:41 - 38:12) - - Explains stemming concepts and limitations and demonstrates Porter and Lancaster stemmers.

Exact Transcript

Load the full timestamped transcript on demand and click any time to jump in the video.