I want to read a file and create a dictionary with each word as a key and the word following it as the value.
For example if I have a file that contains:
'Cake is cake okay.'
The dictionary created should contain:
{'cake': ['is', 'okay'], 'is': ['cake'], 'okay': []}
So far I've managed to do the opposite with my code. I've updated the dictionary value with the previous word in the file. I'm not quite sure how to change it in order to have it work as intended.
def create_dict(file):
word_dict = {}
prev_word = ''
for line in file:
for word in line.lower().split():
clean_word = word.strip(string.punctuation)
if clean_word not in word_dict:
word_dict[clean_word] = []
word_dict[clean_word].append(prev_word)
prev_word = clean_word
Thank you guys for your help in advance!
Edit
Updated with progress:
def create_dict(file):
word_dict = {}
next_word = ''
for line in file:
formatted_line = line.lower().split()
for word in formatted_line:
clean_word = word.strip(string.punctuation)
if next_word != '':
if next_word not in word_dict:
word_dict[next_word] = []
if clean_word == '':
clean_word.
next_word = clean_word
return word_dict