{"id":25841,"date":"2021-12-16T09:19:35","date_gmt":"2021-12-16T03:49:35","guid":{"rendered":"https:\/\/python-programs.com\/?p=25841"},"modified":"2021-12-16T09:19:35","modified_gmt":"2021-12-16T03:49:35","slug":"python-nltk-program-to-implement-n-grams","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-nltk-program-to-implement-n-grams\/","title":{"rendered":"Python NLTK Program to Implement N-Grams"},"content":{"rendered":"

Natural language processing and text mining both make extensive use of text n-grams. It’s basically a string of words that all appear in the same window at the same time.<\/p>\n

When computing n-grams, you usually advance one word at a time (although in more complex scenarios you can move n-words). N-grams can be used for a variety of things.<\/p>\n

N = 1:<\/strong> Welcome to Python Programs<\/p>\n

The Unigrams<\/strong> of this sentence is:\u00a0 welcome, to, Python, Programs<\/p>\n

N = 2:\u00a0<\/strong>Welcome to Python Programs<\/p>\n

bigrams:<\/strong><\/p>\n

Welcome to,<\/p>\n

to Python<\/p>\n

Python Programs<\/p>\n

N=3:<\/strong> trigrams: <\/strong>Welcome to Python, to Python Programs<\/p>\n

For example, when developing language models, n-grams are used to generate not only unigram models but also bigrams and trigrams.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given String = \"Hello\u00a0Welcome\u00a0to\u00a0Python\u00a0Programs\"\r\nGiven n value = 2<\/pre>\n

Output:<\/strong><\/p>\n

('Hello', 'Welcome')\r\n('Welcome', 'to')\r\n('to', 'Python') \r\n('Python', 'Programs')<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given String = \"good morning all this is python programs\"\r\nGiven n value = 3<\/pre>\n

Output:<\/strong><\/p>\n

('good', 'morning', 'all')\r\n('morning', 'all', 'this')\r\n('all', 'this', 'is') \r\n('this', 'is', 'python')\r\n('is', 'python', 'programs')<\/pre>\n

NLTK Program to Implement N-Grams<\/h2>\n

 <\/p>\n

Method #1: Using NLTK Module (Static Input)<\/h3>\n

Approach:<\/strong><\/p>\n