import re


def clean_genre(df):
    """
    Clean genre by reducing multiple genres to singular genre.

    Parameters
    ----------
    df : pd.DataFrame
        A data frame with a column of movie genres.

    Returns
    -------
    df : pd.DataFrame
        A modified data frame with grouped genres.

    """
    # Convert the movie Social to Drama
    df["GenreFix"] = df["Genre"].apply(lambda x: re.sub("social", "drama", x))
    
    # Take the first genre from a tuple (A,B). E.g., Comedy, Romance => Comedy
    df["GenreFix"] = df["GenreFix"].apply(lambda x: re.sub("(?<=[a-z]),\s*[a-z]+", "", x))
    # Take the first genre from A-B. E.g., Comedy-romance => Comedy
    df["GenreFix"] = df["GenreFix"].apply(lambda x: re.sub("(?<=[a-z])\s*-\s*[a-z]+", "", x))
    # Take the first genre A/B. E.g., Comedy/Romance => Comedy
    df["GenreFix"] = df["GenreFix"].apply(lambda x: re.sub("(?<=[a-z])\s*/\s*[a-z]+", "", x))
    
    # Categorize as war movies
    df["GenreFix"] = df["GenreFix"].apply(lambda x: "war" if x.find("ww") != -1 or \
                                          x.find("world war") != -1 or \
                                          x.find("war") != -1 else x)
    
    # Categorize as animations
    df["GenreFix"] = df["GenreFix"].apply(lambda x: "animation" if x.find("anim") != -1 \
                                          else x)
    # Categorize as crime movies
    df["GenreFix"] = df["GenreFix"].apply(lambda x: "crime" if x.find("crime") != -1 \
                                          else x)
        
    # Remove the word "film"
    df["GenreFix"] = df["GenreFix"].apply(lambda x: re.sub("film", " ", x))
    
    # Historical drama => drama etc.
    df["GenreFix"] = df["GenreFix"].apply(lambda x: x.split()[-1] if x != " " else x)
    
    # Remove commas and dots alongside their whitespaces
    df["GenreFix"] = df["GenreFix"].apply(lambda x: re.sub(",|\.", "", x.strip()))
    
    return df