-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathsongbird.py
79 lines (68 loc) · 2.5 KB
/
songbird.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
Songbird: A Markov Model text generator (solutions)
Irene Chen (github/irenetrampoline)
Feb 27, 2016
"""
import re
from random import choice
import py.test
class Songbird:
"""
Given a single source document, generate similar sounding strings/songs
based on a Markov Model.
"""
def __init__(self, source_path):
self.corpus = open(source_path, 'rb').read()
self.tokens = self.get_tokens()
self.bigrams = self.get_bigrams()
def get_tokens(self):
"""
Read corpus file and create a list of all tokens (words). Note that we
are not removing duplicates, only turning a text file into a list.
Removing punctuation is optional but will make everything much easier.
"""
raise NotImplementedError
def get_bigrams(self):
"""
TODO: From the list of tokens, create a dict that has:
- key: word
- value: list of words that appear after it
Bigrams are sequences of two words that are adjacent to each other.
Example: If tokens = ['I', 'am', 'happy.', 'I', 'saw', 'a', 'happy', 'cat.'],
then our dictionary would look like:
{
'I': ['am', 'saw'],
'am': ['happy'],
'happy.': ['I'],
'saw': ['a'],
'a': ['happy'],
'happy': ['cat.'']
}
"""
raise NotImplementedError
def generate(self, size=100):
"""
TODO: Generate a new string given a desired length.
This is the meat of the project. Using the bigrams create above, use
randomness to generate an original Taylor-Swift-inspired song! You have a
LOT of freedom here.
1) Choose how to start the song. Do you pick a random word? An upper case word?
A word that's started other songs?
2) Given one word, pick the next word! The bigrams dictionary that we made
above might be helpful.
3) [optional] Before you publish your song, do you want to clean it up?
Captialize letters, add line breaks, up to you!
"""
raise NotImplementedError
@staticmethod
def format_string(text):
"""
TODO [optional]: Format string into readable format (if desired).
Reformat the text to a nice readable string.
Capitalize sentence beginnings.
Remove space before punctuation.
"""
return text
if __name__ == '__main__':
tswift_bird = Songbird('all_tswift_lyrics.txt')
print tswift_bird.generate(50)