import re


def simplify_text(text):
    """
    Remove nonsense words in text.
    E.g.
        text = "Find value of x."
        simplified_text = "Find x.
    """
    text = " " + text

    nonsense = ['GRIDIN', 'Suppose', 'below', 'exact', 'shown', 'composite', 'total', 'decimal', ' all ']
    nonsense += [' the ', ' this ', ' that ', ' its ', ' as (an |a |)', ' each ', ' then ', ' such ', 'given']
    nonsense += ['information', 'calculator', 'proportion']
    nonsense += ['exactly', 'respectively']
    nonsense += ['refer to', 'at right', ]
    nonsense += ['Express your answer in degrees.']
    nonsense += ['find in degrees.']
    nonsense += ['(square|) meters', '(square|) centimeters', '(square|) millimeters',
                 '(square|) inches', '(square|) feet', '(square|) units', '(square|) miles']
    nonsense += ['value of', 'variable of', 'variable']
    nonsense += ['solve problem', 'your answer']
    nonsense += ['in (diagram|figure)', 'use figure', 'use areas', 'use (an |a )']
    nonsense += ['area A of the shaded region is']
    nonsense += ['unless otherwise stated']
    nonsense += ['find indicated', 'find measure\.']
    nonsense += ['with side lengths indicated']

    for word in nonsense:
        pattern = re.compile(word, re.IGNORECASE)
        text = pattern.sub(" ", text)
        text = re.sub(r'[\s]+', r' ', text)

    # replace some phrased with standard text
    text = re.sub(r'area A of shaded region is', r'', text)
    text = re.sub(r'(cm|m|ft|in)\^2', r'', text)
    text = re.sub(r'what is|solve for|determine|solve|what it|how long is|Which polynomial best represents', r'Find',
                  text, flags=re.IGNORECASE)  # Note: flages=
    text = re.sub(r'to, find', r'to find', text)
    text = re.sub(r'Find \\angle', r'find m \\angle', text, flags=re.IGNORECASE)
    text = re.sub(r'measure of m', r'measure of', text, flags=re.IGNORECASE)
    text = re.sub(r'polygon figure', r'polygon', text)
    text = re.sub(r'express ', r'find ', text, flags=re.IGNORECASE)
    text = re.sub(r'figure (?=[A-Z]+)', r'', text)  # 428
    text = re.sub(r' an ', r' ', text)
    text = re.sub(r' of a ', r' of ', text)
    text = re.sub(r'is equal to', r'=', text)
    text = re.sub(r'(?<=\d) long(?![a-z])', r' ', text)
    text = re.sub(r'in terms of [a-z] and [a-z]', r' ', text)
    text = re.sub(r' is (?:an|a) (?=[a-z\d]{2})', r' is ', text)
    text = re.sub(r'triangle \\triangle', r'\\triangle', text, flags=re.IGNORECASE)
    text = re.sub(r'(m |)\\overrightarrow', r'', text)
    text = re.sub(r'(?<=[A-Z][A-Z]) \\cong (?=[A-Z][A-Z][\s\,\.\?])', r' = ', text)  # AB \cong EF -> AB = EF
    text = re.sub(r', and ', r' and ', text)
    text = re.sub(r'preimeter', r'perimeter', text)
    text = re.sub(r' (?=[\,\.\?])', r'', text)
    text = re.sub(r' side length ', r' side ', text)
    text = re.sub(r'triangle \\triangle', r'triangle', text)
    text = re.sub(r'(?<=\\angle [A-Z]{3}) \\cong (?=\\angle [A-Z]{3})', r' = ', text)
    text = re.sub(r'(?<=\\angle [A-Z\d]) \\cong (?=\\angle [A-Z\d])', r' = ', text)
    text = re.sub(r'BY = CZ = AX = 2.5 diameter of \\odot G = 5', r'BY = CZ = AX = 2.5, diameter of \\odot G = 5',
                  text)  # error, 977
    text = re.sub(r'=', r' = ', text)

    text = re.sub(r'[\s]+', r' ', text)
    text = text.strip()

    return text