Repository: gventuri/pandas-ai
File: pandasai/helpers/anonymizer.py (Python)

Code before:
    domains = [
        "gmail.com",
        "yahoo.com",
        "hotmail.com",
        "outlook.com",
        "icloud.com",
        "aol.com",
        "protonmail.com",
        "zoho.com",
    ]
    name_length = random.randint(6, 12)
    domain = random.choice(domains)
    letters = string.ascii_lowercase + string.digits + "-_"
    username = "".join(random.choice(letters) for i in range(name_length))
    email = username + "@" + domain
    return email


def generate_random_phone_number():
    """
    Generate a random phone number with country code.

    :return: str, generated random phone number
    """
    area_code = str(random.randint(0, 99))
    number = "".join(random.choices("0123456789", k=10))
    phone_number = f"+{area_code} {number}"
    return phone_number


def generate_random_credit_card():
    """
    Generate a random credit card number.

    :return: str, generated random credit card number
    """
    groups = []
    for i in range(4):
        group = "".join(random.choices("0123456789", k=4))
        groups.append(group)
    separator = random.choice(["-", " "])
    return separator.join(groups)


def copy_head(df: pd.DataFrame):
    """
    Copy the head of a DataFrame.

    :param df: pd.DataFrame, the DataFrame to copy the head from
    :return: pd.DataFrame, copied head of the DataFrame
    """ 

Review comment (suggestion):
Noticed here we have a problem: sometimes the script detects a "fake number" (check out the example of the GDP). The result is we convert it the gpd to a phone number like this: `+60 4702356135`, which can be misleading and cause hallucinations to the LLM.

What if we add the country prefix (i.e. +60) only if it was originally present, otherwise we just generate a random number without prefix?

So:
- if the field is, for example, 2411255037952
   - we generate a random number, say 5263101311
- if the field is, for example, +39 11255037952
   - we generate a random number, say +39 5263101311

Code after:
def generate_random_email():
    """
    Generate a random email address using predefined domains.

    :return: str, generated random email address
    """
    domains = [
        "gmail.com",
        "yahoo.com",
        "hotmail.com",
        "outlook.com",
        "icloud.com",
        "aol.com",
        "protonmail.com",
        "zoho.com",
    ]
    name_length = random.randint(6, 12)
    domain = random.choice(domains)
    letters = string.ascii_lowercase + string.digits + "-_"
    username = "".join(random.choice(letters) for i in range(name_length))
    email = username + "@" + domain
    return email


def generate_random_phone_number(original_field):
    """
    Generate a random phone number with country code if originally present.

    :param original_field: str, original phone number field
    :return: str, generated random phone number
    """
    if original_field.startswith("+"):
        # Extract country code if present
        country_code = original_field.split()[0]
    else:
        country_code = ""

    number = "".join(random.choices("0123456789", k=10))

    if country_code:
        phone_number = f"{country_code} {number}"
    else:
        phone_number = number

    return phone_number


def generate_random_credit_card():
    """
    Generate a random credit card number.