text_prediction.py

Created by golem64

Created on May 11, 2023

844 Bytes

Un essai à la prédiction de texte simplifiée


from random import *

marge=float("inf")
limite=3

f=open("txt.txt").read()
f=f.split("\n")
h=[]
while True:
  if not h:
    m={}
    for i in f:
      if i[0] in m: m[i[0]]+=1
      else: m[i[0]]=1
    n=list(m.keys())[list(m.values()).index(max(list(m.values())))]
    print(n,end="")
    h=n
  else:
    m={}
    while not m:
      for i in f:
        for j in range(len(i)-len(h)):
          if i[j:j+len(h)]==h:
            if i[j+len(h)] in m: m[i[j+len(h)]]+=1
            else: m[i[j+len(h)]]=1
      if not m: h=h[1:len(h)]
    l=max(list(m.values()))
    for i in m.keys():
      if m[i]<l-marge: del m[i]
    #n=list(m.keys())[list(m.values()).index(max(list(m.values())))]
    n=list(m.keys())[randint(0,len(list(m.keys()))-1)]
    print(n,end="")
    h+=n
    if limite:
      if len(h)>limite: h=h[1:len(h)]