User:Laura Macchini/prototyping/gameofthrones generator

From XPUB & Lens-Based wiki

The Code

import random

f = open ("cleangame.txt", "r" )
s = f.read()
words = s.split()
nextwords = {}

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())
newText = []
newText.append(first[0].capitalize())
current = first[0]

while len(newText) < 500:
	currentList = nextwords[current]
	next = random.choice(currentList)
	newText.append(next)
	current = next

game = ""
punctuation = ['.',';',',','?','!',':']
for w in newText:
	if w in punctuation:
		if w == ".":
			game += w + "\n"
		else:
			game += w
	else:
		game += " " + w
print game

The Result

 Lowering his broad grassy horse made anew.
 Nymeria.
 The wildlings and lowered his brother.
 My thanks, I feel it or corn, no more anxious.
 I Ser Willis mounted up, no doubt he killed him, echoed.
 And the young princes were true, she said of his furs and smoking star charts hung at him be curious look a stupid sister, the gate.
 As soon as his sister, working at the ship made her bare to Aegon high on them get a moment that understood that would do.
 This man strange and the empty.
 Another snared his up those daughters.
 Wine? I thought, and pleaded with a triangular shield, striped horses that it cling to the sharp enough coin.
 I were bannermen on it, with Lady Lysa Arryn was fighting with both you think I'd rather than it, and looked behind a steward told Arya liked the size of the sun.
 He wondered whether he smelled the armor were soft knock you? Ser Rodrik went through the poppy.
 She was lapping of your mild.
 Oh, the Wall behind him jump, could dance here.
 No, grievously wounded and honored for a black bog where the drape of shadowcats behind masks.
 My lord.
 Syrio, my hands bleeding from the smell, thanks I carry Drogo in the torch, Ser Alliser saw only seven hells was a nimbus of the flapping overhead from the brothel.
 I remember, she knew times, that I'd imagine that happen this time of children? Yes, three acres of sight Tyrion gazed at once.
 And now? Lord Vance are enough when Arya began to boiling on Stark.
 Go? the bowels, tucked them myself.
 The bogs of the silver.
 The world.
 Another voice was a hundred tourneys.
 The hospitality of blood flew to.
 They came whistling.
 She touched your head had closed his forked yellow pages of mail like a weight of the flush with an eyebrow.
 Too old gods are ofttimes wonder.
 The arm and the day with the words have some cook boy was running down his voice that halfbored, around, filled and Jhiqui would be all.
 Spit on all.
 Ser Rodrik or Syrio stepped up at his throat.
 Why shouldn't be better man was something here.
 He wrote one made him, bleed off gently.
 She still his eyes to be salt sea, I am the Night Watch.
 I counseled patience.
 Sister, Arya said they know what she has already dead? Mormont apologized to come to hear them privately, Tyrion waddled over a loss.
 Praying that it.
 All that much to return to be sure stroke.
 Oh, he answered.