-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlorem_ipsum.py
66 lines (47 loc) · 1.88 KB
/
lorem_ipsum.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
#!/usr/bin/env python3
import random
LOREM_IPSUM_FILE_PATH="./lorem_ipsum.txt"
class LoremIpsum:
def __init__(self):
self.lorem_ipsum_words=[]
lorem_ipsum_file=open(LOREM_IPSUM_FILE_PATH,'r')
lorem_ipsum_str=lorem_ipsum_file.read()
temp_str=lorem_ipsum_str.lower()
temp_str=temp_str.replace('\n', '')
temp_str=temp_str.replace(',', '')
temp_str=temp_str.replace('.', '')
self.lorem_ipsum_words=temp_str.split()
def generate_sentence(self,word_cnt_min=8,word_cnt_max=16,comma_max_density=4):
sentence=str()
word_cnt=random.randint(word_cnt_min,word_cnt_max)
comma_cnt=0
previous_word=False
for i in range(word_cnt):
while(True):
word=self.lorem_ipsum_words[random.randint(0,len(self.lorem_ipsum_words)-1)]
if word!=previous_word:
previous_word=word
sentence+=word
if i<word_cnt-1:
comma_cnt+=1
if comma_cnt>=comma_max_density:
r=random.random()
if r<0.4:
sentence+=","
comma_cnt=0
sentence+=" "
break
sentence+="."
sentence=sentence.capitalize()
return sentence
def generate_paragraph(self,sentence_cnt_min=4,sentence_cnt_max=8,word_cnt_min=8,word_cnt_max=16,comma_max_density=4):
sentence_cnt=random.randint(sentence_cnt_min,sentence_cnt_max)
paragraph=str()
for i in range(sentence_cnt):
paragraph+=self.generate_sentence()
if i<sentence_cnt-1:
paragraph+=" "
return paragraph
if __name__=="__main__":
lorem_ipsum=LoremIpsum()
print(lorem_ipsum.generate_paragraph())