We generate lyrics with Markov chains

Today I want to talk about my experience in generating lyrics with python and the Markovify library


Disclaimer: the author wanted to amuse himself in the evening and did not come up with anything better, like:


As a body for the " training " of the chain, I will use the lyrics of the Kees Kies group songs.


image


The piccha above illustrates how the Markov chain works. But a good article .


Start


I assume that the reader is already familiar with python and has pre-installed IDE and Python versions> 3.5.


Install the necessary library:


pip install markovify

Now we accumulate lyrics for ourselves and process them a little, for this we write a simple text processor. We’ll remove “garbage” words like chorus, verse and numbers, for this we’ll write a simple text processor.


IND = ""
Find2 = ":"
INFILE = "corpus.txt"
OUTFILE = "output.txt"
ENC = "utf-8"
with open(INFILE, encoding="UTF-") as infile, open(OUTFILE, "w", encoding="UTF-8") as outfile:
    for line in infile:
        if FIND or Find2 not in line:
            outfile.write(line)

os.remove(INFILE)
os.rename(OUTFILE, INFILE)

the text that I have already processed


And now let's use this wonderful library and finally generate our texts.


with open("C:/Users/alexd/PycharmProjects/untitled/corpus.txt",encoding='utf-8') as f:
    text = f.read()
text_model = markovify.Text(text)
for i in range(10):
    print(text_model.make_short_sentence(380))

Result:


     ,  ,    «».     :    ?

Total


I tried to tell as briefly as possible how to generate a text of almost anything in just a few minutes. What for? -Well, it's fun. Useless, but damn fun.
telegram channel author essay
school essay generator


Used materials


All Articles