User:Amy Suo Wu/markov chains: Difference between revisions
Amy Suo Wu (talk | contribs) No edit summary |
Amy Suo Wu (talk | contribs) No edit summary |
||
Line 27: | Line 27: | ||
#dict([['two', 2], ['one', 1]]) | #dict([['two', 2], ['one', 1]]) | ||
</source> | |||
<source lang = 'python'> | |||
import random | |||
nextwords = { | |||
'she': ['sells'], | |||
'sells': ['sea'], | |||
'sea': ['shells','shore'], | |||
'shells': ['by'], | |||
'by': ['the'], | |||
'the': ['sea'], | |||
'shore': ['.'] | |||
} | |||
word = 'sea' | |||
while True: | |||
#simple loop | |||
print word | |||
# print random.choice(nextwords['she']) | |||
if word == '.': | |||
break | |||
word = random.choice(nextwords[word]) | |||
</source> | </source> |
Revision as of 14:45, 24 May 2011
the dog hacked my homework
text: he saw the cat before he saw the potato.
#!/usr/bin/env.python
import random
#random library
# manually building the dictionary with {}
nextwords = {'he': ['saw'], 'saw': ['the'], 'the': ['cat','potato'], 'cat': ['before'], 'before': ['he'], 'potato': ['.']}
# assigning the item variable from the dictionary. e.g 'he'
focus = 'he'
# call the next words for what ever is stored in the variable focus variable
print nextwords[focus]
#next step is to get him to pick a random word from the values list in the dictionary. build a loop, start anywhere and assign it to variable (focus),
#then search value accordingly and pick out a random word from the value. Then assign the value that he finds to the variable [focus]. jumping to
#the next and then calling it focus. *still missing: storing what is finds in a list*
#dict(one=1, two=2)
#dict({'one': 1, 'two': 2})
#dict(zip(('one', 'two'), (1, 2)))
#dict([['two', 2], ['one', 1]])
import random
nextwords = {
'she': ['sells'],
'sells': ['sea'],
'sea': ['shells','shore'],
'shells': ['by'],
'by': ['the'],
'the': ['sea'],
'shore': ['.']
}
word = 'sea'
while True:
#simple loop
print word
# print random.choice(nextwords['she'])
if word == '.':
break
word = random.choice(nextwords[word])