Search code examples
pythonnonetypesentence

Sentence Generation: TypeError: 'NoneType' object is not subscriptable


i have a code for generating sentences given a word after sentence_start

the code that encountered an error:

def generate_sentences(model, n, index_to_word, word_to_index):
    for i in range(n):
        sent = None
        while not sent:
            for i in range(len(arr)):
                sent = generate_sentence(arr[i], model, index_to_word, word_to_index)
                # print (arr[i])
                print_sentence(sent, index_to_word)
        print("\n")

here is the called function:

def generate_sentence(anot, model, index_to_word, word_to_index, min_length=5):
    # We start the sentence with the start token
    new_sentence = [word_to_index[SENTENCE_START_TOKEN], word_to_index[anot]]
    # Repeat until we get an end token
    while not new_sentence[-1] == word_to_index[SENTENCE_END_TOKEN]:
            next_word_probs = model.predict(new_sentence)[-1]
            samples = np.random.multinomial(1, next_word_probs)
            sampled_word = np.argmax(samples)
            new_sentence.append(sampled_word)
            # Seomtimes we get stuck if the sentence becomes too long, e.g. "........" :(
            # And: We don't want sentences with UNKNOWN_TOKEN's
            if len(new_sentence) > 100 or sampled_word == word_to_index[UNKNOWN_TOKEN]:
                return None
    if len(new_sentence) < min_length:
        return None
    return new_sentence

def print_sentence(s, index_to_word):
    sentence_str = [index_to_word[x] for x in s[1:-1]]
    print(" ".join(sentence_str))
    sys.stdout.flush()

and here is the traceback:

Traceback (most recent call last):

  File "<ipython-input-10-b9a0a1f5bd04>", line 1, in <module>
    runfile('C:/Users/cerdas/Documents/bil/Code_Latihan/rnn-tutorial-gru-lstm-master/train.py', wdir='C:/Users/cerdas/Documents/bil/Code_Latihan/rnn-tutorial-gru-lstm-master')

  File "C:\Users\cerdas\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "C:\Users\cerdas\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/cerdas/Documents/bil/Code_Latihan/rnn-tutorial-gru-lstm-master/train.py", line 53, in <module>
    generate_sentences(model, 20, index_to_word, word_to_index)

  File "C:\Users\cerdas\Documents\bil\Code_Latihan\rnn-tutorial-gru-lstm-master\utils.py", line 190, in generate_sentences
    print_sentence(sent, index_to_word)

  File "C:\Users\cerdas\Documents\bil\Code_Latihan\rnn-tutorial-gru-lstm-master\utils.py", line 179, in print_sentence
    sentence_str = [index_to_word[x] for x in s[1:-1]]

TypeError: 'NoneType' object is not subscriptable

i suspect the error caused by the function print_sentence(sent, index_to_word) i was trying to edit the indentation for exclude the print_sentence function from looping.

but the output only read the last element of array arr


Solution

  • What the error message

    TypeError: 'NoneType' object is not subscriptable
    

    means, is that your line

    sentence_str = [index_to_word[x] for x in s[1:-1]]
    

    is trying to access index_to_word[x] and s[1:-1] though one of them appear to be None

    Seeing that you didn't provide the information what generate_sentences is being called with, we can only speculate what causes this.

    I would recommend you add an if clause, to make sure you don't try to print a sentence, which is None:

    print_sentence(sent, index_to_word)
    

    should be replaced with

    if sent is not None:
        print_sentence(sent, index_to_word)