data = "So if you could just go ahead and pack up your stuff and move it down there, that would be terrific, OK?"words = data.split(' ')
word_you = filter(lambda x:x=='you',words)
word_if = filter(lambda x:x=='if',words)
print float(len(word_you))/len(words)
print float(len(word_if))/len(words)
练习:最大可能性
sample_memo = '''
Milt, we're gonna need to go ahead and move you downstairs into storage B. We have some new people coming in, and we need all thespace we can get. So if you could just go ahead and pack up your stuff and move it down there, that would be terrific, OK?
Oh, and remember: next Friday... is Hawaiian shirt day. So, you know, if you want to, go ahead and wear a Hawaiian shirt and jeans.
Oh, oh, and I almost forgot. Ahh, I'm also gonna need you to go ahead and come inon Sunday, too...
Hello Peter, whats happening? Ummm, I'm gonna need you to go ahead and come in tomorrow. So if you could be here around9that would be great, mmmk... oh oh! and I almost forgot ahh, I'm also gonna need you to go ahead and come inon Sunday too, kay. We ahh lost some people this week and ah, we sorta need to play catch up.
'''
def NextWordProbability(sampletext,word):
words = sampletext.split(' ')
res = {}
for index,itemin enumerate(words):
ifitem == word:
ifwords[index+1] notin res.keys():
res[words[index+1]] = 1else:
res[words[index+1]] += 1 return res