__init__.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. localized = True
  2. # 'Latin' is the default locale
  3. default_locale = 'la'
  4. from .. import BaseProvider
  5. class Provider(BaseProvider):
  6. """Will provide methods to retrieve lorem content
  7. Attributes:
  8. sentence_punctuation (str): End of sentence punctuation
  9. word_connector (str): Default connector between words
  10. Methods:
  11. word: Generate a random word
  12. words: Generate a list containing random words
  13. sentence: Generate a random sentence
  14. sentences: Generate a list containing sentences
  15. paragraph: Generate a single paragraph
  16. paragraphs: Generate many paragraphs
  17. text: Generate a text string.
  18. """
  19. word_connector = ' '
  20. sentence_punctuation = '.'
  21. def words(self, nb=3, ext_word_list=None, unique=False):
  22. """
  23. :returns: An array of random words. for example: ['Lorem', 'ipsum', 'dolor']
  24. Keyword arguments:
  25. :param nb: how many words to return
  26. :param ext_word_list: a list of words you would like to have instead of
  27. 'Lorem ipsum'
  28. :param unique: If True, the returned word list will contain unique words
  29. :rtype: list
  30. """
  31. word_list = ext_word_list if ext_word_list else self.word_list
  32. if unique:
  33. return self.random_sample(word_list, length=nb)
  34. return self.random_choices(word_list, length=nb)
  35. def word(self, ext_word_list=None):
  36. """
  37. :returns: A random word, eg: 'lorem'
  38. :param ext_word_list: a list of words you would like to have instead of
  39. 'Lorem ipsum'
  40. :rtype: str
  41. """
  42. return self.words(1, ext_word_list)[0]
  43. def sentence(self, nb_words=6, variable_nb_words=True, ext_word_list=None):
  44. """
  45. Generate a random sentence
  46. :example 'Lorem ipsum dolor sit amet.'
  47. :param nb_words: around how many words the sentence should contain
  48. :param variable_nb_words: set to false if you want exactly ``nb``
  49. words returned, otherwise the result may include a number of words
  50. of ``nb`` +/-40% (with a minimum of 1)
  51. :param ext_word_list: a list of words you would like to have instead of
  52. 'Lorem ipsum'.
  53. :rtype: str
  54. """
  55. if nb_words <= 0:
  56. return ''
  57. if variable_nb_words:
  58. nb_words = self.randomize_nb_elements(nb_words, min=1)
  59. words = self.words(nb=nb_words, ext_word_list=ext_word_list)
  60. words[0] = words[0].title()
  61. return self.word_connector.join(words) + self.sentence_punctuation
  62. def sentences(self, nb=3, ext_word_list=None):
  63. """
  64. Generate an array of sentences
  65. :example ['Lorem ipsum dolor sit amet.', 'Consectetur adipisicing eli.']
  66. Keyword arguments:
  67. :param nb: how many sentences to return
  68. :param ext_word_list: a list of words you would like to have instead of
  69. 'Lorem ipsum'.
  70. :rtype: list
  71. """
  72. return [self.sentence(ext_word_list=ext_word_list)
  73. for _ in range(0, nb)]
  74. def paragraph(
  75. self,
  76. nb_sentences=3,
  77. variable_nb_sentences=True,
  78. ext_word_list=None):
  79. """
  80. :returns: A single paragraph. For example: 'Sapiente sunt omnis. Ut
  81. pariatur ad autem ducimus et. Voluptas rem voluptas sint modi dolorem amet.'
  82. Keyword arguments:
  83. :param nb_sentences: around how many sentences the paragraph should contain
  84. :param variable_nb_sentences: set to false if you want exactly ``nb``
  85. sentences returned, otherwise the result may include a number of
  86. sentences of ``nb`` +/-40% (with a minimum of 1)
  87. :param ext_word_list: a list of words you would like to have instead of
  88. 'Lorem ipsum'.
  89. :rtype: str
  90. """
  91. if nb_sentences <= 0:
  92. return ''
  93. if variable_nb_sentences:
  94. nb_sentences = self.randomize_nb_elements(nb_sentences, min=1)
  95. para = self.word_connector.join(self.sentences(
  96. nb_sentences, ext_word_list=ext_word_list
  97. ))
  98. return para
  99. def paragraphs(self, nb=3, ext_word_list=None):
  100. """
  101. Generate an array of paragraphs
  102. :example [paragraph1, paragraph2, paragraph3]
  103. :param nb: how many paragraphs to return
  104. :param ext_word_list: a list of words you would like to have instead of
  105. 'Lorem ipsum'.
  106. :rtype: list
  107. """
  108. return [self.paragraph(ext_word_list=ext_word_list)
  109. for _ in range(0, nb)]
  110. def text(self, max_nb_chars=200, ext_word_list=None):
  111. """
  112. Generate a text string.
  113. Depending on the ``max_nb_chars, returns a string made of words, sentences, or paragraphs.
  114. :example 'Sapiente sunt omnis. Ut pariatur ad autem ducimus et. Voluptas rem voluptas sint modi dolorem amet.'
  115. Keyword arguments:
  116. :param max_nb_chars: Maximum number of characters the text should contain (minimum 5)
  117. :param ext_word_list: a list of words you would like to have instead of 'Lorem ipsum'.
  118. :rtype str
  119. """
  120. text = []
  121. if max_nb_chars < 5:
  122. raise ValueError(
  123. 'text() can only generate text of at least 5 characters')
  124. if max_nb_chars < 25:
  125. # join words
  126. while not text:
  127. size = 0
  128. # determine how many words are needed to reach the $max_nb_chars
  129. # once;
  130. while size < max_nb_chars:
  131. word = (self.word_connector if size else '') + \
  132. self.word(ext_word_list=ext_word_list)
  133. text.append(word)
  134. size += len(word)
  135. text.pop()
  136. text[0] = text[0][0].upper() + text[0][1:]
  137. last_index = len(text) - 1
  138. text[last_index] += self.sentence_punctuation
  139. elif max_nb_chars < 100:
  140. # join sentences
  141. while not text:
  142. size = 0
  143. # determine how many sentences are needed to reach the
  144. # $max_nb_chars once
  145. while size < max_nb_chars:
  146. sentence = (self.word_connector if size else '') + \
  147. self.sentence(ext_word_list=ext_word_list)
  148. text.append(sentence)
  149. size += len(sentence)
  150. text.pop()
  151. else:
  152. # join paragraphs
  153. while not text:
  154. size = 0
  155. # determine how many paragraphs are needed to reach the
  156. # $max_nb_chars once
  157. while size < max_nb_chars:
  158. paragraph = ('\n' if size else '') + \
  159. self.paragraph(ext_word_list=ext_word_list)
  160. text.append(paragraph)
  161. size += len(paragraph)
  162. text.pop()
  163. return "".join(text)