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

95 lines
2 KiB
Python
Raw Normal View History

2015-02-08 12:13:45 +01:00
#!/usr/bin/python3
2015-03-31 13:41:43 +02:00
import json
2017-12-10 15:13:33 +01:00
2017-12-11 14:11:02 +01:00
import pickle
import os
2017-12-10 15:13:33 +01:00
import random
2015-03-31 13:41:43 +02:00
from PIL import Image
2017-12-10 15:13:33 +01:00
2015-03-31 13:41:43 +02:00
def gen():
2017-12-11 14:11:02 +01:00
table = [[[0 for i in range(221)] for j in range(221)] for k in range(221)]
2017-12-10 15:13:33 +01:00
# contents = open("ikeaname.txt").read().splitlines()
with open('download.json') as inputfile:
contents = json.load(inputfile)["names"]
count = 0
for name in contents:
if name:
2017-12-11 14:11:02 +01:00
name = " " + name + " "
2017-12-10 15:13:33 +01:00
zeichen = list(name)
zeichenl = len(zeichen)
2017-12-11 14:11:02 +01:00
zeichenl -= 2
2017-12-10 15:13:33 +01:00
a = 0
while a < zeichenl:
2017-12-11 14:11:02 +01:00
table[ord(zeichen[a])][ord(zeichen[a + 1])][ord(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
def save(data):
2017-12-11 14:11:02 +01:00
with open('ikeaname.pickle', 'wb') as outfile:
pickle.dump(data, outfile,pickle.HIGHEST_PROTOCOL)
2017-12-10 15:13:33 +01:00
2015-03-31 13:41:43 +02:00
def load():
2017-12-11 14:11:02 +01:00
with open('ikeaname.pickle',"rb") as inputfile:
table = pickle.load(inputfile)
2017-12-10 15:13:33 +01:00
return table
2015-02-08 12:13:45 +01:00
2017-12-11 14:11:02 +01:00
def letter(a, b):
2017-12-10 15:13:33 +01:00
mylist = []
2017-12-11 14:11:02 +01:00
for c in range(221):
for x in range(table[a][b][c]):
mylist.append(c)
2017-12-10 15:13:33 +01:00
return random.choice(mylist)
def image(table):
img = Image.new('RGB', (221, 221))
maximum = max(max(table))
print(maximum)
row = 0
col = 0
for coln in range(221):
for rown in range(221):
color = 255 - int(table[coln][rown] / maximum * 255)
img.putpixel((coln, rown), (color, color, color))
img = img.resize((2210, 2210), )
img.save('image.png')
2017-12-11 14:11:02 +01:00
if os.path.isfile('ikeaname.pickle'):
2017-12-10 15:13:33 +01:00
table, count = load()
2017-12-11 14:11:02 +01:00
# image(table)
2017-12-10 15:13:33 +01:00
2015-03-31 13:41:43 +02:00
else:
2017-12-10 15:13:33 +01:00
table, count = gen()
save((table, count))
def generate():
2017-12-11 14:11:02 +01:00
a = b = 32
2017-12-10 15:13:33 +01:00
wort = []
while True:
2017-12-11 14:11:02 +01:00
new = letter(a, b)
wort.append(chr(new))
a = b
b = new
if a == 32 and b == 32:
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 = []
a = b = 32
2017-12-10 15:13:33 +01:00
if __name__ == "__main__":
for _ in range(100):
print(generate())