You need to explicitly split the string into spaces:
def word_feats(words):
return dict([(word, True) for word in words.split()])
This uses str.split()no arguments, separating spaces of arbitrary width (including tabs and line separators). A string is a sequence of individual characters otherwise, and a direct iteration will really just intersect each character.
, , , , , . , ? , , , , ? Etc.
, , True, dict.fromkeys():
def word_feats(words):
return dict.fromkeys(words.split(), True)
:
>>> def word_feats(words):
... return dict.fromkeys(words.split(), True)
...
>>> print(word_feats("I love this sandwich."))
{'I': True, 'this': True, 'love': True, 'sandwich.': True}