User:Emily/Prototyping/Trimester 03/07

From XPUB & Lens-Based wiki

Hotpot-literature.jpg

This is a simple prototype used for creating a continuous passage based on existed text files, and I want to make it into CGI script which I hope to will help me to make Hybrid Video in the future.


python script
import re, sys, random
import argparse 

w = sys.argv[1]
files = sys.argv[2:]

def pick(word, filepaths):
	bag = []
	for n in filepaths:
		
		with open(n, "rU") as f:
			lines = f.readlines()
		
		for line in lines:
			for sentence in line.split("."):

				m = re.search(word, sentence, flags=re.I|re.M)
				if m != None:
					bag.append(sentence.strip()+".")
					
	s = random.choice(bag)
	print re.sub(word, word.upper(), s, flags=re.I|re.M)

pick(w, files)

pick("paper", files)