I have a problem. I have the following code:
strCommand = "There is a 20% chance that I fail the test"
# Splits command in words
words = nltk.word_tokenize(strCommand)
#Add tags to words
word_tags = nltk.pos_tag(words)
if (word for (word, pos) in word_tags if(pos[:2] == 'CD')):
value = [word for (word, pos) in word_tags if(pos[:2] == 'CD')][0]
else:
value = ""
This code splits the sentence into words and uses NLTK tagging, to know what type of word each word is. Then I want to ask if there is a word from the type CD (Cardinal Digit), if so... Then set the value to that word. Now when there is a number in my sentence, the code works, but when there isn't a number in it, it crashes because the array is empty where I set the value. I thought the code couldn't get there if it didn't found a number, but apparently the if statement doesn't return true or false.
How can I make it, that it does return true or false, so I won't enter the if?