User:Lieven Van Speybroeck/Prototyping/6-Markov: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "Still trying out some code... <source lang="python"> import random text = "This is a text that is a test to test the text in a test" words = text.split() nextwords = {} i = 0 ...")
 
mNo edit summary
 
(One intermediate revision by the same user not shown)
Line 9: Line 9:
nextwords = {}
nextwords = {}
i = 0
i = 0
b = 0


for w in words:
for w in words:
Line 26: Line 25:
i+=1
i+=1


newtext = []
focus = random.choice(nextwords.keys())
focus = random.choice(nextwords.keys())
newtext = []
for w in words:
for w in words:

Latest revision as of 10:51, 24 May 2011

Still trying out some code...

import random

text = "This is a text that is a test to test the text in a test"

words = text.split()
nextwords = {}
i = 0

for w in words:
	print w
	# for all items stored in words
	if i < len(words)-1:
		nextword = words[i+1]
		# if there's already a dictionary of nextwords for the word, append the new 'nextword' to that dictionary
		if w in nextwords:
			nextwords[w].append(nextword)
		# if there's no dictionary of nextwords for the word yet, make one and append the first 'nextword'	
		else:
			nextwords[w] = []
			nextwords[w].append(nextword)
		print nextwords[w]
		i+=1

newtext = []
focus = random.choice(nextwords.keys())
	
for w in words:
	randnextw = random.choice(nextwords[focus])
	newtext.append(randnextw)
	focus = randnextw
	
for w in newtext:
	print w,