Restore original text from Keras’s imdb dataset

Question:

Restore original text from Keras’s imdb dataset

I want to restore imdb’s original text from Keras’s imdb dataset.

First, when I load Keras’s imdb dataset, it returned sequence of word index.

>>> (X_train, y_train), (X_test, y_test) = imdb.load_data()
>>> X_train[0]
[1, 14, 22, 16, 43, 530, 973, 1622, 1385, 65, 458, 4468, 66, 3941, 4, 173, 36, 256, 5, 25, 100, 43, 838, 112, 50, 670, 22665, 9, 35, 480, 284, 5, 150, 4, 172, 112, 167, 21631, 336, 385, 39, 4, 172, 4536, 1111, 17, 546, 38, 13, 447, 4, 192, 50, 16, 6, 147, 2025, 19, 14, 22, 4, 1920, 4613, 469, 4, 22, 71, 87, 12, 16, 43, 530, 38, 76, 15, 13, 1247, 4, 22, 17, 515, 17, 12, 16, 626, 18, 19193, 5, 62, 386, 12, 8, 316, 8, 106, 5, 4, 2223, 5244, 16, 480, 66, 3785, 33, 4, 130, 12, 16, 38, 619, 5, 25, 124, 51, 36, 135, 48, 25, 1415, 33, 6, 22, 12, 215, 28, 77, 52, 5, 14, 407, 16, 82, 10311, 8, 4, 107, 117, 5952, 15, 256, 4, 31050, 7, 3766, 5, 723, 36, 71, 43, 530, 476, 26, 400, 317, 46, 7, 4, 12118, 1029, 13, 104, 88, 4, 381, 15, 297, 98, 32, 2071, 56, 26, 141, 6, 194, 7486, 18, 4, 226, 22, 21, 134, 476, 26, 480, 5, 144, 30, 5535, 18, 51, 36, 28, 224, 92, 25, 104, 4, 226, 65, 16, 38, 1334, 88, 12, 16, 283, 5, 16, 4472, 113, 103, 32, 15, 16, 5345, 19, 178, 32]

I found imdb.get_word_index method(), it returns word index dictionary like {‘create’: 984, ‘make’: 94,…}. For converting, I create index word dictionary.

>>> word_index = imdb.get_word_index()
>>> index_word = {v:k for k,v in word_index.items()}

Then, I tried to restore original text like following.

>>> ' '.join(index_word.get(w) for w in X_train[5])
"the effort still been that usually makes for of finished sucking ended cbc's an because before if just though something know novel female i i slowly lot of above freshened with connect in of script their that out end his deceptively i i"

I’m not good at English, but I know this sentence is something strange.

Why is this happened? How can I restore original text?

Asked By: Hironsan

||

Answers:

This happened because of a basic NLP data preparation. Loads of the so called stop words were removed from text in order to make learning feasible. Usually – also the most of puntuation and less frequent words are removed from text during preprocessing. I think that the only way to restore original text is to find the most matching texts at IMDB using e.g. a Google’s browser API.

Answered By: Marcin Możejko

You can get the original dataset without stop words removed using get_file from keras.utils.data_utils:

path = get_file('imdb_full.pkl',
               origin='https://s3.amazonaws.com/text-datasets/imdb_full.pkl',
                md5_hash='d091312047c43cf9e4e38fef92437263')
f = open(path, 'rb')
(training_data, training_labels), (test_data, test_labels) = pickle.load(f)

Credit – Jeremy Howards fast.ai course lesson 5

Answered By: kmak

Your example is coming out as gibberish, it’s much worse than just some missing stop words.

If you re-read the docs for the start_char, oov_char, and index_from parameters of the [keras.datasets.imdb.load_data](https://keras.io/datasets/#imdb-movie-reviews-sentiment-classification
) method they explain what is happening:

start_char: int. The start of a sequence will be marked with this character. Set to 1 because 0 is usually the padding character.

oov_char: int. words that were cut out because of the num_words or skip_top limit will be replaced with this character.

index_from: int. Index actual words with this index and higher.

That dictionary you inverted assumes the word indices start from 1.

But the indices returned my keras have <START> and <UNKNOWN> as indexes 1 and 2. (And it assumes you will use 0 for <PADDING>).

This works for me:

import keras
NUM_WORDS=1000 # only use top 1000 words
INDEX_FROM=3   # word index offset

train,test = keras.datasets.imdb.load_data(num_words=NUM_WORDS, index_from=INDEX_FROM)
train_x,train_y = train
test_x,test_y = test

word_to_id = keras.datasets.imdb.get_word_index()
word_to_id = {k:(v+INDEX_FROM) for k,v in word_to_id.items()}
word_to_id["<PAD>"] = 0
word_to_id["<START>"] = 1
word_to_id["<UNK>"] = 2
word_to_id["<UNUSED>"] = 3

id_to_word = {value:key for key,value in word_to_id.items()}
print(' '.join(id_to_word[id] for id in train_x[0] ))

The punctuation is missing, but that’s all:

"<START> this film was just brilliant casting <UNK> <UNK> story
 direction <UNK> really <UNK> the part they played and you could just
 imagine being there robert <UNK> is an amazing actor ..."
Answered By: mdaoust

The indices are offset by 3 because 0, 1 and 2 are reserved indices for “padding”, “start of sequence” and “unknown”. The following should work.

imdb = tf.keras.datasets.imdb
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)

word_index = imdb.get_word_index()
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])

review = [reverse_word_index.get(i-3, "?") for i in train_data[0]]
Answered By: Andreas Gompos

This works for me:

word_index = imdb.get_word_index()                                    
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])            
decoded_review = ' '.join([reverse_word_index.get(i - 3, "") for i in train_data[0]])
Answered By: RevolverRakk

This encoding will work along with the labels:

from keras.datasets import imdb
(x_train,y_train),(x_test,y_test) = imdb.load_data()
word_index = imdb.get_word_index() # get {word : index}
index_word = {v : k for k,v in word_index.items()} # get {index : word}

index = 1
print(" ".join([index_word[idx] for idx in x_train[index]]))
print("positve" if y_train[index]==1 else "negetive")

Upvote if helps. 🙂

Answered By: Hayat

To get an equivalent array of all the reviews:

def decode_imdb_reviews(text_data):
    result = [0 for x in range(len(text_data))]
    word_index = imdb.get_word_index()
    reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
    for review in range(0,len(text_data)):
        for index in enumerate(text_data[review]):
            decoded_review = ' '.join([reverse_word_index.get(index - 3, '#') for index in text_data[review]])
        result[review] = decoded_review
    return result

text_data = []
text_data = decode_imdb_reviews(train_data)
Answered By: coolhand
from keras.datasets import imdb
NUM_WORDS=1000 # only use top 1000 words

(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=NUM_WORDS)

Get the index details:

word_to_id = keras.datasets.imdb.get_word_index()

Build the Key-Value pair:

id_to_word = {value:key for key,value in word_to_id.items()}
print(' '.join(id_to_word[id] for id in x_train[0] ))
Answered By: Boopal Singh

Try below code. this code worked.

# load dataset 
from tensorflow.keras.datasets import imdb 
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=500)


# vec 2 num 
imdb_vec2num = imdb.get_word_index(path="imdb_word_index.json")
# num 2 vec 
imdb_num2vec = {value+3:key for key, value in imdb_vec2num.items()}
imdb_num2vec[0] = "<PAD>"
imdb_num2vec[1] = "<START>"
imdb_num2vec[2] = "<UNK>"
imdb_num2vec[3] = "<UNUSED>"

# index-word table
imdb_num2vec = {value:key for key, value in imdb_index.items()}

# change encoded sentences to sentences
def restore_sentences(num2word, encoded_sentences): 
    sentences = []
    for encoded_sentence in encoded_sentences:
        sentences.append([num2word[ele] for ele in encoded_sentence])
    return sentences

# example 
sentences = restore_sentences(imdb_num2vec, x_train)
Answered By: Soulduck