Utilities


These functions are common utilities used by other modules.

generativepoetry.utils.filter_word(string, spellcheck=True, exclude_words=[], word_frequency_threshold=4e-08)

Filter out a word if it is too short, has invalid characters, is too archaic, or (optionally) cannot be found in a spelling dictionary.

Parameters
  • string – the string to check against

  • spellcheck – Use a spelling dictionary as filter. This helps eliminate abbreviations, proper nouns, and Internet slang–sometimes this is not desirable. It also eliminates many short stopwords like ‘of’ for some reason.

  • exclude_words – list of words to filter out

  • word_frequency_threshold – how frequently the word appears in the word_frequency package’s corpus – filter out word if less frequent than this threshold

generativepoetry.utils.filter_word_list(word_list: List[str], spellcheck: bool = True, exclude_words: List[str] = []) → List[str]

Filter a list of words using the filter_word method.

Parameters

word_list – list of words to filter

:param spellcheck (bool) – Use a spelling dictionary as filter (helps eliminate abbreviations and Internet slang).

generativepoetry.utils.get_random_color(threshold=0.85)

Gets a random color – 2/3 of the rgb values must be below the threshold value

generativepoetry.utils.has_invalid_characters(string)

Check if the string has unpermitted characters: whitespace, digits, and hyphens.

Parameters

string – the string to check for invalid characters in

generativepoetry.utils.too_similar(word1: str, comparison_val: str_or_list_of_str) → bool

Check whether or not two words are too similar to follow one another in a poem, e.g. if one is the other plus s. :param word1: the first word to compare :param comparison_val: a word or list of words to compare against

generativepoetry.utils.validate_str(input_val, msg='Not a string')

Validate the input argument by checking if it is a string.

Parameters
  • input_val – the value to validate

  • msg – the message to display if a ValueError is thrown

generativepoetry.utils.validate_str_list(input_val, msg='Not a list')

Validate the input parameter by checking if it is a list of strings.

Parameters
  • input_val – the value to validate

  • msg – the message to display if a ValueError is thrown

generativepoetry.utils.validate_str_or_list_of_str(input_val) → List[str]

Validate the input parameter by checking if it is a string or a list of strings.

Parameters

input_val – the value to validate

generativepoetry.utils.validate_word(input_val)

Check whether the input argument is a word.

Parameters

input_val – the word to validate