API Reference

trigrams

Simple trigrams-based text generation

class trigrams.TrigramsDB(dbfile=None)[source]

A trigrams database. It has two main methods: feed, to initialize it with some existing text, and generate, to generate some new text. The more text you feed it, the more “random” the generated text will be.

feed(text=None, source=None)[source]

Feed some text to the database, either from a string (text) or a file (source).

>>> db = TrigramsDB()
>>> db.feed("This is my text")
>>> db.feed(source="some/file.txt")
generate(**kwargs)[source]

Generate some text from the database. By default only 70 words are generated, but you can change this using keyword arguments.

Keyword arguments:

  • wlen: maximum length (words)
  • words: a list of words to use to begin the text with
save(output=None)[source]

Save the database to a file. If output is not given, the dbfile given in the constructor is used.