Chunking - Natural Language Processing With Python and NLTK p.5

sentdex · Intermediate ·💻 AI-Assisted Coding ·11y ago

Key Takeaways

The video demonstrates chunking in Natural Language Processing (NLP) using Python and NLTK, focusing on identifying named entities and noun phrases in a sentence. It covers creating chunk grammars, using regular expressions, and modifying chunks to include additional information.

Full Transcript

hello everybody and welcome to part five of our nltk with python for natural language processing tutorial video in this video we're going to be talking about chunking so what is chunking besides a really strange sounding term so consider you have a body of text we know how to split it up by sentence and by even by word and not only that but part of speech tags we're that far what would be the next step to figure out the meaning of a sentence well first we want to know who is the sentence talking about who what whatever uh and so generally we refer to that as the named entity in the context right but also just a noun okay so a person place or thing is generally going to be your subject and I say generally now uh so once you know that okay once you know the named entity what's the Next Step well the next step is going to be or the noun the next step is going to be finding out words that kind of modify or affect that noun so you might have many named entities or many nouns in the same sentence so you might have a sentence like uh Apple releases new phone comes with new color case $100 more and Tesla releases home battery okay okay so these are sent this is one sentence but it's talking about two different things completely and you might even have some opinions in that sentence and you got to figure out who where does that opinion apply is that applying to Apple or is that applying to Tesla so um so that's what we're going to be talking about now is chunking and how we might be able to do stuff like that so generally what that's going to be is is you're going most people will chunk into what are called noun phrases and these are just going to be groups of Na well it'll be a noun and we'll have a bunch of modifiers around that noun and and it'll be kind of like a a descriptive sentence I suppose or descriptive group of words surrounding that noun now the downside of that is you're you're only going to be able to use regular Expressions so what's going to happen is you are F like you can only group things together as a chunk and it can only be a chunk of things that are touching each other as you'll see in a moment so that's kind of the only downside here uh but you can at least chunk important words and then kind of break it out from there uh if if you want it so anyways it'll be more useful I think to kind of visualize what chunking is now so we're going to come down here and the way that we do chunking is we we make use a meshing of these part of speech tags and regular Expressions now if you are unfamiliar with regular Expressions you can still follow along and I'll try to explain it as good as I can but you're going to have two brand new Concepts being thrown at you here and you may not know which is which like are we looking at regular Expressions here or are we looking at like chunking code um so if you need to learn about regular Expressions this is uh python program.net it's my website crlf when you're on the basics tab anyways if you're not here click on start learning Basics regx enter we want this one actually um video tutorial text tutorial we very fun now the main thing that we're going to use with chunking is modifiers so with your modifiers you've got things like uh the plus sign means it m it's one or more question mark means zero or one no more asterisk means zero or more so basically like any number or nothing uh we probably won't use the dollar sign the carrot we won't be using really you might actually uh probably not though the either or you'll use that a lot the range you'll use as well so anyways um just keep that in mind that's kind of what you need to know uh with modifiers I will still hold your hand through this but like I said it might be confusing if you if you're not familiar with either of these Concepts so coming down here uh let's get rid to tagged and then we'll go or print tagged rather keep tagged there now we're ready to create our chunk gram chunk gram is going to be equal to and generally people use triple quotes for their chunk grams because this one will be really simple but a lot of times people are going to put them and separate them by lines uh cuz they can get really long and complicated really fast so chunk gram use a triple quots if you want don't if you don't want to it doesn't matter to me uh and we're going to chunk by now most people are going to put the r in front to denote a form of regular expression here but it I'm pretty sure it's not necessary we'll we'll test that if I remember to so now chunk you can call this anything you want you could call this um H if you wanted uh it's just what it's going to be called when it finds it so anyway a chunk equals and then you put the chunk that you want to you know find inside curly braces now uh to bring up or mention any form of part of speech tag use these little I don't know brackets sideways carrots I don't know what the heck you call these someone tell me what you call these um and first let's say we're looking for any RB now what is an RB let's head over oops actually let's go it's up here RB adverb now we want to find RB rbr or RBS any adverb we want to find how do we do it regular Expressions so we come on down here RB what would be a valid modifier to use we want RB followed by basically any character we could use the W I suppose but we're not going to do that uh we're going to say RB any character is the period and then we're going to use a question mark what does that mean so the period is any character except for new line basically and tabs and stuff and oh actually I'm sorry it's actually any as long as my own uh tutorial is correct it's actually any character except for a new I thought it was also tabs anyways moving on uh RB so anything and then question mark is zero or one because as you can see we don't have any part of speech tag that is longer well at least for RB uh than three characters okay but possessive pronouns can be and they use a dirty dollar sign there anyway moving on uh so that's fine uh any adverb then we're going to say what we'll say asterisk what is asterisk zero or more so we're saying any form of an adverb and we're looking for zero or more of these so this chunk we'll chunk whether or not there is an adverb but if there is one we'll get it then we'll have another one and then we're just going to have VB and then we'll use the exact same code that we used before which is period question mark and again if we find one great if not no problem then we're going to look for an nnp what's an nnp Harrison let's go up nnp proper noun and hot diggity there's an example of a proper noun it's me so come on down here nnp uh we are going to require the existence of a nnp then we're going to do a oops a plausible NN let's go back up NN is just a noun okay so nnp singular proper noun followed by possible noun singular anything now that's our chunk sweet so hopefully you guys follow along there if you have any questions as always ask below now what we're reg ready to do is uh parse via this chunk gram so we're going to say chunk parse equals nltk regex parser and the regex parser we want to use is the chunk gr now chunk is going to equal uh the chunk parser dop parse the tagged part of speech stuff and then we can do the following print Chun [Music] what did I do r x oh right I'm pretty sure in my original tutorial series I swear I hit this rag x p parser and I'm almost positive they're doing this because it's regular expressions and then parser but no one says regx they say reg I don't know anyway let's try this again I'm almost positive that hit me in the original tutorial uh this is going to go for a while so control C to break it and so this is your um you know basically all the output and what you're looking for is chunk right this is chunk so chunk State there's another chunk Union Address chunk United Capital so it's finding all these nouns basically White House chunk chunk okay now this is kind of ugly the computer loves this stuff but the human hates it so what you can do with um nltk if you Gra if you have map plot lib if you don't have map plot Li this probably won't work but chunk. draw save and run that and now you have this look at us wow that's way too many chunks what has happened for maybe because that's the start of it's probably because like this initial yeah right so the initial stuff here uh that was a little rough but anyway present George W bush and as we can see that really we actually would want to chunk these all together right so maybe any amount of nnps so we could come down here nnp um one or more is what we want so we want to do a plus okay so now let's try that again I don't know how far deep this one was but it was it was a few X's so anyways let just rerun it um okay so that's a little better um that's not so hot but this is hard because of the original stuff but President George W bush that is a correct chunk um let's get out of this initial stuff Mr Speaker vice president jie this is a tough this is a tough one for uh python anyway we'll start on this one so this is the original one that we wanted to chunk for sure um so President George W bush good State of the Union uh again you could add this stuff into your chunk like you could have a a possible preposition a possible I forget what DT stands for but it's like useless words I'm pretty sure um close close close White House put that together Eric Draper every dra I'm not that's almost like he's missing a space there um Capital Dome that's a good catch okay have served America all right you get the point so this is how we're chunking um a bit of information together obviously again like I was trying to point out our chunk is extremely basic and as you can see as we move forward we can think of a lot of like little stuff that we're like hm I wouldn't mind adding this or that or a possible of this and so as you can imagine these chunks can get really long okay so anyway feel free to mess with it I actually highly encourage you to kind of play around with this try to catch that of the or yeah I think it was of the see if you can catch that and while I'm thinking about let's look at DT again determiner yeah DT for not important anyway um so that's chunking now in the next tutorial video we're going to talk about basically kind of a a different task and that's going to be chinking and that's going to be so chunking is the you know grouping of things chinking is going to be the removal of something get that out so we'll tuck on that in the next video if you have any questions or comments on this video please feel free to leave them below otherwise as always thanks for watching thanks for all the support and subscriptions and until next time

Original Description

Chunking in Natural Language Processing (NLP) is the process by which we group various words together by their part of speech tags. One of the most popular uses of this is to group things by what are called "noun phrases." We do this to find the main subjects and descriptive words around them, but chunking can be used for any combination of parts of speech. sample code: http://pythonprogramming.net http://hkinsley.com https://twitter.com/sentdex http://sentdex.com http://seaofbtc.com
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This video teaches how to use chunking in NLP to identify named entities and noun phrases, and how to create and modify chunk grammars using regular expressions. It covers the basics of chunking and its applications in NLP. By watching this video, viewers can learn how to apply chunking to improve their NLP tasks.

Key Takeaways
  1. Create a chunk grammar using a combination of part-of-speech tags and regular expressions
  2. Use modifiers such as the plus sign (+), question mark (?), and asterisk (*) to specify the number of occurrences of a pattern
  3. Create a chunk gram using triple quotes
  4. Define a chunk using curly braces
  5. Use the 'r' prefix for regular expressions
  6. Use square brackets to specify a part of speech tag
  7. Use the period and question mark to match any character except newline
  8. Run the code with chunk.draw_save()
  9. Modify the chunk to include additional information
  10. Remove words or phrases from a chunk
💡 Chunking is a powerful technique in NLP that can be used to identify named entities and noun phrases, and to improve the accuracy of NLP tasks

Related Reads

📰
The Visual Reverse Engineering Hypothesis: From Phosphenes to Latent Space
Explore the Visual Reverse Engineering Hypothesis to understand how the brain processes visual information and its potential connection to AI models
Medium · AI
📰
AI-Driven Software Engineering: Advances in Agentic Workflows, Code Generation, and Developer…
Learn how AI-driven software engineering advances agentic workflows, code generation, and developer tools, improving software development efficiency and quality
Medium · AI
📰
Can AI Badger Reduce Local Coding Agent Token Usage?
Learn how AI Badger can reduce local coding agent token usage by 32.1% and runtime by 54.5% through compact handoff and compression
Dev.to AI
📰
Devin, the "First AI Software Engineer," Failed 86% of Its Benchmark Tasks, and Then What
Devin, an AI software engineer, failed 86% of benchmark tasks, highlighting limitations of AI in replacing human engineers
Dev.to · Alex @ Vibe Agent Making
Up next
How to Start a SaaS Business in 2026
Learn With Shopify
Watch →