title: Computing N Grams using Python
body: <p>I needed to compute the Unigrams,  BiGrams and Trigrams for a text file containing text like: </p>
  
  <p>"Cystic fibrosis affects 30,000 children and young adults in the US alone
  Inhaling the mists of salt water can reduce the pus and infection that fills the airways of cystic fibrosis sufferers, although side effects include a nasty coughing fit and a harsh taste. 
  That's the conclusion of two studies published in this week's issue of The New England Journal of Medicine."</p>
  
  <p>I started in Python and used the following code:</p>
  
  <pre><code>#!/usr/bin/env python
  # File: n-gram.py
  def N_Gram(N,text):
  NList = []                      # start with an empty list
  if N&gt; 1:
      space = " " * (N-1)         # add N - 1 spaces
      text = space + text + space # add both in front and back
  # append the slices [i:i+N] to NList
  for i in range( len(text) - (N - 1) ):
      NList.append(text[i:i+N])
  return NList                    # return the list
  # test code
  for i in range(5):
  print N_Gram(i+1,"text")
  # more test code
  nList = N_Gram(7,"Here is a lot of text to print")
  for ngram in iter(nList):
  print '"' + ngram + '"'
  </code></pre>
  
  <p><a href="http://www.daniweb.com/software-development/python/threads/39109/generating-n-grams-from-a-word" rel="noreferrer">http://www.daniweb.com/software-development/python/threads/39109/generating-n-grams-from-a-word</a></p>
  
  <p>But it works for all the n-grams within a word, when I want it from between words as in CYSTIC and FIBROSIS or CYSTIC FIBROSIS. Can someone help me out as to how I can get this done? </p>