How to generate random text that make sense (almost)

i put together a small script the generates random text based on some text you feed it
and it builds a dict of all the words in the provided text. (the longer the input text is the better the generated text become)

here is and example.

snippet:
Blender is a public project, made by hundreds of people from around the world; by studios and individual artists, professionals and hobbyists, scientists, students, VFX experts, animators, game artists, modders, and the list goes on.

turns into this
{’’: None, ‘a’: [‘public’], ‘goes’: [‘on.’], ‘around’: [‘the’], ‘project,’: [‘made’], ‘hobbyists,’: [‘scientists,’], ‘professionals’: [‘and’], ‘is’: [‘a’], ‘people’: [‘from’], ‘game’: [‘artists,’], ‘on.’: [’’], ‘Blender’: [‘is’], ‘world’: [‘by’], ‘individual’: [‘artists,’], ‘list’: [‘goes’], ‘animators,’: [‘game’], ‘of’: [‘people’], ‘experts,’: [‘animators,’], ‘the’: [‘world’, ‘list’], ‘studios’: [‘and’], ‘by’: [‘hundreds’, ‘studios’], ‘VFX’: [‘experts,’], ‘and’: [‘individual’, ‘hobbyists,’, ‘the’], ‘public’: [‘project,’], ‘scientists,’: [‘students,’], ‘students,’: [‘VFX’], ‘artists,’: [‘professionals’, ‘modders,’], ‘from’: [‘around’], ‘made’: [‘by’], ‘modders,’: [‘and’], ‘hundreds’: [‘of’]}

from that text can be generated.

examples.
1.
Blender is a public project, made by studios and the world by hundreds of people
from around the world by studios

Blender is a public project, made by hundreds of people from around the world by
hundreds of people

Blender is a public project, made by studios and the world by studios and
hobbyists, scientists, students, VFX experts, animators

here is the code that make it work


def stripchars(text,chars):
    tmp =  ""
    
    for c in text:
        if c not in chars:
            tmp += c
    
    return tmp
            
    
class textgen:
    def __init__(self):
        self.words = {}
        
    def getallattr(self):
        return vars(self)
        
        
    def insert(self,text=""):
        text = stripchars(text,'()[]";:—')
        text = text.replace("
", " ")
        
        wordlist = text.split(" ")
        
        before = None
        
        for word in wordlist:
            
            if word not in self.words:
                self.words[word] = None
        
            if before:
                if self.words[before]:
                    if word not in self.words[before]:
                        self.words[before].append(word)
                else:
                    self.words[before] = [word]
                
            before = word
            
    def generate(self,word="The",count=20):
        
        txt = word
        
        while count:
            if word in self.words:
                if self.words[word]:
                    word = random.choice(self.words[word])
                    txt += " {}".format(word.strip())
                else:
                    word = random.choice(["Is","The","What"])
            else:
                word = random.choice(["Is","The","What"])
            count -=1
        return txt
    

here is a blend to try it out.
http://15b.dk/blendfiles/textgen.blend