TensorFlow記錄(二)

1.tf.contrib.learn.VocabulayrProcessor()函數

這個函數輸入文本數據,以及指定句子包含單詞長度,可以將文本數據轉化爲索引形式,但是目前來看,好像不能按照詞頻大小進行排列,只是單純地按照單詞出現的順序創建詞典,按照詞典將句子處理成索引形式。

注意對象內置的幾個屬性,可以輸出詞典的內容等。

max_document_length = 4
a = ['this is a test', 'I have an apple and a pen', 'how are I']

vocab_processor = tf.contrib.learn.preprocessing.VocabularyProcessor(max_document_length)
res = vocab_processor.fit_transform(a)

print(type(res))
for i in res:
    print(i)

print('-'*30)
embedding_size = len(vocab_processor.vocabulary_)
print(embedding_size)

vocab_dict = vocab_processor.vocabulary_._mapping
print(type(vocab_dict))
print(vocab_dict)
sorted_dict = sorted(vocab_dict.items(), key = lambda x : x[1])
final_vocab = list(list(zip(*sorted_dict))[0])

print(final_vocab)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章