I just finished working on a coding challenge for learning Python 2.7. Essentially, I'm a function that if it is fed a string such as:
"The man drank the drink and ate the bread and forgot the drink"
I get in return:
{'and': {'ate': 1, 'forgot': 1},
'ate': {'the': 1},
'bread': {'and': 1},
'drank': {'the': 1},
'drink': {'and': 1},
'forgot': {'the': 1},
'man': {'drank': 1},
'the': {'bread': 1, 'drink': 2, 'man': 1}}
In other words, each word (that has a word following it) is a key, and the value is a dictionary of words that come right after, and the number of times that happens. (drink
follows the
twice in the string, hence the 2
value in its dictionary.
Here's the function I wrote to accomplish this end:
def word_counts(f):
#Function to remove punctuation, change to lowercase, etc. from incoming string
def string_clean(file_content):
fc_new = "".join([i.lower() for i in file_content if i not in string.punctuation])
fc_new = fc_new.split()
return fc_new
f = string_clean(f)
unique_f = f[:]
#For next part of function, get the unique words found in string.
#We'll then run each through the string and find words that follow
#Pop() the last word, since nothing follows it
unique_f = list(set(unique_f.pop()))
result = {}
for word in unique_f:
next_word_keeper = {}
for _ in range(0, len(f)-1):
if word == f[_]:
if f[_+1] in next_word_keeper.keys():
next_word_keeper[f[_+1]] = next_word_keeper[f[_+1]] + 1
else:
next_word_keeper[f[_+1]] = 1
result[word] = next_word_keeper
return result
Feedback appreciated, thanks.