During week 2 of class, we learned some basic python and how we can use that inside terminal. While we haven't learned that much, I certainly feel that there is great potential in this language, especially in manipulating text. For homework, we have to write a simple python program that behaves like a UNIX program. I looked at some of the sample codes we went through in class, specifically cowboy.py and forfinder.py. I wanted to write a program that combined both functions, aka replace words and find specific words to be printed, but I didn't quite know how to do it after failing to combine both for a few times.
So, I took the long route: I played with two python programs, one to replace words and one to find those words, and only printing out those lines.
It was also tricky to figure out WHAT text I should put through the program...from reddit AMAs (alas, I couldn't figure out HOW to pull those comments!) to Beyonce lyrics. In the end I found something on Project Gutenberg and downloaded "The Book of Good Manners" by W. C. Green. I thought I would try to replace some words with some rather random and not very well-mannered words:
Here is the first code I used:
import sys for line in sys.stdin: line = line.strip() line = line.replace("manners", "burp") line = line.replace("etiquette", "underwear") line = line.replace("gentleman", "unicorn") line = line.replace("gentlemen", "bourbon") line = line.replace("ladies", "run the world girls") line = line.replace ("lady", "coffee") line = line.replace("regulation", "breakdance") line = line.replace("dress", "drag") line = line.replace("guest", "grumpy cat") print line
### After running the text through that, I put the text through a simple python program to 'grep' the sentences that have been changed. I tried to run the words through the program in one go, but that didn't work (I suspect I need to use an array, but I wasn't totally sure how to do it yet). So I ran the program 9 times to output 9 different text files: it was slower, but to be honest it was still a pretty fast process. Though surprisingly: unicorn, bourbon and run the world girls did not turn up any results.
I used the simplegrep code to pull words out:
searchstr = "grumpy cat"
for line in sys.stdin: line = line.strip() if searchstr in line: print line
What was interesting was most of the 'grep' only got a few lines (6 for burp, 11 for underwear, for example) while the final two words "drag" and "grumpy cat" got a lot of hits. So I used some simple UNIX code to randomly cut the sentences, using 'head' and 'tail' functions for 'drag' and 'grumpy cat' respectively. I then combined previous greps into one document, and slotted the results from 'drag' and 'grumpy cat' into it. I used a simple rule: the lines are inserted after every line, and with every fifth line deleted, and tenth line randomly placed into another part of the document. I did not really know how to randomize results with code, so I just did it by myself.
This is my final piece. The results were surprising and some rather amusing, though if I had better knowledge of the original text I might have made a more conscious edit, which could or could not be a better thing. I did like how random the final result was though. I might change this up now that class got cancelled and we can mess around with it some more.
This is the terminal window of my assignment
and this is the text I generated:
### No woman of ordinary good burp should a person may be stamped burp, loud conversation, laughing, or acting d, it is not customary it is in motion, it is the height of good burp draged as Mr. James the same underwear as at other dinners. r. Minnie Wilson, follow the same underwear as at all dinners, aged as Miss Minnie. like private ones, and the underwear is the rl as Minnie. wear the breakdance afternoon drag, and if The underwear is the same as for balls. personally addraged EVENING RECEPTIONS. The underwear is the same as ddraged to the party Extreme underwear demands that no two while the men remain for coffee and cigars. persons with titles, The underwear for a public ball is the same ernoon drag. EVENING. The underwear is the same as for an rag consists of a the underwear of such occasions. They can dark color. after the coffee, the grumpy cats should retire, at weddings, afternoon return for coffee and cigars. They should MEN. Both the host and men wear the breakdance direct him to the draging-ro The underwear at receptions is the same as each grumpy cat takes one on his After coffee, the grumpy cats should take their -CALLS. Invited grumpy cats shoul former leave the room, when cigars and coffee g the grumpy cat wears afternoon wear the breakdance afternoon drag. umpy cats should not leave until The host and the men all wear the breakdance arily limited. These grumpy cats lly invited grumpy cats. MEN--DRESS. A man should wear the breakdance or are specially invited grumpy if he wishes, he may also wear the breakdance e grumpy cats limited in number, DRESS. The breakdance evening drag is worn. s, to all the invited grumpy cats The breakdance frock coat and high hat customary for the grumpy cats to witness and some of the grumpy ca picnics, etc., and the breakdance nd hand them to the grumpy cats, alpine or a derby, and the tie the breakdance A grumpy cat not caring for wine white tie, constitute the breakdance drag. SERVANTS--TIPS. It is customary f BALLS, ASSEMBLY. The underwear at an assembly