For this week’s assignment to redo an old one with functions and modules, I decided to go with my midterm. I found this exercise to be trickier than I thought it would be, probably because I had to organize my code in a way that I had not really considered when I first wrote it. Anyway, here it is:
Last year, I learned that Petfinder has an API, which was a pretty amazing discovery. For this project, I used its Get Random Pet function, which randomly picks an adoptable pet from its database.
From there, I extracted the pet’s name and description from the JSON file.
I then took that pet’s name, and used the New York Times article search API to find articles with that name as a keyword. I then took the “lead paragraph” from the first article chosen.
I used the NLTK library to split sentences into lines, and then made a little biography poem with alternating lines from each.
I’m pretty happy with the results, though if I had more time I would clean up the code so that it works more consistently. (For example, it currently only really works if the pet name is just one word.)
For my midterm, I made a “new” poetic form I’m calling “Please Visit The Forum.”
There are a lot of forums on the internet, covering pretty much all imaginable subjects for all sorts of niche interests and communities. I wanted to generate new weirdo community forums, each including a URL, moderator name, contact info and some popular topics.
I downloaded a bunch of JSON files from Corpora, and then picked two of those files each time. I then mashed the words together in different ways for each of the categories.
While I found Corpora delightful and very useful, I could only figure out how to make this work for a limited group of lists that were formatted in the same way (so that I could consistently grab the values from a specific key in each list).
My goal was to capture the feeling of surprise and intrigue that you get from discovering a niche community on the internet that you didn’t know about before, and some of the results of my program I thought were pretty effective.
I think this is this could be more effective and interesting with a larger data set, perhaps with more live data or something more generative. Hopefully I can explore that with later projects.
My idea for this assignment was very stupid. It came from the fact that when typing “python” in the command line, I regularly accidentally type it as “pythong” instead.
So I decided to make a mashup of Sisqo’s 1999 classic, Thong Song, and some documentation from python.org.
I knew I wanted it to maintain the structure of the Thong Song, but with the flavor of Python documentation sprinkled throughout.
What I ended up doing was making two source files, thong-song.txt (which had the lyrics) and python.txt (which included a chunk of documentation text). I then replaced one random word in each song line with a random word from the Python text.
I quickly ran into the problem of the program picking words from python.txt that were not very useful to me, like “the” or “it” or “a.” So to solve this problem, I made another file with a list of “boring words,” and I told it to only select a word from python.txt if it was not also in boring.txt:
use_these = [i for i in line.split() if not i.lower() in boring_words]
for individual_word in use_these:
And of course, I also changed every instance of the word “thong” or “Python” to the word “Pythong.”
When I run the program, it’s different each time with varying degrees of success. I think it works best when the song still generally rhymes (so in most cases, if the last word on each line is not changed). I also experimented with trying to get it to only replace each word with a word that began with the same letter, but I couldn’t quite get it to work in time, and I’m not sure it would actually make much of an improvement. Anyway, a couple ideas for building this program further.
Code and text files below, including a full sample of the results:
My first attempt at making digital poetry was interesting and felt quite different from the other types of programming work that I’ve done so far. For one thing, my Python code was very simple. I wonder if it was too simple?
I guess it’s because I tried a lot of different things that I ended up deleting in my code. This feels more like traditional writing and editing rather than programming, in which we often show our work by the volume of code that we produce.
The text I used was Sarah Palin’s recent speech endorsing Donald Trump. The source was written in paragraphs, and I tried to figure out how to computationally organize it so that it would be one sentence per line. But I didn’t figure it out so I ended up doing it manually, which I know defeats the purpose of making computers do mundane tasks for you, but hopefully it’s something I’ll learn for the future.
I wasn’t sure exactly what I wanted the result to be, except that it would take this rather long text and make it into something more like a short poem.
My Python code (below) basically replaces “America” and other forms of it to “Hogwarts,” and “Trump” to “Voldemort.” (Silly, I know.) It then finds all the “Voldemorts” in the text, as well as all the first commas in each line, and then slices from after the first comma to the “Voldemort” in the line. The substring slice looked like this:
substring = line[commas+1:voldemorts+10]
This cut it from immediately after the comma to the Voldemort, plus the “Voldemort” itself and whatever punctuation might be following it.
I realized that I could have done commas+2 in order to get rid of the space at the beginning of each line, but I think I prefer the way it looks with the space so I made it commas+1.
The result, run simply through the command line looks like this:
I decided to try using UNIX’s sort command, so I did this:
$ python munge.py<palin.txt | sort
This game me the final result, shown at the top of this post.