User:Dave Young/Prototyping 2

From XPUB & Lens-Based wiki

"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?


# 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