get_acronym

pyhelpers.text.get_acronym(text, only_capitals=False, capitals_in_words=False, keep_punctuation=False)[source]

Generate an acronym (in capital letters) from textual data.

An acronym is typically formed by taking the initial letters of each word in a phrase and combining them into a single string of uppercase letters.

Parameters:
  • text (str) – The input text from which to generate the acronym.

  • only_capitals (bool) – Whether to include only capital letters in the acronym; defaults to False.

  • capitals_in_words (bool) – Whether to treat all capital letters within words as part of the acronym; defaults to False.

  • keep_punctuation (bool) – Whether to retain punctuation in the input text; defaults to False.

Returns:

The acronym generated from the input text.

Return type:

str

Examples:

>>> from pyhelpers.text import get_acronym
>>> text_a = 'This is an apple.'
>>> get_acronym(text_a)
'TIAA'
>>> text_b = "I'm at the University of Birmingham."
>>> get_acronym(text_b, only_capitals=True)
'IUB'
>>> text_c = 'There is a "ConnectionError"!'
>>> get_acronym(text_c, capitals_in_words=True)
'TIACE'
>>> get_acronym(text_c, only_capitals=True, capitals_in_words=True)
'TCE'
>>> get_acronym(text_c, only_capitals=True, capitals_in_words=True, keep_punctuation=True)
'T"CE"!'
>>> get_acronym(text_c, only_capitals=True, keep_punctuation=True)
'T"C"!'
>>> get_acronym(text_c, capitals_in_words=True, keep_punctuation=True)
'TIA"CE"!'