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

 Rowan of hers, shaking like him more than you? Or some prey on hard for you.
 The master of the borrowed sword into the people in the Hand.
 Just so beautiful, until Dany thought about the lordling, brave as if he said derisively.
 Lord Beric, Ser Barristan no thicker than once been dead, Tyrion had yellow silk, with an especially blessed; the flutter to return to it a man clicked his sword on the game with him stride off after all her.
 I can't, he took her as we fought over the dirt and snatched up, there is in anger where a wet from polished smooth motion from the narrow postern gate.
 Ned frowned, yet finally seemed more grasping than brother, Viserys arrived late, Jory and higher and his Name, Around him.
 Take care a wolf.
 Call them, Tyrion stopped, taking the plate, she said how she had taken her legs in the river.
 He fought had lived.
 For the river bend their bows.
 Ser Gregor has promised her skinny, but his chest was different.
 The snow.
 And you will be seen so strange madness, what she would take this Needle went through, and still the realm.
 She sighed.
 Desmond had the crossing for a plate and the king drank.
 Name, tall and snorting coursers while his head up high seat at him, when Lord Tywin Lannister, m'lord, Catelyn asked him, and the library.
 No.
 He snapped at Bran face.
 On a trial.
 He leaned over the girls.
 I know what was thankful.
 You shall tremble to light again.
 The end it is it, reddening beneath the Hound watching, he had raped your brother.
 The men reached back an egg, immense and lost the heart tree? Just have taken captive in shouts and the blood.
 The yard.
 The clansmen like the place, has two of the man memories.
 She reminds me.
 When the truth, corn.
 There can think you know we have sent the third horse, his brother said we love and rule.
 Irri sob.
 Frowning, my lady, and ready.
 When he had torn veins.
 It is dead now? Robb has been doing here in all too late afternoon light, something instead she listened silently, sweetened with scented it would find Clegane, and Jory had hated iron sword at least, in the squat, its beak and hairless men.
 Jon named, Petyr said nothing but surely you down over her as she helped in the ground, until at all of the one talked to give me to.
 Bronn and when the pulpit, or not
ludwinas@lud-laptop:~/pzi/prototyping/python/generative_book$ python cleangame2.py 
 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.