Create discrimination_model.py
Browse files- discrimination_model.py +68 -0
discrimination_model.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
discrimination_rules = {
|
| 4 |
+
|
| 5 |
+
"Gender Discrimination": [
|
| 6 |
+
"male only",
|
| 7 |
+
"female only",
|
| 8 |
+
"waitress",
|
| 9 |
+
"waiter",
|
| 10 |
+
"salesman",
|
| 11 |
+
"saleswoman",
|
| 12 |
+
"young lady"
|
| 13 |
+
],
|
| 14 |
+
|
| 15 |
+
"Age Discrimination": [
|
| 16 |
+
"young",
|
| 17 |
+
"under 30",
|
| 18 |
+
"under 35",
|
| 19 |
+
"recent graduate",
|
| 20 |
+
"junior only"
|
| 21 |
+
],
|
| 22 |
+
|
| 23 |
+
"Nationality Discrimination": [
|
| 24 |
+
"italian only",
|
| 25 |
+
"eu nationals only",
|
| 26 |
+
"native english speaker",
|
| 27 |
+
"must be italian"
|
| 28 |
+
],
|
| 29 |
+
|
| 30 |
+
"Religion Discrimination": [
|
| 31 |
+
"christian only",
|
| 32 |
+
"muslim only"
|
| 33 |
+
],
|
| 34 |
+
|
| 35 |
+
"Disability Discrimination": [
|
| 36 |
+
"able bodied",
|
| 37 |
+
"physically fit only"
|
| 38 |
+
]
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def clean_text(text):
|
| 43 |
+
|
| 44 |
+
text = text.lower()
|
| 45 |
+
text = re.sub(r"[^\w\s]", "", text)
|
| 46 |
+
|
| 47 |
+
return text
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def detect_discrimination(text):
|
| 51 |
+
|
| 52 |
+
text = clean_text(text)
|
| 53 |
+
|
| 54 |
+
categories = []
|
| 55 |
+
keywords = []
|
| 56 |
+
|
| 57 |
+
for category, words in discrimination_rules.items():
|
| 58 |
+
|
| 59 |
+
for word in words:
|
| 60 |
+
|
| 61 |
+
if word in text:
|
| 62 |
+
categories.append(category)
|
| 63 |
+
keywords.append(word)
|
| 64 |
+
|
| 65 |
+
if len(categories) == 0:
|
| 66 |
+
return "No discrimination detected", "None"
|
| 67 |
+
|
| 68 |
+
return ", ".join(set(categories)), ", ".join(set(keywords))
|