def get_context(lines, backwards=False):
    """Get context.

    Given a relevant string from a TeX file, this function will extract text
    from it as far as it is deemed contextually relevant, either backwards or
    forwards in the text.
    The level of relevance allowed is configurable. When it reaches some
    point in the text that is determined to be out of scope from the current
    context, like text that is identified as a new paragraph, a complex TeX
    structure ('/begin', '/end', etc.) etc., it will return the previously
    allocated text.

    For use when extracting text with contextual value for an figure or plot.

    :param lines (string): string to examine
    :param reversed (bool): are we searching backwards?

    :return context (string): extracted context
    """
    tex_tag = re.compile(r".*\\(\w+).*")
    sentence = re.compile(r"(?<=[.?!])[\s]+(?=[A-Z])")
    context = []

    word_list = lines.split()
    if backwards:
        word_list.reverse()

    # For each word we do the following:
    #   1. Check if we have reached word limit
    #   2. If not, see if this is a TeX tag and see if its 'illegal'
    #   3. Otherwise, add word to context
    for word in word_list:
        if len(context) >= CFG_PLOTEXTRACTOR_CONTEXT_WORD_LIMIT:
            break
        match = tex_tag.match(word)
        if match and match.group(1) in CFG_PLOTEXTRACTOR_DISALLOWED_TEX:
            # TeX Construct matched, return
            if backwards:
                # When reversed we need to go back and
                # remove unwanted data within brackets
                temp_word = ""
                while len(context):
                    temp_word = context.pop()
                    if '}' in temp_word:
                        break
            break
        context.append(word)

    if backwards:
        context.reverse()
    text = " ".join(context)
    sentence_list = sentence.split(text)

    if backwards:
        sentence_list.reverse()

    if len(sentence_list) > CFG_PLOTEXTRACTOR_CONTEXT_SENTENCE_LIMIT:
        return " ".join(
            sentence_list[:CFG_PLOTEXTRACTOR_CONTEXT_SENTENCE_LIMIT])
    else:
        return " ".join(sentence_list)
def get_context ( lines , backwards = False ) : tex_tag = re . compile ( r".*\\(\w+).*" ) sentence = re . compile ( r"(?<=[.?!])[\s]+(?=[A-Z])" ) context = [ ] word_list = lines . split ( ) if backwards : word_list . reverse ( ) for word in word_list : if len ( context ) >= CFG_PLOTEXTRACTOR_CONTEXT_WORD_LIMIT : break match = tex_tag . match ( word ) if match and match . group ( 1 ) in CFG_PLOTEXTRACTOR_DISALLOWED_TEX : if backwards : temp_word = "" while len ( context ) : temp_word = context . pop ( ) if '}' in temp_word : break break context . append ( word ) if backwards : context . reverse ( ) text = " " . join ( context ) sentence_list = sentence . split ( text ) if backwards : sentence_list . reverse ( ) if len ( sentence_list ) > CFG_PLOTEXTRACTOR_CONTEXT_SENTENCE_LIMIT : return " " . join ( sentence_list [ : CFG_PLOTEXTRACTOR_CONTEXT_SENTENCE_LIMIT ] ) else : return " " . join ( sentence_list )
Get context.

    Given a relevant string from a TeX file, this function will extract text
    from it as far as it is deemed contextually relevant, either backwards or
    forwards in the text.
    The level of relevance allowed is configurable. When it reaches some
    point in the text that is determined to be out of scope from the current
    context, like text that is identified as a new paragraph, a complex TeX
    structure ('/begin', '/end', etc.) etc., it will return the previously
    allocated text.

    For use when extracting text with contextual value for an figure or plot.

    :param lines (string): string to examine
    :param reversed (bool): are we searching backwards?

    :return context (string): extracted context