<jupyter_start><jupyter_text>Day 69 Chunking While tokenizing allows you to identify words and sentences, **chunking** allows you to identify **phrases**. Note: A phrase is a word or group of words that works as a single unit to perform a grammatical function. Noun phrases are built around a noun.Here are some examples:“A planet”“A tilting planet”“A swiftly tilting planet” Chunking makes use of POS tags to group words and apply chunk tags to those groups. Chunks don’t overlap, so one instance of a word can be in only one chunk at a time.Here’s how to import the relevant parts of NLTK in order to chunk:<jupyter_code>from nltk.tokenize import word_tokenize<jupyter_output><empty_output><jupyter_text>Before you can chunk, you need to make sure that the parts of speech in your text are tagged, so create a string for POS tagging. You can use this quote from The Lord of the Rings:<jupyter_code>lotr_quote = "It's a dangerous business, Frodo, going out your door."<jupyter_output><empty_output><jupyter_text>Now tokenize that string by word:<jupyter_code>words_in_lotr_quote = word_tokenize(lotr_quote)
words_in_lotr_quote<jupyter_output><empty_output><jupyter_text>Now you’ve got a list of all of the words in lotr_quote.The next step is to tag those words by part of speech:<jupyter_code>import nltk
# nltk.download("averaged_perceptron_tagger")

lotr_pos_tags = nltk.pos_tag(words_in_lotr_quote)
lotr_pos_tags<jupyter_output><empty_output><jupyter_text>You’ve got a list of tuples of all the words in the quote, along with their POS tag. In order to chunk, you first need to define a chunk grammar.**Note**: A **chunk grammar** is a combination of rules on how sentences should be chunked. It often uses  regular expressions, or **regexes**.For this tutorial, you don’t need to know how regular expressions work, but they will definitely  come in handy for you in the future if you want to process text.Create a chunk grammar with one regular expression rule:<jupyter_code>grammar = "NP: {<DT>?<JJ>*<NN>}"<jupyter_output><empty_output><jupyter_text>NP stands for noun phrase. You can learn more about **noun phrase chunking** in  Chapter 7 of Natural Language Processing with Python—Analyzing Text with the Natural Language Toolkit. According to the rule you created, your chunks:    1.Start with an optional (?) determiner ('DT')2.Can have any number (*) of adjectives (JJ)3.End with a noun (&lt; NN>)     Create a **chunk parser** with this grammar:<jupyter_code>chunk_parser = nltk.RegexpParser(grammar) # creating a parse/model with the grammar we made
chunk_parser<jupyter_output><empty_output><jupyter_text>Now try it out with your quote:<jupyter_code>tree = chunk_parser.parse(lotr_pos_tags)<jupyter_output><empty_output><jupyter_text>Here’s how you can see a visual representation of this tree:<jupyter_code>tree.draw()<jupyter_output><empty_output><jupyter_text>From visual representation we get the following: You got two noun phrases:1.**'a dangerous business'** has a determiner, an adjective, and a noun.2.**'door'** has just a noun.  Example<jupyter_code>sentence2="the little yellow dog barked at the cat "
# word tokkenise
words=word_tokenize(sentence2)
print(words)
# parts of speech
pos_chunk = nltk.pos_tag(words)
print(pos_chunk)
# define grammar

# ? atleast 0 or 1 time
# * is 0 or more times
# ^ starts with
grammar=("NP:{<DT>?<JJ>*<NN>}")
chunkParser=nltk.RegexpParser(grammar) # RegexpParser is used to take the phrase using the help of grammar
chunkParser
t = chunkParser.parse(pos_chunk) #pass tagged words in the chucking parser
print(t)
t.draw()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08111849390919158, 'fraction_numerical': 0.0035991140642303433, 'mean_word_length': 6.084313725490196, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '463023', 'license': 'MIT', 'n_tokens_mistral': 1155, 'n_tokens_neox': 1116, 'n_words': 441, 'path': 'NLP/6.Chunking(Jan 17,2022).ipynb', 'repo_name': 'SuryaReginaAA/Learn', 'size': '3612'}