version: | 1.0.5 |
---|---|
author: | Luca De Vitis <luca@monkeython.com> |
contact: | http://www.monkeython.com |
copyright: | 2011-2014, Luca De Vitis <luca@monkeython.com> |
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>
The purpose of this package is to generate random (plausible) text sentences and paargraphs based on a dictionary and a sample text. By default this package will generate Lorem Ipsum style text, but you can customize the generator to effectively load any dictionary and any sample text you like.
This package has en extensive docstring documentation, so you can read more on the online documentation or in the python interactive shell as well.
Most of the code of this module is taken from lorem-ipsum-generator by James Hales. James stated that his package served his purpose and he was not interested in further development, so I took it over.
This package provides a text generator class and some utility functions that can simply return the text you desire. There are 2 sets of functions:
On the average, you probably want to import the get_ prefixed functions and just get the text:
>>> from loremipsum import get_sentences
>>>
>>> sentences_list = get_sentences(5)
>>> len(sentences_list)
5
>>>
If you fancy some statistics, you want to import the generate_ prefixed functions:
>>> from loremipsum import generate_paragraph
>>>
>>> sentences_count, words_count, paragraph = generate_paragraph()
If you need generate text based on your own sample text and/or dictionary, you want to import the Generator class:
>>> from loremipsum import Generator
>>>
>>> with open('data/sample.txt', 'r') as sample_txt
>>> sample = sample_txt.read()
>>> with open('data/dictionary.txt', 'r') as dictionary_txt
>>> dictionary = dictionary_txt.read().split()
>>>
>>> g = Generator(sample, dictionary)
>>> sentence = g.get_sentence()
>>>
The sample text must contain one or more empty-line delimited paragraphs, and each paragraph must contain one or more period, question mark, or exclamation mark delimited sentences.
The dictionary must be a list of one or more words.
Utility function to generate a single random sentence with stats.
Parameters: | start_with_lorem (bool) – if True, then the text will begin with the standard “Lorem ipsum...” first sentence. |
---|---|
Returns: | a tuple with amount of sentences, words and the text |
Return type: | tuple(int, int, str) |
Generator function that yields specified amount of random sentences with stats.
Parameters: |
|
---|---|
Returns: | a tuple with amount of sentences, words and the text |
Return type: | tuple(int, int, str) |
Utility function to generate a single random paragraph with stats.
Parameters: | start_with_lorem – if True, then the text will begin with the standard “Lorem ipsum...” first sentence. |
---|---|
Returns: | a tuple with amount of sentences, words and the text |
Return type: | tuple(int, int, str) |
Generator function that yields specified amount of random paragraphs with stats.
Parameters: | start_with_lorem – if True, then the text will begin with the standard “Lorem ipsum...” first sentence. |
---|---|
Returns: | a tuple with amount of sentences, words and the text |
Return type: | tuple(int, int, str) |
Utility function to get a single random sentence.
Parameters: | start_with_lorem – if True, then the text will begin with the standard “Lorem ipsum...” first sentence. |
---|---|
Returns: | a random sentence |
Return type: | str |
Utility function to get specified amount of random sentences.
Parameters: |
|
---|---|
Returns: | a list of random sentences. |
Return type: | list |
Utility function to get a single random paragraph.
Parameters: | start_with_lorem – if True, then the text will begin with the standard “Lorem ipsum...” first sentence. |
---|---|
Returns: | a random paragrpah |
Return type: | str |
Utility function to get specified amount of random paragraphs.
Parameters: | start_with_lorem – if True, then the text will begin with the standard “Lorem ipsum...” first sentence. |
---|---|
Returns: | a list of random paragraphs |
Return type: | list |
Generates random strings of “lorem ipsum” text.
Markov chains are used to generate the random text based on the analysis of a sample text. In the analysis, only paragraph, sentence and word lengths, and some basic punctuation matter – the actual words are ignored. A provided list of words is then used to generate the random text, so that it will have a similar distribution of paragraph, sentence and word lengths.
Parameters: |
|
---|
Generates a single lorem ipsum paragraph, of random length.
Parameters: | start_with_lorem (bool) – if True, then the text will begin with the standard “Lorem ipsum...” first sentence. |
---|
Generator method that yields paragraphs, of random length.
Parameters: | start_with_lorem (bool) – if True, then the text will begin with the standard “Lorem ipsum...” first sentence. |
---|
Generates a single sentence, of random length.
Parameters: | start_with_lorem (bool) – if True, then the text will begin with the standard “Lorem ipsum...” first sentence. |
---|
Generator method that yields sentences, of random length.
Parameters: | start_with_lorem (bool) – if True, then the text will begin with the standard “Lorem ipsum...” first sentence. |
---|
Resets the values of sentence_mean, sentence_sigma, paragraph_mean, and paragraph_sigma to their values as calculated from the sample text.
A dictionary of words that generated sentences are made of, grouped by words length.
Parameters: | words (list) – list of words |
---|---|
Return type: | dict |
Raises: | DictionaryError if no valid words in dictionary |
A non-negative value determining the mean paragraph length (in sentences) of generated sentences. Is changed to match the sample text when the sample text is updated.
Return type: | int |
---|---|
Raises: | ValueError if value is lesser then 0 |
A non-negative value determining the standard deviation of paragraph lengths (in sentences) of generated sentences. Is changed to match the sample text when the sample text is updated.
Return type: | int |
---|---|
Raises: | ValueError if value is lesser then 0 |
The sample text that generated sentences are based on.
Sentences are generated so that they will have a similar distribution of word, sentence and paragraph lengths and punctuation.
Sample text should be a string consisting of a number of paragraphs, each separated by empty lines. Each paragraph should consist of a number of sentences, separated by periods, exclamation marks and/or question marks. Sentences consist of words, separated by white space.
Parameters: | sample (str) – the sample text |
---|---|
Return type: | str |
Raises: | SampleError if no words in sample text |
A non-negative value determining the mean sentence length (in words) of generated sentences. Is changed to match the sample text when the sample text is updated.
Return type: | int |
---|---|
Raises: | ValueError if value is lesser then 0 |
A non-negative value determining the standard deviation of sentence lengths (in words) of generated sentences. Is changed to match the sample text when the sample text is updated.
Return type: | int |
---|---|
Raises: | ValueError if value is lesser then 0 |
The plain list of words in the dictionary.