Only change that needs to be made is that features
must be initialized to a dict
({}
) rather than a list
([]
) and then you could populate it’s contents.
The TypeError
was because word_features
is a list of strings which you were trying to index using a list and lists can’t have string indices.
features={}
for w in word_features:
features[w] = (w in words)
Here, the elements present in word_features
constitute the keys
of dictionary, features
holding boolean values, True
based on whether the same element appears in words
(which holds unique items due to calling of set()
) and False
for the vice-versa situation.