Tensorflow vocabularyprocessor

Nitin picture Nitin · Nov 17, 2016 · Viewed 14.2k times · Source

I am following the wildml blog on text classification using tensorflow. I am not able to understand the purpose of max_document_length in the code statement :

vocab_processor = learn.preprocessing.VocabularyProcessor(max_document_length)

Also how can i extract vocabulary from the vocab_processor

Answer

Nitin picture Nitin · Nov 22, 2016

I have figured out how to extract vocabulary from vocabularyprocessor object. This worked perfectly for me.

import numpy as np
from tensorflow.contrib import learn

x_text = ['This is a cat','This must be boy', 'This is a a dog']
max_document_length = max([len(x.split(" ")) for x in x_text])

## Create the vocabularyprocessor object, setting the max lengh of the documents.
vocab_processor = learn.preprocessing.VocabularyProcessor(max_document_length)

## Transform the documents using the vocabulary.
x = np.array(list(vocab_processor.fit_transform(x_text)))    

## Extract word:id mapping from the object.
vocab_dict = vocab_processor.vocabulary_._mapping

## Sort the vocabulary dictionary on the basis of values(id).
## Both statements perform same task.
#sorted_vocab = sorted(vocab_dict.items(), key=operator.itemgetter(1))
sorted_vocab = sorted(vocab_dict.items(), key = lambda x : x[1])

## Treat the id's as index into list and create a list of words in the ascending order of id's
## word with id i goes at index i of the list.
vocabulary = list(list(zip(*sorted_vocab))[0])

print(vocabulary)
print(x)