title: Vectorizing trigrams with all possible 3-grams - Python
body: <p>I'm trying to create a 3-gram model to apply machine learning techniques. </p>
  
  <p>Basically I'm trying as follow:</p>
  
  <pre><code>import nltk
  from sklearn.feature_extraction.text import CountVectorizer
  import itertools
  
  my_array = ['worda', 'wordb']
  vector = CountVectorizer(analyzer=nltk.trigrams,ngram_range=(3,3))
  vector.fit_transform(my_array)
  </code></pre>
  
  <p>My vocabulary:</p>
  
  <pre><code>{('o', 'r', 'd'): 0,
  ('r', 'd', 'a'): 1,
  ('r', 'd', 'b'): 2,
  ('w', 'o', 'r'): 3}
  </code></pre>
  
  <p>None of my words have spaces or special characters.
  So when I run this:</p>
  
  <pre><code>tr_test = vector.transform(['word1'])
  print(tr_test)
  print(tr_test.shape)
  </code></pre>
  
  <p>I get this return:</p>
  
  <pre><code>(0, 0)  1
  (0, 1)  1
  (0, 3)  1
  (1, 4) #this is the shape
  </code></pre>
  
  <p>I think this is right... at least makes sense...
  But I would like to represent each word with a matrix containing all 3-gram possibilities. So, each work would be represented by a (1x17576) matrix.
  Now I'm using 1x4 matrix (in this particular case), because my vocabulary is built based on my data.</p>
  
  <p>17576 (26ˆ3)- Represents all 3 letters combination in the alphabet (aaa, aab, aac, etc...)</p>
  
  <p>I tried to set my vocabulary to an array with all 3-grams possibilities, like this:</p>
  
  <pre><code>#This creates an array with all 3 letters combination
  #['aaa', 'aab', 'aac', ...]
  keywords = [''.join(i) for i in itertools.product(ascii_lowercase, repeat = 3)]
  vector = CountVectorizer(analyzer=nltk.trigrams,ngram_range=(3,3), vocabulary=keywords)
  </code></pre>
  
  <p>This didn't work... Someone can figure out how to do this?</p>
  
  <p>Thanks!!!</p>