๐Ÿ“ฆ wrussell1999 / royal-hackaway-v2

๐Ÿ“„ generator.py ยท 48 lines
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
48import os
import re
import random

import tswift

from .context import Context

class Generator:
    def __init__(self, songs):
        self.songs = songs

    def generate_lyrics(self, word, lines=20):
        song = random.choice(self.songs)
        song = tswift.Song(*song)

        verses = self.parse_lyrics(song.lyrics)

        content = []
        for verse in verses:
            content.extend(verse)
            if len(content) > lines:
                break

        lyrics = []
        for line in content:
            ctx = Context(line)
            nouns = list(ctx.nouns())
            if len(nouns) > 0:
                random.choice(nouns).set(word)

            part = ctx.generate()
            if not re.search(r'[,.?!]$', part):
                part += ','
            lyrics.append(part)

        return '\n'.join(lyrics)

    def parse_lyrics(self, lyrics, clean = True):
        if clean:
            lyrics = re.sub('\[.*\]', '', lyrics)
            lyrics = lyrics.strip()

        verses = re.split('\n\n+', lyrics)
        verses = [line.split('\n') for line in verses]

        return verses