Tensorflow词汇处理器

Nit*_*tin 16 vocabulary tensorflow

我正在使用tensorflow关注文本分类的wildml博客.我无法理解代码语句中max_document_length的用途:

vocab_processor = learn.preprocessing.VocabularyProcessor(max_document_length)
Run Code Online (Sandbox Code Playgroud)

另外我如何从vocab_processor中提取词汇表

Nit*_*tin 27

我已经想出如何从vocabularyplrocessor对象中提取词汇表.这对我很有用.

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)
Run Code Online (Sandbox Code Playgroud)