import re


# we use the following function to extract the reasoning steps (or conditional information) from the model outputs
def extract_reasoning_steps(output_text, ref_end=False):
    # clean the unnecessary format of the output
    lines = output_text.split("\n")
    filtered_lines = [
        line.strip() for line in lines
        if not (line.startswith("First, let's") or line.startswith("Next, let's")) and line.strip()
    ]
    cleaned_text = "\n".join(filtered_lines)

    # extract the reasoning steps based on "#"
    sections = re.split(r"\n#", cleaned_text)
    steps_list = [section.lstrip("#") for section in sections]

    reasoning_parts = []
    foundational_info = []

    if ref_end:
        reasoning_parts = [step for step in steps_list if "Reference" in step or "Step" in step]
    else:
        for step in steps_list:
            if "(by" in step or "Step" in step:
                reasoning_parts.append(step)
            else:
                foundational_info.append(step)

    # Combine basic information (remove the last element to avoid redundant content)
    base_summary = "\n".join(foundational_info[:-1]) + "\n" if foundational_info else ""

    return steps_list, reasoning_parts, base_summary


# the following function is used to analyze the results of verification (verify_step)
def extract_yes_or_no(verify_step):
    verify_model_outputs_filtered = []
    verify_model_outputs_raw = []
    for _ in verify_step:
        __ = [__ for __ in _.split("\n") if len(__) > 0][-1]
        verify_model_outputs_raw.append(__)
        if "not applicable" in __ or "N/A" in __ or "passes" in __:
            verify_answer = "yes"
        elif "fails" in __:
            verify_answer = "no"
        else:
            verify_answer = re.findall(
                '[ :\\"](Yes|No|yes|no|NO|YES|is correct or not|is incorrect|is not correct|partially correct|cannot determine|cannot confirm)[ ,\.\\"]',
                __,
            )
            if len(verify_answer) > 0:
                verify_answer = verify_answer[0].lower()
                verify_answer = (
                    "no"
                    if verify_answer
                    in [
                        "no",
                        "is not correct",
                        "is incorrect",
                        "is correct or not",
                        "partially correct",
                        "cannot determine",
                        "cannot confirm",
                    ]
                    else "yes"
                )
            else:
                verify_answer = re.findall('(Yes|No|yes|no|NO|YES)[ ,\.\\"]', __)
                verify_answer = "yes" if len(verify_answer) == 0 else verify_answer[0].lower()

        verify_model_outputs_filtered.append(verify_answer == "yes")
    return verify_model_outputs_raw, verify_model_outputs_filtered