{"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 Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"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. 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 …<\/p>\nGiven String = \"Hello\u00a0Welcome\u00a0to\u00a0Python\u00a0Programs\"\r\nGiven n value = 2<\/pre>\n
('Hello', 'Welcome')\r\n('Welcome', 'to')\r\n('to', 'Python') \r\n('Python', 'Programs')<\/pre>\n
Given String = \"good morning all this is python programs\"\r\nGiven n value = 3<\/pre>\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
Method #1: Using NLTK Module (Static Input)<\/h3>\n
\n
# Import ngrams from the nltk module using the import keyword\r\nfrom nltk import ngrams\r\n# Give the string as static input and store it in a variable.\r\ngvn_str = \"Hello Welcome to Python Programs\"\r\n# Give the n value as static input and store it in another variable.\r\ngvn_n_val = 2\r\n# Split the given string into list of words using the split() function\r\nsplt_lst = gvn_str.split()\r\n# Pass the above split list and the given n value as the arguments to the ngrams()\r\n# function and store it in another variable.\r\nrslt_n_grms = ngrams(splt_lst, gvn_n_val)\r\n# Loop in the above result obtained using the for loop\r\nfor itr in rslt_n_grms:\r\n # Inside the loop, print the iterator value.\r\n print(itr)\r\n<\/pre>\n
('Hello', 'Welcome')\r\n('Welcome', 'to')\r\n('to', 'Python') \r\n('Python', 'Programs')<\/pre>\n
Method #2: Using NLTK Module (User Input)<\/h3>\n
\n
# Import ngrams from the nltk module using the import keyword\r\nfrom nltk import ngrams\r\n# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Give the n value as user input using the int(input()) function and store it in another variable.\r\ngvn_n_val = int(input(\"Enter some random number(n) = \"))\r\n# Split the given string into list of words using the split() function\r\nsplt_lst = gvn_str.split()\r\n# Pass the above split list and the given n value as the arguments to the ngrams()\r\n# function and store it in another variable.\r\nrslt_n_grms = ngrams(splt_lst, gvn_n_val)\r\n# Loop in the above result obtained using the for loop\r\nfor itr in rslt_n_grms:\r\n # Inside the loop, print the iterator value.\r\n print(itr)\r\n<\/pre>\n
Enter some random string = good morning all this is python programs \r\nEnter some random number(n) = 3 \r\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