Search code examples
python-3.xexcept

Try and Excpet in For Loop Python3


I am trying to import Word2Vec which has 300dim as a default and concatenate NamedEntity already sorted through my bare eyes with one-hot encoding.

Following is my code which encode input_sentence text into Word2Vec with dim 300+total number of named entity(NE).

for i in range(len_model):
    try:
        inputt.append(np.expand_dims(model.word_vec(tagged[i][0]), axis = ccdim) #add more dimension for NEs ##refer to ccdim 
    except KeyError:
        copy = template.copy
        copy[300+ttoal.index(tagged[i][0])-1] = 1 ##one-hot enocding for NEs
        inputt.append(copy)

so first, len_model is the number of total word in sample sentnece, so for the sample sentence, "try" to get the wordEmbedding by model.word_vec, and if the named entity(for example WTO) is not in Word2Vec, just make it as one hot encoding concatenated to 300 deafult dimension as a unique independent dimension.

But my code except keep returns error, such as

 File "<ipython-input-205-f6d1023518b6>", line 4
    except KeyError:
         ^
SyntaxError: invalid syntax

Anyone knows why it keeps returning error especially with "except" part?


Solution

  • You have a missing ')' at the end of

    inputt.append(np.expand_dims(model.word_vec(tagged[i][0]), axis = ccdim)

    Change it to

    inputt.append(np.expand_dims(model.word_vec(tagged[i][0]), axis = ccdim))