Created
June 25, 2021 14:51
-
-
Save johnbumgarner/035fd1dda6044a03cefca0ea1d66fbec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
This Python script is designed to colorize specific words in a string of | |
text. The colorized text is written to the console. | |
""" | |
__author__ = 'John Bumgarner' | |
__date__ = 'June 25, 2021' | |
__status__ = 'Production' | |
__license__ = 'MIT' | |
__copyright__ = "Copyright (C) 2021 John Bumgarner" | |
################################################################################## | |
# “AS-IS” Clause | |
# | |
# Except as represented in this agreement, all work produced by Developer is | |
# provided “AS IS”. Other than as provided in this agreement, Developer makes no | |
# other warranties, express or implied, and hereby disclaims all implied warranties, | |
# including any warranty of merchantability and warranty of fitness for a particular | |
# purpose. | |
################################################################################## | |
import random | |
import re as regex | |
from rich import color | |
from rich import print | |
def create_dynamic_regex(search_words): | |
""" | |
This function is used to create a dynamic regular expression | |
string and a list of random colors. Both these elements will | |
be used in the function colorize_text() | |
:param search_words: list of search terms | |
:return: regular expression search string and a list of colors | |
:rtype: string, list | |
""" | |
colors_required = create_list_of_colors(len(search_words)) | |
number_of_search_words = len(search_words) | |
combined_string = '' | |
for search_word in search_words: | |
number_of_search_words -= 1 | |
if number_of_search_words != 0: | |
current_string = ''.join(r'(\b' + search_word + r'\b)|') | |
combined_string = (combined_string + current_string) | |
elif number_of_search_words == 0: | |
current_string = ''.join(r'(\b' + search_word + r'\b)') | |
combined_string = (combined_string + current_string) | |
return combined_string, colors_required | |
def random_color(): | |
""" | |
This function is used to create a random color using the | |
Python package rich. | |
:return: color name | |
:rtype: string | |
""" | |
selected_color = random.choice(list(color.ANSI_COLOR_NAMES.keys())) | |
return selected_color | |
def create_list_of_colors(number_of_colors): | |
""" | |
This function is used to generate a list of colors, | |
which will be used in the function colorize_text() | |
:param number_of_colors: | |
:return: list of colors | |
:rtype: list | |
""" | |
list_of_colors = [random_color() for _ in range(number_of_colors)] | |
return list_of_colors | |
def colorize_text(text, regex_string, array_of_colors): | |
""" | |
This function is used to colorize specific words in a text string. | |
:param text: text string potentially containing specific words to colorize. | |
:param regex_string: regular expression search string | |
:param array_of_colors: list of colors | |
:return: colorized text | |
:rtype: string | |
""" | |
available_colors = array_of_colors | |
word_regex = regex.compile(f"{regex_string}", regex.IGNORECASE) | |
i = 0 | |
output = "" | |
for word in word_regex.finditer(text): | |
get_color = available_colors[word.lastindex - 1] | |
output += "".join([text[i:word.start()], | |
"[%s]" % available_colors[word.lastindex - 1], | |
text[word.start():word.end()], "[/%s]" % available_colors[word.lastindex - 1]]) | |
i = word.end() | |
return ''.join([output, text[word.end():]]) | |
def generate_console_output(text_to_search, words_to_find): | |
""" | |
This function is used generate colorized text that will | |
be outputting to the console. | |
:param text_to_search: text string potentially containing specific words to colorize. | |
:param words_to_find: list of search terms. | |
:return: A string containing colorized words. | |
:rtype: string | |
""" | |
search_terms, colors = create_dynamic_regex(words_to_find) | |
colorize_html = colorize_text(text_to_search, search_terms, colors) | |
print(colorize_html) | |
text = "The dog chased the cat that was looking for the mouse that the dog was playing with." | |
words = ['dog', 'cat', 'mouse'] | |
generate_console_output(text, words) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment