In [2]:
import tensorflow as tf
import numpy as np
sess = tf.InteractiveSession()

In [9]:
vocab = tf.contrib.lookup.string_to_index_table_from_file(
    vocabulary_file="vocab.txt", num_oov_buckets=0, default_value=1)

vocab_inv = tf.contrib.lookup.index_to_string_table_from_file(
    vocabulary_file="vocab.txt", default_value="<UNK>")

s = vocab.size()

tf.tables_initializer().run()
sess.run(tf.tables_initializer()) # make sure to call the table initializer


Tensor("hash_table_Size_6:0", shape=(), dtype=int64)
11

In [24]:
sentence = tf.constant('<PAD> the quick brown fox jumped over the lazy dog .'.split(), tf.string)
print sentence.eval()
idx = sess.run(vocab.lookup(sentence))

print idx
idxs = tf.constant(idx)
strings = sess.run(vocab_inv.lookup(idxs))
print strings


['<PAD>' 'the' 'quick' 'brown' 'fox' 'jumped' 'over' 'the' 'lazy' 'dog' '.']
[ 0 10  9  3  5  6  8 10  1  4  2]
['<PAD>' 'the' 'quick' 'brown' 'fox' 'jumped' 'over' 'the' '<UNK>' 'dog'
 '.']

In [ ]: