import re


def text_in_windows(keyword, corpus, window_size):
    """
    Function that takes a keyword, the corpus, and a window size and gathers all text 
    appearing within the windows around the keyword appearing in the corpus.
    The function returns the window library.
    """
    # Creating a library of all text co-occuring with the keyword
    window_library = ""

    for match in re.finditer(keyword, corpus):
        
        # first character index of match
        word_start = match.start()
        # last character index of match
        word_end = match.end()
        
        # Left window
        left_window_start = max(0, word_start-window_size)
        left_window = corpus[left_window_start:word_start]
        
        # Right window
        right_window_end = word_end + window_size
        right_window = corpus[word_end : right_window_end]

        # Save right/left windows in the window library
        window_library = window_library + left_window + right_window
    
    return(window_library)