title: How to create a unigram and bigram count matrix for a text file along with a class variable into csv using Python?
body: <p>I want to create a unigram and bigram count matrix for a text file along with a class variable into csv using Python
  The text file contains two columns which look like this</p>
  
  <pre><code>Text                                                  Class
  I love the movie                                      Pos
  I hate the movie                                      Neg
  </code></pre>
  
  <p>I want the unigram and bigram count for text column and the output should be written into csv file</p>
  
  <pre><code>I     hate      love        movie   the        class
  1     0         1           1       1          Pos
  1     1         0           1       1          Neg
  </code></pre>
  
  <p>Bigram</p>
  
  <pre><code>I love     love the     the movie     I hate    hate the         class
  1            1              1         0          0               Pos
  0            0              1         1          1               Neg
  </code></pre>
  
  <p>Anybody can help me to improve the below code into the above mentioned output format?</p>
  
  <pre><code>&gt;&gt;&gt; import nltk
  &gt;&gt;&gt; from collections import Counter
  &gt;&gt;&gt; fo = open("text.txt")
  &gt;&gt;&gt; fo1 = fo.readlines()
  &gt;&gt;&gt; for line in fo1:
         bigm = list(nltk.bigrams(line.split()))
         bigmC = Counter(bigm)
         for key, value in bigmC.items():
             print(key, value)
  
  ('love', 'the') 1
  ('the', 'movie') 1
  ('I', 'love') 1
  ('I', 'hate') 1
  ('hate', 'the') 1
  ('the', 'movie') 1
  </code></pre>