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 |
||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[http://pzwart3.wdka.hro.nl/wiki/Markov_chants markov chains]] | |||
[[http://pzwart3.wdka.hro.nl/wiki/Poetry_generators poetry generators]] | |||
== the dog hacked my homework == | == the dog hacked my homework == | ||
text: he saw the cat before he saw the potato. | text: he saw the cat before he saw the potato. | ||
Line 8: | Line 12: | ||
#random library | #random library | ||
# | # manually building the dictionary with {} | ||
nextwords = {'he': ['saw'], 'saw': ['the'], 'the': ['cat','potato'], 'cat': ['before'], 'before': ['he'], 'potato': ['.']} | nextwords = {'he': ['saw'], 'saw': ['the'], 'the': ['cat','potato'], 'cat': ['before'], 'before': ['he'], 'potato': ['.']} | ||
Line 17: | Line 21: | ||
#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), | #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* | #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* | |||
Line 28: | Line 33: | ||
</source> | </source> | ||
text: she sells sea shells by the sea shore. | |||
<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> | |||
text: each step is moving, its moving me up, moving, its moving me up. | |||
<source lang = 'python'> | |||
import random | |||
nextwords = { | |||
'each': ['step'], | |||
'step': ['is'], | |||
'is': ['moving'], | |||
'moving': [',','me'], | |||
',': ['its', 'moving'], | |||
'its': ['moving'], | |||
'me': ['up'], | |||
'up': [',','.'] | |||
} | |||
word = 'each' | |||
while True: | |||
#simple loop | |||
print word | |||
# print random.choice(nextwords['she']) | |||
if word == '.': | |||
break | |||
word = random.choice(nextwords[word]) | |||
</source> | |||
results: |
Latest revision as of 16:14, 24 May 2011
[markov chains] [poetry generators]
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]])
text: she sells sea shells by the sea shore.
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])
text: each step is moving, its moving me up, moving, its moving me up.
import random
nextwords = {
'each': ['step'],
'step': ['is'],
'is': ['moving'],
'moving': [',','me'],
',': ['its', 'moving'],
'its': ['moving'],
'me': ['up'],
'up': [',','.']
}
word = 'each'
while True:
#simple loop
print word
# print random.choice(nextwords['she'])
if word == '.':
break
word = random.choice(nextwords[word])
results: