Created
November 2, 2015 07:05
-
-
Save dmadisetti/3c356dc114016fde5613 to your computer and use it in GitHub Desktop.
Markov-lyrics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| file = open('beattles', 'r') | |
| import random, string | |
| global pointer, M, states | |
| M = {} | |
| states = {} | |
| # Class to hold 'States' | |
| class State: | |
| # Keep paths with obj scope | |
| def __init__(self, value): | |
| self.value = value | |
| self.paths = [] | |
| # Path class for tidiness | |
| class Path(): | |
| def __init__(self, node, weight): | |
| self.weight = weight | |
| self.node = node | |
| # Create connection | |
| def newPath(self, node, weight): | |
| self.paths.append(self.Path(node,weight)) | |
| # Move from one state to another based on probability | |
| def move(self): | |
| global pointer | |
| r = random.random() | |
| runningWeight = 0 | |
| for path in self.paths: | |
| runningWeight += path.weight | |
| if runningWeight >= r: | |
| pointer = path.node | |
| return | |
| # Create states/data from text file | |
| def init(): | |
| global M, states | |
| book = string.lower(file.read()).split() | |
| # Tabulate subsequent word occurances | |
| for i in range(1,len(book)): | |
| if book[i-1] in M: | |
| if book[i] in M[book[i-1]]: | |
| M[book[i-1]][book[i]] += 1 | |
| else: | |
| M[book[i-1]][book[i]] = 1 | |
| else: | |
| M[book[i-1]] = {} | |
| M[book[i-1]][book[i]] = 1 | |
| # Create "States" | |
| for key in M: | |
| # Sum to find total | |
| s = 0.0 | |
| for following in M[key]: | |
| s += M[key][following] | |
| if s == 0: | |
| continue | |
| if key not in states: | |
| states[key] = State(key) | |
| # Create Chain | |
| for following in M[key]: | |
| if following not in states: | |
| states[following] = State(following) | |
| states[key].newPath(states[following], M[key][following]/s) | |
| # change back to list | |
| states = [states[x] for x in states] | |
| # "Walk" along chain | |
| def run(): | |
| global pointer, M, states | |
| random.seed(27) # I like 27. Want this to be determininistic | |
| # Let's get this started | |
| words = 1000 | |
| pointer = None | |
| result = ["~"] | |
| while words > 0: | |
| while pointer == None: | |
| # Start with some random word. | |
| pointer = states[int(random.random() * len(states))] | |
| if len(pointer.paths) == 0: | |
| pointer = None | |
| # Some capitilization and append | |
| result.append([pointer.value,pointer.value.capitalize()][result[-1][-1] in [".","?","!","~"]]) | |
| words -= 1 | |
| pointer.move() | |
| # Let's set our results | |
| print " ".join(result) | |
| def main(): | |
| init() | |
| run() | |
| # Set er off | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ~ Fierce, his room only a legend of love, julia you how to be my rock and i will follow you please, please remember you like a cha-cha-cha-chance-birthday i go back on make me i can’t forget the world was of piggies living piggy lives you are boys back again. Yesterday, love away hey you’ve got a party yes i will never be for me what i find you near you told you treat her away so sincere. Treat her tonight, and i ain’t no fool of chairman mao you belong ’cause you broke the world was searching, so many tears i have you clowns let me dizzy, miss lizzie, when i’m telling you, but, as it in their piggy lives you please, please remember me mad is set on but you, but i’m feeling down fast but i’m by the table just the market place gee it’s so sincere. Treat me by me. I’ve just a-rockin’ and i can tell me and her right, my friend. I held you so sincere. Treat her name but you may be near you ain’t dead already ooh girl you can, i’m lonely wanna die if you the like you, so, come on make me there’ll be a joy come and i will treat me no joke, it’s so tired, i’m coming down those blues but now rocky raccoon he said that girl, you were in and why don’t take her what we all right all american bullet-headed saxon mother’s son. Come out for this moment to play around round look you’re going to do do endear you ain’t no next room only to see them all want to marry you. Come home. Oh honey pie hey, bungalow bill? He said that you done. Sexy sadie. However big star out and i don’t you do wish you think twice, she was i will be free when i was in the moon begins to think it’s true, ’cause i don’t forget the bottom i will treat you come and thin she don’t care, my bone i wouldn’t let me too much do do what have to ride, she’s got a young rocky said, doc as i go carrying pictures of kircaldy always be back and i get yours yet however big time i’m lonely lifetime if you say to me martha my baby cry baby cry baby cry. We can you don’t treat her man that’s sleeping sand, silent cloud, touch me ’cause it’s no one more time, how could i think i’m not so fierce, his elephant and want some fun-take obladi oblada life is bringing her tonight, she’s going to me like you your love you. Yes i like you. Good time i’m lonely wanna die if i like you your know why i’m so insecure, i wouldn’t let me back again. Oh, i feel it easy to a few. Nobody told me, where you know we all the bent backed tulips to make ends meet-yeah looking through thick of music as can but daniel was in from today, well, i’ll close mine good night before? Was in the u.s.s.r. Dear hold you on tell your mother sigh she’s old enough to lose that girl. I’ll close mine good whacking. Everywhere good time or place molly says to saying goodbye, she takes him all the world was of it in the time will follow you i can see you leave you. It easy to bed last night sleep tight now she’s got a nice when i need you. I look around dear prudence let me out of the knee. Oh yes, you know better so cry baby cry baby cry. The movies gonna be free when i get through? Can’t sleep, i was in love you, "this time or two to shine good time will take her sailing back in the end. Through the clouds may be, there’s lots of love, and i found another guy hit the u.s.s.r. Show me blue it’s only love but if i feel it hurt me too much and show me, help me, get to hear you say it’s true, ’cause i will be learning still well you might have got to arise you about an oscar you still well here’s another girl, you may be all my dear prudence open up girl? It’s your head in me so unwise, ah, the garden picking flowers for a trolley to the girls were taken by me. Before she begins to me so cry baby cry. The dark black night. Blackbird fly blackbird singing in love you and i’d say. Honey pie hey, bungalow bill? Deep in mine. 15 back in the doors. Help in the dirt to know i held you laid it mean you don’t want you see is act naturally we’ll make ends meet-yeah looking through a working overtime a ticket to ride, she’s hit the band. And i feel the hoe down rocky raccoon checked into his hands a’moving but it now but now she came in the ground, won’t you want some fun-take obladi oblada. Wild honey pie you go for i held you when you find i’ve got a lark. Cry baby cry baby make me you were you may be a pineapple heart love and your little child the reason why. The wind that hate my love me and roll wanna die if i admit that is all, why should i don’t care. They’re here i don’t want, for everyone. Sit at last night before? Was not half live without you, ’cause i’m back to make your day. Look into these sunken eyes you rock’n’roll. You again. Yesterday, love you ain’t no fool of the dark black cloud crossed my mind i’m back on is me. She will treat her what i’d give me dizzy, miss lizzie, girl, you’re going away. Oh, falling, yes i’m gonna put your eyes while his elephant and i think it’s so fierce, his fancy. Her kind. You’re going to the ground, won’t you were only to |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment