Skip to content

Instantly share code, notes, and snippets.

@vinniec
Created Sep 17, 2019
Embed
What would you like to do?
genword
from random import randint, choice, choices
import json, jsonpickle
alfabeto = {chr(i) for i in range(97, 123)}
vocali = {'a', 'e', 'i', 'o', 'u', 'y', 'j'}
consonanti = alfabeto - vocali
cr_nin = " àùéòìèó"
with open("./caratteri.json", 'r') as f:
caratteri = json.loads(f.read())
# ~ voc_pesate = {c:caratteri[c] for c in vocali}
# ~ con_pesate = {c:caratteri[c] for c in consonanti}
# ~ with open("./grp_voc.json", 'r') as f:
# ~ gruppi_voc = json.loads(f.read())
# ~ with open("./grp_con.json", 'r') as f:
# ~ gruppi_con = json.loads(f.read())
# ~ with open("./sillabe.json", 'r') as f:
# ~ sillabe = json.loads(f.read())
# ~ slb_vocali = {s:sillabe[s] for s in sillabe if s[0] in vocali}
# ~ slb_conson = {s:sillabe[s] for s in sillabe if s[0] in consonanti}
# ~ with open("./memorie.json", "r") as f:
# ~ memorie = json.loads(f.read())
# ~ scelte = {'vocali': (voc_pesate, gruppi_voc, slb_vocali),
# ~ 'conson': (con_pesate, gruppi_con, slb_conson),
# ~ 'markov': memorie
# ~ }
# ~ jsn_scelte = jsonpickle.encode(scelte)
# ~ with open('scelte_tutte.json', 'w') as f:
# ~ f.write(jsn_scelte)
with open('scelte_tutte.json', 'r') as f:
scelte = jsonpickle.decode(f.read())
def prendi_cas(lst):
tipo = 'vocali' if lst in vocali else 'conson'
v_or_c = scelte[tipo]
s_or_g = choices(v_or_c, [100, 20, 20])[0]
sez = choices(list(s_or_g.keys()), list(s_or_g.values()))[0]
return sez
def prendi_mrk(lst):
diz = scelte['markov'][lst]
sez = choices(list(diz.keys()), list(diz.values()))[0]
return sez
def prendi(parola, tipo, lun):
lst = parola[-1]
if tipo == "markov":
while True:
sez = prendi_mrk(lst)
if (len(parola+sez) >= lun and sez.rstrip()[0:1] != " ") \
or not any([c in sez for c in cr_nin]):
break
else:
while True:
sez = prendi_cas(lst)
if sez[0] != lst:
break
return sez
def gen_par(lun=5, num=1):
try: lun = randint(*lun)
except: pass
parole = []
for i in range(num):
parola = choices(list(caratteri.keys()), list(caratteri.values()))[0]
while len(parola) < lun:
tipo = choice(list(scelte.keys()))
sez = prendi(parola, tipo, lun)
parola += sez
parole.append(parola.strip())
return ' '.join(parole)
if __name__ == "__main__":
# ~ print(genword((1,15), numero=10))
# ~ print(genword(10))
print(gen_par(6))
print(gen_par((4,10), 15))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment