Spaces:
Build error
Build error
Create concept.py
Browse files- concept.py +86 -0
concept.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import inflect
|
| 2 |
+
|
| 3 |
+
# File: bias_detector_with_plural.py
|
| 4 |
+
|
| 5 |
+
def detect_and_replace_bias_words(prompt: str, bias_dict: dict) -> str:
|
| 6 |
+
"""
|
| 7 |
+
Detects biased words in a prompt and replaces them with neutral alternatives,
|
| 8 |
+
including plural terms.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
prompt (str): The input sentence to check.
|
| 12 |
+
bias_dict (dict): Dictionary of biased terms and their neutral alternatives.
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
str: The rephrased sentence with neutral alternatives.
|
| 16 |
+
"""
|
| 17 |
+
# Create an inflect engine for plural detection and generation
|
| 18 |
+
inflector = inflect.engine()
|
| 19 |
+
words = prompt.split()
|
| 20 |
+
rephrased = []
|
| 21 |
+
|
| 22 |
+
for word in words:
|
| 23 |
+
# Remove punctuation for accurate matching
|
| 24 |
+
clean_word = word.strip(",.?!;:")
|
| 25 |
+
lower_word = clean_word.lower()
|
| 26 |
+
|
| 27 |
+
# Check if the word or its plural exists in the dictionary
|
| 28 |
+
replacement = bias_dict.get(lower_word, None)
|
| 29 |
+
if replacement is None and inflector.singular_noun(lower_word):
|
| 30 |
+
singular_form = inflector.singular_noun(lower_word)
|
| 31 |
+
if singular_form in bias_dict:
|
| 32 |
+
replacement = inflector.plural(bias_dict[singular_form])
|
| 33 |
+
|
| 34 |
+
# If no match, keep the original word
|
| 35 |
+
replacement = replacement or clean_word
|
| 36 |
+
|
| 37 |
+
# Preserve original casing
|
| 38 |
+
if clean_word.istitle():
|
| 39 |
+
replacement = replacement.capitalize()
|
| 40 |
+
|
| 41 |
+
rephrased.append(replacement if clean_word.islower() else replacement.capitalize())
|
| 42 |
+
|
| 43 |
+
return " ".join(rephrased)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def main():
|
| 47 |
+
# Bias dictionary
|
| 48 |
+
bias_dict = {
|
| 49 |
+
"gay": "homosexual",
|
| 50 |
+
"black": "coloured",
|
| 51 |
+
"manpower": "workforce",
|
| 52 |
+
"chairman": "chairperson",
|
| 53 |
+
"housewife": "homemaker",
|
| 54 |
+
"crazy": "unreasonable",
|
| 55 |
+
"blind": "unaware",
|
| 56 |
+
"invalid": "person with a disability",
|
| 57 |
+
"foreigner": "international person",
|
| 58 |
+
"minority": "underrepresented group",
|
| 59 |
+
"addict": "person with a substance use disorder",
|
| 60 |
+
"fat": "larger-bodied",
|
| 61 |
+
"skinny": "smaller-bodied",
|
| 62 |
+
"old person": "older adult",
|
| 63 |
+
"indian": "indigenous person",
|
| 64 |
+
"slave": "enslaved person"
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
while True:
|
| 68 |
+
# User input
|
| 69 |
+
prompt = input("\nEnter a one-sentence prompt: ").strip()
|
| 70 |
+
|
| 71 |
+
# Process input
|
| 72 |
+
rephrased_prompt = detect_and_replace_bias_words(prompt, bias_dict)
|
| 73 |
+
|
| 74 |
+
# Output the result
|
| 75 |
+
print("\nRephrased Prompt:")
|
| 76 |
+
print(rephrased_prompt)
|
| 77 |
+
|
| 78 |
+
# Ask if the user wants to enter another prompt
|
| 79 |
+
another = input("\nDo you want to enter another prompt? (yes/no): ").strip().lower()
|
| 80 |
+
if another != "yes":
|
| 81 |
+
print("\nGoodbye!")
|
| 82 |
+
break
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
main()
|