forked from LeoEkky/OpenAI-Codex-Code-Generation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLyrics_Generator.py
More file actions
66 lines (48 loc) · 1.64 KB
/
Copy pathLyrics_Generator.py
File metadata and controls
66 lines (48 loc) · 1.64 KB
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
# Lyrics generator
import random
import sys
def read_file(filename):
"""Reads a file and returns the text as a string"""
with open(filename, 'r') as f:
return f.read()
def make_chains(text):
"""Takes an input text as a string and returns a dictionary of
markov chains."""
# Split the text into words
words = text.split()
# Create an empty dictionary to hold our markov chains
chains = {}
# Iterate through the words in the text
for i in range(len(words) - 2):
key = (words[i], words[i + 1])
value = words[i + 2]
if key not in chains:
chains[key] = []
chains[key].append(value)
# or we could replace the last three lines with:
# chains.setdefault(key, []).append(value)
return chains
def make_text(chains):
"""Takes a dictionary of markov chains and returns random text
based off an original text."""
key = random.choice(chains.keys())
words = [key[0], key[1]]
while key in chains:
# Keep looping until we have a key that isn't in the chains
# (which would mean it was the end of our original text)
#
# Note that for long texts (like a full book), this might mean
# it would run for a very long time.
word = random.choice(chains[key])
words.append(word)
key = (key[1], word)
return " ".join(words)
def main():
args = sys.argv
# Change this to read input_text from a file
input_text = read_file(args[1])
chain_dict = make_chains(input_text)
random_text = make_text(chain_dict)
print random_text
if __name__ == "__main__":
main()