-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsentence-url.py
44 lines (31 loc) · 1.2 KB
/
sentence-url.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
from random import choice, sample
vowels = {'a', 'e', 'i', 'o', 'u'}
with open("words/adjectives.txt") as adjs_file:
adjectives = adjs_file.read().split()
with open("words/nouns.txt") as words_file:
nouns = words_file.read().split()
class SentenceURL:
def __init__(self, word_count = 3, capitalize = True, seperator = ''):
self.word_count = word_count
self.capitalize = capitalize
self.seperator = seperator
def generate():
"""Generates readable URLs like Twitch's clips"""
if self.word_count < 2:
raise ValueError('Minimum value expected: 2')
elif self.word_count > 10:
raise ValueError('Maximum value expected: 10')
noun = choice(nouns)
word_list = []
if self.word_count > 3:
if noun in vowels:
word_list = ['an']
else:
word_list = [choice(('a', 'the'))]
word_list.extend(sample(adjectives, k = self.word_count-1))
if self.word_count > 4:
word_list.insert(2, 'and')
word_list.append(noun)
if self.capitalize:
word_list = map(str.title, word_list)
return self.seperator.join(word_list)