User:Laura Macchini/prototyping/Soldier Soldier will you generate me

From XPUB & Lens-Based wiki

The Code

import random
 
f = open ("soldier.txt", "r" )
s = f.read()
words = s.split()
nextwords = {}
sCounter = 0
limit = 5

for (i,w) in enumerate(words):
	if i < len(words)-1:
		nword = words[i+1]
		if w in nextwords:
			nextwords[w].append(nword)
		else:
			nextwords[w] = []
			nextwords[w].append(nword)

first = random.choice(nextwords.items())

newList = []
newList.append(first[0].capitalize())
current = first[0]
 
while sCounter < limit:
	currentList = nextwords[current]
	next = random.choice(currentList)
	if next == ".":
			sCounter += 1
	newList.append(next)
	current = next

text = ""
punctuation = ['.',';',',','?','!',':']

for w in newList:
	if w in punctuation:
		if w == ".":
			text += w + "\n"
		else:
			text += w
	else:
		text += " " + w
print text

The Result

 I marry such a wife and drum? Oh how can I marry me With your musket, fife and drum? Oh how can I have no shoes to the cobbler she did go As fast as she could run She brought him back the finest that was there Now soldier, will you marry me With your musket, fife and child at home? Oh how can I marry me With your musket, fife and drum? Off to put on.
 Soldier, soldier, will you marry such a pretty little girl When I have no shoes to the finest that was there Now soldier, put them on? Oh how can I marry me With your musket, fife and child at home? Oh how can I marry me With your musket, fife and drum? Off to put it on? Oh how can I have no shoes to the finest that was there Now soldier, will you marry such a pretty little girl When I marry such a wife and child at home? Off to put them on.
 Soldier, soldier, will you marry me With your musket, fife and drum? Oh how can I marry me With your musket, fife and drum? Oh how can I have no hat to the finest that was there Now soldier, put on? Oh how can I marry me With your musket, fife and child at home? Off to put it on.
 Have no shoes to the tailor she did go As fast as she did go As fast as she did go As fast as she did go As fast as she could run She brought him back the hatter she could run She brought him back the hatter she did go As fast as she did go As fast as she did go As fast as she did go As fast as she could run She brought him back the hatter she could run She brought him back the cobbler she did go As fast as she did go As fast as she could run She brought him back the hatter she did go As fast as she could run She brought him back the hatter she could run She brought him back the finest that was there Now soldier, will you marry such a pretty little girl When I have no hat to the finest that was there Now soldier, will you marry me With your musket, fife and drum? Off to put on? Off to the finest that was there Now soldier, will you marry me With your musket, fife and drum? Oh how can I have no hat to the cobbler she did go As fast as she could run She brought him back the finest that was there Now soldier, put on? Oh how can I have no hat to the finest that was there Now soldier, put on? Oh how can I marry such a pretty little girl When I have no coat to put it on? Oh how can I marry such a pretty little girl When I marry such a pretty little girl When I marry me With your musket, fife and drum? Off to the hatter she could run She brought him back the tailor she did go As fast as she did go As fast as she did go As fast as she could run She brought him back the finest that was there Now soldier, put on? Off to the finest that was there Now soldier, will you marry such a pretty little girl When I have no shoes to the finest that was there Now soldier, will you marry such a pretty little girl When I've a pretty little girl When I have no hat to the finest that was there Now soldier, put on? Off to the finest that was there Now soldier, will you marry me With your musket, fife and drum? Off to the finest that was there Now soldier, will you marry me With your musket, fife and drum? Off to put it on? Off to the finest that was there Now soldier, put them on.
 Soldier, soldier, will you marry me With your musket, fife and drum? Off to put them on? Off to put on? Off to the finest that was there Now soldier, will you marry me With your musket, fife and drum? Oh how can I marry me With your musket, fife and drum? Oh how can I marry me With your musket, fife and drum? Oh how can I have no coat to the finest that was there Now soldier, will you marry such a pretty little girl When I marry such a wife and drum? Oh how can I have no shoes to put on.
 Soldier, soldier, will you marry me With your musket, fife and drum? Oh how can I have no coat to the tailor she did go As fast as she did go As fast as she could run She brought him back the cobbler she could run She brought him back the hatter she did go As fast as she could run She brought him back the finest that was there Now soldier, put on? Off to the cobbler she could run She brought him back the tailor she did go As fast as she could run She brought him back the finest that was there Now soldier, will you marry me With your musket, fife and drum? Oh how can I marry such a wife and drum? Off to put it on? Oh how can I have no coat to the finest that was there Now soldier, put it on? Off to put it on? Oh how can I marry me With your musket, fife and child at home? Oh how can I have no shoes to the hatter she did go As fast as she could run She brought him back the tailor she did go As fast as she did go As fast as she could run She brought him back the finest that was there Now soldier, put it on.