PromptNeutralizer / concept.py
LouisGPT's picture
Create concept.py
91691f9 verified
Raw History Blame Contribute Delete
2.76 kB
import inflect
# File: bias_detector_with_plural.py
def detect_and_replace_bias_words(prompt: str, bias_dict: dict) -> str:
"""
Detects biased words in a prompt and replaces them with neutral alternatives,
including plural terms.
Args:
prompt (str): The input sentence to check.
bias_dict (dict): Dictionary of biased terms and their neutral alternatives.
Returns:
str: The rephrased sentence with neutral alternatives.
"""
# Create an inflect engine for plural detection and generation
inflector = inflect.engine()
words = prompt.split()
rephrased = []
for word in words:
# Remove punctuation for accurate matching
clean_word = word.strip(",.?!;:")
lower_word = clean_word.lower()
# Check if the word or its plural exists in the dictionary
replacement = bias_dict.get(lower_word, None)
if replacement is None and inflector.singular_noun(lower_word):
singular_form = inflector.singular_noun(lower_word)
if singular_form in bias_dict:
replacement = inflector.plural(bias_dict[singular_form])
# If no match, keep the original word
replacement = replacement or clean_word
# Preserve original casing
if clean_word.istitle():
replacement = replacement.capitalize()
rephrased.append(replacement if clean_word.islower() else replacement.capitalize())
return " ".join(rephrased)
def main():
# Bias dictionary
bias_dict = {
"gay": "homosexual",
"black": "coloured",
"manpower": "workforce",
"chairman": "chairperson",
"housewife": "homemaker",
"crazy": "unreasonable",
"blind": "unaware",
"invalid": "person with a disability",
"foreigner": "international person",
"minority": "underrepresented group",
"addict": "person with a substance use disorder",
"fat": "larger-bodied",
"skinny": "smaller-bodied",
"old person": "older adult",
"indian": "indigenous person",
"slave": "enslaved person"
}
while True:
# User input
prompt = input("\nEnter a one-sentence prompt: ").strip()
# Process input
rephrased_prompt = detect_and_replace_bias_words(prompt, bias_dict)
# Output the result
print("\nRephrased Prompt:")
print(rephrased_prompt)
# Ask if the user wants to enter another prompt
another = input("\nDo you want to enter another prompt? (yes/no): ").strip().lower()
if another != "yes":
print("\nGoodbye!")
break
if __name__ == "__main__":
main()