Spaces:
Sleeping
Sleeping
from src.application.text.helper import ( | |
extract_starts_ends, | |
filter_indices, | |
) | |
def color_text( | |
text: str, | |
colored_idx: list[dict], | |
highlighted_idx: list[int], | |
) -> str: | |
""" | |
Colors specific words in a text based on provided indices. | |
1. splits the text into words | |
2. filters the indices | |
3. wraps the words within the specified ranges with a coloring tag | |
Args: | |
text (str): The input text. | |
colored_idx (list): A list of dictionaries, | |
where each dictionary contains | |
'start' and 'end' keys representing indices of words to color. | |
highlighted_idx (list): A list of indices to exclude from coloring. | |
Returns: | |
str: The text with colored words. | |
""" | |
sentence = "" | |
words = text.split() | |
# Extract start and end indices from colored_idx. | |
starts, ends = extract_starts_ends(colored_idx) | |
# Filter the start and end indices to exclude highlighted_idx. | |
starts, ends = filter_indices(starts, ends, highlighted_idx) | |
previous_end = 0 | |
for start, end in zip(starts, ends): | |
# Add the words before the current colored range to the sentence. | |
sentence += " ".join(words[previous_end:start]) | |
# Add the colored range to the sentence. | |
equal_words = " ".join(words[start:end]) | |
sentence += f" <span style='color:#00FF00;'>{equal_words}</span> " | |
# Update the previous end index. | |
previous_end = end | |
# Add the remaining words after the last colored range to the sentence. | |
sentence += " ".join(words[previous_end:]) | |
return sentence | |
def format_entity_count(entity_count: int) -> str: | |
""" | |
Generates a text description based on the number of altered entities. | |
Args: | |
entity_count (int): The number of altered entities. | |
Returns: | |
str: A text description of the entity count. | |
- "" if entity_count is 0 or negative. | |
- "with 1 altered entity" if entity_count is 1. | |
- "with altered entities" if entity_count is greater than 1. | |
""" | |
if entity_count <= 0: | |
entity_count_text = "" | |
elif entity_count == 1: | |
entity_count_text = "with 1 altered entity" | |
else: | |
entity_count_text = "with altered entities" | |
return entity_count_text | |