/
Anekdotes.py
125 lines (97 loc) · 3.27 KB
/
Anekdotes.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import config
from os import listdir
from os.path import isfile, join
#from bs4 import BeautifulSoup
from random import choice
from markov.markov_model import make_markov_model
from tqdm import tqdm
def prettify(str):
while "<br>" in str:
str = str.replace("<br>","\n")
return str
def make_data_for_model(aneks, count, func):
data = []
for text in aneks:
if(not func(text)):
continue
anek_data = []
words = text.split(" ")
for k in range(count):
#anek_data.append("#START#")
start_s = " ".join(words[:k])
if(start_s !="" and start_s!=" "):
anek_data.append(start_s)
j =-1
for i in range(k, len(words) - count + 1, count):
s=""
for j in range(count):
s+= words[i+j]+" "
s=s[:-1]
anek_data.append(s)
j=i
last_s = " ".join(words[j+count:])
if(last_s!=" " and last_s!=""):
anek_data.append(last_s)
anek_data.append("#END#")
data += anek_data
#print(data)
return data
def make_model(aneks,count,func):
return make_markov_model(make_data_for_model(aneks,count,func))
import DB
def get_random():
return DB.get_random_anek()
mdl = []#make_markov_model(DB.get_model("all_1")+DB.get_model("all_2") + DB.get_model("all_3"))
from markov.sentence_generator import generate_random_sentence
def generate_anek(model = mdl):
return generate_random_sentence(model,fixedLength = -1, max_words = (35),rand=-1)
def generate_hat_anek():
return "Данная опция пока не доступна ... "#generate_random_sentence(-1, hat_model,max_words = 100)
def generate_short():
return generate_random_sentence(-1, mdl,max_words=10,rand=1)
def generate(model):
return generate_random_sentence(-1, model,max_words = (360*2))
def Answer(message):
index = -1
txt = message.text
txt = txt.split(" ")
msg = ""
index = txt[0][len("/generate"):]
if ("@" in index):
index = index[:index.find('@')]
if (index == "hat"):
msg = generate_hat_anek()
elif (index == "short"):
msg = generate_short()
elif (index == ""):
index = 2
else:
index = int(index)
if (index < 1 or index > 6):
index = 2
msg = generate_anek(index)
return msg
def generate_post(model = mdl):
text = ""
text += generate_anek(model)
return text
def generate_posts(model = mdl,count = 100, unique_check = True):
from aGeneratedDB import add
from DB import is_unique
from Things.StringHash import hash_string
for i in tqdm(range(count)):
while True:
tmp = generate_anek(model)
if (not unique_check) or is_unique(hash_string(tmp)):
add(tmp)
break
def generate_posts_donut(model = mdl,count = 100, unique_check = True):
from aGeneratedDB import add_donut
from DB import is_unique
from Things.StringHash import hash_string
for i in tqdm(range(count)):
while True:
tmp = generate_anek(model)
if (not unique_check) or is_unique(hash_string(tmp)):
add_donut(tmp)
break