Spaces:
Build error
Build error
File size: 2,757 Bytes
91691f9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | 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()
|