User:Dave Young/Prototyping 2
"Soldier, Soldier"
During last week's "Soldier, Soldier" marathon playlist that Michael put together, I was struck by how the words were occasionally swapped around a little bit, or a here and there a word might be replaced by another entirely. It being a folk song, it makes perfect sense: folk music is (or was at least) an oral tradition - meaning that songs would be passed down through the generations by word of mouth rather than "solid" artefacts such as manuscripts or sheet music. The result is an incredibly slow game of Chinese whispers, with the message suffering interpretive interference with each retelling. Curious about the abilities of Wordnet's abilities to replicate this effect, I wrote the below python script. In brief, it breaks the text down into individual words, and replaces each word for a random synonym from Wordnet's library.
First Generation
Soldier, soldier, bequeath you tie me With your musket, fife and drum? Oh how arse I tie such a moderately little girl When I wealthy_person no coat to put_option on? slay to the cut she answer spell As fast as she could test She make_for him gage the fine that was in_that_location today soldier, set it on Soldier, soldier, volition you get_hitched_with me With your musket, fife and drum? Oh how give_the_axe I splice such a passably little fille When I make no skid to set on? away to the cobbler she execute go As fast as she could scarper She add him dorsum the fine that was there now soldier, place them on Soldier, soldier, bequeath you splice me With your musket, fife and drum? Oh how keister I tie such a middling little girl When I have no chapeau to put on? dispatch to the hatter she answer conk_out As dissolute as she could prevail She bring him book_binding the fine that was thither now soldier, couch it on Soldier, soldier, will you marry me With your musket, fife and drum? Oh how can_buoy I wed such a moderately small daughter When I've a married_woman and youngster at home?
Second Generation
Soldier, soldier, leave you tie me With your musket, fife and drum? Oh how ass I bind such a jolly picayune female_child When I have no coat to put on? dispatch to the rationalise she answer piece As profligate as she could test She work him punt the fine that was in_that_location today soldier, coiffure it on Soldier, soldier, willing you marry me With your musket, fife and drum? Oh how give_notice I splice such a moderately small miss When I make no skid to go_under on? by to the cobbler she execute cristal As libertine as she could run_away She add_together him back the very_well that was thither now soldier, position them on Soldier, soldier, will you marry me With your musket, fife and drum? Oh how nates I tie such a passably niggling girl When I have no hat to arrange on? dispatch to the hatmaker she answer run_down As fast as she could prevail She bring him back the all_right that was thither now soldier, couch it on Soldier, soldier, will you espouse me With your musket, fife and drum? Oh how can I espouse such a moderately belittled girl When I've a wife and youngster at home?
Code
# Simulating oral traditions in folk music with python+wordnet
# Prototyping 2
#
# Depends: nltk > http://nltk.sourceforge.net/
# import external libs
from nltk.corpus import wordnet
import string
import random
from random import choice
soldier = open("soldier.txt")
# filter out some words
ignoreStrings = ["you", "me", "a", "Oh", "I", "no", "to", "on", "As", "as", "was", "it"]
# set up arrays
for lines in soldier:
line = lines.split(" ")
line = map(string.strip, line) # removes newline '\n' character
newLine = []
for word in line:
synsets = wordnet.synsets(word)
allSyns = [] # this list contains every synonym for 'word' from wordnet
syns = [] # this is an edited list of synonyms, each one only appears once
# make a list of synonyms in "synsets"
for synonym in synsets:
allSyns.append(synonym.lemma_names)
# if 'word' is on the ignore list, skip synonym checks
if word in ignoreStrings != 0:
syns = [word]
else:
# check that the synonym list isn't empty
if(len(allSyns) != 0):
# result is a single list containing all synonyms
result = sum(allSyns, [])
# remove elements that appear more than once
for syn in result:
if syn in allSyns != -1:
print ""
else:
syns.append(syn)
else:
syns = [word]
# make a random selection
newWord = choice(syns)
newLine.append(newWord)
theLine = ' '.join(newLine)
print theLine