File size: 2,399 Bytes
b73a4fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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.

    This method takes a text, a list of indices to color, and a list of indices to exclude.
    It splits the text into words, filters the indices, and then wraps the words within
    the specified ranges with a green span tag for coloring.

    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