1
0
Fork 0
mirror of https://github.com/Findus23/nonsense.git synced 2024-09-19 16:03:50 +02:00
nonsense/ikeagen.py

76 lines
1.9 KiB
Python
Raw Permalink Normal View History

2015-02-08 12:13:45 +01:00
#!/usr/bin/python3
2017-12-11 14:11:02 +01:00
import os
2023-02-04 21:45:50 +01:00
import pickle
2017-12-10 15:13:33 +01:00
import random
2023-02-04 21:45:50 +01:00
import resource
from pathlib import Path
2015-03-31 13:41:43 +02:00
2018-01-20 21:26:30 +01:00
import utils
2017-12-10 15:13:33 +01:00
2015-03-31 13:41:43 +02:00
def gen():
2018-03-27 22:19:08 +02:00
table = {}
2018-01-20 21:26:30 +01:00
crawldata = utils.crawl_data()
2017-12-27 12:14:52 +01:00
names = {result["name"] for result in crawldata}
2017-12-10 15:13:33 +01:00
count = 0
2017-12-27 12:14:52 +01:00
for name in names:
2018-03-27 22:19:08 +02:00
if name is not None:
2017-12-11 14:11:02 +01:00
name = " " + name + " "
2017-12-10 15:13:33 +01:00
zeichen = list(name)
zeichenl = len(zeichen)
a = 0
2018-03-27 22:19:08 +02:00
while a < zeichenl - 2:
if (zeichen[a], zeichen[a + 1]) not in table:
table[(zeichen[a], zeichen[a + 1])] = {}
if zeichen[a + 2] in table[(zeichen[a], zeichen[a + 1])]:
table[(zeichen[a], zeichen[a + 1])][zeichen[a + 2]] += 1
else:
table[(zeichen[a], zeichen[a + 1])][zeichen[a + 2]] = 1
2017-12-10 15:13:33 +01:00
count += 1
a += 1
return table, count
2015-03-31 13:41:43 +02:00
2017-12-11 14:11:02 +01:00
def letter(a, b):
2017-12-10 15:13:33 +01:00
mylist = []
2018-03-27 22:19:08 +02:00
for c in table[(a, b)]:
mylist.extend([c] * table[(a, b)][c])
2017-12-10 15:13:33 +01:00
return random.choice(mylist)
2023-02-04 21:45:50 +01:00
use_saving = False # Loading uses twice the memory and is therefore disabled
save_file=Path('runtime/ikeaname.pickle')
if save_file.exists() and use_saving:
with save_file.open("rb") as handle:
2018-03-27 22:19:08 +02:00
b = pickle.load(handle)
2015-03-31 13:41:43 +02:00
else:
2017-12-10 15:13:33 +01:00
table, count = gen()
2023-02-04 21:45:50 +01:00
if use_saving:
with save_file.open("wb") as handle:
pickle.dump((table, count), handle, protocol=pickle.HIGHEST_PROTOCOL)
2017-12-10 15:13:33 +01:00
def generate():
2018-03-27 22:19:08 +02:00
a = b = " "
2017-12-10 15:13:33 +01:00
wort = []
while True:
2017-12-11 14:11:02 +01:00
new = letter(a, b)
2018-03-27 22:19:08 +02:00
wort.append(new)
2017-12-11 14:11:02 +01:00
a = b
b = new
2018-03-27 22:19:08 +02:00
if a == " " and b == " ":
2017-12-10 15:13:33 +01:00
if len(wort) > 5:
2017-12-11 14:11:02 +01:00
return "".join(wort).strip()
else:
wort = []
2018-03-27 22:19:08 +02:00
a = b = " "
2017-12-11 14:11:02 +01:00
2017-12-10 15:13:33 +01:00
if __name__ == "__main__":
for _ in range(100):
print(generate())
2018-03-27 22:19:08 +02:00
print("used {mb}MB".format(mb=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss // 1024))