title: Is there a simpler way to build a dictionary from strings and then vectorize the strings? Python
body: <p>My question on how to build a dictionary from string is a little more linguistically / NLP inclined than <a href="https://stackoverflow.com/questions/4627981/creating-a-dictionary-from-a-string">Creating a dictionary from a string</a></p>
  
  <p>Given a list of string sentences, <strong>is there a simpler way to build a unique word dictionary and then vectorize the string sentences?</strong> I know that there are external libraries to do this like <code>gensim</code> but i would like to avoid them. I've been doing it this way:</p>
  
  <pre><code>from itertools import chain
  
  def getKey(dic, value):
    return [k for k,v in sorted(dic.items()) if v == value]
  
  # Vectorize will return a list of tuples and each tuple is made up of 
  # (&lt;position of word in dictionar&gt;,&lt;number of times it occurs in sentence&gt;)
  def vectorize(sentence, dictionary): # is there simpler way to do this?
    vector = []
    for word in sentence.split():
      word_count = sentence.lower().split().count(word)
      dic_pos = getKey(dictionary, word)[0]
      vector.append((dic_pos,word_count))
    return vector
  
  s1 = "this is is a foo"
  s2 = "this is a a bar"
  s3 = "that 's a foobar"
  
  uniq = list(set(chain(" ".join([s1,s2,s3]).split()))) # is there simpler way for this?
  dictionary = {}
  for i in range(len(uniq)): # can this be done with dict(list_comprehension)?
    dictionary[i] = uniq[i]
  
  v1 = vectorize(s1, dictionary)
  v2 = vectorize(s2, dictionary)
  v3 = vectorize(s3, dictionary)
  
  print v1
  print v2
  print v3
  </code></pre>