First you break the phrase into words:
words = texto.lower().split()
With this list of words, simply iterate over it by attaching the next word. So you don’t have much work, you can use the works collections.defaultdict
, that will create a list dictionary for Windows. The code would look like this:
import collections
adjacente = collections.defaultdict(list)
for (i, word) in enumerate(words[:-1]):
next_word = words[i + 1]
adjacente[word].append(next_word)
Remembering that we do -1 to pick up n - 1 words, since the last word has no words adjacent to it.
And the result:
adjacente
defaultdict(list,
{'But': ['at'],
'We': ['are', 'are'],
'are': ['not', 'not', 'not'],
'at': ['least'],
'be': ['We', 'But'],
'least': ['we'],
'need': ['to'],
'not': ['what', 'what', 'what'],
'should': ['be'],
'to': ['be', 'be'],
'used': ['to'],
'we': ['should', 'need', 'are', 'used'],
'what': ['we', 'we', 'we']})
In case you wanted the words to be unique, change the defaultdict
of list
for set
and instead of append, use update by passing an array with next_word.